Page 1 of 1

restart the simulation when collision happens

Posted: 02 Mar 2018, 16:25
by Zhuang
Hello, what I want to do is when collision happens, restart the simulation, so I judge if there is collision I first stop the simulation and then start the simulation, the relative code is:

Code: Select all

res1, _ = simReadCollision(collisionHandle)
if (res1 == 1) then
    res1 = 0
    print("Collision!")
    simStopSimulation()
    res = simStartSimulation()
    print(res)
end
But when I execute it, the robot just stops after seconds and doesn't start again, the Linux terminal shows:
Collision!
0
Collision!
0
Collision!
0
It means the if statement loops three times and the operation simStartSimulation could not be performed. I guess there should be time delay to finish the operation, so I change the code like this

Code: Select all

require "socket"
function sleep(sec)
    socket.select(nil, nil, sec)
end
res1, _ = simReadCollision(collisionHandle)
if (res1 == 1) then
    print("Collision!")
    simStopSimulation()
    sleep(0.5)
    res = simStartSimulation()
    print(res)
    sleep(0.5)
end
But the result is the same! I am so confused, what happened ?

Re: restart the simulation when collision happens

Posted: 02 Mar 2018, 16:51
by coppelia
Hello,

you don't give any information about what type of script you are using. Is it a threaded or non-threaded child script? Or is it a customization script?
Child scripts do not run when simulation is stopped. So you can't start a simulation from a child script. You will have to use a customization script.

Make sure you familiarize yourself with the different script types before going ahead.

Cheers