Start/reset simulation from python command

Software: Visual Components + KUKA.Sim 4.3

I would like to programmatically start, stop or reset the simulation through a UI button.

I tried the following code without success. Reset works but starting the simulation doesn’t see to move the robot at all

from vcCommand import *
from vcScript import *
from vcApplication import *
from kuka import *

app = getApplication()

def first_state():
   
    sim = app.getSimulation()
    sim.update()
    sim.reset()
    print "Simulation reset!"    
    # sim.run()  >> hangs for a while and then completes without executing the robot motions
    sim.run(10000) # completes without executing the robot motions
    print "Simulation completed!"

addState(first_state)

Are you using generic VC robots or KUKA?
I did a quick test, and everything seemed okay there. Maybe some extra commands are needed to start the KUKA robot executor?

start_stop_test.vcmx (461.2 KB)

from vcScript import *

def start_pressed(arg):
  app.startSimulation()
  print("start")

def stop_pressed(arg):
  app.stopSimulation()
  print("Stop")

app = getApplication()
comp = getComponent()
start_button = comp.getProperty("Start")
start_button.OnChanged = start_pressed
stop_button = comp.getProperty("Stop")
stop_button.OnChanged = stop_pressed

Uploading: CONTEXT.vcmx…
CONTEXT.vcmx (63.3 KB)

I am using a KUKA Robot within KUKA.Sim 4.3 software (which runs an older version of VisualComponents, not sure how to check which version exactly)

I can’t run your sim file as it is but I modified the code to use app.startSimulation() instead but the robot still does not move. The Play button of the simulation changes to Pause but that’s about it.

from vcCommand import *
from vcScript import *
from vcApplication import *
from kuka import *

app = getApplication()

def first_state():
    app.resetSimulation()
    print "Simulation reset!"
    app.startSimulation()
    print "Simulation completed!"
  
addState(first_state)

I was able to test the same code with a VC robot and it works as expected. Looks like KUKA robot requires some additional steps?

Maybe @mastu could help here? :innocent: If you have any idea why the KUKA robot does not start moving when the simulation is started from a Python command.

Hi @Este @Star-Dust,

I tried to test this real quick with the same result as Star-Dust already stated. Right now my guess is, that starting the simulation via Python is out of scope. I will check some more but for now this seems to be the status.

FYI - I think it is possible through the .NET API.

var app = IoC.Get();
app.Simulation.Run(10);

I don’t have any knowledge in .NET but I got this info from another case.

@mastu Thanks for the suggestion! Could we use the netCommand within the python API to run this?

something like this:

  cmd = app.findCommand("netCommand")
  cmd.execute("Run",[10])

Also do you know the commands for reset and stop?

Note that “running simulation” and “starting simulation” are two different things.

Starting simulation means the same as pressing the play button in the UI, it starts running the simulation using defined speed and other settings.

Running simulation (forward) is something that can be done from the API, but usually there is no need. It means executing the simulation forward a certain amount of simulation time, and only then returning back to the caller. This “running simulation” may take less or more real time to compute.

@TSy Thanks for clarifying. Can you provide an example code for “starting” and “reset” of simulation? Can this .NET command be invoked via the python API through the “netCommand” object?

If starting simulation from Python doesn’t work properly in KUKA.Sim but works in VC, then it is a bug in KUKA.Sim and you should report it to KUKA, and ask their customer support for any workarounds.

Anyway, starting simulation from .NET works like this:

I don’t think there is any command for starting / stopping simulation because there is direct API for it. You could create your own, though.