Statecharts
Statecharts
The top level state machine always start from the Init state to initialize everything. After that, it moves to the FirstAction state to wait for the follower PIC to send the team color and align with the first branch.
Then, it enters the branch state to push the first commit. After exiting the branch, the bot starts to translate in the Repo and the state machine moves to the Repo state. Whenever the bot senses that a branch has a commit that is sufficiently close to it, the state machine will enter the branch state again to push the commit.
The branch State consists of two sub-states, MoveForward and MoveBackward. In the MoveForward state, the bot first moves forward at full speed. When it is close to the commit, the bot starts to decelerate and then push at full strength with the turbine on to push the commit.
In the MoveBackward state, the bot exits the branch state when the back distance sensor senses that the back distance is close. To read the distance sensor reading, we use input capture interrupts to get the time-of-flight measurement.
After exiting the branch, the bot needs to rotate into the repo. After finishing rotation, the front distance sensor is activated to sense when the bot enters the next lane. After entering the next lane, the bot activates the left/right distance sensor to sense whether the commit is close or not. If commit is close, the bot will look for tape only with front distance sensing and align with that branch to move to the branch state in the higher level state machine.
If the commit is far, the bot will decide what is the motion to check the next lane. If the bot is at the left/right most side, it will rotate 180 degree and move to FrontSensing again. Otherwise, it will directly move to the FrontSensing State.
The SPI service sends a new command when the ES_SPI_CM event is received. For specific commands that need a response, a querying response from the follower will start. With the ISR, when a follower response is received, it will be checked for the expected response. The response will be translated to an event only when it is an expected response.
The distance service generates a 10 microsecond high voltage pulse when the Distance timer times out.
The servo service only responds to the ES_GAMESTART to rise the in game flag. It then lowers the flag when the ES_GAMEOVER event is received.
The Top Level Hierarchical State Machine (TopHSM_MotorPIC) is divided into four states:
The "Init Top State" serves as a pseudo state to ensure that all Services have been initialized before the actual program starts.
In the "Detect Beacon State", the robot rotates until two beacons have been found. Only then can the robot calculate the angle between those, and deduce which team we are on (every beacon sends a distinct pulse frequency). The robot then aligns with the target beacon on the opposite side. This state serves as a kind of calibration, since the robot can now move forward into the first branch. Note: This state is only active once every game, the alignment with a beacon is not necessary for the rest of the game.
During most of the time, the robot will be in the "Moving State". This state consists of another, low level state machine which itself involves three states.
In the "Move Forward In Branch State", the the robot starts moving forward to push the obstacle to the other side of the lane. Throughout this process, it slows down before hitting the obstacle to reduce the force upon impact. From the Logic PIC, it then receives the command to go backwards.
In the "Move Backwards in Branch State", the robot drives backwards until it reaches the repo area. Depending on whether we want to push the next commit in the lane to our right or left, once it arrives in our repo area, it rotates 90 degrees clock- or counterclockwise.
In the "Shift Branches State", the robot moves from one branch to the next. There are only two scenarios in which it rotates into a lane: Either, it has reached the end of the arena and the Logic PIC sends a command to rotate 180 degrees, or the Logic PIC detected that a commit is close enough to be pushed and sends a command to find the next repo tape. In the latter, the robot moves forward until tape is detected and then let's the Logic PIC know. Since the tape detection sensors are mounted with an offset from the robot's center of rotation, the Logic PIC will continue measuring it's distance to the front to let the robot move a little further before it then sends the command to rotate. Once the robot has rotated 90 degrees, the "Moving State" will be entered once again.
When the timer (2:18min) expires, the state is automatically switched into the "Game Over State". In this state, both motors are stopped.
Tape Service State Chart
The Tape Tracking State Machine was included to make sure the robot stays in the middle of the lane at all times while moving forward and backward in the lanes. Since the rotation into the branch always puts the robot in a position where it is already centered, we are using only two reflectance sensors to detect when the robot deviates from the tape.
DCMotorService State Chart
Since the Motor Service is a simple Service that did not need any separate states, it is here displayed as only one single pseudo state.
The Motor Service simply receives events from other Services and executes them, e.g. controls the motors' speed and activates the counting of encoder ticks.
SPIService State Chart
The SPI Service as well can be visualized with only one state since it is a simple Service. The events can be divided into two categories:
Receive Events
The ES_SPI_BUF_NOT_EMPTY event comes from the SPI Interrupt Service Routine and tells us that we just received an event from the leader PIC (Logic PIC). The event parameter holds the information which particular command was being sent
Transmit Events
All other displayed events had been posted by the Motor PIC itself. After receiving a SPI message (ES_SPI_BUF_NOT_EMPTY ), the message is read from the buffer and then overwritten with the message that should be send to the leader PIC. This approach is necessary since SPI communication does not allow a follower device to initialize a communication (only if the leader PIC has sent something, we can immediately answer to that).
The Turbine PIC's only task is to check for the status at pin RB9. If it's logic is high, it switches on the Turbine, otherwise it switches it off.
Complete State Chart PDF