The python script can't interact with CoppeliaSim in headless mode normally.

Report crashes, strange behaviour, or apparent bugs
Post Reply
weijiang
Posts: 4
Joined: 04 Apr 2022, 07:14

The python script can't interact with CoppeliaSim in headless mode normally.

Post by weijiang »

Hi~ when I try to use the following code to control YouBot to move forward and keep recording the position of the robot, the code works fine under the GUI mode. However, if I launch CoppeliaSim in headless mode with parameter "-h -gREMOTEAPISERVERSERVICE_19998_False_True" and run the code, the car doesn't seem to be moving as the output keeps the same. I have no idea why there is such a difference in the two different modes. The problem exists in CoppeliaSim V4.0/4.1/4.2 and the system I used is Ubuntu 18.04/macOS Monterey 12.1.

Code: Select all

import sim
sim.simxFinish(-1) 
clientID = sim.simxStart('127.0.0.1',19998,True,True,5000,5) 

_, youBot_handle = sim.simxGetObjectHandle(clientID, 'youBot', sim.simx_opmode_blocking)
wheel_joints_handle = [-1,-1,-1,-1]; 
_, wheel_joints_handle[0] = sim.simxGetObjectHandle(clientID, 'rollingJoint_fl', sim.simx_opmode_blocking)
_, wheel_joints_handle[1] = sim.simxGetObjectHandle(clientID, 'rollingJoint_fr', sim.simx_opmode_blocking)
_, wheel_joints_handle[2] = sim.simxGetObjectHandle(clientID, 'rollingJoint_rr', sim.simx_opmode_blocking)
_, wheel_joints_handle[3] = sim.simxGetObjectHandle(clientID, 'rollingJoint_rl', sim.simx_opmode_blocking)

[forward_back_vel, left_right_vel, rotation_vel] = [0.5, 0, 0]
while True:
    sim.simxSetJointTargetVelocity(clientID, wheel_joints_handle[0], -forward_back_vel + left_right_vel - rotation_vel, sim.simx_opmode_blocking)
    sim.simxSetJointTargetVelocity(clientID, wheel_joints_handle[1], -forward_back_vel - left_right_vel + rotation_vel, sim.simx_opmode_blocking)
    sim.simxSetJointTargetVelocity(clientID, wheel_joints_handle[2], -forward_back_vel + left_right_vel + rotation_vel, sim.simx_opmode_blocking)
    sim.simxSetJointTargetVelocity(clientID, wheel_joints_handle[3], -forward_back_vel - left_right_vel - rotation_vel, sim.simx_opmode_blocking)
    _, youBot_position = sim.simxGetObjectPosition(clientID, youBot_handle, -1, sim.simx_opmode_blocking)
    print(youBot_position)

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

Re: The python script can't interact with CoppeliaSim in headless mode normally.

Post by coppelia »

Hello,

are you sure simulation is started when in headless mode?
Additionally, we highly recommend using the ZeroMQ remote API instead, since more reliable, easier to use, and it also offers all the same API functions as from witin CoppeliaSim.
Also, make sure to upgrade to CoppeliaSim V4.3

Cheers

weijiang
Posts: 4
Joined: 04 Apr 2022, 07:14

Re: The python script can't interact with CoppeliaSim in headless mode normally.

Post by weijiang »

Thanks for your reply.

(1) Actually, I'm not sure the simulation is started when in headless mode. However, what I can confirm is that the simulation connection is normal, because I can get the initial location information of the object (youbot) in the model(*.ttt) and print it as the code shows. So, my biggest confusion is why the model can be initialized normally, but doesn't run properly. This is also the case in CoppeliaSim V4.3. Do you have any suggestions for me to figure out the problem?

(2) I have tried using ZeroMQ remote API on Ubuntu 18.04 and CoppeliaSim V4.3. I follow the instruction in the page(https://www.coppeliarobotics.com/helpFi ... erview.htm) and make sure everything is ready. However, when I run the example (zmqRemoteApi/clients/python/simpleTest.py), it only prints the first line ("Program Started") then stuck here without normal exit or any further actions. I believe this is not how it should actually behave. How should I use ZeroMQ remote API properly?

I am new to using CoppeliaSim so pardon me if the question is naive. Thanks for your attention again.

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

Re: The python script can't interact with CoppeliaSim in headless mode normally.

Post by coppelia »

Simply add a child script to your scene with following content:

Code: Select all

function sysCall_init()
    sim.addLog(sim.verbosity_loadinfos,"Simulation started")
end

function sysCall_cleanup()
    sim.addLog(sim.verbosity_loadinfos,"Simulation ended")
end
Then save your sene. Then launch CoppeliaSim in headless mode. Now if the simulation is running, it should print so.

However, when you connect to CoppeliaSim, are you actually starting the simulation? i.e. by calling sim.startSimulation (ZeroMQ remote API) or simx.startSimulation (legacy remote API)?
You can also immediately start a simulation with the command line option -s (you'll however also have to specifiy the scene to load in the command line).

About the ZeroMQ remote API on Ubuntu: did you try first in GUI mode, one of the demo script, e.g. zmqRemoteApi/clients/python/simpleTest.py?

Cheers

weijiang
Posts: 4
Joined: 04 Apr 2022, 07:14

Re: The python script can't interact with CoppeliaSim in headless mode normally.

Post by weijiang »

The problem is indeed solved by adding -s. As for ZeroMQ remote API, I will try to check it again. Thanks a million for your kindness!

Post Reply