Setting object position in V-REP

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

Setting object position in V-REP

Post by trabias »

Hi!

I have arrays of values calculated in MATLAB for the x, y, and z position and I'm trying to find the best way to send that information to V-REP. My initial idea is to use simSetObjectPosition within a for loop in a child script so that it runs through the all of the values and changes the position in real time. I'm not fluent in C programming, is there a good example that I can follow along?

Thanks!
Sarah

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

Re: Setting object position in V-REP

Post by coppelia »

Hello Sarah,

if you are using Matlab, use the function simxSetObjectPosition:

Code: Select all

vrep.simxSetObjectPosition(clientID,objectHandle,-1,objectPosition,vrep.simx_opmode_oneshot);
Cheers

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

Re: Setting object position in V-REP

Post by trabias »

Thanks for the quick reply! I received this error and was wondering if you had any ideas what is the issue? I tried to adjusting the input for the position array different ways and I keep getting the same error.

No appropriate method, property, or field simxSetObjects for class remApi.
Error in startvrep (line 15)
[errorCode]=vrep.simxSetObjects(clientID,0,-1,[x(100) y(100) z(100)],vrep.simx_opmode_oneshot);
Error in quadrotor_sim (line 8)

Thanks!
Sarah

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

Re: Setting object position in V-REP

Post by coppelia »

Oops, I made a typo, the function is not simxSetObjects, but simxSetObjectPosition.

Cheers

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

Re: Setting object position in V-REP

Post by trabias »

Ohh ok! Thanks! I ran the program now and it gives no issues, but the quadricopter doesn't move.... is there something missing from my code (included below) or should I add something to the child script to read the information given?

Thanks!
Sarah

Code: Select all

function startvrep(xyz)
x=xyz(:,1);
y=xyz(:,2);
z=xyz(:,3);
disp('Program started')
vrep=remApi('remoteApi','extApi.h');
clientID=vrep.simxStart('127.0.0.1',19999,true,true,5000,5);
if (clientID>-1)
    disp('Connected to remote API server');
    [errorCode]=vrep.simxStartSimulation(clientID,vrep.simx_opmode_oneshot_wait);
    [res,objs]=vrep.simxGetObjects(clientID,vrep.sim_handle_all,vrep.simx_opmode_oneshot_wait);
    [errorCode,handle]=vrep.simxGetObjectHandle(clientID,'Quadricopter',vrep.simx_opmode_oneshot)
    disp('Controlling the xyz position')
    for i=1:1601
        [errorCode]=vrep.simxSetObjectPosition(clientID,handle,-1,[x(i) y(i) z(i)],vrep.simx_opmode_oneshot);
    end
    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
        vrep.delete();
disp('Program ended');
end

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

Re: Setting object position in V-REP

Post by coppelia »

You have 3 problems in your code:
  • when retrieving the handle of the object, use a blocking mode operation, i.e. vrep.simx_opmode_oneshot_wait, otherwise you won't retrieve the handle. Make sure you have understood how the remote API operates.
  • you should control the position of Quadricopter_target, not Quadricopter!
  • when sending positions, put some wait commands, otherwise everything is sent and arrives more or less at the same time.
Cheers

cpscps
Posts: 3
Joined: 09 Sep 2020, 11:37

Re: Setting object position in V-REP

Post by cpscps »

Hi Sarah!

I want to use a quadricopter in the same way and I have the same problems so I would like to know if you finally could make your project work.

Thanks in advance!

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

Re: Setting object position in V-REP

Post by coppelia »

Hello,

first you need to determine how you want to connect CoppeliaSim to Matlab. Then, there are several options to transmit data from one to the other.

Cheers

Post Reply