How to quick reset the simulation like clicking the stop and start buttons?

Typically: "How do I... ", "How can I... " questions
Post Reply
JackL
Posts: 1
Joined: 19 Dec 2019, 11:45

How to quick reset the simulation like clicking the stop and start buttons?

Post by JackL » 23 Dec 2019, 09:32

My simulation model in v-rep is a quadrotor and I want to control it in Matlab through the remote API. Each time I restart the simulation I need to reclick the stop and start buttons in v-rep to reset the simulation. Is there any way to avoid clicking buttons to achieve a quick reset though the remote API in Matlab? I know the function 'simResetDynamicObject'. but the function will not reset the object to starting position and attitude. Is there any way to reset simulation that has the same effect as clicking the stop and start buttons. Thanks.

fferri
Posts: 456
Joined: 09 Sep 2013, 19:28

Re: How to quick reset the simulation like clicking the stop and start buttons?

Post by fferri » 23 Dec 2019, 09:47

You can store the position and orientation of all the relevant objects in a variable, and reset it (and also call simResetDynamicObject).

e.g.:

Code: Select all

function sysCall_init()
    movingObjects={'Cuboid'}
    pos={}
    orient={}
    for k, v in pairs(movingObjects) do
        h=sim.getObjectHandle(v)
        movingObjects[k]=h
        pos[h]=sim.getObjectPosition(h,-1)
        orient[h]=sim.getObjectOrientation(h,-1)
    end
end

function reset()
    for k, v in pairs(movingObjects) do
        sim.setObjectPosition(v,-1,pos[v])
        sim.setObjectOrientation(v,-1,orient[v])
        sim.resetDynamicObject(v)
    end
end

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

Re: How to quick reset the simulation like clicking the stop and start buttons?

Post by coppelia » 31 Dec 2019, 08:59

Hello,

what about simply stopping and restarting the simulation from your remote API client side, using simxStopSimulation, followed by simxStartSimulation? After calling simxStopSimulation, you should however wait a few pings, since stopping a simulation will not immediately stop it (the simulation first need to inform threaded scripts to stop, wait for that, then run a last simulation step, then stop)

e.g.

Code: Select all

sim.simxStopSimulation(clientID,sim.simx_opmode_blocking);
is_running = true;
while is_running
    error_code, ping_time = sim.simxGetPingTime(clientID);
    error_code, server_state = sim.simxGetInMessageInfo(clientID, sim.simx_headeroffset_server_state);
    is_running = bitand(server_state,1);
end
sim.simxStartSimulation(clientID,sim.simx_opmode_blocking);

Cheers

Post Reply