Print statistics in Output using Python

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
    delay(2.0)

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: https://academy.visualcomponents.com/lessons/component-states/?course=854

Good luck!

1 Like