MATLAB机器⼈⼯具箱--双⾜机器⼈建模2、代码:
%% creat walking robot model
clear all
%leg length
L1=0.15;L2=0.25;
%form a leg
leg=SerialLink([0, 0, 0, pi/2; 0, 0, L1, 0; 0, 0, -L2, 0 ],...
'name', 'leg', 'base', eye(4,4),'tool', ...
trotz(-pi/2)*trotx(-pi/2)*trotz(-pi/2),'offset', [pi/2  0  -pi/2]);
%% diplay the leg
%body wide and length
W = 0.2; L = 0.2;
%form a body
legs(1) = SerialLink(leg, 'name', 'leg1','base', transl(0, 0, -0.05)*trotz(pi/2));
legs(2) = SerialLink(leg, 'name', '.', 'base', transl(0, -W, -0.05)*trotz(pi/2));
% create a fixed size axis for the robot, and set z positive downward
clf;
axis([-0.3 0.25 -0.6 0.4 -0.19 0.45]); set(gca,'Zdir', 'reverse')
hold on
legs(1).plot([0 pi/3 -pi/2.5],'nobase','noshadow','nowrist');%leg pose
legs(2).plot([0 pi/1.8 -pi/2.5],'nobase','noshadow','nowrist');
%plot body
plotcube([0.1 0.2 -0.12],[ -0.05  -0.2  0],1,[1 1 0]);
%% simulate moving
for i=0.01:0.02:0.4
legs(1).plot([0  pi/3+i    -pi/2.5-i],'nobase','noshadow');%leg pose
legs(2).plot([0  pi/1.8-i  -pi/2.5+i],'nobase','noshadow');
end
plotcube画⽅块函数:
function plotcube(varargin)
% PLOTCUBE - Display a 3D-cube in the current axes
%
%  PLOTCUBE(EDGES,ORIGIN,ALPHA,COLOR) displays a 3D-cube in the current axes %  with the following properties:
%  * EDGES : 3-elements vector that defines the length of cube edges
%  * ORIGIN: 3-elements vector that defines the start point of the cube
%  * ALPHA : scalar that defines the transparency of the cube faces (from 0
%            to 1)
%  * COLOR : 3-elements vector that defines the faces color of the cube
%
% Example:
%  >> plotcube([5 5 5],[ 2  2  2],.8,[1 0 0]);
%  >> plotcube([5 5 5],[10 10 10],.8,[0 1 0]);
%  >> plotcube([5 5 5],[20 20 20],.8,[0 0 1]);
%% plotcube函数长宽⾼三维空间起点颜⾊属性
% Default input arguments
inArgs = { ...
[10 56 100] , ... % Default edge sizes (x,y and z)
[10 10  10] , ... % Default coordinates of the origin point of the cube
.7          , ... % Default alpha value for the cube's faces
[1 0 0]      ... % Default Color for the cube
};
% Replace default input arguments by input values
inArgs(1:nargin) = varargin;
% Create all variables
[edges,origin,alpha,clr] = deal(inArgs{:});
XYZ = { ...
[0 0 0 0]  [0 0 1 1]  [0 1 1 0] ; ...
tool工具箱[1 1 1 1]  [0 0 1 1]  [0 1 1 0] ; ...
[0 1 1 0]  [0 0 0 0]  [0 0 1 1] ; ...
[0 1 1 0]  [1 1 1 1]  [0 0 1 1] ; ...
[0 1 1 0]  [0 0 1 1]  [0 0 0 0] ; ...
[0 1 1 0]  [0 0 1 1]  [1 1 1 1]  ...
};
XYZ = mat2cell(...
cellfun( @(x,y,z) x*y+z , ...
XYZ , ...
repmat(mat2cell(edges,1,[1 1 1]),6,1) , ...
repmat(mat2cell(origin,1,[1 1 1]),6,1) , ...
'UniformOutput',false), ...
6,[1 1 1]);
cellfun(@patch,XYZ{1},XYZ{2},XYZ{3},...
repmat({clr},6,1),...
repmat({'FaceAlpha'},6,1),...
repmat({alpha},6,1)...
);
% view(3);
结果:
接下来感受下动画出图的可怕,若要实现⾝体前移可能还需要研究⾝体(⽅块)移动与双⾜运动的规律进⾏对应plot更新。
完全没有使⽤Simscape搭建的双⾜机器⼈移动流畅,如下,与地⾯接触⾝体移动:
simscape参考demo:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。