Matlab robotic arm modeling: use of robotic toolbox && importing your own robotic arm model

        This article mainly introduces how to build a robotic arm model in matlab ( the prerequisite is to download the Robotics Toolbox robot toolbox~ ), and perform trajectory motion based on forward and inverse kinematics calculations . How to import the existing 3D model of Solidworks robotic arm into Matlab and perform motion control on it.

        For a detailed explanation of the installation and functions of the Robot Toolbox, you can refer to this article (very detailed, very good!!): (1 message) Matlab Robot Toolbox (Robotics Toolbox) study notes_Mist_Orz's blog-CSDN blog

Table of contents

1 Establishment of the robotic arm

Core function - Link function explanation

Robotic arm simulation code

 2 Trajectory planning based on kinematic solution

Forward and inverse kinematics solution

3 Import and use of custom robotic arm models based on Solidworks and Matlab

Import Solidworks model into Matlab

 Perform forward kinematics trajectory planning


1 Establishment of the robotic arm

Core function - Link function explanation

        The Link function establishes the connecting rod based on the DH parameters, which contains the main information of the joint. The input sequence of the DH parameters when establishing the connecting rod is: joint angle θ, joint distance d, link length a, link angle α, joint type (0 rotate,1move);

       In addition, the parameters of joint variables are:

        qlim specifies joint limits, [ note that the moving joint variables in the toolbox do not allow negative values ] ;

        jointtype specifies the joint type, which defaults to a rotating joint. L(4).jointtype='P' means that the fourth link is connected by a moving joint;
        offset is the offset of the joint's initial value. [It should be noted here that after defining the joint type, the corresponding variable must be 0, and the initial value must be defined by offset . For example, joint 2 is a rotating joint, then the theta of L(2) must be 0, but we also hope that In the initial state, joint 2 can have an offset, which can be achieved through the statement "L(2).offset=pi/2;" after initialization ].
        Finally, there is the choice of modeling parameter type : standard DH parameters/improved DH parameters . The difference lies in the different fixed coordinate systems and the order of performing transformations. The analogy is as follows:

Standard DH parameters Improved DH parameters

Fixed coordinate system

selection of

Take the last joint coordinate system of the connecting rod as the fixed coordinate system Take the previous joint coordinate system of the connecting rod as the fixed coordinate system

X-axis direction

OK

Determine the X-axis by the cross product of the current Z-axis and the Z-axis of the "previous" coordinate system Determine the X-axis by cross-multiplying the Z-axis of the "next" coordinate with the current Z-axis

Between coordinate systems

transformation rules

The order of parameter changes between adjacent joint coordinate systems is:
θ, d, a, α
The order of parameter changes between adjacent joint coordinate systems is:
α, a, θ, d

        The format of the Link function call is

L(1) = Link([theta1, D1, A1, alpha1, offset1], 'standard')    %标准型D-H参数

L(1) = Link([theta1, D1, A1, alpha1, offset1], 'modified')    %改进型D-H参数

Robotic arm simulation code

        Example 1: Create an articulated six-axis robotic arm

%% 基于MATLAB的关节型六轴机械臂仿真

%% 参数定义
clear;
close all;
clc;
 
%角度转换
angle=pi/180;  %转化为角度制
 
%D-H参数表
theta1 = 0;   D1 = 0.4;   A1 = 0.025; alpha1 = pi/2; offset1 = 0;
theta2 = pi/2;D2 = 0;     A2 = 0.56;  alpha2 = 0;    offset2 = 0;
theta3 = 0;   D3 = 0;     A3 = 0.035; alpha3 = pi/2; offset3 = 0;
theta4 = 0;   D4 = 0.515; A4 = 0;     alpha4 = pi/2; offset4 = 0;
theta5 = pi;  D5 = 0;     A5 = 0;     alpha5 = pi/2; offset5 = 0;
theta6 = 0;   D6 = 0.08;  A6 = 0;     alpha6 = 0;    offset6 = 0;

