About using MATLAB to get joint angle and set joint angle

Typically: "How do I... ", "How can I... " questions
youngsea
Posts: 18
Joined: 11 Apr 2016, 12:40

About using MATLAB to get joint angle and set joint angle

Hi~
I use other devices to control the robot in V-rep.Now I want to get the data of the joint angle changing with time during the simulation.
And then I want to control the robot using the joint angle obtained by the above method.
Which remote API functions (Matlab) can do that?simxGetJointPosition and simxSetJointPosition?

youngsea
Posts: 18
Joined: 11 Apr 2016, 12:40

Re: About using MATLAB to get joint angle and set joint angle

I write a matlab function as follows,

Code: Select all

function baxter()
disp('Program started');
% vrep=remApi('remoteApi','extApi.h'); % using the header (requires a compiler)
vrep=remApi('remoteApi'); % using the prototype file (remoteApiProto.m)
vrep.simxFinish(-1); % just in case, close all opened connections
clientID=vrep.simxStart('127.0.0.1',19999,true,true,5000,5);

if (clientID>-1)
disp('Connected to remote API server');

%-- Handle of the right arm joint
rightArmjoint1=vrep.simxGetObjectHandle(clientID,'Baxter_rightArm_joint1',vrep.simx_opmode_blocking);
rightArmjoint2=vrep.simxGetObjectHandle(clientID,'Baxter_rightArm_joint2',vrep.simx_opmode_blocking);

%-- Handle of the left arm joint
leftArmjoint1=vrep.simxGetObjectHandle(clientID,'Baxter_leftArm_joint1',vrep.simx_opmode_blocking);
leftArmjoint2=vrep.simxGetObjectHandle(clientID,'Baxter_leftArm_joint2',vrep.simx_opmode_blocking);

%Now read right arm joint angle
[res,r1angle]=vrep.simxGetJointPosition(clientID,rightArmjoint1,vrep.simx_opmode_blocking);
%to test
r1angle
[res,r2angle]=vrep.simxGetJointPosition(clientID,rightArmjoint2,vrep.simx_opmode_blocking);
r2angle
%Now read left arm joint angle
[res,l1angle]=vrep.simxGetJointPosition(clientID,leftArmjoint1,vrep.simx_opmode_blocking);
[res,l2angle]=vrep.simxGetJointPosition(clientID,leftArmjoint2,vrep.simx_opmode_blocking);

% Before closing the connection to V-REP, make sure that the last command sent out had time to arrive. You can guarantee this with (for example):
vrep.simxGetPingTime(clientID);

% Now close the connection to V-REP:
vrep.simxFinish(clientID);
else
disp('Failed connecting to remote API server');
end
vrep.delete(); % call the destructor!

disp('Program ended');
end

And then run the demo Baxter and the Matlab fuction,the result as follows,

r1angle =
43
r2angle =
43

While I use the code as follow,

Code: Select all

[res,r1angle]=vrep.simxGetJointPosition(clientID,rightArmjoint1,vrep.simx_opmode_streaming);
while res ~= 0
[res,r1angle]=vrep.simxGetJointPosition(clientID,rightArmjoint1,vrep.simx_opmode_buffer);
r1angle
end

The result is,
r1angle =
43
r1angle =
43
.......
This value will not change or end.
How can I read the date in a certain sampling time？
Thanks!

youngsea
Posts: 18
Joined: 11 Apr 2016, 12:40

Re: About using MATLAB to get joint angle and set joint angle

The question about using MATLAB to get joint angle has been solved.The main code of Matlab is shown below

Code: Select all

   if (clientID>-1)
disp('Connected to remote API server');
% get handle for Baxter_rightArm_joint1
[res,handle_rigArmjoint1] = vrep.simxGetObjectHandle(clientID,'Baxter_rightArm_joint1',vrep.simx_opmode_oneshot_wait);
while(vrep.simxGetConnectionId(clientID) ~= -1),  % while v-rep connection is still active
t = vrep.simxGetLastCmdTime(clientID) / 1000.0;  % get current simulation time
if (t > 60) break;
end  % stop after t = 60 seconds
[res,r1angle]=vrep.simxGetJointPosition(clientID,handle_rigArmjoint1,vrep.simx_opmode_oneshot_wait);
r1= [r1 r1angle];
end

