MATLAB机器人可视化

1、前记:可能用Robotics Toolbox建立的机器人模型与实际机器人在外观上存在天壤之别吧,直接将CAD软件(UG、SolidWorks、CATIA、Proe等)做好的3D模型导入MATLAB中是一个很好的选择。

     下面记录MATLAB官网上的如何显示具有可视几何图形的机器人建模

     1)导入具有.stl文件的机器人与统一的机器人描述格式 (URDF) 文件相关联, 以描述机器人的视觉几何。每个刚体都有一个单独的视觉几何特征。importrobot函数对 URDF 文件进行解析, 得到机器人模型和视觉几何。使用show功能可视化的机器人模型显示在一个figure图框中。然后, 您可以通过单击组件来检查它们并右键单击以切换可见性来与模型进行交互。

     将机器人模型作为 URDF 文件导入。此 URDF 中必须正确指定.stl文件位置。要将其他.stl文件添加到单个刚性体, 请参见 addVisual .如:iiwa的URDF描述文件中的<geometry.....geometry>部分。

 

在命令窗输入代码:

robot = importrobot('iiwa14.urdf');

用相关的视觉模型可视化机器人。单击 "正文" 或 "框架" 进行检查。右键单击 "正文" 可切换每个可视几何图形的可见性。

代码:show(robot);%机器人显示如下

   

2)如此,在具有机器人的URDF文件和对应的stl文件条件下运行以上两行代码即可显示具有几何特征的可视化机器人模型。

     如下:ABB机器人的IRB120

                                              

和ABB机器人的YUMI

2、仿真

     机器人可视化到MATLAB环境下后,就可以做仿真了。在MATLAB的实例中可以直接学习到一些思路,代码和结果如下:

     具有多个约束的轨迹规划---->

%% Plan a Reaching Trajectory With Multiple Kinematic Constraints
%% Introduction
% This example shows how to use generalized inverse kinematics to plan a
% joint-space trajectory for a robotic manipulator. It combines multiple
% constraints to generate a trajectory that guides the gripper to a cup
% resting on a table. These constraints ensure that the gripper approaches
% the cup in a straight line and that the gripper remains at a safe
% distance from the table, without requiring the poses of the gripper to be
% determined in advance.

%   Copyright 2016 The MathWorks, Inc.

%% Set Up the Robot Model
% This example uses a model of the KUKA LBR iiwa, a 7 degree-of-freedom
% robot manipulator. |<docid:robotics_ref.bvlvwcs-1 importrobot>| generates
% a |<docid:robotics_ref.bvan8uq-1 robotics.RigidBodyTree>| model from a
% description stored in a Unified Robot Description Format (URDF) file.
lbr = importrobot('iiwa14.urdf'); % 14 kg payload version
lbr.DataFormat = 'row';
gripper = 'iiwa_link_ee_kuka';

%%
% Define dimensions for the cup.
cupHeight = 0.2;
cupRadius = 0.05;
cupPosition = [-0.5, 0.5, cupHeight/2];
%%
% Add a fixed body to the robot model representing the center of the cup.
body = robotics.RigidBody('cupFrame');
setFixedTransform(body.Joint, trvec2tform(cupPosition))
addBody(lbr, body, lbr.BaseName);
%% Define the Planning Problem
% The goal of this example is to generate a sequence of robot
% configurations that satisfy the following criteria:
%
% * Start in the home configuration
% * No abrupt changes in robot configuration
% * Keep the gripper at least 5 cm above the "table" (z = 0)
% * The gripper should be aligned with the cup as it approaches
% * Finish with the gripper 5 cm from the center of the cup
%
% This example utilizes constraint objects to generate robot configurations
% that satisfy these criteria. The generated trajectory consists of five
% configuration waypoints. The first waypoint, |q0|, is set as the home
% configuration. Pre-allocate the rest of the configurations in
% |qWaypoints| using |repmat|.
numWaypoints = 5;
q0 = homeConfiguration(lbr);
qWaypoints = repmat(q0, numWaypoints, 1);
%%
% Create a |<docid:robotics_ref.bvlvs3d-1
% robotics.GeneralizedInverseKinematics>| solver that accepts the following
% constraint inputs:
%
% * Cartesian bounds - Limits the height of the gripper
% * A position target - Specifies the position of the cup relative to the
% gripper.
% * An aiming constraint - Aligns the gripper with the cup axis
% * An orientation target - Maintains a fixed orientation for the gripper
% while approaching the cup
% * Joint position bounds - Limits the change in joint positions between waypoints.
gik = robotics.GeneralizedInverseKinematics('RigidBodyTree', lbr, ...
    'ConstraintInputs', {'cartesian','position','aiming','orientation','joint'})
