1、matlab与vrep的通信,好些博主都写过了,简单附上代码
vrep端
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',19997,true,true,5000,5);
%将remApi.m、remoteApiProto.m和remoteApi.dll添加进对应的文件夹下
2、运行代码angle
不同的机器人,需要将关节对应的名称改一致,要不会在写入的angle文本里,出现一堆43......
function position_read()
clear all
clc
disp('Program started');
vrep=remApi('remoteApi'); % using the prototype file (remoteApiProto.m)
% vrep=remApi('remoteApi','extApi.h'); % using the header (requires a compiler)
vrep.simxFinish(-1); % just in case, close all opened connections
clientID=vrep.simxStart('127.0.0.1',19997,true,true,5000,5);
%创建空矩阵用于接收关节位置
r1=[];
r2=[];
r3=[];
r4=[];
r5=[];
r6=[];
if (clientID>-1)
disp('Connected to remote API server');
% vrep.simxSynchronous(clientID,true);%是否要开启同步,不同步的话可能有失真,不清楚这个
vrep.simxStartSimulation(clientID,vrep.simx_opmode_oneshot);%启动仿真
%下面的 NiryoOneJoint1 一定要和vrep里采用的模型的关节一致,要不就会出现43......
[res,NiryoOneJoint1] = vrep.simxGetObjectHandle(clientID,'NiryoOneJoint1',vrep.simx_opmode_oneshot_wait);
[res,NiryoOneJoint2] = vrep.simxGetObjectHandle(clientID,'NiryoOneJoint2',vrep.simx_opmode_oneshot_wait);
[res,NiryoOneJoint3] = vrep.simxGetObjectHandle(clientID,'NiryoOneJoint3',vrep.simx_opmode_oneshot_wait);
[res,NiryoOneJoint4] = vrep.simxGetObjectHandle(clientID,'NiryoOneJoint4',vrep.simx_opmode_oneshot_wait);
[res,NiryoOneJoint5] = vrep.simxGetObjectHandle(clientID,'NiryoOneJoint5',vrep.simx_opmode_oneshot_wait);
[res,NiryoOneJoint6] = vrep.simxGetObjectHandle(clientID,'NiryoOneJoint6',vrep.simx_opmode_oneshot_wait);
% 下面是某博主采用的批量获取对象句柄
% NiryoOneJoint=zeros(1,6);
% for i=1:6
% [res,NiryoOneJoint(i)] = vrep.simxGetObjectHandle(clientID,['handle_joint' num2str(i)],vrep.simx_opmode_blocking);
% end
while(vrep.simxGetConnectionId(clientID) ~= -1), % while v-rep connection is still active
t = vrep.simxGetLastCmdTime(clientID) / 1000.0; % get current simulation time
if (t > 30) break;
end % stop after t = 30 seconds 建议时间设的小点
[res,r1angle]=vrep.simxGetJointPosition(clientID,NiryoOneJoint1,vrep.simx_opmode_oneshot_wait);
[res,r2angle]=vrep.simxGetJointPosition(clientID,NiryoOneJoint2,vrep.simx_opmode_oneshot_wait);
[res,r3angle]=vrep.simxGetJointPosition(clientID,NiryoOneJoint3,vrep.simx_opmode_oneshot_wait);
[res,r4angle]=vrep.simxGetJointPosition(clientID,NiryoOneJoint4,vrep.simx_opmode_oneshot_wait);
[res,r5angle]=vrep.simxGetJointPosition(clientID,NiryoOneJoint5,vrep.simx_opmode_oneshot_wait);
[res,r6angle]=vrep.simxGetJointPosition(clientID,NiryoOneJoint6,vrep.simx_opmode_oneshot_wait);
r1= [r1 r1angle];
r2= [r2 r2angle];
r3= [r3 r3angle];
r4= [r4 r4angle];
r5= [r5 r5angle];
r6= [r6 r6angle];
end
r=[r1' r2' r3' r4' r5' r6'];
% 下面的代码参考的这位博主的,写入文本形式下,
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.simxStopSimulation(clientID,vrep.simx_opmode_oneshot_wait); %停止仿真
vrep.simxFinish(clientID);
else
disp('Failed connecting to remote API server');
end
vrep.delete(); % call the destructor!
disp('Program ended');
end
3、运行结果
vrep端
matlab端
matlab端的数据跟vrep端的不一样,(不清楚),具体再了解
4、参考博主
感谢这些博主的付出,有问题及时交流哈!
(1)[机器人仿真软件(一)]V-REP与MATLAB进行通讯的方法_weixin_34144450的博客-CSDN博客
5、接下来计划
获取关节力矩,matlab控制vrep进行力控,主要应用在抓取作业中。