Print statistics in Output using Python

Hello everyone! I am new to Visual Components and I am currently facing some problems I hope someone could help me out.

I am trying to print a data from a “Statistics” behaviour in the “Behaviours” section of the GenericRobot onto the Output panel just as how I have done for the Conveyor.

The data I am trying to collect is the PartsTotalTime. I would like the Robot to print how long it was in Operation, I figured if the robot detects that a part has exited the Robot, it would print PartsTotalTime.

I am fairly new to Python as well shifting from C, however this logic seems to work in my head but doesn’t seem to be working on Visual Components. There is definitely something I am understanding wrongly.

Here is the code I have written:

from vcScript import *
from vcStatistics import *
from vcHelpers.Robot2 import *

comp = getComponent()
app = getApplication()
stats = comp.findBehaviour(“Statistics”)
robot_executor = comp.findBehaviour(“Executor”)

def OnRun():
if (stats.PartsIntervalExited != 0):
print stats.PartsTotalTime

Thank you very much.

1 Like

Hi @Thenolypus and welcome to the forum!

OnRun is an event that is called when the simulation clock starts to run.

In your example, this is what happens:

  1. simulation starts and OnRun event handler is called
  2. in your code “if (stats.PartsIntervalExited != 0)” is evaluated which translates to: “if there has been one or more exits from the robot’s grasp container during the last interval (60 s by default)…”
  3. and continues with the print “…then print the cumulative time over the simulation duration when at least one part has been in the gripper” and
  4. after that OnRun method returns (exits). So, that code is run only once right at the point when the simulation begins.

If I understood your description correctly, this is not what you want. Proper way to do this would be to listen to the component container “GraspContainer” under the link “mountplate” and when ever something enters, mark the robot to Busy state and to e.g. Idle when part leaves.

If you want OnRun event handler to be “alive” (not return) throughout the simulation you have to implement an infinite loop in it and you also must have some method within the loop that will “spend time” and block the code execution for some time. Otherwise, the loop will run too fast and consume all system resources and most likely cause app freeze. You can try something like the following that prints the sim time each 2 seconds:

def OnRun():
  sim = getSimulation()
  while True:
    print sim.SimTime

But it should be sufficient enough just to add an event handler for the “GraspContainer” OnTransition event. You don’t even need the OnRun event. See vcContainer in the Python API ref. See also the vcStatistics and this tutorial:

Good luck!

1 Like

Hi @jouha and thank you for your quick and clear response!

Your response had been very detailed and newbie-friendly for me to understand, I thank you for that.

I took some time tinkering with the lines of codes and I have also took into consideration of what you have suggested with adding an event handler “GraspContainer” with an OnTransition event, however I am unable to make it function and lack of clear examples on the Python API or online contributed to its failure.

However, I have made a different attempt which worked for me. I remained with using an OnRun event handler, writing the code as follows:

comp = getComponent()
time = comp.findBehaviour(“Statistics”)

def OnRun():
while True:
if (time.PartsIntervalExited == 1):
print (“Robot time:”), time.PartsTotalTime

This seems to work for me as it waits a second before a component exits and prints the total time that the part has spent in the robot.

Due to my project reaching its deadline, I was unable to fully read up the tutorial you have linked me to. I will however spend the next time learning them.

Thank you for your help once again.