Path Following For A Differential Drive Robot - MATLAB & Simulink Example
Path Following For A Differential Drive Robot - MATLAB & Simulink Example
Note: Starting in R2016b, instead of using the step method to perform the operation defined by the System object, you
can call the object with arguments, as if it were a function. For example, y = step(obj,x) and y = obj(x) perform
equivalent operations.
Define waypoints
Define a set of waypoints for the desired path for the robot
% Set the current location and the goal location of the robot as defined by the path
robotCurrentLocation = path(1,:);
robotGoal = path(end,:);
Assume an initial robot orientation (the robot orientation is the angle between the robot heading and the positive X-axis,
measured counterclockwise).
initialOrientation = 0;
Initialize the robot simulator and assign an initial pose. The simulated robot has kinematic equations for the motion of a
two-wheeled differential drive robot. The inputs to this simulated robot are linear and angular velocities. It also has
plotting capabilities to display the robot's current location and draw the trajectory of the robot.
robotRadius = 0.4;
robot = ExampleHelperRobotSimulator('emptyMap',2);
robot.enableLaser(false);
robot.setRobotSize(robotRadius);
robot.showTrajectory(true);
robot.setRobotPose(robotCurrentPose);
plot(path(:,1), path(:,2),'k--d')
xlim([0 13])
ylim([0 13])
controller = robotics.PurePursuit
Use the path defined above to set the desired waypoints for the controller
controller.Waypoints = path;
Set the path following controller parameters. The desired linear velocity is set to 0.3 meters/second for this example.
controller.DesiredLinearVelocity = 0.3;
The maximum angular velocity acts as a saturation limit for rotational velocity, which is set at 2 radians/second for this
example.
controller.MaxAngularVelocity = 2;
As a general rule, the lookahead distance should be larger than the desired linear velocity for a smooth path. The robot
might cut corners when the lookahead distance is large. In contrast, a small lookahead distance can result in an
unstable path following behavior. A value of 0.5 m was chosen for this example.
controller.LookaheadDistance = 0.5;
Using the path following controller, drive the robot over the desired waypoints
The path following controller provides input control signals for the robot, which the robot uses to drive itself along the
desired path.
Define a goal radius, which is the desired distance threshold between the robot's final location and the goal location.
Once the robot is within this distance from the goal, it will stop. Also, you compute the current distance between the
robot location and the goal location. This distance is continuously checked against the goal radius and the robot stops
when this distance is less than the goal radius.
Note that too small value of the goal radius may cause the robot to miss the goal, which may result in an unexpected
behavior near the goal.
goalRadius = 0.1;
distanceToGoal = norm(robotCurrentLocation - robotGoal);
The robotics.PurePursuit object computes control commands for the robot. Drive the robot using these control
commands until it reaches within the goal radius. If you are using an external simulator or a physical robot, then the
controller outputs should be applied to the robot and a localization system may be required to update the pose of the
robot. The controller runs at 10 Hz.
controlRate = robotics.Rate(10);
while( distanceToGoal > goalRadius )
% Extract current location information ([X,Y]) from the current pose of the
% robot
robotCurrentPose = robot.getRobotPose;
waitfor(controlRate);
end
The simulated robot has reached the goal location using the path following controller along the desired path. Close
simulation.
delete(robot)
You can compute the path using the PRM path planning algorithm. See Path Planning in Environments of Different
Complexity for details.
mapInflated = copy(robot.Map);
inflate(mapInflated,robotRadius);
prm = robotics.PRM(mapInflated);
prm.NumNodes = 100;
prm.ConnectionDistance = 10;
Find a path between the start and end location. Note that the path will be different due to the probabilistic nature of the
PRM algorithm.
release(controller);
controller.Waypoints = path;
Set current location and the goal of the robot as defined by the path
robotCurrentLocation = path(1,:);
robotGoal = path(end,:);
initialOrientation = 0;
Reset the current position of the simulated robot to the start of the path.
robot.setRobotPose(robotCurrentPose);
Compute distance to the goal location
goalRadius = 0.1;
Drive the robot using the controller output on the given map until it reaches the goal. The controller runs at 10 Hz.
reset(controlRate);
while( distanceToGoal > goalRadius )
waitfor(controlRate);
end
The simulated robot has reached the goal location using the path following controller along the desired path. Stop the
robot.
drive(robot, 0, 0);
Close Simulation.
delete(robot);
See Also
Path Planning in Environments of Different Complexity
Mapping With Known Poses