Design a Counter on your program
This example will allow you to keep track of counting cycles, quantity, etc.
Examples are valid starting from:
CB2 Software version: 1.8.16941
CB3 Software version: 3.1.17779
E-series Software version: All
Note that older or newer software versions may behave differently.
These examples can be used for both CB2, CB3 and E-series.
Simple use:
This simple example is showing how to count events using a digital input.
1) First set up an BeforeStart/INT variable (Counter=0).
2) We just use wait timer 4 sec as an example.
3) And then the counter will be added in an Event
Advance use:
Suppose we have a proximity sensor connected to a digital input, in such a way the input is High when the sensor detects an object, and low otherwise.
Now we want to count objects as they pass by the sensor, so we can let the robot perform an action, when eg. 24 objects have passed.
This picture shows how to make such a program:
I order to detect an object passing by the sensor, we need to detect the low to high transition on the digital input the sensor is connected to.
We therefor need to know not only the current sensor value, but also the last sensor value we read from the input. Given this information, we can now conclude the sensor has detected an object when the old sensor value is "False" and the current sensor value is "True".
The rest of the program should be pretty self-explanatory, but we should remember to give the variables some sensible starting values, using the "Set initial variable values" feature:
The program presented above, solves the problem, but it has a disadvantage. Suppose, the robot had to do a time consuming job in the DoStuff section of the program, in that case we run the risk of missing some objects passing by the sensor! This happens because programs are executed sequentially, so we will not read the sensor value before the program returns to the top of the program. I order to overcome this limitation, we can make the counter run inside a thread of its own:
This way the counter is running in parallel with the main program, and the sensor is read independently of the main program. The wait statement at the beginning of the thread can be used to control how often we wish to read the sensor.
Download Polyscope .urp example below