MATLAB机器人工具箱--双足机器人建模

1、前记:出于好奇和学习目的,主要参考了知乎:https://zhuanlan.zhihu.com/p/290641155,并使用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] ; ...
  [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:

https://www.mathworks.com/help/reinforcement-learning/ug/train-biped-robot-to-walk-using-reinforcement-learning-agents.html?searchHighlight=Biped%20robot&s_tid=srchtitle

https://www.mathworks.com/support/search.html/videos/matlab-and-simulink-robotics-arena-walking-robots-pattern-generation-1546434170253.html?fq=asset_type_name:video%20category:mpc/index&page=1

https://www.mathworks.com/matlabcentral/fileexchange/64227-matlab-and-simulink-robotics-arena-walking-robot

https://www.mathworks.com/support/search.html/videos/model-based-control-of-humanoid-walking-1574399243682.html?fq=asset_type_name:video%20category:physmod/sm/index&page=1

猜你喜欢

转载自blog.csdn.net/weixin_39090239/article/details/111877942