%% DH法建立模型,关节转角,关节距离,连杆长度,连杆转角,关节类型(0转动,1移动),'standard':建立标准型D-H参数
L(1) = Link([theta1, D1, A1, alpha1, offset1], 'standard')
L(2) = Link([theta2, D2, A2, alpha2, offset2], 'standard')
L(3) = Link([theta3, D3, A3, alpha3, offset3], 'standard')
L(4) = Link([theta4, D4, A4, alpha4, offset4], 'standard')
L(5) = Link([theta5, D5, A5, alpha5, offset5], 'standard')
L(6) = Link([theta6, D6, A6, alpha6, offset6], 'standard')

% 定义关节范围
L(1).qlim =[-180*angle, 180*angle];
L(2).qlim =[-180*angle, 180*angle];
L(3).qlim =[-180*angle, 180*angle];
L(4).qlim =[-180*angle, 180*angle];
L(5).qlim =[-180*angle, 180*angle];
L(6).qlim =[-180*angle, 180*angle];

%% 显示机械臂(把上述连杆“串起来”)
robot0 = SerialLink(L,'name','six');
theta = [0 pi/2 0 0 pi 0];				%初始关节角度
figure(1)
robot0.plot(theta);
title('六轴机械臂模型');

        The running results are available (the second picture indicates the six axes):

        

 Teachable : Add the teach command to make the joint angle of the robot arm adjustable:

        Continue execution from the previous section of code:

%% 加入teach指令,则可调整各个关节角度
robot1 = SerialLink(L,'name','sixsix');
figure(2)
robot1.plot(theta);
robot1.teach
title('六轴机械臂模型可调节');

        The running results are shown in the figure above. You can observe the posture of the robotic arm at different joint angles by dragging the progress bar of each joint q.

 2 Trajectory planning based on kinematic solution

Forward and inverse kinematics solution

        For the robot arm robot0 = SerialLink(L,'name','six');

        Forward kinematics : robot0.fkine(): input the desired joint angle and output the terminal homogeneous transformation matrix.

        Inverse kinematics : robot0.ikine() [ only used in the standard DH modeling method ]: input the homogeneous transformation matrix executed at the end and output the resulting joint angle.

        Example 2: Forward kinematics solution and trajectory implementation

%% 已知机械臂初始和目标的关节角度,利用五次多项式进行轨迹规划

robot0 = SerialLink(L,'name','six');
T1=transl(0.5,0,0);						%根据给定起始点,得到起始点位姿
T2=transl(0,0.5,0);						%根据给定终止点,得到终止点位姿
init_ang=robot0.ikine(T1);				%根据起始点位姿,得到起始点关节角
targ_ang=robot0.ikine(T2);				%根据终止点位姿,得到终止点关节角
step = 20;

%轨迹规划方法
figure(3)

%关节空间轨迹规划
[q ,qd, qdd]=jtraj(init_ang,targ_ang,step); %五次多项式轨迹,得到关节角度,角速度,角加速度,20为采样点个数
grid on
T=robot0.fkine(q);						%根据插值,得到末端执行器位姿
nT=T.T;
plot3(squeeze(nT(1,4,:)),squeeze(nT(2,4,:)),squeeze(nT(3,4,:)));%输出末端轨迹
title('根据运动学求正解得到目标轨迹');
robot0.plot(q);							%动画演示 


%% 求解上述运行过程中的位置、速度、加速度的变化曲线
figure(4)
subplot(3,2,[1,3]); 					%subplot 对画面分区 三行两列 占用1到3的位置
plot3(squeeze(nT(1,4,:)),squeeze(nT(2,4,:)),squeeze(nT(3,4,:)));%输出末端轨迹
robot2.plot(q);							%动画演示

figure(f)
subplot(3, 2, 2);
i = 1:6;
plot(q(:,1));
title('位置');
grid on;

figure(f)
subplot(3, 2, 4);
i = 1:6;
plot(qd(:,1));
title('速度');
grid on;

