sync external Python script and internal Coppelia child script

Typically: "How do I... ", "How can I... " questions
Post Reply
javl0p
Posts: 9
Joined: 24 Mar 2023, 10:42

sync external Python script and internal Coppelia child script

Post by javl0p »

I am currently running a Coopelia scene from an external Pyhton script using the zmqRemoteAPI module.

The pipeline goes as follows:

1. Some calculations are performed in external Python script.
2. The resulting variables are sent from external Python script to Coppelia via sim.writeCustomTableData() command.
3. A child script is executed in Coppelia before the actual simulation starts
4. The child script is then disabled.
5. The simulation starts via API Python command sim.startSimulation()
6. Simulation takes place within Coppelia.
7. The resulting variables are sent from Coppelia to external Python script.

The actual simulation in Coppelia is sync with the Python script using the following code:

Code: Select all

sim.startSimulation()

    while sim.getStringSignal(signal_tag) == 'on':
        client.step()

sim.stopSimulation()
The problem comes when I try to run step 3.

I'm trying to do something similar to the code above, having the Python script running in loop until a confirmation is received from Coppelia script that the actual child script has finished executing.

Im trying something similar to:

Code: Select all

sim.initScript(coppelia_script_handle)

while sim.getStringSignal(signal_tag) == 'on':
	pass
but the Coppelia child script never actually runs. I assume I somehow have to manage the multithreading process via sim.setThreadAutomaticSwitch() and sim.switchThread() commands but I'm quite lost, to be honest.

How could I manage to resume the external Python execution once the Coppelia child script is fully executed?

I appreciate any help!

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

Re: sync external Python script and internal Coppelia child script

Post by coppelia »

Hello,

I am myself a little bit confused with your description...

First of all, a child script, which is a simulation script, will never execute when a simulation is not running. If you need a script to execute while simulation is not running, use customization scripts.

When sending data to CoppeliaSim, it is often more convenient to do this by simply calling a script function, that will receive data as arguments, and that can then store the data somewhere. But using sim.writeCustomTableData and similar works fine too.

Finally, if possible, do not disable/enable, init/reinit a script manually, since it will be quite confusing. If you use a customization script (instead of a child script), you will be able to execute code only when simulation is not running (e.g. in the sysCall_nonSimulation, only when simulation is running (e.g. in sysCall_actuation), or in any case. You just need to correctly react to the various possible callback functions that are available

Cheers

Post Reply