path following with obstacle avoidance in simulink -凯发k8网页登录
use simulink to avoid obstacles while following a path for a differential drive robot. this example uses ros to send and receive information from a matlab®-based simulator. you can replace the simulator with other ros-based simulators such as gazebo®.
prerequisites: (ros toolbox)
introduction
this example uses a model that implements a path following controller with obstacle avoidance. the controller receives the robot pose and laser scan data from the simulated robot and sends velocity commands to drive the robot on a given path. you can adjust parameters while the model is running and observe the effect on the simulated robot.
start a robot simulator
start a simple matlab-based simulator:
type (ros toolbox) at the matlab command line. this creates a local ros master with network address (uri) of
http://localhost:11311
.type
examplehelpersimulinkrobotros('obstacleavoidance')
to start the robot simulator. this opens a figure window:
this matlab-based simulator is a ros-based simulator for a differential-drive robot. the simulator receives and sends messages on the following topics:
it receives velocity commands, as messages of type
geometry_msgs/twist
, on the/mobile_base/commands/velocity
topicit sends ground truth robot pose information, as messages of type
nav_msgs/odometry
, to the/ground_truth_pose
topicit sends laser range data, as messages of type
sensor_msgs/laserscan
, to the/scan
topic
replacing the matlab-based simulator with gazebo:
you can also use gazebo simulator with a simulated turtlebot®. see (ros toolbox) for instructions on setting up the gazebo environment. see (ros toolbox) for instructions on setting up network connection with gazebo. after starting the virtual machine, launch gazebo office world using the desktop shortcut. the simulated turtlebot in the gazebo simulator, receives velocity commands, as messages of type geometry_msgs/twist
, on the /cmd_vel
topic. you also need a localization algorithm to get robot position in the gazebo. see localize turtlebot using monte carlo localization algorithm for instructions on finding robot location in gazebo environment.
open existing model
this implements the path following with obstacle avoidance algorithm. the model is divided into four subsystems. the following sections explain each subsystem.
open_system('pathfollowingwithobstacleavoidanceexample');
process inputs
the 'inputs' subsystem processes all the inputs to the algorithm.
there are two subscribers to receive data from the simulator. the first subscriber receives messages sent on the /scan
topic. the laser scan message is then processed to extract scan ranges and angles. the second subscriber receives messages sent on the /ground_truth_pose
topic. the (x,y)
location and yaw orientation of the robot is then extracted from the pose message.
the path is specified as a set of waypoints. this example uses a 3x2 constant input. you can specify any number of waypoints as an nx2 array. to change the size of the path at run-time, you can either use a variable sized signal or use a fixed size signal with nan
padding. this example uses a fixed size input with nan
padding for the waypoints that are unknown.
open_system('pathfollowingwithobstacleavoidanceexample/inputs','tab');
compute velocity and heading for path following
the 'compute velocity and heading for path following' subsystem computes the linear and angular velocity commands and the target moving direction using the pure pursuit block. the pure pursuit block is located in the mobile robot algorithms sub-library within the robotics system toolbox tab in the library browser. alternatively, you can type robotalgslib
on the command-line to open the mobile robot algorithms sub-library.
you also need to stop the robot once it reaches a goal point. in this example, the goal is the last waypoint on the path. this subsystem also compares the current robot pose and the goal point to determine if the robot is close to the goal.
open_system('pathfollowingwithobstacleavoidanceexample/compute velocity and heading for path following','tab');
adjust velocities to avoid obstacles
the 'adjust velocities to avoid obstacles' subsystem computes adjustments to the linear and angular velocities computed by the path follower.
the vector field histogram block uses the laser range readings to check if the target direction computed using the pure pursuit block is obstacle-free or not based on the laser scan data. if there are obstacles along the target direction, the vector field histogram block computes a steering direction that is closest to the target direction and is obstacle-free. the vector field histogram block is also located in the mobile robot algorithms sub-library.
the steering direction is nan
value when there are no obstacle-free directions in the sensor field of view. in this case, a recovery motion is required, where the robot turns on-the-spot until an obstacle-free direction is available.
based on the steering direction, this subsystem computes adjustments in linear and angular velocities.
open_system('pathfollowingwithobstacleavoidanceexample/adjust velocities to avoid obstacles','tab');
send velocity commands
the 'outputs' subsystem publishes the linear and angular velocities to drive the simulated robot. it adds the velocities computed using the pure pursuit path following algorithm with the adjustments computed using the vector field histogram obstacle avoidance algorithm. the final velocities are set on the geometry_msgs/twist
message and published on the topic /mobile_base/commands/velocity
.
this is an enabled subsystem which is triggered when new laser message is received. this means a velocity command is published only when a new sensor information is available. this prevents the robot from hitting the obstacles in case of delay in receiving sensor information.
open_system('pathfollowingwithobstacleavoidanceexample/outputs','tab');
note: to use gazebo simulator, select the /cmd_vel
topic in the publish block.
configure and run the model
configure and run your model and observe the motion of the robot in the simulator.
set simulation stop time to
inf
.click the play button to start simulation. observe that the robot starts moving in the simulation.
while the simulation is running, open 'compute velocity and heading for path following' subsystem and double-click on the pure pursuit block. change the desired linear velocity parameter to
0.5
. observe increase in the velocity of the robot.the default path
[2 2; 8 8]
passes through an obstacle. observe that the robot takes a detour around the obstacle to reach the end point of the path.open the 'inputs' subsystem and double-click on the waypoints input block. change the constant value from
[2 2;8 8;nan nan]
to[2 2; 8 8; 12 5]
. notice that robot continues to follow the new path and reaches the new goal point(12,5)
while avoiding obstacles.to stop the simulation, click the stop button.
see also
(ros toolbox)
(robotics system toolbox)