plan a reaching trajectory with multiple kinematic constraints -凯发k8网页登录
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.
set up the robot model
this example uses a model of the kuka lbr iiwa, a 7 degree-of-freedom robot manipulator. importrobot
generates a 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 = 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 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 = generalizedinversekinematics('rigidbodytree', lbr, ... 'constraintinputs', {'cartesian','position','aiming','orientation','joint'})
gik = generalizedinversekinematics with properties: numconstraints: 5 constraintinputs: {'cartesian' 'position' 'aiming' 'orientation' 'joint'} rigidbodytree: [1x1 rigidbodytree] solveralgorithm: 'bfgsgradientprojection' solverparameters: [1x1 struct]
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 = constraintcartesianbounds(gripper); heightabovetable.bounds = [-inf, inf; ... -inf, inf; ... 0.05, inf]
heightabovetable = constraintcartesianbounds with properties: endeffector: 'iiwa_link_ee_kuka' referencebody: '' targettransform: [4x4 double] bounds: [3x2 double] weights: [1 1 1]
create a constraint on the position of the cup relative to the gripper, with a tolerance of 5 mm.
distancefromcup = constraintpositiontarget('cupframe');
distancefromcup.referencebody = gripper;
distancefromcup.positiontolerance = 0.005
distancefromcup = constraintpositiontarget with properties: endeffector: 'cupframe' referencebody: 'iiwa_link_ee_kuka' targetposition: [0 0 0] positiontolerance: 0.0050 weights: 1
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 = constraintaiming('iiwa_link_ee');
alignwithcup.targetpoint = [0, 0, 100]
alignwithcup = constraintaiming with properties: endeffector: 'iiwa_link_ee' referencebody: '' targetpoint: [0 0 100] angulartolerance: 0 weights: 1
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 = constraintjointbounds(lbr)
limitjointchange = constraintjointbounds with properties: bounds: [7x2 double] weights: [1 1 1 1 1 1 1]
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 = constraintorientationtarget(gripper); fixorientation.orientationtolerance = deg2rad(1)
fixorientation = constraintorientationtarget with properties: endeffector: 'iiwa_link_ee_kuka' referencebody: '' targetorientation: [1 0 0 0] orientationtolerance: 0.0175 weights: 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 to avoid overshoots, which might violate the joint limits of the robot.
framerate = 15; r = ratecontrol(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');