global robot numJoints trajTimes waypointTimes ;
robot = loadrobot("kinovaGen3");
robot.Gravity = [0 0 -9.81];
robot.DataFormat = 'column';
waypointTimes = 1*[0 5 10 15 20];
ts = waypointTimes(length(waypointTimes))/Number_of_steps;
trajTimes = 0:ts:waypointTimes(end);
numJoints = numel(robot.homeConfiguration);
waypointTimes_guess = 1 * [0 5 10 15 20];
waypointTimes_guess(1) = 0;
waypointTimes_guess(end) = 20;
waypointTimes = waypointTimes_guess;
for iter = 1:maxIterations
[q,qd,qdd] =generateTrajectory(waypointTimes,trajTimes);
energy = computeEnergyConsumption(q, qd, qdd, trajTimes);
gradient = computeGradient(q, qd, qdd, waypointTimes,trajTimes);
waypointTimes(2:end-1) = waypointTimes(2:end-1) - learningRate * gradient;
if norm(gradient) < tolerance
end
Arrays have incompatible sizes for this operation.
Error in solution>generateTrajectory (line 67)
waypoints = toolPositionHome + ...
finalWaypointTimes = waypointTimes;
disp(finalWaypointTimes);
function [q,qd,qdd] =generateTrajectory(waypointTimes,trajTimes)
eeName = string(robot.BodyNames(length(robot.BodyNames)));
q_home = [0 15 180 -130 0 55 90]'*pi/180;
T_home = getTransform(robot, q_home, eeName);
toolPositionHome = T_home(1:3,4);
waypoints = toolPositionHome + ...
[0 0 0 ; -0.1 0.2 0.2 ; -0.2 0 0.1 ; -0.1 -0.2 0.2 ; 0 0 0];
orientations = [pi/2 0 pi/2;
waypointVels= 1*[0 0 0 0 0 0 0;
waypointAccels = 1*[0 0 0 0 0 0 0;
waypointAccelTimes = diff(waypointTimes)/4;
ik = inverseKinematics('RigidBodyTree',robot);
ikWeights = [1 1 1 1 1 1];
ikInitGuess(ikInitGuess > pi) = ikInitGuess(ikInitGuess > pi) - 2*pi;
ikInitGuess(ikInitGuess < -pi) = ikInitGuess(ikInitGuess < -pi) + 2*pi;
includeOrientation = false;
numWaypoints = size(waypoints,2);
jointWaypoints = zeros(numJoints,numWaypoints);
tgtPose = trvec2tform(waypoints(:,idx)') * eul2tform(orientations(:,idx)');
tgtPose = trvec2tform(waypoints(:,idx)') * eul2tform([pi/2 0 pi/2]);
[config,info] = ik(eeName,tgtPose,ikWeights',ikInitGuess');
jointWaypoints(:,idx) = config';
[q,qd,qdd] = trapveltraj(jointWaypoints,numel(trajTimes), ...
'AccelTime',repmat(waypointAccelTimes,[numJoints 1]), ...
'EndTime',repmat(diff(waypointTimes),[numJoints 1]));
[q,qd,qdd] = cubicpolytraj(jointWaypoints,waypointTimes,trajTimes, ...
'VelocityBoundaryCondition',waypointVels);
[q,qd,qdd] = quinticpolytraj(jointWaypoints,waypointTimes,trajTimes, ...
'VelocityBoundaryCondition',waypointVels, ...
'AccelerationBoundaryCondition',waypointAccels);
ctrlpoints = jointWaypoints;
[q,qd,qdd] = bsplinepolytraj(ctrlpoints,waypointTimes([1 end]),trajTimes);
error('Invalid trajectory type! Use ''trap'', ''cubic'', ''quintic'', or ''bspline''');
function energy = computeEnergyConsumption(q, qd, qdd, trajTimes)
jointTorques = zeros(numJoints, numel(trajTimes));
for i = 1:numel(trajTimes)
jointTorques(:,i) = inverseDynamics(robot, q(:,i), qd(:,i), qdd(:,i));
Power(i) = dot(qd(:,i),jointTorques(:,i));
JointPower(:,i) = qd(:,i).*jointTorques(:,i);
TotalPower(i) = sumabs(JointPower(:,i));
Energy = trapz(trajTimes, TotalPower);
function gradient = computeGradient( waypointTimes,trajTimes)
numWaypoints = numel(waypointTimes);
gradient = zeros(size(waypointTimes)-2);
waypointTimes_perturbed = waypointTimes;
waypointTimes_perturbed(i) = waypointTimes_perturbed(i) + epsilon;
trajTimes_perturbed = trajTimes;
trajTimes_perturbed(1+(i-1)*Number_of_steps/4)=waypointTimes_perturbed(i);
[q_perturbed,qd_perturbed,qdd_perturbed ] = generateTrajectory(waypointTimes_perturbed,trajTimes_perturbed);
energy_perturbed = computeEnergyConsumption(q_perturbed, qd_perturbed, qdd_perturbed, trajTimes_perturbed);
gradient(i) = (energy_perturbed - energy) / epsilon;