V-rep Hexapod tutorial(3)-- Matlab简单控制
1.Matlab联合仿真环境搭建 (1)设置Matlab端
-
找到安装CoppeliaSim的位置 我的位置如下: C:\Program Files\CoppeliaRobotics\CoppeliaSimEdu\programming\remoteApiBindings\matlab\matlab -
把里面的全部文件拷贝到文件夹1 -
把reomteApi.dll导入到文件夹1 reomteApi.dll文件在v-rep的安装目录下(C:\Program Files\CoppeliaRobotics\CoppeliaSimEdu\programming\remoteApiBindings\lib\lib\Windows) -
把之前搭建好的Hexapod文件复制到文件夹1 下图中重命名为testOneLeg (2)设置仿真文件端 在hexa_body的脚本文件中添加一句话用以跟matlab通讯 2.Matlab代码 简单控制Hexapod的一条腿运动
disp('Program started');
vrep=remApi('remoteApi');
vrep.simxFinish(-1);
clientID=vrep.simxStart('127.0.0.1',19999,true,true,5000,5);
disp(clientID);
if (clientID>-1)
disp('Connected to remote API server');
vrep.simxStartSimulation(clientID,vrep.simx_opmode_oneshot); %开启仿真
%-------------------------------声明节点,初始化对象----------------------------------
[rec ,antBase] =vrep.simxGetObjectHandle (clientID,'hexa_legBase',vrep.simx_opmode_blocking);
[rec ,rb_rot_3]=vrep.simxGetObjectHandle (clientID,'rb_rot_3',vrep.simx_opmode_blocking);
[rec ,legTips1]=vrep.simxGetObjectHandle(clientID,'hexa_footTip0',vrep.simx_opmode_blocking);
[rec ,legTips2]=vrep.simxGetObjectHandle(clientID,'hexa_footTip1',vrep.simx_opmode_blocking);
[rec ,legTips3]=vrep.simxGetObjectHandle(clientID,'hexa_footTip2',vrep.simx_opmode_blocking);
[rec ,legTips4]=vrep.simxGetObjectHandle(clientID,'hexa_footTip3',vrep.simx_opmode_blocking);
[rec ,legTips5]=vrep.simxGetObjectHandle(clientID,'hexa_footTip4',vrep.simx_opmode_blocking);
[rec ,legTips6]=vrep.simxGetObjectHandle(clientID,'hexa_footTip5',vrep.simx_opmode_blocking);
[rec ,legTargets1]=vrep.simxGetObjectHandle(clientID,'hexa_footTarget0',vrep.simx_opmode_blocking);
[rec ,legTargets2]=vrep.simxGetObjectHandle(clientID,'hexa_footTarget1',vrep.simx_opmode_blocking);
[rec ,legTargets3]=vrep.simxGetObjectHandle(clientID,'hexa_footTarget2',vrep.simx_opmode_blocking);
[rec ,legTargets4]=vrep.simxGetObjectHandle(clientID,'hexa_footTarget3',vrep.simx_opmode_blocking);
[rec ,legTargets5]=vrep.simxGetObjectHandle(clientID,'hexa_footTarget4',vrep.simx_opmode_blocking);
[rec ,legTargets6]=vrep.simxGetObjectHandle(clientID,'hexa_footTarget5',vrep.simx_opmode_blocking);
[rec ,initialPos1]=vrep.simxGetObjectPosition(clientID,legTips1,antBase,vrep.simx_opmode_blocking);
[rec ,initialPos2]=vrep.simxGetObjectPosition(clientID,legTips2,antBase,vrep.simx_opmode_blocking);
[rec ,initialPos3]=vrep.simxGetObjectPosition(clientID,legTips3,antBase,vrep.simx_opmode_blocking);
[rec ,initialPos4]=vrep.simxGetObjectPosition(clientID,legTips4,antBase,vrep.simx_opmode_blocking);
[rec ,initialPos5]=vrep.simxGetObjectPosition(clientID,legTips5,antBase,vrep.simx_opmode_blocking);
[rec ,initialPos6]=vrep.simxGetObjectPosition(clientID,legTips6,antBase,vrep.simx_opmode_blocking);
legMovementIndex={1,4,2,6,3,5};
stepVelocity=0.5;
stepAmplitude=0.16;
stepHeight=0.04;
movementDirection=0*pi/180;
rotation=0;
movementStrength=1;
stepProgression=0;
realMovementStrength=0;
k = 0;
%--------------------------------实际控制----------------------------------
%注意:!!!!这里位置的表示要用中括号
while 1
if k < 20
p=[0.2039999961853,-7.4505805969238e-09,6.2592327594757e-05];
vrep.simxSetObjectPosition(clientID,legTargets1,antBase,p,vrep.simx_opmode_oneshot);
elseif k < 30
p=[0.25250998136207,-7.4505805969238e-09,0.0024225975695262];
vrep.simxSetObjectPosition(clientID,legTargets1,antBase,p,vrep.simx_opmode_oneshot);
else
p=[0.20795997916936,-7.4505805969238e-09,0.018622598092557];
vrep.simxSetObjectPosition(clientID,legTargets1,antBase,p,vrep.simx_opmode_oneshot);
k = 0;
end
k=k+1;
pause(0.01)
end
end
3.运行结果
|