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

时间: 2024-08-26 01:29:37

V-REP与MATLAB进行通讯的方法的相关文章

【转】matlab读取文件各种方法

本技术支持指南主要处理:ASCII, binary, and MAT files.要得到MATLAB中可用来读写各种文件格式的完全函数列表,可以键入以下命令: help iofun MATLAB中有两种文件I/O程序:high level and low level. High level routines: 包括现成的函数,可以用来读写特殊格式的数据,并且只需要少量的编程.Low level routines: 可以更加灵活的完成相对特殊的任务,需要较多的额外编程. High level ro

phpcms v9 中 PHPSSO无法通讯解决方法(PHPSSO无法通讯间接影响phpcms v9注册会员无法通过“重名无法通过”)

问题的由来 怎么样使用 Cocos2d-x 快速开发游戏,方法很简单,你可以看看其自带的例程,或者从网上搜索教程,运行起第一个HelloWorld,然后在 HelloWorld 里面写相关逻辑代码,添加我们的层.精灵等 ~ 我们并不一定需要知道 Cocos2d-x 是如何运行或者在各种平台之上运行,也不用知道 Cocos2d-x 的游戏是如何运行起来的,它又是如何渲染界面的 ~~~ 两个入口 程序入口的概念是相对的,AppDelegate 作为跨平台程序入口,在这之上做了另一层的封装,封装了不同

Matlab调用外部库函数方法和注意事项

在MATLAB环境下访问外部函数的共享库文件,必须首先把该库文件加载到内存中.一旦加载成功,就 能直接在MATLAB中直接请求关于函数的任何信息.而当不再需要该库时,就应当及时把库文件从内存 中卸载以节省内存开销. 加载库 加载库加载库 加载库 语法:loadlibrary('shrlib','hfile') 其中shrlib为加载的动态链接库文件名(filename.dll),hfile为头文件名,它包含函数原型.例如,当加载包 含MATLAB中mx程序的libmx库时,可以使用下列语句. h

matlab中plot使用方法

MATLAB有非常强的图形功能,能够方便地实现数据的视觉化.强大的计算功能与图形功能相结合为MATLAB在科学技术和教学方面的应用提供了更加广阔的天地.以下着重介绍二维图形的画法,对三维图形仅仅作简单叙述. 5.1 二维图形的绘制 5.1.1 基本形式 二维图形的绘制是MATLAB语言图形处理的基础,MATLAB最经常使用的画二维图形的命令是plot,看两个简单的样例: >> y=[0 0.58 0.70 0.95 0.83 0.25]; >> plot(y) 生成的图形见图5-1

Matlab中函数定义方法

Matlab自定义函数的六种方法 n1.函数文件+调用函数(命令)文件:需单独定义一个自定义函数的M文件: n2.函数文件+子函数:定义一个具有多个自定义函数的M文件: n3.Inline:无需M文件,直接定义: n4.匿名函数: n5.Syms+subs:无需M文件,直接定义: n6.字符串+subs:无需M文件,直接定义. ------------ 1.函数文件+调用函数文件:定义多个M文件 % 调用函数文件:myfile.m clear clc for t=1:10 y=mylfg(t);

两种改变matlab默认目录的方法

修改matlab的默认打开路径,即自己想要的工作目录,省去自己改来改去的麻烦,怎样才能设置成自己想要的目录呢,如下两种方法,本人比较喜欢第一种: 1.Matlab的快捷方式 ->  属性 ->  起始位置 -> 更改为所希望的默认目录 2.找到安装目录下Matlab里的toolbox\local\matlabrc.m ,打开,在文件最后新增cd yourpath(你要设置的工作目录),保存即可.

使用GCJ编译Java程序供Matlab调用Java对象方法实践

1 引言 以Matlab作为开发平台,进行数值计算,具有直接.高效的特点. 然而,在面向对象程序设计方面,现有的Matlab支持特性在开发和运行效率上并不高. 将Java语言面向对象及其平台特性引入Matlab,能够拓展其模型表达能力与手段.此外,Java本身的特性,也决定了其具有良好的开发性能. 与C语言编写mex动态链接库的开发目的不同(mex主要以速度见长),基于Java的matlab扩展能为matlab带来更大的灵活性.甚至于,Java平台下更多的类库,能够丰富matlab平台功能. 2

Matlab三种归一化方法

归一化的具体作用是归纳统一样本的统计分布性.归一化在0-1之间是统计的概率分布,归一化在-1--+1之间是统计的坐标分布.归一化有同一.统一和合一的意思.无论是为了建模还是为了计算,首先基本度量单位要同一,神经网络是以样本在事件中的统计分别几率来进行训练(概率计算)和预测的,且sigmoid函数的取值是0到1之间的,网络最后一个节点的输出也是如此,所以经常要对样本的输出归一化处理.归一化是统一在0-1之间的统计概率分布,当所有样本的输入信号都为正值时,与第一隐含层神经元相连的权值只能同时增加或减

C#调用C++的dll及MATLAB的dll的方法(一)

为了记录踩坑的过程,避免以后再踩坑,居然专门开通了这么专业的技术博客,正好督促自己以后好好研究技术. 最近需要做一个界面系统来包装一下之前做的人脸属性识别的模型,希望用户随机选取一张图像(后面会实现摄像头拍现场图像),系统自动给出该图像中所包含的人脸属性(有没有戴眼镜,有没有戴帽子之类的).其中人脸属性预测之前需要进行人脸识别以及人脸对齐等操作,人脸属性识别是由C++写的,人脸对齐是由MATLAB写的,考虑到界面的友好性以及开发的难易性,最终选了C#作为开发语言.(不要问我为啥不用Java,因为