工程师的浪漫:用机械臂画一个爱心
Posted FrigidWinter
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了工程师的浪漫:用机械臂画一个爱心相关的知识,希望对你有一定的参考价值。
工程师的浪漫:用机械臂画一个爱心
0 写在前面
🔥本文基于过去的博客📚平面2R机器人(二连杆)运动学与动力学建模+附仿真模型做一个运动学应用,即控制机械臂末端执行特定的轨迹,事实上,在机械臂工作空间内可以绘制任意图形,这里以爱心为例,效果动图如下所示,看完本文相信你也可以做到!
1 生成爱心轨迹
定义二连杆机械臂的臂长为1,则工作空间应在半径为2的圆内。
x_ = -1.15:0.01:1.15;
y1 = real(1/2*(x_.^2.^(1/3)+(x_.^4.^(1/3)-4*x_.^2+4).^(1/2)));
y2 = real(1/2*(x_.^2.^(1/3)-(x_.^4.^(1/3)-4*x_.^2+4).^(1/2)));
x = [x_,fliplr(x_)];
y = [y1,y2];
plot(x,y);
saveddata.x = x;
saveddata.y = y;
效果如下:
将其保存为.mat文件以便复用:
save a2 saveddata
2 机械臂逆运动学实现
根据:平面2R机器人(二连杆)运动学与动力学建模+附仿真模型可知平面2R机器人运动学反解为:
θ
1
=
π
2
−
(
β
±
ψ
)
,
θ
2
>
0
时取
+
θ
1
=
π
2
+
(
β
±
ψ
)
,
θ
2
>
0
时取
−
θ
1
=
3
π
2
−
(
β
±
ψ
)
,
θ
2
>
0
时取
+
θ
1
=
3
π
2
+
(
β
±
ψ
)
,
θ
2
>
0
时取
−
\\begincases \\theta _1=\\frac\\pi2-\\left( \\beta \\pm \\psi \\right) , \\theta _2>0\\text时取+\\\\ \\theta _1=\\frac\\pi2+\\left( \\beta \\pm \\psi \\right) , \\theta _2>0\\text时取-\\\\ \\theta _1=\\frac3\\pi2-\\left( \\beta \\pm \\psi \\right) , \\theta _2>0\\text时取+\\\\ \\theta _1=\\frac3\\pi2+\\left( \\beta \\pm \\psi \\right) , \\theta _2>0\\text时取-\\\\\\endcases
⎩⎪⎪⎪⎨⎪⎪⎪⎧θ1=2π−(β±ψ),θ2>0时取+θ1=2π+(β±ψ),θ2>0时取−θ1=23π−(β±ψ),θ2>0时取+θ1=23π+(β±ψ),θ2>0时取−
运动学正解为
T B T ( θ ) = [ cos ( θ 1 + θ 2 ) − sin ( θ 1 + θ 2 ) 0 l sin ( θ 1 + θ 2 ) + l sin θ 1 sin ( θ 1 + θ 2 ) cos ( θ 1 + θ 2 ) 0 − l cos ( θ 1 + θ 2 ) − l cos θ 1 0 0 1 0 0 0 0 1 ] _T^B\\boldsymbolT\\left( \\boldsymbol\\theta \\right) =\\left[ \\beginmatrix \\cos \\left( \\theta _1+\\theta _2 \\right)& -\\sin \\left( \\theta _1+\\theta _2 \\right)& 0& l\\sin \\left( \\theta _1+\\theta _2 \\right) +l\\sin \\theta _1\\\\ \\sin \\left( \\theta _1+\\theta _2 \\right)& \\cos \\left( \\theta _1+\\theta _2 \\right)& 0& -l\\cos \\left( \\theta _1+\\theta _2 \\right) -l\\cos \\theta _1\\\\ 0& 0& 1& 0\\\\ 0& 0& 0& 1\\\\\\endmatrix \\right] TBT(θ)=⎣⎢⎢⎡cos(θ1+θ2)sin(θ1+θ2)00−sin(θ1+θ2)cos(θ1+θ2)000010lsin(θ1+θ2)+lsinθ1−lcos(θ1+θ2)−lcosθ101⎦⎥⎥⎤
下面封装一个函数实现上面的模型:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% @file : IKrob.m
% @function : [theta] = IKrob(coord,l)
% brief : 二轴机械臂逆运动学求解函数
% version : 1.0
% input : coord ------------- 笛卡尔空间坐标
% l ------------- 连杆长度
% output: theta ------------- 机械臂关节角
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [theta] = IKrob(coord,l)
x = coord(:,1);
y = coord(:,2);
L1 = l(1);
pts = size(x);
fai = abs(acos(sqrt(x.^2+y.^2)/(2*L1)));
beta = abs(atan(y./x));
theta2 = acos((x.^2+y.^2)/(2*L1^2)-1);
theta1 = zeros(pts(1), 1);
for k=1:pts(1)
if(x(k,1) >= 0 && y(k,1) >= 0)
theta1(k,1) = pi/2 - (beta(k,1) + fai(k,1));
elseif(x(k,1) < 0 && y(k,1) >= 0)
theta1(k,1) = - pi/2 + (beta(k,1) - fai(k,1));
elseif(x(k,1) < 0 && y(k,1) < 0)
theta1(k,1) = -pi/2 - (beta(k,1) - fai(k,1));
elseif(x(k,1) >= 0 && y(k,1) < 0)
theta1(k,1) = pi/2 + (beta(k,1) - fai(k,1));
end
end
theta = [theta1 theta2];
end
实现了逆运动学后只需要根据轨迹点反解当前机械臂的位姿参数并记录即可。
3 实现机械臂画指定轨迹
3.1 读取数据
% 读取轨迹信息
load a2.mat % 轨迹数据对应名称为saveddata,包含坐标数据x,y等,轨迹为手写字母a的轨迹
trajactory_length = size(saveddata.x,2); % 读取轨迹长度
trajcoord = [saveddata.x',saveddata.y']; % 读取轨迹坐标
trajcoord(:,1) = trajcoord(:,1) - 1; % 改变一下轨迹的位置,方便机械臂运动
dt = 0.02;
%% 机械臂逆运动学求关节空间轨迹(求解thetaA(包含theta1,theta2)),并作出机械臂运动图(需要求关节1的位置)<---------
thetaA &以上是关于工程师的浪漫:用机械臂画一个爱心的主要内容,如果未能解决你的问题,请参考以下文章