Typically: "How do I... ", "How can I... " questions
trabias
Posts: 11
Joined: 23 Jul 2013, 23:50

Hi!

I am new to V-REP and I was wondering if it was possible to use a Simulink file through MATLAB to run the simulation in V-REP. Any input would be greatly appreciated!

Thanks!
Sarah

coppelia
Posts: 8223
Joined: 14 Dec 2012, 00:25

Hello Sarah,

This should be no problem. Have a look at the remote API, its Matlab binding and its Matlab API. From there, there should be no difficulty to refer to those API functions from within a Simulink file, have a look here for instance.

Cheers

trabias
Posts: 11
Joined: 23 Jul 2013, 23:50

Thanks so much!! All of those files were very helpful! I had another question. My simulink file models the motion and dynamics of a quadrotor. Do you know what the best way to send the information calculated from MATLAB to V-REP and have the simulation be controlled by the simulink? Also, should this portion of the code be added after the simulink model has done all of the calculations?

Thanks!
Sarah

coppelia
Posts: 8223
Joined: 14 Dec 2012, 00:25

Hello Sarah,

You can do the high-level control of the quadrotor via Simulink and the remote API. Typically you can send data to your objects/models with simxSetIntegerSignal, simxSetFloatSignal, simxSetStringSignal, or simxAppendStringSignal.

For the low-level control (which has to happen synchronously with the simulation loop and much faster), you should use a child script if possible. You could also do it via the remote API, but in that case you should enable the synchronous mode with the functions simxSynchronous and simxSynchronousTrigger. But your simulation will run slowlier (since after each simulation step it will wait for a trigger signal).

Cheers

trabias
Posts: 11
Joined: 23 Jul 2013, 23:50

Thanks!! I'm attempting to use the simxSetIntegerSignal to control the x position of the quadrotor, however when I run my Simulink, I get this error about the remapi.m file: "Code generation does not support the 'delete' method. Handle objects are only destroyed when program execution ends, and destructors are never called." Below is my code so far, I modeled it off of the simpletest MATLAB file found in the V-REP programming folder. Any ideas of what could be missing or incorrect?

Thanks!
Sarah

Code: Select all

function startvrep(x,y,z,phi,th,psi)
disp('Program started');
vrep=remApi('remoteApi','extApi.h');
clientID=vrep.simxStart('10.18.6.99',1,true,true,5000,5);
if (clientID>-1)
[errorCode]=vrep.simxStartSimulation(clientID,vrep.simx_opmode_oneshot);
[errorCode]=simxSetIntegerSignal(clientID,'x',x,vrep.simx_opmode_oneshot)
if (res==vrep.simx_error_noerror)
fprintf('Number of objects in the scene: %d\n',length(objs));
else
fprintf('Remote API function call returned with error code: %d\n',res);
end
vrep.simxFinish(clientID);
else
disp('Failed connecting to remote API server');
end
[errorCode]=vrep.simxStopSimulation(clientID,vrep.simx_opmode_oneshot);
vrep.simxFinish(clientID);
vrep.delete();
disp('Program ended');
end

coppelia
Posts: 8223
Joined: 14 Dec 2012, 00:25

Hello Sarah,

could you try to remove the vrep.delete(); instruction and see if there is a difference? Or possibly also remove the delete function in the remApi.m file just to see?

Are you generating any MEX-file?

Cheers

trabias
Posts: 11
Joined: 23 Jul 2013, 23:50

I have commented the two delete functions and it seems that MATLAB doesn't recognize the command remApi (error: "Undefined function or variable 'remApi'.") I don't think I am generating an MEX-file... could that be the issue?

Thanks!
Sarah

coppelia
Posts: 8223
Joined: 14 Dec 2012, 00:25

Your program looks weird, you should remove the 2 following lines near the end, since at that position the connection has already ended:

Code: Select all

[errorCode]=vrep.simxStopSimulation(clientID,vrep.simx_opmode_oneshot);
vrep.simxFinish(clientID);
(you should only call simxFinish once, and only if clientID was not -1)

also, after loading the scene, your program will prepare to send 2 commands (start simulation and set integer signal) and directly after end the connection with simxFinish. The result is that those 2 commands won't have the time to be sent! (unless you send them with the operation mode simx_opmode_oneshot_wait)

If you run the function from Matlab directly (i.e. not simulink), do you get the same error?
If you run the simpleTest.m function, do you get the same error?

Cheers

trabias
Posts: 11
Joined: 23 Jul 2013, 23:50

Oh I didn't notice I had it in there twice! I've removed those lines and added the newer operation modes. When I run either of the programs in MATLAB, I receive this error:

Code: Select all

>> simpleTest
Program started
A 'Selected' compiler was not found.  You may need to run mex -setup.

Error in remApi (line 568)

Error in simpleTest (line 21)
vrep=remApi('remoteApi','extApi.h');
I have tried to look up how to implement a mex-setup but I couldn't find anything that explained this. Do you know where I can read about this or how to do so?

Thanks!
Sarah

coppelia
Posts: 8223
Joined: 14 Dec 2012, 00:25