## Simulate multiple times the same scenario using CoppeliaSim and MATLAB

### Simulate multiple times the same scenario using CoppeliaSim and MATLAB

Hi everyone,

I'm trying to optimize the process of throwing a ball with an Asti Robot.

So for that I'm using bayesopt in MATLAB (Bayesian Optimization) and it is optimizing the angle and the velocity of the process.

So, bayesopt needs 30 iterations for optimize the values of the angle and the velocity(Asti must throw the ball 30 times with 30 different values of angle and velocity sent by MATLAB).

Now, It throws the ball only one time and honestly I don't know how can I do in Coppelia for simulate 30 times the same process with 30 different values of velocity and angle.

Thanks in advance

Cheers!

I'm trying to optimize the process of throwing a ball with an Asti Robot.

So for that I'm using bayesopt in MATLAB (Bayesian Optimization) and it is optimizing the angle and the velocity of the process.

So, bayesopt needs 30 iterations for optimize the values of the angle and the velocity(Asti must throw the ball 30 times with 30 different values of angle and velocity sent by MATLAB).

Now, It throws the ball only one time and honestly I don't know how can I do in Coppelia for simulate 30 times the same process with 30 different values of velocity and angle.

Thanks in advance

Cheers!

### Re: Simulate multiple times the same scenario using CoppeliaSim and MATLAB

Hello,

it really depends how you do it, since you have many different possibilities, you do not tell us anything on how you proceed.

If you are using the legacy remote API, then you could call 30 times a script function via

You can also call a single time simxCallScriptFunction, and give CoppeliaSim the 30 values to perform the 30 throws in a row.

Cheers

it really depends how you do it, since you have many different possibilities, you do not tell us anything on how you proceed.

If you are using the legacy remote API, then you could call 30 times a script function via

`simxCallScriptFunction`

, which could trigger 30 different throws. Of course you could also start/stop the simulation each time.You can also call a single time simxCallScriptFunction, and give CoppeliaSim the 30 values to perform the 30 throws in a row.

Cheers

### Re: Simulate multiple times the same scenario using CoppeliaSim and MATLAB

Hi coppelia,

thank you for all your support!

Finally, I did it with simxStopSimulation and simxStartSimulation and in MATLAB I've a script that executes 30 times all the simulation process on Coppelia!

Thank you very much!

thank you for all your support!

Finally, I did it with simxStopSimulation and simxStartSimulation and in MATLAB I've a script that executes 30 times all the simulation process on Coppelia!

Thank you very much!

### Re: Simulate multiple times the same scenario using CoppeliaSim and MATLAB

Hi,

I am not creating a new topic since this suits my question.

I have a scene with a UR5 robot (force-torque joints) and using OMPL and a MATLAB controller (communicated via ROS), I get the UR5 to navigate sequentially to four different points.

I would like to do that automatically multiple times from differential initial joint positions. I tried to do it via MATLAB, but I couldn't find a way to set the joints to passive mode (from MATLAB) in order to set their initial positions to a different value.

I am trying to create an add-on lua script to do it but I am facing issues. Here's what I currently have (I just cp-pasted something I found on the website and modified it, I m pretty sure it's not a good way to do it)
However, I am getting the error "Simulation is not stopped. (in function 'sim.closeScene')". Is it that stopping the simulation takes some time so I somehow need to put a waiting command?

Also, how can I specify in the add-on when to stop the simulation? For instance, if I want it to stop after 30 simulation seconds or a specific condition is satisfied in the lua script of the scene.

Thanks.

I am not creating a new topic since this suits my question.

I have a scene with a UR5 robot (force-torque joints) and using OMPL and a MATLAB controller (communicated via ROS), I get the UR5 to navigate sequentially to four different points.

I would like to do that automatically multiple times from differential initial joint positions. I tried to do it via MATLAB, but I couldn't find a way to set the joints to passive mode (from MATLAB) in order to set their initial positions to a different value.

I am trying to create an add-on lua script to do it but I am facing issues. Here's what I currently have (I just cp-pasted something I found on the website and modified it, I m pretty sure it's not a good way to do it)

Code: Select all

```
function sysCall_init()
print("Executing the initialization section. Starting together with CoppeliaSim")
for i=1,10,1 do
sim.loadScene('/home/.../ur5_scene.ttt')
print('scene loaded',i)
jh={-1,-1,-1,-1,-1,-1}
hi = 0.2
lo = -0.2
for i=1,6,1 do
jh[i]=sim.getObjectHandle('UR5_joint'..i)
sim.setJointPosition(jh[i], (hi-lo)*sim.getRandom() + lo)
end
sim.startSimulation()
sim.stopSimulation()
sim.closeScene()
end
end
function sysCall_addOnScriptSuspend()
print("Ending... (triggered by the user)")
return {cmd='cleanup'} -- end this add-on. The cleanup section will be called
end
function sysCall_cleanup()
print("Executing the clean-up section")
end
```

Also, how can I specify in the add-on when to stop the simulation? For instance, if I want it to stop after 30 simulation seconds or a specific condition is satisfied in the lua script of the scene.

Thanks.

### Re: Simulate multiple times the same scenario using CoppeliaSim and MATLAB

Hello,

not sure I caught everything you meant but:

