Let’s display the value of the angle in degrees here to see what rotation the PM Align Tool found.
So we go back to the Main task and in the Vision Pro Tool block we have our PM Align Tool, which also returns rotation as a result.
We can see that the value is quite small, 0.213, this is because this result is in radians. If we look inside the PM Align Tool in the results, here is an angle that is 12.25, but ultimately we have an output in radians, so we’ll have to deal with that.
I will take it out on the output of the tool block. I will name it for example Part Rotation and I have this output available in Task and map it to a new global variable called Part Rotation In Radians for example.
Or we simply leave the Part Rotation name. Click accept. A nd in order to display it on the page, we choose a label to which we give the text “rotation, colon”. Change the text size to 20.
We’ll add another label. We could also add numeric entry. But now we are not going input values here, nor do we want to confuse the operator into typing something there. We just want to simply display the value, that’s why I used the label.
I’ll give it a font size 20 again so that it’s the same and the text will now be mapped, using these two arrows, to Part Rotation value. We see that the result will be of Double. We can try it. When I give Run Once, I see that my rotation has changed to 0.2137
Run, Run, ej ha, the value is the same and it changes only when the part found is green, that is, when it finds a pattern.
Which is fine, but it is not desirable to display number when Pattern is not found.
This could be confusing for the operator or whoever is standing by the line.
So let’s change this behavior and display the value only when Pattern is found.
So we can write it as a condition directly to this Expression Builder to this Binding.
We write it down as IF condition, I look through the Tag selector to find the variable – Found Patterns Count, and when this number is greater than 0 then I write out the part rotation,
otherwise I write NA for example representing not defined value. But it throws invalid expression message here. And that’s because it couldn’t find any IF function that has a Boolean input, then a double, and then string.
Just can’t handle that these two variables are different, that one is Double and the other is String because it does not know what output type it will have.
Cannot determine the output type.
Whether it’s Double or String.
So either we can change this to String or this to Double.
For example, if we wrote 0.0
the value is already ok, because the result is Double and this one is also a Double. But we don’t want to be a number there, so it is not confusing for the operator, so we’ll put that NA back in there and try changing this to String from Double. And now we’re going to kill two birds with one stone, because we’re going to convert it to String by even rounding that number to, let’s say 4 decimal places.
Because when we look at Help, Expression Builder has Format function. Format Options in Expression Builder, and we see that Float, i.e. a number with a decimal point, can use this notation to display Format F3 as a number with three decimal places.
Let’s do that. But we want with 4 decimal places, so we write F4 instead. And now Expression Builder is satisfied because the output type will be always String. This gives String and this also gives String. We give OK, and when we give Run, we should see, when Pattern Found is 0, not found, then Rotation is NA.
And when Pattern is found, that’s when Rotation is some number, albeit still in radians.
So, let’s convert this value to degrees.
But I don’t know of any easy way we could do it here in Expression Builder, although we could look at the features to see if we happen to find something like that. Math, there are some Cosines, Sinuses, Absolute values, Ceiling Max Median but I don’t know of anything that would convert.
So, we will write our own little converter, i.e. our own script for that. We will write it in that main task, right behind the Vision Pro Tool Block. We will use the script block, which we can name radToDeg, and we drag this output Part Rotation into the script block.
And we want to save the new value to this Part Rotation tag. We would like to have some output from this script to connect to the tag. But we don’t have one yet. So we right click on script, we choose Reconfigure I.O.
and we check Return Type and change to Double.
Well, we can see that the name of the input variable is quite long.
We could shorten it a bit, but we will leave it as is for now.
I give Accept so that you can actually see where is the name of the variable that enters the script.
This is the name of the variable.
This script is actually a public function that returns Double and receives one argument of type Double that will have this name in the script.
So, to convert radians to degrees, I create some local variable result, or Angle in Degrees, and assign
the name of the variable that enters the script, so Vision Pro Tool Block Part Rotation x 180 I can put it in parentheses, divided by PI and I can pull the PI value from the Math library.
This result, that is, the result from this expression, will be written into this local variable, but now we want to put that local variable out of the script, because the Double result is expected.
So, we use the keyword Return and write Angle in Degrees.
This puts this result into the output of this script.
We compile to current script via this green tick or via F 6 shortcut.
And now we have a script ready that converts this number Part Rotation.
But we haven’t assigned the output anywhere yet.
So, let’s try to assign it again to that Part Rotation variable, which is global variable – or Tag if you will.
this Part Rotation.
So, from this Vision Pro Tool block we get a number in radians into that tag, but through the script the value changes to degrees and stores in the Part Rotation Tag. So at the end we will have a number in degrees and when I run, I give run once, and I see that the result here is 12.24, which is already in degrees. N/A when it finds no rotation.
There is a small number on the image which we took for training – but it’s very small so it is fine.
There is another option how to calculate this angle or write that script.
We can also write it down in this way.
Now that I’ve used two slashes, I’ve commented out this line in the script.
I cut this out, I delete this. Ok. Vision Pro has a method for converting the angles. So we can also try this method. From library Cognex, VisionPro, CogMisc we will call RadToDeg() function.
I found this by going through Help -> Vision Pro Online Documentation and when I studied some things there, I found the RadToDeg method, which is in the Cognex Vision Pro library in the Cognex Miscelanous namespace and it is the RadToDeg method.
So now that RadToDeg expects input in radians, so that’s going to be our variable and it should give the same results.
And yes.
Minus 0.0023 Minus 7.9 and 12.24, so it’s the same thing.
So now, we have the output in degrees and we can still enable in the Tool block in the PM Align tool, Run params, a larger angle tolerance, for example those minus 180°, 180°, to see how it really changes. I start the program and now I see that this is 61°, 95°, 134° and so on.
I’ll save the program so I don’t forget.