Reference herein MathWorks
in Help Center
the Build a Robot Step by Step
, and make their own understanding
Original URL: https://ww2.mathworks.cn/help/robotics/ug/build-a-robot-step-by-step.html
This article is based on Matlab R2020a
This example will complete the process of building a robot step by step, showing you the different robot components and how to call functions to build it.
-
Create a rigid body object.
body1 = rigidBody('body1');
Then a rigid body is created as shown below (for example):
Object resolution:
rigidBody
The rigid body object represents a rigid body. Rigid body is the building block of any tree structure robot manipulator. Each rigid body has a rigid body joint object attached to it, which defines how the rigid body moves. By default, the body has a fixed joint.
grammar:
body = rigidBody(bodyname)
Input parameter: the name of the rigid body (string scalar/character vector)
Attributes:
Attribute english Property Chinese Explanation Name The name of the rigid body The default is empty, the name must be unique to the body, so that you can rigidBodyTree
find it in the object. Typechar
orstring
Joint Joints connected to rigid bodies This joint is a tree joint, not a circular joint. The default is a fixed joint. Type is handle
Mass The mass of the rigid body The mass of a rigid body, specified as a numerical scalar, in kilograms. Default is 1 CenterOfMass Rigid body centroid The position of the center of mass of the rigid body, specified as the [xyz] vector. The vector describes the position of the center of mass relative to the object frame, in meters. The default is [0 0 0]m. Inertia Moment of inertia of rigid body (kg ∗ m 2) (kg*m^2)(kg∗m2) The inertia of a rigid body, specified as the [I xx I yy I zz I yz I xz I xy ] vector in kilogram square meters. The first three elements of the vector are the diagonal elements of the inertia tensor. The last three elements are the off-diagonal elements of the inertia tensor. The inertia tensor is a positive semi-definite symmetric matrix: [I xx I xy I xz I xy I yy I yz I xz I yz I zz] \begin{bmatrix} I_{xx} & I_{xy} & I_{xz} \ \ I_{xy} & I_{yy} & I_{yz} \\ I_{xz} & I_{yz} & I_{zz}\end {bmatrix} \quad⎣⎡IxxIxyIxzIxyIyyIyzIxzIyzIzz⎦⎤ Parent Parent rigid body The default is empty. When a rigid body is added to the tree, this property automatically updates the rigid body parent class and is designated as the rigid body object handle. The rigid body joint defines how the object moves relative to the parent object. This property is empty, until the rigid body is added to the rigidBodyTree
robot model.Children Sub-rigid body The default value is {1 × 0 cell} \{1 × 0 cell\}{ 1×0 c e l l } , the sub-element of the rigid body, specified as a cell array of the handle of the rigid body object. These rigid sub-objects are all attached to this rigid object. This property is empty, until the rigid body is added to rigidBodyTree
the robot model, and at least one other rigid body is added to the tree, the rigid body as its parent.Visuals Visual geometric model A cell array specified as a string scalar or character vector. Each character vector describes the type and source of visual geometry. For example, if the grid file link_0.stl
attached to the rigid body, the visual data would beMesh:link_0.stl
. Using theaddVisual
function added to the rigid body visualizationfor example:
body1 = rigidBody('body1'); body1.Name %输出为body1
-
Create a joint and assign it to the rigid body. Defined joint
home position
properties. Use the homogeneous conversiontform
is set to co-parent conversion. Using thetrvec2tform
function to convert a translation vector homogeneous transformation.ChildToJointTransform
Is set to the identity matrix.jnt1 = rigidBodyJoint('jnt1','revolute'); jnt1.HomePosition = pi/4; tform = trvec2tform([0.25, 0.25, 0]); % User defined setFixedTransform(jnt1,tform); body1.Joint = jnt1;
Then as shown below:
Object resolution:
rigidBodyJoint
The rigid body joint object defines the movement of the rigid body relative to the attachment point. In a tree structure robot, a joint always belongs to a specific rigid body, and each rigid body has a joint.
Rigid body joint objects can describe various types of joints. Use
rigidBodyTree
constructing rigid tree structure, mustrigidBody
class object to the assigned joint rigid body.The different joint types supported are:
- fixed-fixed, a fixed joint that prevents relative movement between two objects.
- revolute-a revolute joint, a single degree of freedom joint that rotates around a given axis, also called a pin or hinge joint.
- prismatic-a single-degree-of-freedom joint that moves a joint and slides along a given axis, also called a sliding joint.
According to the defined geometry, each joint type has different properties and different dimensions.
grammar:
jointObj = rigidBodyJoint(jname) %只指定名字 jointObj = rigidBodyJoint(jname,jtype) %不仅指定名字,还指定了类型
Attributes:
Attribute english Property Chinese Explanation Type Joint type By default fixed
, this attribute is read-only. The connection type is returned as a string scalar or a character vector. When creating a connection, certain properties are predefined for the connection type. If the joint comprising a rigid body model is added to the robot, it must be used byreplaceJoint
replacing the joint type articulation is changed.Name Joint name The name of the joint, returned as a scalar or feature vector as a string. The connection name must be unique to access it from the rigid body tree. If a rigid body containing the joint added to the robot model, you must use by replaceJoint
replacing the joint to change the name of the joint.PositionLimits Position limits of joints (range of motion) Designated as a [min max]
vector of values. Depending on the type of joint, these values have different definitions. (1)fixed
—[NaN NaN]
(default). There is no joint limit for fixed joints. Keep fixed between objects. (2)revolute
—[-pi pi]
(default). The limit defines the angle of rotation around the axis expressed in radians. (3)prismatic
—[-0.5 0.5]
(default). The limit defines the linear motion along the axis in meters.HomePosition The main position of the joint (initial position) Specified as a scalar that depends on the joint type. The main position must be within the range set by the position limit. homeConfiguration
Use this attribute to generate a predefined master configuration for the entire rigid body tree. (1)fixed
-0
(default value). There is no home position associated with a fixed joint. (2)revolute
-0
(default value). The positioning of the revolute joint is determined by the angle of rotation (in radians) around the joint axis. (3)prismatic
-0
(default value). The positioning of the moving joint is determined by the linear motion along the joint axis, in meters.JointAxis Articulation axis 关节的运动轴,指定为三元素单位向量。向量可以在3D空间中以局部坐标表示的任何方向(1)固定关节-固定的关节没有相关的运动轴。(2)转动关节-转动关节在垂直于关节轴的平面上旋转身体。(3)移动关节-移动关节沿关节轴方向做直线运动。 JointToParentTransform 从关节到父坐标系的固定变换 只读,返回一个4*4齐次变换矩阵,默认值为 eye(4)
ChildToJointTransform 从子坐标系到关节的固定变换 只读,返回一个4*4齐次变换矩阵,默认值为 eye(4)
函数:
名字 作用 copy
创建 rigidBodyJoint
对象的副本setFixedTransform
设置固定的关节变换特性 -
创建一个刚体树。该树初始化时使用一个基础坐标框架来附加主体。
robot = rigidBodyTree;
对象解析:
rigidBodyTree
刚体树是刚体带关节的连通性的表示。使用这个类在MATLAB中构建机器人机械手模型。如果您有一个使用统一机器人描述格式(URDF)指定的机器人模型,那么使用
importrobot
来导入机器人模型。刚体树模型由刚体作为刚体对象组成。每个刚体都有一个与之相关联的
rigidBodyJoint
对象,该对象定义了它如何相对于它的父体移动。使用setFixedTransform
来定义关节的框架和相邻物体的框架之间的固定转换。您可以使用RigidBodyTree
类的方法从模型中添加、替换或删除刚体。机器人动力学计算也是可能的。指定机器人模型中每个刚体的质量、质心和惯性特性。你可以计算和逆动力学有或没有外部力量和数量计算动力学机器人关节运动和关节的输入。若要使用与动态相关的函数,请将
DataFormat
属性设置为“row”
或“column”
。对于给定的刚体树模型,您还可以使用机器人模型使用机器人逆运动学算法计算所需末端执行器位置的关节角。当使用逆运动学或广义逆运动学时,指定刚体树模型。
显示方法支持体网格的可视化。网格被指定为。
stl
文件,可以使用addVisual
添加到单个刚体中。另外,在默认情况下,importrobot
函数加载在URDF机器人模型中指定的所有可访问的.stl
文件。语法:
robot = rigidBodyTree %创建一个树形结构的机器人对象。使用addBody向它添加刚体。 robot = rigidBodyTree("MaxNumBodies",N,"DataFormat",dataFormat) % 指定在生成代码时机器人中允许的主体数量的上限。还必须将DataFormat属性指定为 名称/值 。
属性:
属性英语 属性中文 解释 NumBodies 刚体数量 此属性为只读属性。机器人模型中的主体数(不包括基值),返回值为整数。 Bodies 刚体的列表 只读属性。机器人模型中刚体的列表,返回为 cell array of handles
。使用此列表来访问模型中的特定RigidBody
对象。您还可以调用getBody
来通过名称获取body。BodyNames 刚体名字的列表 此属性是只读的。刚体的名称作为字符向量的单元格数组返回。 BaseName 关节的主位置(初始位置) 指定为依赖于关节类型的标量。主位置必须在位置限制设置的范围内。 homeConfiguration
使用此属性为整个刚体树生成预定义的主配置。(1)fixed
-0
(默认值)。固定关节没有相关的home位置。(2)revolute
-0
(默认值)。转动关节的定位是由绕关节轴的旋转角度(以弧度表示)确定的。(3)prismatic
-0
(默认值)。移动关节的定位是由沿关节轴的直线运动确定的,单位为米。Gravity 重力 机器人所经历的重力加速度,指定为 [x y z]
矢量,单位为米每秒的平方。每个单元对应于机器人基架在那个方向上的加速度。[0 0 0]
m/s2 (默认值)DataFormat 运动学和动力学函数的输入/输出数据格式 运动学和动力学函数的输入/输出数据格式,指定为“结构”、“行”或“列”。要使用动力学函数,必须使用“行”或“列”。 "struct"
(默认)相关函数: 具体内容解释直接点击名称即可
函数名称 函数作用 addBody
Add body to robot addSubtree
Add subtree to robot centerOfMass
Center of mass position and Jacobian copy
Copy robot model externalForce
Compose external force matrix relative to base forwardDynamics
Joint accelerations given joint torques and states geometricJacobian
Geometric Jacobian for robot configuration gravityTorque
Joint torques that compensate gravity getBody
Get robot body handle by name getTransform
Get transform between body frames homeConfiguration
Get home configuration of robot inverseDynamics
Required joint torques for given motion massMatrix
Joint-space mass matrix randomConfiguration
Generate random configuration of robot removeBody
Remove body from robot replaceBody
Replace body on robot replaceJoint
Replace joint on body show
Show robot model in a figure showdetails
Show details of robot model subtree
Create subtree from robot model velocityProduct
Joint torques that cancel velocity-induced forces -
第一个刚体添加到树中。指定要将其附加到树的
base
上。前面定义的固定转换是从base
(parent
)到第一个刚体。addBody(robot,body1,'base')
-
创造第二个刚体。定义该刚体的属性并将其附加到第一个刚体上。定义相对于前一个body框架的转换。
body2 = rigidBody('body2'); jnt2 = rigidBodyJoint('jnt2','revolute'); jnt2.HomePosition = pi/6; % User defined tform2 = trvec2tform([1, 0, 0]); % User defined setFixedTransform(jnt2,tform2); body2.Joint = jnt2; addBody(robot,body2,'body1'); % Add body2 to body1
-
Add other bodies. Connect body 3 and 4 to body 2.
body3 = rigidBody('body3'); body4 = rigidBody('body4'); jnt3 = rigidBodyJoint('jnt3','revolute'); jnt4 = rigidBodyJoint('jnt4','revolute'); tform3 = trvec2tform([0.6, -0.1, 0])*eul2tform([-pi/2, 0, 0]); % User defined tform4 = trvec2tform([1, 0, 0]); % User defined setFixedTransform(jnt3,tform3); setFixedTransform(jnt4,tform4); jnt3.HomePosition = pi/4; % User defined body3.Joint = jnt3 body4.Joint = jnt4 addBody(robot,body3,'body2'); % Add body3 to body2 addBody(robot,body4,'body2'); % Add body4 to body2
-
If you have a control of a specific terminal actuator that you care about, it is defined as a rigid body with a fixed joint. For this robot, add an end effector to body4 so you can transform it.
bodyEndEffector = rigidBody('endeffector'); tform5 = trvec2tform([0.5, 0, 0]); % User defined setFixedTransform(bodyEndEffector.Joint,tform5); addBody(robot,bodyEndEffector,'body4');
-
Now that the robot has been created, the robot configuration can be generated. For a given configuration, you can also use
getTransform
to get the transition between the two body frame. Transform from end effector to base.config = randomConfiguration(robot) tform = getTransform(robot,config,'endeffector','base')
-
You can use subtree to create a subtree from an existing robot or other robot models. Specifies the name of the principal to be used as the base of the new subtree. You can modify this subtree by adding, changing or deleting subjects.
newArm = subtree(robot,'body2'); removeBody(newArm,'body3'); removeBody(newArm,'endeffector')
-
You can also add these subtrees to the robot. Adding a subtree is similar to adding a subject. The specified body name serves as the base of the attachment, and all transformations on the subtree are relative to the body frame. Before adding subtrees, you must ensure that all names of body and joints are unique. Create a copy of the body and joints, rename them, and replace them in the subtree. Call
addSubtree
the sub-tree attached to the specified subject.newBody1 = copy(getBody(newArm,'body2')); newBody2 = copy(getBody(newArm,'body4')); newBody1.Name = 'newBody1'; newBody2.Name = 'newBody2'; newBody1.Joint = rigidBodyJoint('newJnt1','revolute'); newBody2.Joint = rigidBodyJoint('newJnt2','revolute'); tformTree = trvec2tform([0.2, 0, 0]); % User defined setFixedTransform(newBody1.Joint,tformTree); replaceBody(newArm,'body2',newBody1); replaceBody(newArm,'body4',newBody2); addSubtree(robot,'body1',newArm);
-
Finally, you can
showdetails
see a robot that you build. Verify that the connection type is correct.showdetails(robot)