if you need to run several simulation scenarios with different initial conditions, it is indeed best to stop the simulation, change the initial conditions, then start the simulation again. You can automate this of course. From Matlab, you'd call

Your add-on script does not run threaded. That's why also (plus, you should wait until the simulation has really stopped, here too). Using a threaded add-on, something like:

and using a non-threaded add-on:

Above two add-ons, once activated, will constantly start/stop a simulation.

And yes, loading or closing a scene while simulation is not stopped does not work.

Cheers

not sure I caught everything you meant but:

if you need to run several simulation scenarios with different initial conditions, it is indeed best to stop the simulation, change the initial conditions, then start the simulation again. You can automate this of course. From Matlab, you'd call

`simxStopSimulation`

and `simxStartSimulation`

. Be aware that stopping a simulation might take a few simulation steps. Best is to use the last CoppeliaSim release (V4.2.0), which reduces this to a minimum.Your add-on script does not run threaded. That's why also (plus, you should wait until the simulation has really stopped, here too). Using a threaded add-on, something like:

Code: Select all

```
function sysCall_info()
return {autoStart=false}
end
function sysCall_init()
corout=coroutine.create(coroutineMain)
end
function sysCall_actuation()
resumeThread()
end
function sysCall_nonSimulation()
resumeThread()
end
function resumeThread()
if coroutine.status(corout)~='dead' then
local ok,errorMsg=coroutine.resume(corout)
if errorMsg then
error(debug.traceback(corout,errorMsg),2)
end
end
end
function coroutineMain()
while true do
-- Stop the simulation:
sim.stopSimulation()
-- Wait until the simulation has really stopped:
while sim.getSimulationState()~=sim.simulation_stopped do
sim.switchThread()
end
-- Restart the simulation
sim.startSimulation()
end
end
```

Code: Select all

```
function sysCall_info()
return {autoStart=false}
end
function sysCall_init()
end
function sysCall_actuation()
sim.stopSimulation()
end
function sysCall_nonSimulation()
sim.startSimulation()
end
```

And yes, loading or closing a scene while simulation is not stopped does not work.

Cheers

### Re: Simulate multiple times the same scenario using CoppeliaSim and MATLAB

Thanks.

Optimally I would like to do it via MATLAB via simxStopSimulation and simxStartSimulation as you said. However, how do I create different initial conditions for the robot joint positions every time I start a new Simulation?

I need the joints to be in force/torque mode for the controller, which means I cannot use simxSetJointPosition. Is there a way to change the joint mode from force/torque to passive and back from MATLAB? I tried looking at simxSetObjectIntParameter but couldn't find a suitable parameterID.

Thanks again.

Optimally I would like to do it via MATLAB via simxStopSimulation and simxStartSimulation as you said. However, how do I create different initial conditions for the robot joint positions every time I start a new Simulation?

I need the joints to be in force/torque mode for the controller, which means I cannot use simxSetJointPosition. Is there a way to change the joint mode from force/torque to passive and back from MATLAB? I tried looking at simxSetObjectIntParameter but couldn't find a suitable parameterID.

Thanks again.

### Re: Simulate multiple times the same scenario using CoppeliaSim and MATLAB

Yes you can also do that from Matlab directly. From there, also make sure that the simulation has really stopped (e.g. by using

Cheers

`simxGetInMessageInfo`

) before adjusting the initial conditions, and restarting the simulation. When simulation is stopped, you can use `sim.setJointPosition`

and/or `simxSetJointPosition`

. While simulation is running too, but that won't have the desired effect, unless you dynamically reset the joint with `sim.resetDynamicObject`

.Cheers

### Re: Simulate multiple times the same scenario using CoppeliaSim and MATLAB

Thanks. Although I found a way using the threaded add-on that you posted, I would still like to use just MATLAB.

I do the following from MATLAB:
The scene is loaded and the simulation is not running. The variables clientID and jh(i) have value 0 but the result of simxSetJointPosition is 8 and I don't see the change of the joints to 1 in the scene. Am I doing something wrong?

Thanks again.

I do the following from MATLAB:

Code: Select all

```
vrep=remApi('remoteApi');
vrep.simxFinish(-1); % just in case, close all opened connections
clientID=vrep.simxStart('127.0.0.1',19997,true,true,5000,5)
if (clientID>-1)
disp('Connected to remote API server');
end
for i=1:6
jh(i) = vrep.simxGetObjectHandle(clientID,sprintf('UR5_joint%d',i),vrep.simx_opmode_blocking);
vrep.simxSetJointPosition(clientID,jh(i),1,vrep.simx_opmode_blocking);
end
```

Thanks again.

### Re: Simulate multiple times the same scenario using CoppeliaSim and MATLAB

Make sure you understand how the legacy remote API works. e.g. carefully read the documentation for

Cheers

`simxGetObjectHandle`

: the first return value is **not**the handle of the object, but the return code of the call.Cheers

### Re: Simulate multiple times the same scenario using CoppeliaSim and MATLAB

Thank you, I should have checked on that already. I changed to
and it works now.

Code: Select all

```
[res(i),jh(i)] = vrep.simxGetObjectHandle(clientID,sprintf('UR5_joint%d',i),vrep.simx_opmode_blocking);
```