[Getting Started with Stateflow] Task 11. Chart Hierarchy

Robot Vacuum Driving Modes

Introduction

Background
As you've just learned, using hierarchy in your Stateflow models helps you group states with common functionality. In this project, you will use hierarchy to model something similar to the driving logic of the robotic vacuum that you previously modeled. , the robot could drive in two modes: docking or vacuuming. The robot's instructions for each of these modes were: (
As you just learned, using hierarchies in Stateflow models can help you group states with common functionality. In In this project, you will use hierarchies to model something similar to the driving logic of the robot vacuum you modeled earlier. In this project, the robot can be driven in two modes: docking or vacuuming. The robot is responsive to each mode The instructions are as follows :)

SeekDock – drive to the dock and then stop. (SeekDock – drive to the dock and then stop.)

Vacuum – drive to the corner of the room and then follow a zig-zag pattern.

Thus, the robot required two driving behaviors: drive to a location and follow a zig-zag pattern. These can be implemented as the top-level states in a hierarchy. The substates for each are the particular states required to implement each behavior. For example, when driving to a location, the robot's motion can be broken down into: orient using pure rotation, drive using pure translation, and stop. (Thus, the robot requires two
driving behaviors: driving to a location and following a zigzag pattern. These can be implemented as top-level states in a hierarchy. The sub-states of each are the specific states required to implement each behavior. For example, when traveling to a location, the motion of the robot can be broken down into: Orientation using pure rotation, Orientation using Pure translation is driven, as well as stopped.)
insert image description here
Project
In this project, you will implement a driving pattern in which the robot can either dock or drive a random pattern around the room. (Many early or discount models of robotic vacuums follow a random pattern, which is simple to implement, although less efficient than using a map.) Thus, DriveToLocation and RandomDrive are two superstates. Your Stateflow chart will also have a TaskComplete state in which no further driving commands are sent. (
In this project, you will implement a drive mode in which , the robot can dock or drive in a random pattern around a room. (Many early or discount model robot vacuums follow a random pattern, which is easy to implement, although not as efficient as using a map.) Therefore, DriveToLocation and RandomDrive are the two super states .Your state flowchart will also be in the TaskComplete state, in which no further driver commands will be sent.)
insert image description here
As with the Robot Vacuum Supervisory Control project, the driving dynamics of the robotic vacuum have been built in Simulink. You will connect simulated sensor data and other information from this model to your Stateflow chart in order to set the desired velocity and angular velocity commands to
(As with the Robot Vacuum Monitoring project, the driving dynamics of the robot vacuum are established in Simulink. You connect the simulated sensor data and other information from this model to a state flow diagram to set the settings to be sent to the robot . The required velocity and angular velocity commands.) insert image description here
You will build this model in two parts. In Part 1, you will define the high-level structure of the driving mode logic. Then, in Part 2, you will fill in the implementation details and connect the Stateflow chart to Simulink.
(You will build this model in two parts. In part 1, you will define the high-level structure of the driving mode logic. Then, in part 2, you will fill in the implementation details and connect the Stateflow The diagram is connected to Simulink.)

Part1

Task1(Task 1)

As previously noted, the driving behavior can be captured by three states: drive to a specified location, drive randomly, and stop
. parking.)

Create the states DriveToLocation , RandomDrive , and TaskComplete . Designate DriveToLocation as the default state . _

insert image description here

Task2(Task 2)

Although there are three internal states to this system, there are only two overall drive modes
.

1.Drive to the dock.(Drive to the dock.)
2.Drive to the center of the room, then begin the random pattern.(Drive to the center of the room, then begin the random pattern.)

The mode is set by a chart input, driveMode. In both behaviors, the first step is to drive to a specified location. Thus, both drive modes first enter the DriveToLocation state. Then, if driveMode == 1, the task is complete. If driveMode2, begin the random drive.
(The mode is set by the chart input driveMode. In both actions, the first step is to drive the car to the specified location. Therefore, both driving modes first enter the DriveToLocation state. Then, if driveMode1, the task is completed. if driveMode2, then start random driving. )

The system can leave the DriveToLocation state when positionReached == 1.
(When positionReached ==1, the system can leave the DriveToLocation State.)

  1. Add transitions from DriveToLocation to RandomDrive and to TaskComplete . Using the variables positionReached and driveMode , add the necessary conditions to implement the behavior described above . conditions to achieve the above behavior.)

  2. Add a state entry action to initialize the value of positionReached to 0.
    (2. Add a state entry action to initialize the value of positionReached to 0. )

Set the Data scope of positionReached to Local Data. driveMode is a user input, so its scope should be Input Data.
insert image description here

Task3(Task 3)


The random vacuum task consists of driving until bumping into a wall, rotating a random amount, and repeating the bump/rotate sequence. The task is complete after the bump sensor registers more than 50 times. , rotate a random amount, and repeat the collision/rotation sequence. After the ventilation sensor registers more than 50 times, the task is complete.)

Add the appropriate transition and condition based on a counter , bumpCount . Set bumpCount to 0 upon entering the RandomDrive state .

Define the Symbol as Local Data.
insert image description here

Task4(Task 4)

The “drive to location” behavior occurs in three steps, in the following order: pure rotation to the correct orientation, pure translation to the correct location, and then stop
. : Pure rotation to the correct direction, pure translation to the correct position, and then stop.)

