V-REP是一款多功能的機器人仿真器,
1.具有4種物理引擎((ODE, Bullet, Vortex, Newton));
2.支持Windows,Linux,MacOS三種操作系統;
3.支持六種編程方法;
4.七種編程語言( (C/C++、Python、Java、Lua、Matlab、Octave、和 Urbi))。
本文將簡單地介紹如何將MATLAB與V-REP進行通訊,分別實現簡單的讀取機器人關節角,傳送機器人關節角這兩種功能。
一.所需的m文件
在路徑...\V-REP3\V-REP_PRO_EDU\programming\remoteApiBindings\matlab\matlab中將所有的m文件復制到項目文件夾中。
二.關鍵的語句
V-REP端:
simExtRemoteApiStart(19999)
MATLAB端:
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);
三.具體做法
1.在V-REP中新建一個空白的場景,並從模型瀏覽器(Modle Browser)填加一個Baxter機器人。
(此時運行仿真,機器人會運動。我們的目標就是把各個關節角變化的情況記錄下來)
2.點擊控制右臂的相應代碼,在開頭增加一句:
simExtRemoteApiStart(19999)
3.新建一個matlab函數,其代碼如下:
function baxter_read()
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);
r1=[];
r2=[];
r3=[];
r4=[];
r5=[];
r6=[];
r7=[];
k=0;
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);
while(vrep.simxGetConnectionId(clientID) ~= -1), % while v-rep connection is still active
t = vrep.simxGetLastCmdTime(clientID) / 1000.0; % get current simulation time
if (t > 1000) break;
end % stop after t = 1000 seconds
[res,r1angle]=vrep.simxGetJointPosition(clientID,handle_rigArmjoint1,vrep.simx_opmode_oneshot_wait);
[res,r2angle]=vrep.simxGetJointPosition(clientID,handle_rigArmjoint2,vrep.simx_opmode_oneshot_wait);
[res,r3angle]=vrep.simxGetJointPosition(clientID,handle_rigArmjoint3,vrep.simx_opmode_oneshot_wait);
[res,r4angle]=vrep.simxGetJointPosition(clientID,handle_rigArmjoint4,vrep.simx_opmode_oneshot_wait);
[res,r5angle]=vrep.simxGetJointPosition(clientID,handle_rigArmjoint5,vrep.simx_opmode_oneshot_wait);
[res,r6angle]=vrep.simxGetJointPosition(clientID,handle_rigArmjoint6,vrep.simx_opmode_oneshot_wait);
[res,r7angle]=vrep.simxGetJointPosition(clientID,handle_rigArmjoint7,vrep.simx_opmode_oneshot_wait);
r1= [r1 r1angle];
r2= [r2 r2angle];
r3= [r3 r3angle];
r4= [r4 r4angle];
r5= [r5 r5angle];
r6= [r6 r6angle];
r7= [r7 r7angle];
k=k+1 %to test
end
r=[r1' r2' r3' r4' r5' r6' r7'];
fid=fopen('angle.txt','wt');
[m,n]=size(r);
for i=1:1:m
for j=1:1:n
if j==n
fprintf(fid,'%g\n',r(i,j));
else
fprintf(fid,'%g\t',r(i,j));
end
end
end
fclose(fid);
% 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
4.運行V-REP仿真,同時運行MATLAB。如果運行成功,會生成一個angle的txt文件,導入到MATLAB可以觀測到到Baxter機器人7個關節角的變化如圖所示:
5.接下來嘗試將這組關節角輸入到V-REP中,控制機器人運動。
首先是再建一個V-REP場景,添加Baxter機器人,把機器人右臂相關的代碼刪除,添加上下面一句:
simExtRemoteApiStart(19999)
6.新建一個MATLAB函數,其代碼如下:
function baxter_write()
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);
%read the joint angle data from 'angle.txt'
jointValue=load('angle.txt'); %A matrix of 7 x 150.Each column vector recorded the changes of each joint Angle
[m n]=size(jointValue);
if (clientID>-1)
disp('Connected to remote API server');
% get handle for Baxter_rightArm_joint1
[res,handle_rightArmjoint1] = vrep.simxGetObjectHandle(clientID,'Baxter_rightArm_joint1',vrep.simx_opmode_oneshot_wait);
[res,handle_rightArmjoint2] = vrep.simxGetObjectHandle(clientID,'Baxter_rightArm_joint2',vrep.simx_opmode_oneshot_wait);
[res,handle_rightArmjoint3] = vrep.simxGetObjectHandle(clientID,'Baxter_rightArm_joint3',vrep.simx_opmode_oneshot_wait);
[res,handle_rightArmjoint4] = vrep.simxGetObjectHandle(clientID,'BaxterrightArm_joint4',vrep.simx_opmode_oneshot_wait);
[res,handle_rightArmjoint5] = vrep.simxGetObjectHandle(clientID,'Baxter_rightArm_joint5',vrep.simx_opmode_oneshot_wait);
[res,handle_rightArmjoint6] = vrep.simxGetObjectHandle(clientID,'Baxter_rightArm_joint6',vrep.simx_opmode_oneshot_wait);
[res,handle_rightArmjoint7] = 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
for i=1:m
vrep.simxPauseCommunication(clientID,1);
vrep.simxSetJointTargetPosition(clientID,handle_rightArmjoint1,jointValue(i,1),vrep.simx_opmode_oneshot);
vrep.simxSetJointTargetPosition(clientID,handle_rightArmjoint2,jointValue(i,2),vrep.simx_opmode_oneshot);
vrep.simxSetJointTargetPosition(clientID,handle_rightArmjoint3,jointValue(i,3),vrep.simx_opmode_oneshot);
vrep.simxSetJointTargetPosition(clientID,handle_rightArmjoint4,jointValue(i,4),vrep.simx_opmode_oneshot);
vrep.simxSetJointTargetPosition(clientID,handle_rightArmjoint5,jointValue(i,5),vrep.simx_opmode_oneshot);
vrep.simxSetJointTargetPosition(clientID,handle_rightArmjoint6,jointValue(i,6),vrep.simx_opmode_oneshot);
vrep.simxSetJointTargetPosition(clientID,handle_rightArmjoint7,jointValue(i,7),vrep.simx_opmode_oneshot);
vrep.simxPauseCommunication(clientID,0);
pause(0.1);
end
vrep.simxGetConnectionId(clientID)=1;
end
% 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
7.運行V-REP仿真,再運行MATLAB函數。
如果運行成功,會發現機器人重復之前的運動。
以上的項目文件可以在Github中獲取:
https://github.com/scutXuYang/Communication-between-MATALB-and-V-REP