介绍
v-rep 内嵌的代码时用lua编写的,这个lua小巧但毕竟小众,而我们很多时候分析处理数据时会用到别人的库,一般这些库只能找到通用编程语言的版本,如c/c++/python/matlab等。或者说使用以前已有的工作去控制v-rep时用lua重写代码会耗费很多时间。v-rep通过计算机网络的网络层(端口)以sever-client的形式和其他程序进行通讯控制,很大程度上提高了v-rep的使用便利性。当然,通过端口进行网络通讯是会有时延的,对于实时性要求较高的任务,到最后可能还是会绕回去lua直接实现,v-rep好像是有通过内存直接通讯的,这样会快很多,后面可能会接触到。
初试牛刀使用matlab控制上篇博客设计的小车。
辅助文件准备
matlab 和 C++类似 需要调用头文件和动态库以快速利用端口对v-rep进行通讯
matlab 把当前的运行的m文件所在目录添加到搜索目录中,因此只要把API相应的头文件和dll和脚本放一个目录下即可
新建目录 \v-rep-matlab-test
把 C:\Program Files\V-REP3\V-REP_PRO_EDU\programming\remoteApiBindings\matlab\matlab 下所有文件
拷贝到 \v-rep-matlab-test 中
把 C:\Program Files\V-REP3\V-REP_PRO_EDU\programming\remoteApiBindings\lib\lib\Windows\64Bit 中的dll
拷贝到 \v-rep-matlab-test 中
新建脚本 \v-rep-matlab-test\test.m
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',19997,true,true,5000,5);
if (clientID<0)
disp('Failed connecting to remote API server');
else
vrep.simxStartSimulation(clientID,vrep.simx_opmode_oneshot);
[res,MotorHandle_Left] = vrep.simxGetObjectHandle(clientID,'Leftmotor',vrep.simx_opmode_blocking);
[res,MotorHandle_Right] = vrep.simxGetObjectHandle(clientID,'Rightmotor',vrep.simx_opmode_blocking);
tic;
Left_vel = 20;
Right_vel = -20;
while toc<100
vrep.simxSetJointTargetVelocity(clientID,MotorHandle_Left,Left_vel,vrep.simx_opmode_oneshot );
vrep.simxSetJointTargetVelocity(clientID,MotorHandle_Right,Right_vel,vrep.simx_opmode_oneshot );
pause(0.1);
end
end
vrep.simxStopSimulation(clientID,vrep.simx_opmode_oneshot_wait);
vrep.simxFinish(clientID);
vrep.delete(); % call the destructor!
运行该matlab脚本,即可控制v-rep仿真启动停止,以及期间的速度控制。