Create the states PureRotate , PureTranslate , and Stop as substates of DriveToLocation . Add the transitions to implement the described sequence. (You will add conditions in Part 2. ) Implement the sequence described. (You will add the conditions in Part 2.))

insert image description here

Task5 (task 5)

The “random drive” behavior consists of two actions: find the wall and rotate. During random driving, the robot repeatedly cycles through these actions
. , the robot will repeat these actions.)

Create states FindWall and RotateRandom as substates of RandomDrive , as well as the necessary transitions. FindWall should be the default state for this level of hierarchy . The default state at the structure level.)

insert image description here

Part2

Task1(Task 1)


The chart on the right contains the driving mode framework that you built in the last lesson. Now you will add specific behaviors for each state. Add specific behavior for each state.)

This chart will output the commands for velocity and angular velocity. These will be passed to the Simulink model of the robot. In pure rotation, you will set velocity, v, to 0 and angular velocity, w, to 1. In pure translation, v=3 and w=0. When stopped, both values ​​should equal 0.
(This graph will output commands for velocity and angular velocity. These will be passed into the Simulink model of the robot. In pure rotation, set velocity v to 0 , setting the angular velocity w to 1. In pure translation, v=3, w=0. When stopped, both values ​​should equal 0.)

1. Add state entry actions to PureRotate , PureTranslate , and Stop to set the appropriate values ​​for v and w in each state.
(1. Add state entry actions to PureRate , PureTranslate , and Stop to set the appropriate values ​​for v and w in each state. w Set appropriate values.)
2. Define the scope of v and w so that they can be passed to the Simulink model.
(2. Define the scope of v and w so that they can be passed to the Simulink model.)
3. Add an entry action to the Stopstate to set positionReached to 1.
(3. Add the entry operation to the stop state and set the position reached to 1. )
insert image description here

Task2(Task 2)

Four additional inputs from Simulink help track the robot's progress during a given behavior
.

desiredDist – desired distance to destination (desiredDist – desired distance
to destination) desiredRot – desired rotation to destination (desiredRot – desired rotation to destination)
elapsedDist – instantaneous elapsed distance (elapsedDist – instantaneous elapsed distance)
elapsedRot – instantaneous elapsed rotation (elapsedRot – Instantaneous rotation) These provide the conditions
insert image description here
for transitioning between the DriveToLocation substates: as soon as the current rotation or distance meets or exceeds the target, the task is complete.
Once the current rotation or distance reaches or exceeds the target, the mission is complete.)

Add the conditions to the transitions between the DriveToLocation substates . Set all of the new Symbols to Input Data .

Note: For rotation, the angle needs to be wrapped, so the value of interest is mod(elapsedRot,2*pi ) . ) .)

insert image description here

Task3(Task 3)

The DriveToLocation behavior is now complete. The RandomDrive behavior is similar, consisting of a pure translation while in FindWall and a pure rotation in RotateRandom
. pure rotation.)

Add state entry actions to FindWall and RotateRandom to set the velocity, v , and angular velocity, w , in each state. Use the same values ​​as the PureTranslate and PureRotate states . Velocity v and angular velocity w in each state . Use the same values ​​as for PureTranslate and PureRate states.)

insert image description here

Task4(Task 4)


After bumping into a wall, the robot transitions to the random rotation behavior. This is detected by a bump sensor and recorded in the Simulink signal, bump. A value of 1 indicates a detetion. Rotation behavior. This is detected by the collision sensor and recorded in the Simulink signal, i.e. collision. A value of 1 indicates a detetion.)

Add a condition on the transition from FindWall to RotateRandom such that the transition is vaild when a bump is detected. Define bump as Input Data . Define bump as input data.)

Then, add a condition action that increments bumpCount by 1 when the condition is true. (Then, add a condition action that increments bumpCount by 1
when the condition is true. )
insert image description here

Task5 (task 5)

To achieve a random rotation, you can create a random variable, tRotate, that represents the amount of time to remain in the rotation state. You can then use this value as the argument in a temporal operator to determine when to transition back to the FindWall state.
(To implement random rotation, you can create a random variable tRotate that represents the amount of time to remain in the rotated state. You can then use that value as an argument in the temporal operator to determine when to transition back to the FindWall state .)

Add the following MATLAB command to the RotateRandom entry action. The command generates a random number between 2.5 and 4 .

tRotate = row(1) * 1.5+2.5;

Use tRotate and a temporal operator to transition from RotateRandom to FindWall after 2.5-4 seconds . Remember to Resolve undefined symbols to define tRotate as Local Data . Remember to resolve undefined symbols to define t-rotation as local data.)

insert image description here

Task6(Task 6)

When a given drive mode task is complete, send a flag back to Simulink
.

Set complete to 1 upon entering the TaskComplete state . Define this as Output Data in the Symbols Pane .

insert image description here

Task7(Task 7)

Navigate to Simulink and connect the unconnected signals with their corresponding ports on the Stateflow chart, based on the signal names
.
insert image description here
insert image description here
insert image description here

Task8 (task 8)

Finally, to uncross the signals, change the port order in the Stateflow chart.
(Finally, to uncross the signals, change the port order in the Stateflow chart.)

Change the input port of bump to port 2, so the resulting input port order is as follows.
(Change the input port of bump to port 2, so the resulting input port order is as follows.)

1 – driveMode
2 – bump
3 – desiredDist
4 – desiredRot
5 – elapsedDist
6 – elapsedRot
insert image description here

Guess you like

Origin blog.csdn.net/J_WangJiang/article/details/129811679