[机器人仿真软件(一)]V-REP与MATLAB进行通讯的方法

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

 

posted on 2017-08-14 00:13  剑本凡铁  阅读(15387)  评论(6编辑  收藏  举报