fid = fopen('r.txt','wt');
fprintf(fid,'%g\n',r);
fclose(fid);

And then I write another M file to set joint angle.Run the V-REP and MATLAB,the robot in another scene can move.However,when the robot moves, its jitter is too great.How can I improve it~Thanks~

Code: Select all

      %read the joint angle data from 'r.txt'
jointValue=load('r.txt');   %A matrix of 7 x 150.Each column vector recorded the changes of each joint Angle

if (clientID>-1)
disp('Connected to remote API server');
% get handle for Baxter_rightArm_joint1
[res,handle_rigArmjoint1] = vrep.simxGetObjectHandle(clientID,'Baxter_rightArm_joint1',vrep.simx_opmode_oneshot_wait);
[res,handle_rigArmjoint2] = vrep.simxGetObjectHandle(clientID,'Baxter_rightArm_joint2',vrep.simx_opmode_oneshot_wait);
[res,handle_rigArmjoint3] = vrep.simxGetObjectHandle(clientID,'Baxter_rightArm_joint3',vrep.simx_opmode_oneshot_wait);
[res,handle_rigArmjoint4] = vrep.simxGetObjectHandle(clientID,'Baxter_rightArm_joint4',vrep.simx_opmode_oneshot_wait);
[res,handle_rigArmjoint5] = vrep.simxGetObjectHandle(clientID,'Baxter_rightArm_joint5',vrep.simx_opmode_oneshot_wait);
[res,handle_rigArmjoint6] = vrep.simxGetObjectHandle(clientID,'Baxter_rightArm_joint6',vrep.simx_opmode_oneshot_wait);
[res,handle_rigArmjoint7] = vrep.simxGetObjectHandle(clientID,'Baxter_rightArm_joint7',vrep.simx_opmode_oneshot_wait);

%Set the position of every joint
while(vrep.simxGetConnectionId(clientID) ~= -1),  % while v-rep connection is still active
t = vrep.simxGetLastCmdTime(clientID) / 1000.0;  % get current simulation time
if (t > 60) break;
end  % stop after t = 60 seconds
for i=1:100
vrep.simxPauseCommunication(clientID,1);
vrep.simxSetJointTargetPosition(clientID,handle_rigArmjoint1,jointValue(i,1),vrep.simx_opmode_oneshot);
vrep.simxSetJointTargetPosition(clientID,handle_rigArmjoint2,jointValue(i,2),vrep.simx_opmode_oneshot);
vrep.simxSetJointTargetPosition(clientID,handle_rigArmjoint3,jointValue(i,3),vrep.simx_opmode_oneshot);
vrep.simxSetJointTargetPosition(clientID,handle_rigArmjoint4,jointValue(i,4),vrep.simx_opmode_oneshot);
vrep.simxSetJointTargetPosition(clientID,handle_rigArmjoint5,jointValue(i,5),vrep.simx_opmode_oneshot);
vrep.simxSetJointTargetPosition(clientID,handle_rigArmjoint6,jointValue(i,6),vrep.simx_opmode_oneshot);
vrep.simxSetJointTargetPosition(clientID,handle_rigArmjoint7,jointValue(i,7),vrep.simx_opmode_oneshot);
vrep.simxPauseCommunication(clientID,0);
end
end


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

Re: About using MATLAB to get joint angle and set joint angle

Hello,

in the first step, you are reading the joint values from V-REP. You are not using the data streaming, so things might get quite slow (for now you are only reading one joint, but as soon as you are trying to read several joints, things will get slow with the blocking method).

When you apply joints, you almost apply them at once, you don't have any timing: your loop that starts with
for i=1:100
will almost instantaneously execute. You need to put some waits in there, or check the V-REP side timing to know if you are ready to send the next joint positions.

Cheers

youngsea
Posts: 18
Joined: 11 Apr 2016, 12:40

Re: About using MATLAB to get joint angle and set joint angle

Using the Matlab function pause(),the results have been improved.And I will I will follow your advice to test it.
I have a questions about using two remote API in one child script.
simExtRemoteApiStart(19999) is used to connect a C++ application,while simExtRemoteApiStart(19998) is used to connect Matlab.
Can it work in the same script?
Thank you very much!

coppelia