%% Create Constraint Objects
% Create the constraint objects that are passed as inputs to the solver.
% These object contain the parameters needed for each constraint. Modify
% these parameters between calls to the solver as necessary.
%%
% Create a Cartesian bounds constraint that requires the gripper to be at
% least 5 cm above the table (negative z direction). All other values are
% given as |inf| or |-inf|.
heightAboveTable = robotics.CartesianBounds(gripper);
heightAboveTable.Bounds = [-inf, inf; ...
                           -inf, inf; ...
                           0.05, inf]
%% 
% Create a constraint on the position of the cup relative to the gripper,
% with a tolerance of 5 mm.
distanceFromCup = robotics.PositionTarget('cupFrame');
distanceFromCup.ReferenceBody = gripper;
distanceFromCup.PositionTolerance = 0.005
%%
% Create an aiming constraint that requires the z-axis of the
% |iiwa_link_ee| frame to be approximately vertical, by placing the target
% far above the robot. The |iiwa_link_ee| frame is oriented such that this
% constraint aligns the gripper with the axis of the cup.
alignWithCup = robotics.AimingConstraint('iiwa_link_ee');
alignWithCup.TargetPoint = [0, 0, 100]
%%
% Create a joint position bounds constraint. Set the |Bounds| property of
% this constraint based on the previous configuration to limit the change
% in joint positions.
limitJointChange = robotics.JointPositionBounds(lbr)
%%
% Create an orientation constraint for the gripper with a tolerance of one
% degree. This constraint requires the orientation of the gripper to match
% the value specified by the |TargetOrientation| property. Use this
% constraint to fix the orientation of the gripper during the final
% approach to the cup.
fixOrientation = robotics.OrientationTarget(gripper);
fixOrientation.OrientationTolerance = deg2rad(1)
%% Find a Configuration That Points at the Cup
% This configuration should place the gripper at a distance from the cup,
% so that the final approach can be made with the gripper properly aligned.
intermediateDistance = 0.3;
%%
% Constraint objects have a |Weights| property which determines how the
% solver treats conflicting constraints. Setting the weights of a
% constraint to zero disables the constraint. For this configuration,
% disable the joint position bounds and orientation constraint.
limitJointChange.Weights = zeros(size(limitJointChange.Weights));
fixOrientation.Weights = 0;
%%
% Set the target position for the cup in the gripper frame. The cup should
% lie on the z-axis of the gripper at the specified distance.
distanceFromCup.TargetPosition = [0,0,intermediateDistance];
%%
% Solve for the robot configuration that satisfies the input constraints
% using the |gik| solver. You must specify all the input constraints. Set
% that configuration as the second waypoint.
[qWaypoints(2,:),solutionInfo] = gik(q0, heightAboveTable, ...
                       distanceFromCup, alignWithCup, fixOrientation, ...
                       limitJointChange);
%% Find Configurations That Move Gripper to the Cup Along a Straight Line
% Re-enable the joint position bound and orientation constraints.
limitJointChange.Weights = ones(size(limitJointChange.Weights));
fixOrientation.Weights = 1;
%%
% Disable the align-with-cup constraint, as the orientation constraint
% makes it redundant.
alignWithCup.Weights = 0;
%%
% Set the orientation constraint to hold the orientation based on the
% previous configuration (|qWaypoints(2,:)|). Get the transformation from
% the gripper to the base of the robot model. Convert the homogeneous
% transformation to a quaternion.
fixOrientation.TargetOrientation = ...
    tform2quat(getTransform(lbr,qWaypoints(2,:),gripper));
