Skip to content

Commit bdf333e

Browse files
authored
Merge pull request #7 from mathworks-robotics/MPC_BugFix_And_Improvement
MPC improvement + CreatePathFollowingMPCController example MPPI behav…
2 parents 4b46409 + 426b07a commit bdf333e

6 files changed

+45
-19
lines changed

CreatePathFollowingMPCController.mlx

-149 KB
Binary file not shown.

Helpers/MPCController/exampleHelperGenerateMPCReferenceSegment.m

+21-6
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,31 @@
2020
% Find the closes point on the reference path to the current position
2121
[~, ii] = min(vecnorm(path-pos,2,2));
2222

23-
% Find the next 10 points on the path, make sure we don't run over the
24-
% end of the path
25-
for jj=1:length(ind)
26-
ind(jj) = min(ii+(jj-1), size(refPath,1));
23+
remainingPath = refPath(ii:end,:);
24+
numStates = size(remainingPath,1);
25+
refEndPose = refPath(end,:);
26+
dist = vecnorm(curPose(1:2)-refEndPose(1:2),2,2);
27+
if numStates < predictionHorizon
28+
if numStates >= 2 && dist > 2
29+
% Interpolate evenly between curPose and end pose at reference
30+
% path to have predictionHorizon number of states for MPC.
31+
xBounds = [min(remainingPath(:,1))-10, max(remainingPath(:,1))+10];
32+
yBounds = [min(remainingPath(:,2))-10, max(remainingPath(:,2))+10];
33+
ss = stateSpaceSE2([xBounds;yBounds;[-pi pi]]);
34+
pathObj = navPath(ss, remainingPath);
35+
interpolate(pathObj, 10);
36+
refPathToMPC = pathObj.States;
37+
else
38+
refPathToMPC = repmat(refEndPose,predictionHorizon,1);
39+
end
40+
else
41+
refPathToMPC = remainingPath(1:predictionHorizon,:);
2742
end
28-
43+
2944
% Assemble trajectory. In the first stage, the trajectory is not part of the cost function, therefore we set the first row to zero.
3045
trajectory = [ zeros(1,3);
3146
curPose(:)';
32-
refPath(ind(2:end),:)
47+
refPathToMPC(2:end,:)
3348
];
3449
% MPC needs a smooth pose angle trajectory. If the pose angle is
3550
% wrapping around e.g. 2pi, we need to catch it and fix it to avoid the discontinuity.

Helpers/TEBController/MPPIControllerFcn.m

+24-13
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
persistent mppiObj
99
persistent mapObj
1010
persistent vehicleObj;
11-
persistent constraintViolated;
11+
persistent numberOfViolations;
1212

13-
if isempty(constraintViolated)
14-
constraintViolated = 0;
13+
if isempty(numberOfViolations)
14+
numberOfViolations = 0;
1515
end
1616

1717
if isempty(mapObj)
@@ -30,10 +30,15 @@
3030
mppiObj.ReferencePath = refPathXY;
3131
end
3232

33-
[velcmds,optpath,info,numTrajs,needLocalReplan,needGlobalReplan] = stepImp(mppiObj, controllerParams, constraintViolated,curpose,curvel);
33+
[velcmds,optpath,info,numTrajs,needLocalReplan,needGlobalReplan,resetNumViolations] = stepImp(mppiObj, controllerParams, numberOfViolations,curpose,curvel);
3434
sampleTime = mppiObj.SampleTime;
3535
trajs = info.Trajectories;
3636
lookaheadPoses = info.LookaheadPoses;
37+
if resetNumViolations
38+
numberOfViolations = 0;
39+
else
40+
numberOfViolations = numberOfViolations + 1;
41+
end
3742
end
3843

3944

@@ -65,38 +70,44 @@
6570
mppi.Parameter.VehicleCollisionInformation = struct("Dimension",[length, width],"Shape","Rectangle");
6671
end
6772

68-
function [velcmds,optpath,info,numTrajs,needLocalReplan,needGlobalReplan] = stepImp(mppiObj, controllerParams, constraintViolated,curpose,curvel)
73+
function [velcmds,optpath,info,numTrajs,needLocalReplan,needGlobalReplan,resetNumViolations] = stepImp(mppiObj, controllerParams, numberOfViolations,curpose,curvel)
6974
% stepImp return the control commands from the MPPI controller.
7075

7176
needLocalReplan = 0;
7277
needGlobalReplan = 0;
73-
74-
if constraintViolated == 1
78+
resetNumViolations = true;
79+
if numberOfViolations > 0
7580
% When constraints are violated in previous MPPI output, relax the
7681
% parameters in MPPI temporarily. The number of trajectories are doubled
7782
% increase the standard deviation, and keep the PathFollowing cost
7883
% to 0 and restore them back after velocity commands are generated.
7984
mppiObj.NumTrajectory = 2*controllerParams.NumTrajectory;
8085
stdDev = controllerParams.StandardDeviation;
8186
mppiObj.StandardDeviation = [stdDev(1) 2*stdDev(2)];
82-
mppiObj.Parameter.CostWeight.PathFollowing = 0.1;
87+
mppiObj.Parameter.CostWeight.PathAlignment = 0;
8388
numTrajs = mppiObj.NumTrajectory;
8489
% Generate optimal local path
8590
[velcmds,optpath,info] = mppiObj(curpose(:)',curvel(:)');
8691
mppiObj.NumTrajectory = controllerParams.NumTrajectory;
8792
mppiObj.StandardDeviation = stdDev;
88-
mppiObj.Parameter.CostWeight.PathFollowing = controllerParams.PathFollowingCost;
93+
mppiObj.Parameter.CostWeight.PathAlignment = controllerParams.PathAlignmentCost;
8994
else
9095
[velcmds,optpath,info] = mppiObj(curpose(:)',curvel(:)');
9196
numTrajs = mppiObj.NumTrajectory;
9297
end
9398

94-
% Trigger local replan if the mppi output trajectory has constraints violated.
9599
if info.ExitFlag == 1
96-
needLocalReplan = 1;
97-
% Trigger global replan if the vehicle is far from reference path or it did
98-
% not move using previous controls.
100+
if numberOfViolations == 0
101+
% Trigger local replan if the mppi output trajectory has constraints
102+
% violated and previous mppi call did not result in violation.
103+
needLocalReplan = 1;
104+
resetNumViolations = false;
105+
elseif numberOfViolations >= 1
106+
% Trigger global replan if constraint violation occured on consecutive MPPI calls
107+
needGlobalReplan = 1;
108+
end
99109
elseif info.ExitFlag == 2
110+
% Trigger global replan if the vehicle is far from reference path.
100111
needGlobalReplan = 1;
101112
end
102113

-8.67 KB
Binary file not shown.

SimModels/MPCFollower.slx

7.04 KB
Binary file not shown.

SimModels/MPPIController.slx

427 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)