main content

plan and execute collision-凯发k8网页登录

this example shows how to plan closed-loop collision-free robot trajectories from an initial to a desired end-effector pose using nonlinear model predictive control. the resulting trajectories are executed using a joint-space motion model with computed torque control. obstacles can be static or dynamic, and can be either set as primitives (spheres, cylinders, boxes) or as custom meshes.

robot description and poses

load the kinova gen3 rigid body tree (rbt) model.

robot = loadrobot('kinovagen3', 'dataformat', 'column');

get the number of joints.

numjoints = numel(homeconfiguration(robot));

specify the robot frame where the end-effector is attached.

endeffector = "endeffector_link"; 

specify initial and desired end-effector poses. use inverse kinematics to solve for the initial robot configuration given a desired pose.

% initial end-effector pose
taskinit = trvec2tform([[0.4 0 0.2]])*axang2tform([0 1 0 pi]);
% compute current robot joint configuration using inverse kinematics
ik = inversekinematics('rigidbodytree', robot);
ik.solverparameters.allowrandomrestart = false;
weights = [1 1 1 1 1 1];
currentrobotjconfig = ik(endeffector, taskinit, weights, robot.homeconfiguration);
% the ik solver respects joint limits, but for those joints with infinite
% range, they must be wrapped to a finite range on the interval [-pi, pi].
% since the the other joints are already bounded within this range, it is
% sufficient to simply call wraptopi on the entire robot configuration
% rather than only on the joints with infinite range.
currentrobotjconfig = wraptopi(currentrobotjconfig);
% final (desired) end-effector pose
taskfinal = trvec2tform([0.35 0.55 0.35])*axang2tform([0 1 0 pi]);  
anglesfinal = rotm2eul(taskfinal(1:3,1:3),'xyz');
posefinal = [taskfinal(1:3,4);anglesfinal']; % 6x1 vector for final pose: [x, y, z, phi, theta, psi]

collision meshes and obstacles

to check for and avoid collisions during control, you must setup a collision world as a set of collision objects. this example uses objects as obstacles to avoid. change the following boolean to plan using static instead of moving obstacles.

ismovingobst = true;

the obstacle sizes and locations are initialized in the following helper function. to add more static obstacles, add collision objects in the world array.

helpercreateobstacleskinova;

visualize the robot at the initial configuration. you should see the obstacles in the environment as well.

x0 = [currentrobotjconfig', zeros(1,numjoints)];
helperinitialvisualizerkinova;

figure contains an axes object. the axes object with xlabel x, ylabel y contains 11 objects of type patch, line. one or more of the lines displays its values using only markers these objects represent base_link, shoulder_link, halfarm1_link, halfarm2_link, forearm_link, wrist1_link, wrist2_link, bracelet_link, endeffector_link, shoulder_link_mesh, halfarm1_link_mesh, halfarm2_link_mesh, forearm_link_mesh, wrist1_link_mesh, wrist2_link_mesh, bracelet_link_mesh, base_link_mesh.

specify a safety distance away from the obstacles. this value is used in the inequality constraint function of the nonlinear mpc controller.

safetydistance = 0.01; 

design nonlinear model predictive controller

you can design the nonlinear model predictive controller using the following helper file, which creates an nlmpc (model predictive control toolbox) controller object. to views the file, type edit helperdesignnlmpcobjkinova.

helperdesignnlmpcobjkinova;

the controller is designed based on the following analysis. the maximum number of iterations for the optimization solver is set to 5. the lower and upper bounds for the joint position and velocities (states), and accelerations (control inputs) are set explicitly.

  • the robot joints model is described by double integrators. the states of the model are x=[q,q˙], where the 7 joint positions are denoted by q and their velocities are denoted by q˙. the inputs of the model are the joint accelerations u=q¨. the dynamics of the model are given by

x˙=[0i700]x [0i7]u

where i7 denotes the 7×7 identity matrix. the output of the model is defined as

y=[i70]x.

therefore, the nonlinear model predictive controller (nlobj) has 14 states, 7 outputs, and 7 inputs.

  • the cost function for nlobj is a custom nonlinear cost function, which is defined in a manner similar to a quadratic tracking cost plus a terminal cost.

j=0t(pref-p(q(t)))qr(pref-p(q(t))) u(t)quu(t)dt (pref-p(q(t)))qt(pref-p(q(t))) q˙(t)qvq˙(t)

here, p(q(t)) transforms the joint positions q(t) to the frame of end effector using forward kinematics and , and pref denotes the desired end-effector pose.

the matrices qr, qu, qt, and qv are constant weight matrices.

  • to avoid collisions, the controller has to satisfy the following inequality constraints.

di,jdsafe

here, di,j denotes the distance from the i-th robot body to the j-th obstacle, computed using .

in this example, i belongs to {1,2,3,4,5,6} (the base and end-effector robot bodies are excluded), and j belongs to {1,2} (2 obstacles are used).

the jacobians of the state function, output function, cost function, and inequality constraints are all provided for the prediction model to improve the simulation efficiency. to calculate the inequality constraint jacobian, use the function and the jacobian approximation in [1].

closed-loop trajectory planning

simulate the robot for a maximum of 50 steps with correct initial conditions.

maxiters = 50;
u0 = zeros(1,numjoints);
mv = u0;
time = 0;
goalreached = false;

initialize the data array for control.

positions = zeros(numjoints,maxiters);
positions(:,1) = x0(1:numjoints)';
velocities = zeros(numjoints,maxiters);
velocities(:,1) = x0(numjoints 1:end)';
accelerations = zeros(numjoints,maxiters);
accelerations(:,1) = u0';
timestamp = zeros(1,maxiters);
timestamp(:,1) = time;

use the (model predictive control toolbox) function for closed-loop trajectory generation. specify the trajectory generation options using an (model predictive control toolbox) object. each iteration calculates the position, velocity, and acceleration of the joints to avoid obstacles as they move towards the goal. the helpercheckgoalreachedkinova script checks if the robot has reached the goal. the helperupdatemovingobstacles script moves the obstacle locations based on the time step.

options = nlmpcmoveopt;
for timestep=1:maxiters
    disp(['calculating control at timestep ', num2str(timestep)]);
    % optimize next trajectory point 
    [mv,options,info] = nlmpcmove(nlobj,x0,mv,[],[], options);
    if info.exitflag < 0
        disp('failed to compute a feasible trajectory. aborting...')
        break;
    end
    % update states and time for next iteration
    x0 = info.xopt(2,:);
    time = time   nlobj.ts;
    % store trajectory points
    positions(:,timestep 1) = x0(1:numjoints)';
    velocities(:,timestep 1) = x0(numjoints 1:end)';
    accelerations(:,timestep 1) = info.mvopt(2,:)';
    timestamp(timestep 1) = time;
    % check if goal is achieved 
    helpercheckgoalreachedkinova;
    if goalreached
        break;
    end
    % update obstacle pose if it is moving
    if ismovingobst
        helperupdatemovingobstacleskinova;
    end
end
calculating control at timestep 1
slack variable unused or zero-weighted in your custom cost function.
all constraints will be hard.
calculating control at timestep 2
slack variable unused or zero-weighted in your custom cost function.
all constraints will be hard.
calculating control at timestep 3
slack variable unused or zero-weighted in your custom cost function.
all constraints will be hard.
calculating control at timestep 4
slack variable unused or zero-weighted in your custom cost function.
all constraints will be hard.
calculating control at timestep 5
slack variable unused or zero-weighted in your custom cost function.
all constraints will be hard.
calculating control at timestep 6
slack variable unused or zero-weighted in your custom cost function.
all constraints will be hard.
calculating control at timestep 7
slack variable unused or zero-weighted in your custom cost function.
all constraints will be hard.
target configuration reached.

execute planned trajectory using joint-space robot simulation and control

trim the trajectory vectors based on the time steps calculated from the plan.

tfinal = timestep 1;
positions = positions(:,1:tfinal);
velocities = velocities(:,1:tfinal);
accelerations = accelerations(:,1:tfinal);
timestamp = timestamp(:,1:tfinal);
vistimestep = 0.2;

use a jointspacemotionmodel to track the trajectory with computed-torque control. the helpertimebasedstateinputskinova function generates the derivative inputs for the ode15s function for modelling the computed robot trajectory.

motionmodel = jointspacemotionmodel('rigidbodytree',robot);
% control robot to target trajectory points in simulation using low-fidelity model
initstate = [positions(:,1);velocities(:,1)];
targetstates = [positions;velocities;accelerations]';    
[t,robotstates] = ode15s(@(t,state) helpertimebasedstateinputskinova(motionmodel,timestamp,targetstates,t,state),...
                                    [timestamp(1):vistimestep:timestamp(end)],initstate);

visualize the robot motion.

helperfinalvisualizerkinova;

figure contains an axes object. the axes object with xlabel x, ylabel y contains 47 objects of type patch, line. one or more of the lines displays its values using only markers these objects represent base_link, shoulder_link, halfarm1_link, halfarm2_link, forearm_link, wrist1_link, wrist2_link, bracelet_link, endeffector_link, shoulder_link_mesh, halfarm1_link_mesh, halfarm2_link_mesh, forearm_link_mesh, wrist1_link_mesh, wrist2_link_mesh, bracelet_link_mesh, base_link_mesh.

[1] schulman, j., et al. "motion planning with sequential convex optimization and convex collision checking." the international journal of robotics research 33.9 (2014): 1251-1270.

网站地图