%%
% Define the distance between the cup and gripper for each waypoint
finalDistanceFromCup = 0.05;
distanceFromCupValues = linspace(intermediateDistance, finalDistanceFromCup, numWaypoints-1);
%%
% Define the maximum allowed change in joint positions between each
% waypoint.
maxJointChange = deg2rad(10);
%%
% Call the solver for each remaining waypoint.
for k = 3:numWaypoints
    % Update the target position.
    distanceFromCup.TargetPosition(3) = distanceFromCupValues(k-1);
    % Restrict the joint positions to lie close to their previous values.
    limitJointChange.Bounds = [qWaypoints(k-1,:)' - maxJointChange, ...
                               qWaypoints(k-1,:)' + maxJointChange];
    % Solve for a configuration and add it to the waypoints array.
    [qWaypoints(k,:),solutionInfo] = gik(qWaypoints(k-1,:), ...
                                         heightAboveTable, ...
                                         distanceFromCup, alignWithCup, ...
                                         fixOrientation, limitJointChange);
end
%% Visualize the Generated Trajectory
% Interpolate between the waypoints to generate a smooth trajectory. Use
% |<docid:matlab_ref.bvjbxbd pchip>| to avoid overshoots, which might
% violate the joint limits of the robot.
framerate = 15;
r = robotics.Rate(framerate);
tFinal = 10;
tWaypoints = [0,linspace(tFinal/2,tFinal,size(qWaypoints,1)-1)];
numFrames = tFinal*framerate;
qInterp = pchip(tWaypoints,qWaypoints',linspace(0,tFinal,numFrames))';
%%
% Compute the gripper position for each interpolated configuration.
gripperPosition = zeros(numFrames,3);
for k = 1:numFrames
    gripperPosition(k,:) = tform2trvec(getTransform(lbr,qInterp(k,:), ...
                                                    gripper));
end
%%
% Show the robot in its initial configuration along with the table and cup
figure;
show(lbr, qWaypoints(1,:), 'PreservePlot', false);
hold on
exampleHelperPlotCupAndTable(cupHeight, cupRadius, cupPosition);
p = plot3(gripperPosition(1,1), gripperPosition(1,2), gripperPosition(1,3));
%%
% Animate the manipulator and plot the gripper position.
hold on
for k = 1:size(qInterp,1)
    show(lbr, qInterp(k,:), 'PreservePlot', false);
    p.XData(k) = gripperPosition(k,1);
    p.YData(k) = gripperPosition(k,2);
    p.ZData(k) = gripperPosition(k,3);
    waitfor(r);
end
hold off
%%
% If you want to save the generated configurations to a MAT-file for later
% use, execute the following:
% 
% |>> save('lbr_trajectory.mat', 'tWaypoints', 'qWaypoints');|
displayEndOfDemoMessage(mfilename)

           

3、后记:MATLAB机器人可视化的方法很多,对于URDF自己写太麻烦,可以利用SolidWorks设计出三维模型后中的插件功能直接生成。有些可以利用xml文件生成,simulink中有示例,也可查看Simscape   Simscape Multibody  Simulink 3D Animation          Simulink Control Design 等网上资源。    

如:Simscape Multibody中的一个如何将STEP文件导入的方法。

4、根据上面的solid 文件from的选项我尝试了下从Robotstudio中保存机器人各部件到MATLAB目录下然后使其可视化从Robotstudiox下保存个部件参考----->一种从Robotstudio环境中导出机器人模型并在MATLAB下使其可视化的研究记录。结果表明还是可行的,但是运行会出现错误,需要自己定义坐标的转换关系和各部件的质量等。。。如下:

 

参看网址:https://ww2.mathworks.cn/help/robotics/ref/robotics.rigidbodytree-class.html

                  http://www.ilovematlab.cn/thread-504445-1-1.html

                  https://www.zhihu.com/question/56559082/answer/150314069

                  https://www.zhihu.com/question/58793939/answer/343951305

                  https://blog.csdn.net/weixin_37719670/article/details/79027035#commentBox

                  https://www.zhihu.com/question/40801341/answer/134010201

                  https://blog.csdn.net/sunbibei/article/details/52297524

                  https://blog.csdn.net/weixin_39090239/article/details/80557032

                  https://www.youtube.com/watch?v=F2kE2vGY8_A&t=11s

                 https://ww2.mathworks.cn/help/physmod/sm/rigid-bodies-1.html

  • 31
    点赞
  • 301
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 56
    评论
### 回答1: 当使用importdata函数时,可能会出现以下错误: 1. "Undefined function or variable 'importdata'" 这个错误通常是因为没有正确加载MATLAB的数据处理工具箱。可以通过在命令窗口中输入“ver”来检查是否已经安装了数据处理工具箱。如果没有安装,可以通过MATLAB的“添加或删除程序”功能来安装。 2. "Error using importdata (line 139) Unable to open file." 这个错误通常是因为指定的文件路径不正确或文件不存在。可以通过检查文件路径是否正确或者使用绝对路径来解决这个问题。 3. "Error using importdata (line 139) File contains unexpected non-numeric data." 这个错误通常是因为文件中包含了非数字数据,例如文本或日期。可以使用其他函数来读取这些数据,例如textscan或xlsread。 4. "Error using importdata (line 139) File contains no data." 这个错误通常是因为文件是空的或者没有数据。可以检查文件是否存在并且包含数据。 总之,要正确使用importdata函数,需要确保正确加载MATLAB的数据处理工具箱,指定正确的文件路径并且文件中包含正确的数据。 ### 回答2: 在使用 Matlab 编程时,一些错误是难免的,其中使用 importdata 函数时出现错误是一种常见情况。下面我会简单介绍关于 Matlabimportdata 函数使用出现错误的几种可能原因及对应的解决方法。 1. 导入的文件不存在或路径不正确 当使用 importdata 函数时,如果指定的文件不存在或者路径不正确,就会出现错误。解决方法是检查文件名和路径是否正确,特别是空格和大小写是否匹配。 2. 文件格式不被支持 Matlabimportdata 函数支持多种格式的文件,如 txt、csv、dat、xls 等。如果导入的文件格式不被支持,会出现错误。解决方法是检查文件格式是否正确,并尝试使用其他适合的函数导入文件。 3. 文件中的数据格式不规范 当导入的文件中数据格式不规范时,例如数据不对齐,数据类型不匹配等,会导致 importdata 函数出现错误。解决方法是检查数据格式是否正确,并进行必要的数据处理。 4. 文件过大 如果导入的文件过大,会导致内存不足,甚至 Matlab 工作状态无响应。解决方法是使用适当的方式分块读取文件,或者使用其他函数,如 textscan 函数。 综上所述,发生 Matlabimportdata 函数使用出现错误的原因有很多,我们在使用 importdata 函数时,需要注意文件路径的正确性、文件格式的支持性、文件格式的规范性以及导入的文件大小等问题,若出现了问题,我们要细心地分析错误原因,逐一排查解决。 ### 回答3: Matlab是一款强大的科学计算软件,它提供了许多自带的函数和工具箱,可以方便地处理各种数据和进行科学计算。其中importdata函数是一种非常常用的函数,可以用来读取不同格式的数据文件,并将其转化为Matlab中的矩阵格式。 然而,在使用importdata函数时,可能会出现错误。下面我们就来分析一下可能出现的几种情况及其解决方法。 1. 文件路径错误 在使用importdata函数时,如果路径或文件名有误,将会出现如下错误提示:Error using importdata (line 98) Unable to open file. 这种情况下,需要检查一下路径和文件名是否正确,并且确认文件是否已保存。在Matlab中可以使用pwd函数来查看当前的工作路径,使用cd函数来更改当前工作路径。 2. 数据格式错误 在使用importdata函数时,如果数据文件的格式不符合Matlab的要求,将会出现如下错误提示: Error using importdata (line 174) Unable to determine file format of file. 这种情况下,需要检查一下数据文件的格式是否正确,并且确认数据是否已保存。在Matlab中可以使用file命令来查看数据文件的格式,如file('data.csv')。 3. 编码格式错误 在使用importdata函数时,如果数据文件的编码格式不符合Matlab的要求,将会出现如下错误提示: Error using importdata (line 172) Failed to convert input to UTF8. 这种情况下,需要将数据文件的编码格式改成Matlab支持的编码格式,如ISO-8859-1或UTF-8。 4. 数据维度不匹配 在使用importdata函数时,如果数据文件的维度与Matlab中的矩阵维度不匹配,将会出现如下错误提示: Error using importdata (line 209) Data dimensions must agree. 这种情况下,需要检查一下数据文件的维度与Matlab中的矩阵维度是否匹配,并且确认数据是否已保存。在Matlab中可以使用size函数来查看矩阵的维度,如size(data)。 总之,Matlab错误使用importdata的原因可能有很多,我们需要仔细分析错误提示,并且结合具体情况进行解决。同时,我们还可以查看Matlab的帮助文档或者在Matlab的用户论坛上寻求帮助,以便更好地利用importdata函数进行数据处理和科学计算。
评论 56
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JianRobSim

嘤嘤其名,求其友声!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值