robot trajectory planning with reusable components -凯发k8网页登录
this example shows how to use entry and exit ports to create multiple connections into and out of linked atomic subcharts. entry and exit ports enable your chart to transition across boundaries in the stateflow® hierarchy while isolating the logic for entering and exiting atomic subcharts. for more information about entry and exit ports, see .
in this example, a simulink® model simulates a robot that maneuvers through an obstacle course in search of a docking station. the model contains three stateflow charts:
route control
defines the strategy used by the robot to search for the dock and navigate the obstacle course.robot
defines the physical characteristics of the robot, such as position and direction of motion, in relation to the dock and the obstacles that surround it.plot trajectory
creates a visual representation of the path that the robot takes as it avoids obstacles and searches for the dock.
define search strategy
the route control
chart defines the strategy that the robot uses to search for the docking station. the chart consists of a combination of linked atomic subcharts from the simulink library model sfrobotexamplelib.slx
. the linked atomic subcharts behave as macros that instruct the robot to move forward, rotate left or right, and make radio contact with the dock. you can combine one or more instances of these subcharts to program your own search strategy.
in this example, the robot moves in a straight line and makes radio contact with the dock at regular intervals. if the robot senses that it is moving away from the dock, it rotates 45 degrees to the right and continues searching. if the robot runs into an obstacle, it rotates 45 degrees to the left and continues searching. initially, the robot moves with a speed of 0.5 meters per second. when the robot comes within 2 meters of the dock, it slows down to a speed of 0.1 meters per second. when the robot comes close to the docking station, the simulation stops. otherwise, after tmax
seconds, the simulation stops and returns a warning.
create multiple entry connections into subchart
in the route control
chart, the linked atomic subchart rotate
contains two entry ports labeled left
and right
. during simulation, the rotate
subchart becomes active when the chart takes a transition that leads to one of these entry ports.
inside the subchart, each entry port has a matching entry junction. the transitions that connect from these entry junctions to the state rotate
set the value of the local data object direction
to 1
or -1
. this value tells the robot whether to turn clockwise or counterclockwise. then, the state entry
action calls the rotate
function in the robot
chart. this function changes the direction of motion for the robot by an angle of direction*angle
. the parameter angle
is specified as pi/4
in the mappings tab of the properties dialog box for the subchart. for more information, see .
create multiple exit connections out of subchart
the linked atomic subchart move
contains two exit ports labeled done
and hit
. inside the subchart, each exit port has a matching exit junction. during simulation, the state actions in the substate forward
call the move
function in the robot
chart. this function changes the position of the robot by a distance of step
, which is specified as 0.1
in the mappings tab of the properties dialog box for the subchart. if the function obstacle
indicates that the robot collided with an obstacle, the robot returns to the last safe position and the chart transitions out of the subchart by using the exit port hit
. otherwise, the robot continues to move forward for a full second and the chart transitions out of the subchart by using the exit port done
.
similarly, the linked atomic subchart contactdock
contains two exit ports, closertodock
and fartherfromdock
. inside the subchart, each exit port has a matching exit junction. during simulation, the state actions in the substate contactdock
call the distancetodock
function in the robot
chart. this function determines the distance from the robot to the docking station. if this distance decreased in the last time step, the chart transitions out of the subchart by using the exit port closertodock
. otherwise, the chart transitions out of the subchart by using the exit port fartherfromdock
.
model physical environment and obstacle course
the robot
chart maintains the position and direction of the robot. the chart also exports several functions to move and rotate the robot, determine the distance from the robot to the dock, and detect obstacles. by calling these functions, the route control
chart never interacts directly with position and direction of the robot.
the mask parameters starting position (x,y) and starting direction (o'clock) specify the values of startpos
and startdir
. to modify these values, open the block parameters dialog box by double-clicking the robot
chart. for more information, see .
the workspace variables dock
, dockwidth
, circles
, and boxes
specify the position and size of the docking station and the obstacles around it.
dock
is a two-element vector that specifies the horizontal and vertical coordinates of the dock.dockwidth
is a scalar that specifies the width of the dock.circles
is an -by-3 matrix that specifies the horizontal coordinate, the vertical coordinate, and the radius of each circular obstacle, where is the number of circular obstacles.boxes
is an -by-4 matrix that specifies the horizontal coordinate, the vertical coordinate, the width, and the height of each rectangular obstacle, where is the number of rectangular obstacles.
the obstacle course can contain arbitrarily many obstacles, but it must contain at least one circular and one rectangular obstacle. by default, the preloadfcn
callback for the model in this example defines an obstacle course with three circular obstacles and three rectangular obstacles.
package reusable components in a library
the robot
chart is a library chart that is linked from the simulink library model sfrobotexamplelib.slx
. this library includes an all-in-one toolkit that defines this chart as well as the linked atomic subcharts for programming your own robot simulation. for more information, see (simulink).
plot robot trajectory
the chart plot trajectory
reads the workspace variables dock
, dockwidth
, circles
, and boxes
, as well as the output signals x
and y
from the robot
chart and done
from the route control
chart to produce a visual representation of the obstacle course and the path that the robot takes as it searches for the dock.
when you start the simulation, the chart calls the helper function sfrobotscene
to create a matlab® figure that shows the docking station in green and the obstacles in red. the code for this function appears at the end of this example. then, at each step of the simulation, the chart plots the location of the robot by using a blue circle. when the input signal done
indicates the end of the simulation, the chart plots the final location of the robot and sets the value of the output signal stop
to true
, causing the stop
block to end the simulation.
to call sfrobotscene
, , and as extrinsic functions, the chart uses the (simulink) function. for more information, see .
display elements of obstacle course
the helper function sfrobotscene
creates a matlab figure that shows the docking station in green and the obstacles in red.
function sfrobotscene(dock,width,boxes,circles) plot(nsidedpoly(8,center=dock,radius=2*width),facecolor="green"); daspect([1 1 1]) hold on for i = 1:height(circles) center = circles(i,1:2); radius = circles(i,3); plot(nsidedpoly(20,center=center,radius=radius),facecolor="red"); end for i = 1:height(boxes) box = boxes(i,:); x = [box(1) box(1) box(3) box(1) box(3) box(1)]; y = [box(2) box(2) box(2)-box(4) box(2)-box(4)]; plot(polyshape(x,y),facecolor="red"); end fig = gcf; fig.name = "robot obstacle course"; fig.numbertitle = 'off'; figure(fig) end
see also
| | (simulink)
related topics
- (simulink)