figure(f)
subplot(3, 2, 6);
i = 1:6;
plot(qdd(:,1));
title('加速度');
grid on;

        Obtained running results :

The forward solution of the robot arm is solved and the trajectory is run

3 Import and use of custom robotic arm models based on Solidworks and Matlab

        Refer to the video 3D model_bilibili_bilibili (Thanks to up!! The model is also downloaded by up, the video explains it in detail~), taking the four-axis robotic arm as an example, in Solidworks, each joint is in turn Export to STL format file, the specific steps are as follows:

Import Solidworks model into Matlab

        1) Download the 3D model [ folder Fdof ] (2 messages) 3D model of the four-axis robotic arm (each Link part and 4DOFmanipulator assembly) resource-CSDN library , and open the assembly 4DOFmanipulator.sldasm in Solidworks.

2) Export the base Base as link0.STL file, and select the path of the folder Fdof         you just downloaded as the path to save as .

 [ Note : All parts below must check the two options "Do not convert STL output to the entire coordinate space" and "Save all components of the assembly in a single file" when exporting to STL , otherwise you will encounter problems when exporting to STL. When using Matlab, components are separated]

        3) Export the first joint Link1 as link1.STL file

        4) Export the second joint Link2 as link2.STL file

         5) In the same way, export the third joint Link3 to the link3.STL file, and the output coordinate system is Frame3;

                Export the fourth joint Link4 as link4.STL file, and the output coordinate system is Frame4;

                The export tool Tool is the link5.STL file, and the output coordinate system is Frame5;

The files present in         the final folder Fdof are:

         6) According to the size of the physical parameters when modeling in Solidworks, set the parameter values ​​of the Link function in Matlab (joint rotation angle θ, joint distance d, connecting rod length a, connecting rod rotation angle α), and import the code of the above STL model into Matlab as follows:

        Example 3: Matlab kinematics solution for custom robotic arm 3D model

clear;
clc;
L(1) = Link('revolute','d',0.216,'a',0,'alpha',pi/2);
L(2) = Link('revolute','d',0,'a',0.5,'alpha',0,'offset',pi/2);
L(3) = Link('revolute','d',0,'a',sqrt(0.145^2+0.42726^2),'alpha',0,'offset',-atan(427.46/145));
L(4) = Link('revolute','d',0,'a',0,'alpha',pi/2,'offset',atan(427.46/145));
L(5) = Link('revolute','d',0.258,'a',0,'alpha',0);

Five_dof = SerialLink(L,'name','4-dof');
Five_dof.base = transl(0,0,0.28);           % 相当于将link1的frame1向上提升0.28(基座的高度),将机械臂放置在基座上

q0 = [0 0 0 0 0];
view = [35 20];        %plot函数的展示视角【方位角,仰角】
w = [-1 1 -1 1 0 2];        %工作空间

% 变量path的值'D:\Matlab2022a\...\Fdof','nowrist'仅为示意,改为自己的文件夹Fdof的路径
Five_dof.plot3d(q0,'tilesize',0.1,'workspace',w,'path','D:\Matlab2022a\...\Fdof','nowrist','view',view)

L1 = light('Position',[1 1 1],'color','w');     %加光源

         The result of the operation is:

 Perform forward kinematics trajectory planning

        Continue execution from the previous section of code:

%% 给定目标位置为(0.5,0.5,0.5)机械臂求解正运动学的解并运动到该位置
Position = [0.5 0.5 0.5];

T1 = transl(Position)*rpy2tr(180,0,0);         % 机械臂到达的末端位置(rpy2tr:Roll-pitch-yaw angles to SE(3) homogeneous transform)

q1 = Five_dof.ikunc(T1);                    % 末端机械臂的关节位姿

q = jtraj(q0,q1,60);                    % 计算从q0到q1的关节空间轨迹,T=60为时间(60帧)

Five_dof.plot3d(q,'view',view,'fps',60,'nowrist');         % 'nowrist'表示绘制末端的坐标系

Guess you like

Origin blog.csdn.net/m0_46427461/article/details/131281691