Start simulation with given parameters from external script

Typically: "How do I... ", "How can I... " questions
Post Reply
ask4jubad
Posts: 21
Joined: 21 Sep 2021, 07:46

Start simulation with given parameters from external script

Post by ask4jubad »

Please how do I set the simulation parameters from an external script (ZMQ api)?

I have the following for instance from an external python script:

Code: Select all

sim.startSimulation()
time.sleep(2)
sim.callScriptFunction("setParameters@Bill", sim.scripttype_childscript, timestepprime, velprime)   
sim.callScriptFunction("setParameters@LBR_iiwa_7_R800", sim.scripttype_childscript, timestep, vel)
which starts the simulation, waits for 2 secs (I really don't want this, I only do it because the simulator doesn't respond to external stimulus until after its environment has been prepared), sets the wait time and velocity of a human and then sets the wait time and velocity of a robot.

Wait time refers to the amount of time the robot or human should wait before going into motion.

Note: setParameters function is already defined from the objects' child scripts.

e.g.

Code: Select all

function setParameters(timestep, vel)
    print("Robot parameters set!!!")
    waitTime = timestep
    speed = vel
end
What I want in essence, is to set waitTime and speed at the EXACT time that I am starting the simulator.

coppelia
Site Admin
Posts: 10381
Joined: 14 Dec 2012, 00:25

Re: Start simulation with given parameters from external script

Post by coppelia »

Hello,

if you want to call a script function from an extenal application (or from another script), you need to make sure that the script you want to call is initialized. Simulation scripts are not initialized until simulation starts. On the other hand, customization scripts are initialized as soon as the model/scene is loaded.

So use a customization script instead.

Another, simpler solution would be to set a signal from your external application. e.g. sim.setFloatSignal.

Cheers

ask4jubad
Posts: 21
Joined: 21 Sep 2021, 07:46

Re: Start simulation with given parameters from external script

Post by ask4jubad »

Hi Coppelia,

Thank you for your response.

But it appears that your response does not really address my concern.

For instance, customization script does not work since I only want to set simulation parameters (e.g. speed of a robot) at the the instant that I run sim.startSimulation() and not some static scene or model parameter. (Note: I tried moving the signal code shown later below in the child script into the sysCall_init() function of the customization script of the robot, but it had no effect.)

I also tried using signal in the sysCall_init() function to get some certain value from an external script thus:

External script

Code: Select all

sim.startSimulation() #I wan to set the following values exactly at the point of starting the simulator
sim.setInt32Signal("waitTimeRobot", timestep)
sim.setFloatSignal("velRobot", vel)
        
Robot Child script

Code: Select all

local waitTimeRobot = sim.getInt32Signal("waitTimeRobot")
waitTime = waitTimeRobot
local speedRobot = sim.getFloatSignal("velRobot")
speed = speedRobot

Now what I noticed is this; when I use signals for setting the variables of interest in the sysCall_init(), it works but unfortunately the sysCall_actuation() would have already started, thereby returning an error at this point in my actuation function:

Code: Select all

if counter < waitTimeRobot then --this is line 90
     counter = counter + 1
end
An example error is this:
[LBR_iiwa_7_R800@childScript:error] [string "LBR_iiwa_7_R800@childScript"]:90: attempt to compare number with nil
stack traceback:
[string "LBR_iiwa_7_R800@childScript"]:90: in function 'sysCall_actuation'

So please could you point me to an example to explain to me how I really can do this?

coppelia
Site Admin
Posts: 10381
Joined: 14 Dec 2012, 00:25

Re: Start simulation with given parameters from external script

Post by coppelia »

You can use a customization script without problem: the customization script offers the same functionalities as the child script, and more. Check the various callback functions available for a customization script. The point is really: the customization script is initialized as soon as it is in the scene. You can then detect a simulation start with the sysCall_beforeSimulation callback.

But if you use a child script and signals, that works too. You will have to read the signals in the init phase of the child script. And set the signals in your external program before starting the simulation.

Cheers

ask4jubad
Posts: 21
Joined: 21 Sep 2021, 07:46

Re: Start simulation with given parameters from external script

Post by ask4jubad »

Thanks for your feedback

Apparently using Signals worked. Using a customization script is still proving abortive, so, I'd stick to Signals for now.

Cheers.

Post Reply