调用MATLAB内部类的方法和属性
Robotics System Toolbox工具箱中,函数的代码是逐层嵌套的,如果想要基于RST工具箱实现自己的功能,调用其内部函数要远比外部函数高效。
以实现逆动力学函数为例,实现逆动力学的功能需要用到
getbody()
函数来获取机械臂各连杆的质心、惯量等参数,但是运算过程中会发现,调用此函数花费了大量的时间。查看RST工具箱的
inverseDynamics
函数,可以发现其对
RigidBodyTree
的内部属性
TreeInternal
进行的操作,因此效率很高。但是如果我们在自定义的函数中调用内部类,则会报错“没有权限”访问或调用,或者说类是非公有(public)的,这就遇到了问题。
但是,总是有方法的!
内部类的属性(properties)和方法(methods)是根据访问权限分类的,如下所示部分
RigidBodyTree
类代码
classdef RigidBodyTree < robotics.manip.internal.InternalAccess
properties(SetAccess = {?robotics.manip.internal.InternalAccess})
%NumBodies Number of rigid bodies in the robot
NumBodies
%Base Base of the robot
Base
end
properties (Dependent)
%BodyNames Names of all rigid bodies in the robot
BodyNames
%BaseName Name of the base of the robot
BaseName
end
properties
%Gravity Gravitational Acceleration the robot experiences
Gravity
end
properties (Access = {?robotics.manip.internal.InternalAccess})
%Bodies
Bodies
%NumBodies Number of rigid bodies with non-fixed joint in the robot
NumNonFixedBodies
%PositionNumber Robot's position DoF
PositionNumber
%VelocityNumber Robot's velocity (real) DoF
VelocityNumber
%PositionDoFMap A map that keeps track of joint position
% indices in the configuration (q) vector
PositionDoFMap
%VelocityDoFMap A map that keeps track of joint velocity
% (acceleration) indices in the qdot vector
VelocityDoFMap
%MaxNumBodies Upper bound on number of bodies allowed in the tree.
% This is a non-tunable property and is useful when generating code.
% In MATLAB execution, MaxNumBodies is not required and is ignored
% if specified.
MaxNumBodies
%MaxNameLength Maximum length of body/joint name char vector
MaxNameLength
%MaxJointPositionNumber Maximum joint position dof
MaxJointPositionNumber
%VisualizationInfo
VisualizationInfo
%DataFormatInternal This property determines input and output data format
% for all the kinematics and dynamics functions.
DataFormatInternal
end
从代码中可以看到
PositionDoFMap,VelocityDoFMap
等属性的访问权限是(
Access = {?robotics.manip.internal.InternalAccess}
)。那我们只需要定义一个
robotics.manip.internal.InternalAccess
的子类即可访问
PositionDoFMap,VelocityDoFMap
等属性。同样的,我们可以用同样的方法,就可以调用RigidbodyTree的私有属性
TreeInternal
,提高计算效率。
需要注意的是,新定义的子类必须要获取管理员权限(用管理员身份打开MATLAB),保存在对应的文件夹下,才能够实现对内部类的调用。对于
robotics.manip.internal.InternalAccess
,可放在文件夹
****\MATLAB\R2020a\toolbox\robotics\robotmanip
下。