You are on page 1of 22

SendingCommand State Machine

Purpose:
Communicating with PAC by writing data to the data register of TIVA and read
data after EOT interrupt.
Module Level Variables:
#define PS_STATUS_COMMAND 0b11000000
#define PS_CHANGE_COMMAND 0b10000000
#define PS_QUERY_COMMAND 0b01110000
#define BYTETRANSFER_TIMER_PERIOD 2
uint8_t ZEROZEROBYTE = 0x00;
uint8_t FinalDataReceived[5] = {0,0,0,0,0};
bool EOTFlag = false;
static SendingCommand_t CurrentState = SendingCommandSTATE_Waiting;

Functions:
ES_Event RunSendingCommandSM( ES_Event CurrentEvent )
Check the status of current state
If CurrentState is SendingCommandSTATE_Waiting
Check the event type of current event
If event type is ES_SENDING_COMMAND
- Make state transition to true
- Set NextState as SendingCommandSTATE_Wait4EOT
- Consume the event
- Enable the NVIC interrupt for EOT interrupt
- Write data to the data register of TIVA
End if
End if
If CurrentState is SendingCommandSTATE_Wait4EOT
Check the event type of current event
If event type is ES_EOT
- Make state transition to true
- Set NextState as SendingCommandSTATE_Wait4EOT
- Consume the event
- Set a time interval (timer) between each data transfer
End if
End if
If CurrentState is SendingCommandSTATE_Wait4Timeout
Check the event type of current event
If event type is ES_TIMEOUT
- Make state transition to true
- Set NextState as SendingCommandSTATE_Waiting
End if
End if
If make state transition is true
Execute exit function for current state
Modify state variables

Execute entry function for new state. This defaults to ES_ENTRY


End if
Return ReturnEvent to upper level state machine
void EOTInterruptRoutine ( void ) (Interrupt response routine for EOT interrupt)
Disable the EOT interrupt in the NVIC
Grab data from the SSI data register
Self-post ES_EOT to this service

ByteTransfer State Machine


Purpose:
Determining the correct command to send to PAC based on the event type and then
calling SendingCommand service to communication with PAC.

Module Level Variables


static ByteTransfer_t CurrentState = ByteTransferSTATE_Waiting;
static uint16_t DataType = 0;
#define BYTETRANSFER_TIMER_PERIOD 2
static uint8_t OverallPSStatus[12] = {0,0,0,0,0,0,0,0,0,0,0,0};

Functions:
ES_Event RunByteTransferSM( ES_Event CurrentEvent )
Check the status of current state
If CurrentState is ByteTransferSTATE_Waiting
Check the event type of current event
If event type is ES_BYTETRANSFER_OCCUPY
- Make state transition to true
- Set NextState as
ByteTransferSTATE_SendingOccupyingCommand
- Consume the event
- Self post ES_SEND_COMMAND to Master State
Machine with corresponding command as event
parameter
End if
If event type is ES_BYTETRANSFER_STATUS
- Make state transition to true
- Set NextState as
ByteTransferSTATE_SendingStatusCommand
- Consume the event
- Self post ES_SEND_COMMAND to Master State
Machine with corresponding command as event
parameter
End if
If event type is ES_BYTETRANSFER_QUERY
- Make state transition to true

- Set NextState as
ByteTransferSTATE_SendingQueryCommand
- Consume the event
- Self post ES_SEND_COMMAND to Master State
Machine with corresponding command as event
parameter
End if
End if
If CurrentState is ByteTransferSTATE_SendingOccupyingCommand
Execute During function for sending occupy command. ES_ENTRY &
ES_EXIT are processed here allow the lower level state machines to remap or consume the event.
Check the event type of current event
If event type is ES_TIMEOUT from SendingCommand module
- Make state transition to true
- Set NextState as ByteTransferSTATE_Waiting
- Consume the event
- Self post ES_PAC_BUSY to the Master State Machine
End if
End if
If CurrentState is ByteTransferSTATE_SendingStatusCommand
Execute During function for sending status command. ES_ENTRY &
ES_EXIT are processed here allow the lower level state machines to remap or consume the event.
Check the event type of current event
If event type is ES_TIMEOUT from SendingCommand module
- Make state transition to true
- Set NextState as ByteTransferSTATE_Waiting
- Consume the event
- Update overall polling station status based on the
return data
End if
End if
If CurrentState is ByteTransferSTATE_SendingQueryCommand
Execute During function for sending query command. ES_ENTRY &
ES_EXIT are processed here allow the lower level state machines to remap or consume the event.
Check the event type of current event
If event type is ES_TIMEOUT from SendingCommand module
- Make state transition to true
- Set NextState as ByteTransferSTATE_Waiting
- Consume the event
- Check the query response and post event to Master
State Machine based on return data
End if
End if
If make state transition is true

Execute exit function for current state


Modify state variables
Execute entry function for new state. This defaults to ES_ENTRY
End if
Return ReturnEvent to upper level state machine

Occupying State Machine


Purpose:
Performing the task for occupying polling station

Module Level Variables:


static Occupying_t CurrentState = OccupyingSTATE_Waiting;
#define QUERY_COMMAND 0x0070
#define PS_OCCUPY_COMMAND_MASK 0x0080;
#define BAD_MAGNETIC_FIELD_SENSEING 0xff;
uint8_t OccupyCommand = PS_OCCUPY_COMMAND_MASK;
uint8_t AbortOccupyingIndex = 0;
uint8_t MagneticFieldFrequency = 0xff;
uint8_t NumberOfSucceedCaptures = 0;

Functions:
ES_Event RunOccupyingSM( ES_Event CurrentEvent )
Check the status of current state
If CurrentState is OccupyingSTATE_Waiting
Check the event type of current event
If event type is ES_STATT_OCCUPY_PS
Increment AbortOccupyingIndex by 1
Construct occupy command based on team color
Read the magnetic field frequency
If the frequency is valid
- Construct occupy command based on
frequency reading
- Make state transition to true
- Set NextState as
OccupyingSTATE_Wait4FirstACK
- Consume the event
- Self post ES_BYTETRANSFER_OCCUPY to
the Master State Machine
- Set 0 to AbortOccupyingIndex
End if
Else if AbortOccupyingIndex equals 5
- Make state transition to true
- Set NextState as OccupyingSTATE_Waiting
- Self post ES_ABORT_OCCUPYING to the
Master State Machine
- Set 0 to AbortOccupyingIndex
End if
Else

- Self post ES_START_OCCUPY_PS to the


Master State Machine
End if
End if
If CurrentState is OccupyingSTATE_Wait4FirstACK
Execute During function for sending query command. ES_ENTRY &
ES_EXIT are processed here allow the lower level state machines to remap or consume the event.
Process any events and check the event type of current event
If event type is ES_PAC_BUSY
- Make state transition to true
- Set NextState as
CommnunicatingSTATE_Wait4FirstPACReady
- Consume the event
- Self post ES_BYTETRANSFER_QUERY to the
Master State Machine
End if
If event type is ES_NACK
Increment AbortOccupyingIndex by 1
Construct occupy command based on team color
Read the magnetic field frequency
If the frequency is valid
- Construct occupy command based on
frequency reading
- Make state transition to true
- Set NextState as
OccupyingSTATE_Wait4FirstACK
- Consume the event
- Self post ES_BYTETRANSFER_OCCUPY to
the Master State Machine
- Set 0 to AbortOccupyingIndex
End if
Else if AbortOccupyingIndex equals 5
- Make state transition to true
- Set NextState as OccupyingSTATE_Waiting
- Self post ES_ABORT_OCCUPYING to the
Master State Machine
- Set 0 to AbortOccupyingIndex
End if
Else
- Self post ES_NACK to the Master State
Machine
End if
End if
If event type is ES_ACK
Start a timeout for 200 miliseconds
End if
If event type is ES_BLOCKED
- Make state transition to true

- Set NextState as OccupyingSTATE_Waiting


- Self post ES_ABORT_OCCUPYING to the Master
State Machine
- Set 0 to AbortOccupyingIndex
End if
If event type is ES_TIMEOUT
Increment AbortOccupyingIndex by 1
Construct occupy command based on team color
Read the magnetic field frequency
If the frequency is valid
- Construct occupy command based on
frequency reading
- Make state transition to true
- Set NextState as
OccupyingSTATE_Wait4SecondACK
- Consume the event
- Self post ES_BYTETRANSFER_OCCUPY to
the Master State Machine
- Set 0 to AbortOccupyingIndex
End if
Else if AbortOccupyingIndex equals 5
- Make state transition to true
- Set NextState as OccupyingSTATE_Waiting
- Self post ES_ABORT_OCCUPYING to the
Master State Machine
- Set 0 to AbortOccupyingIndex
End if
Else
- Self post ES_TIMEOUT to the Master State
Machine
End if
End if
End if
If CurrentState is OccupyingSTATE_Wait4SecondACK
Execute During function for sending query command. ES_ENTRY &
ES_EXIT are processed here allow the lower level state machines to remap or consume the event.
Process any events and check the event type of current event
If event type is ES_PAC_BUSY
- Make state transition to true
- Set NextState as
CommnunicatingSTATE_Wait4FirstPACReady
- Consume the event
- Self post ES_BYTETRANSFER_QUERY to the
Master State Machine
End if
If event type is ES_NACK
Increment AbortOccupyingIndex by 1
Construct occupy command based on team color
Read the magnetic field frequency

If the frequency is valid


- Construct occupy command based on
frequency reading
- Make state transition to true
- Set NextState as
OccupyingSTATE_Wait4FirstACK
- Consume the event
- Self post ES_BYTETRANSFER_OCCUPY to
the Master State Machine
- Set 0 to AbortOccupyingIndex
End if
Else if AbortOccupyingIndex equals 5
- Make state transition to true
- Set NextState as OccupyingSTATE_Waiting
- Self post ES_ABORT_OCCUPYING to the
Master State Machine
- Set 0 to AbortOccupyingIndex
End if
Else
- Self post ES_NACK to the Master State
Machine
End if
End if
If event type is ES_ACK
- Make state transition to true
- Set NextState as OccupyingSTATE_Waiting
- Self post ES_OCCUPY_SUCCEED to the Master
State Machine
- Set 0 to AbortOccupyingIndex
End if
If event type is ES_BLOCKED
- Make state transition to true
- Set NextState as OccupyingSTATE_Waiting
- Self post ES_ABORT_OCCUPYING to the Master
State Machine
- Set 0 to AbortOccupyingIndex
End if
End if
End if
If make state transition is true
Execute exit function for current state
Modify state variables
Execute entry function for new state. This defaults to ES_ENTRY
End if
Return ReturnEvent to upper level state machine

Communication State Machine


Purpose:
Sending correct command to the byte transfer module and making response based on the
event returned from the lower level modules, i.e. ByteTransfer Module and SendingCommand
Module.

Module Level Variables:


#define PS_STATUS_COMMAND 0x00C0;
static Communicating_t CurrentState = CommunicatingSTATE_Wait4Command;

Functions:
ES_Event RunCommunicatingSM( ES_Event CurrentEvent )
Check the status of current state
If CurrentState is CommunicatingSTATE_Waiting4Command
Check the event type of current event
If event type is ES_OCCUPY_PS
- Start Hall Effect Sensor interrupt response
- Make state transition to true
- Set NextState as
CommnunicatingSTATE_OccupyingPSStation
- Consume the event
- Self-post ES_START_OCCUPY_PS to Master SM
End if
If event type is ES_INQUIRY_PS_STATUS
- Make state transition to true
- Set NextState as
CommnunicatingSTATE_InquiryingPSStatus
- Consume the event
- Self-post ES_BYTETRANSFER_STATUS to Master
SM
End if
End if
If CurrentState is CommnunicatingSTATE_OccupyingPSStation
Execute During function for occupying polling station. ES_ENTRY &
ES_EXIT are processed here allow the lower level state machines to remap or consume the event.
Process any events and check the event type of current event
If event type is ES_OCCUPY_SUCCEED
- Stop Hall Effect Sensor interrupt response
- Make state transition to true
- Set NextState as
CommnunicatingSTATE_InquiryingPSStatus
- Consume the event
- Self-post ES_BYTETRANSFER_STATUS to Master
SM
End if
If event type is ES_BLOCKED ( we are under attack)
- Stop Hall Effect Sensor interrupt response
- Make state transition to true

- Set NextState as
CommnunicatingSTATE_Waiting4Command
End if
If event type is ES_ABORT_OCCUPYING
- Stop Hall Effect Sensor interrupt response
- Make state transition to true
- Set NextState as
CommnunicatingSTATE_Waiting4Command
End if
End if
If CurrentState is CommnunicatingSTATE_InquiringPSStation
Execute During function for inquiring overall polling station status.
ES_ENTRY & ES_EXIT are processed here allow the lower level state
machines to re-map or consume the event.
Process any events and check the event type of current event
If event type is ES_PS_STATUS_COMPLETED
- Make state transition to true
- Set NextState as
CommnunicatingSTATE_Waiting4Command
End if
If event type is ES_PS_STATUS_INCOMPLETED
- Make state transition to true
- Set NextState as
CommnunicatingSTATE_Waiting4Command
- Consume the event
End if
End if
If make state transition is true
Execute exit function for current state
Modify state variables
Execute entry function for new state. This defaults to ES_ENTRY
End if
Return ReturnEvent to upper level state machine

Calibrating State Machine


Purpose:
Determining current bots position on the field by rotating 360 degrees and identify the
angles between adjacent beacon.

Module Level Variables:


#define UNDETERMINED_BEACON_NUMBER 5
#define FIRST_BEACON 0

#define SECOND_BEACON 1
#define THIRD_BEACON 2
#define FOURTH_BEACON 3
#define FULL_ROTATION 360
#define OFFSET 12
#define IR_SAMPLING_TIME 2
uint8_t CurrentBeaconNumber = UNDETERMINED_BEACON_NUMBER;
uint8_t LastBeaconNumber = UNDETERMINED_BEACON_NUMBER;
float CurrentRotatedAnlge = 0.0;
float AngleRotated4EachBeacon[4] = {0.0, 0.0, 0.0, 0.0};
uint8_t NumberOfZerosinBeaconAlignment = 0;
float FirstAngleSummation = 0.0;
uint8_t FirstAngleIndex = 0;
float SecondAngleSummation = 0.0;
uint8_t SecondAngleIndex = 0;
float ThirdAngleSummation = 0.0;
uint8_t ThirdAngleIndex = 0;
float FourthAngleSummation = 0.0;
uint8_t FourthAngleIndex = 0;
uint8_t IRSignalAcquired = 5;
float Angle_Roated = 0;
float Last_Angle_Roated = 0;
bool FirstAngleFlag = true;
bool SecondAngleFlag = true;
bool ThirdAngleFlag = true;
bool FourthAngleFlag = true;
float LastFirstAngleRotated = 0.0;
float LastSecondAngleRotated = 0.0;
float LastThirdAngleRotated = 0.0;
float LastFourthAngleRotated = 0.0;

Functions:
ES_Event RunCalibratingSM( ES_Event CurrentEvent )
Check the status of current state
If CurrentState is CalibratingSTATE_Waiting4Command
Check the event type of current event
If event type is ES_CALIBRATING
- Enable interrupts for IR beacons
- Make state transition to true
- Set NextState as
CommnunicatingSTATE_Wait4Sampling
- Consume the event
- Start a timer for sampling IR signals
End if
End if
If CurrentState is CalibratingSTATE_Waiting4Sampling
Check the event type of current event
If event type is ES_TIMEOUT
- Make state transition to true

- Set NextState as
CommnunicatingSTATE_Wait4Beacon
- Consume the event
- Start a timer for sampling IR signals
- Start rotating for 360 degree
End if
End if
If CurrentState is CalibratingSTATE_Wait4Beacon
Check the event type of current event
If event type is ES_TIMEOUT
- Check if there are IR signals presenting
- If there are IR signals presenting, determine which IR
beacon it is based on the frequency reading and
determine how many degree of angle the bot has rotated
since the beginning of the 360 degree rotation
- Start a timer for sampling IR signals
End if
If event type is ES_MOTION_END
If there are at least three IR beacon signals detected
- Determine the angle between each adjacent
beacons
- Make state transition to true
- Set NextState as
CommnunicatingSTATE_Wait4Command
- Consume the event
- Self-post ES_CALIBRATION_COMPLETED
to Master SM
End if
Else
- Make state transition to true
- Set NextState as
CommnunicatingSTATE_Wait4Command
- Consume the event
- Self-post ES_CALIBRATING to Master SM
End if
End if
End if
If make state transition is true
Execute exit function for current state
Modify state variables
Execute entry function for new state. This defaults to ES_ENTRY
End if
Return ReturnEvent to upper level state machine

Navigating State Machine


Purpose:
Control the motion of the bot based on the event posted

Module Level Variables:


#define PS_STATUS_COMMAND 0x00C0;
MoveCommand_t Instance;
// bool ShootingFlag = true;
#define FULL_ROTATION 360
#define REVERSE_INCHES 6.0f
uint8_t ES_Stall_Counts = 0;
#define ES_STALL_TRIGGER 30
bool Flag4Shooting = false;
static Navigating_t CurrentState = NavigatingSTATE_Wait4Command;
static uint8_t City2Go = 9;
static uint8_t Hall_Effect_Read;
static uint8_t Last_Hall_Effect_Read;
static uint8_t LastCity = 9;

Functions:
ES_Event RunNavigatingSM( ES_Event CurrentEvent )
Check the status of current state
If CurrentState is NavigatingSTATE_Waiting4Command
Check the event type of current event
If event type is ES_AUTONOMOUS_NAVIGATION
- Stop interrupts for IR beacons
- Set 0 to ES_Stall_Counts
- Make state transition to true
- Set NextState as
NavigatingSTATE_AutonomousNavigating_Rotation
- Consume the event
- Determine the next polling station to go
- Calculate the angle need to rotate and distance need to
go for reaching the next polling state
- Start rotating the angle required for heading to next
polling state
End if
If event type is ES_CALIBRATING
- Start interrupts for IR beacons
- Make state transition to true
- Set NextState as NavigatingSTATE_Calibrating
- Consume the event
- Self-post ES_CALIBRATING to Master SM
End if
If event type is ES_ROTATE_360
- Set 0 to ES_Stall_Counts
- Start interrupts for hall effect measurement
- Make state transition to true
- Set NextState as NavigatingSTATE_Rotate_360
- Consume the event

- Start rotating 360 degrees


- Start a timer for measuring magnetic field frequencies
End if
If event type is ES_SHOOTING
- Make state transition to true
- Set NextState as
NavigatingSTATE_AutonomousNavigating_Rotation
- Consume the event
- Calculate the angle required to rotate for shooting the
opponents AD Bucket
- Rotate the bot
- Set true to Flag4Shooting
End if
End if
If CurrentState is NavigatingSTATE_Calibrating
Execute During function for calibrating and determining current position
and orientation. ES_ENTRY & ES_EXIT are processed here allow the
lower level state machines to re-map or consume the event.
Process any events and check the event type of current event
If event type is ES_CALIBRATION_COMPLETED
- Make state transition to true
- Set NextState as NavigatingSTATE_Wait4Command
End if
End if
If CurrentState is NavigatingSTATE_AutonomousNavigating_Rotation
Process any events and check the event type of current event
If event type is ES_MOTION_END
- Set 0 to ES_Stall_Counts
- Set NextState as
NavigatingSTATE_AutonomousNavigating_Forward
- If the Flag4Shooting is true
If the distance for shooting is greater than 75.88
inches
Set NextState as
NavigatingSTATE_AutonomousNavigat
ing_Forward
Move forward the distance 75.88
inches
End if
Else
Move in reverse 75.88 the distance
Set NextState as
NavigatingSTATE_AutonomousNavigat
ing_Reverse
End if
End if
Else
Move forward the distance between current
position and next polling station

End if
- Make state transition to true
- Consume the event
End if
If event type is ES_STALL
- Increment ES_Stall_Counts by 1
- If ES_Stall_Counts greater than 10 times
Set NextState as
NavigatingSTATE_AutonomousNavigating_Rev
erse
Move in reverse direction for 5.5 inches
Consume the event
Set 0 to ES_Stall_Counts
Set false to Flag4Shooting
End if
End if
End if
If CurrentState is NavigatingSTATE_AutonomousNavigating_Forward
Process any events and check the event type of current event
If event type is ES_MOTION_END
- Set 0 to ES_Stall_Counts
- Set NextState as NavigatingSTATE_Wait4Command
- Make state transition to true
- Consume current event
- If Flag4Shooting is true
Set Event2Post to
ES_NAVIGATIONG_2_SHOOTING
Set false to Flag4Shooting
End if
- Else
Set Event2Post to
ES_NAVIGATIONG_2_COMMUNICATION
End if
- Post Event2Post to the Master SM
End if
If event type is ES_STALL
- Increment ES_Stall_Counts by 1
- If ES_Stall_Counts greater than 10 times
Set NextState as
NavigatingSTATE_AutonomousNavigating_Rev
erse
Move in reverse direction for 5.5 inches
Consume the event
Set 0 to ES_Stall_Counts
Set false to Flag4Shooting
End if
End if
End if

If CurrentState is NavigatingSTATE_Rotate_360
Process any events and check the event type of current event
If event type is ES_MOTION_END
- Set 0 to ES_Stall_Counts
- Start interrupts for IR beacons
- Set NextState to NavigatingSTATE_Calibrating
- Make state transition to true
- Consume current event
- Self-post ES_CALIBRATING to Master State Machine
End if
If event type is ES_TIMEOUT
- Measure the magnetic field frequency
- If the frequency is valid
- Set NextState to NavigatingSTATE_Wait4Command
- Make state transition to true
- Consume current event
- Self-post ES_NAVIGATING_2_COMMUNICATION
End if
- Else
- Start a timer for measuring magnetic field frequencies
End if
If event type is ES_STALL
- Increment ES_Stall_Counts by 1
- If ES_Stall_Counts greater than 10 times
Set NextState as
NavigatingSTATE_AutonomousNavigating_Reverse
Move in reverse direction for 5.5 inches
Consume the event
Set 0 to ES_Stall_Counts
Set false to Flag4Shooting
End if
End if
End if
If CurrentState is NavigatingSTATE_AutonomousNavigating_Reverse
Process any events and check the event type of current event
If event type is ES_MOTION_END
- Set 0 to ES_Stall_Counts
- Set NextState as NavigatingSTATE_Wait4Command
- Make state transition to true
- Consume current event
- If Flag4Shooting is true
Set Event2Post to
ES_NAVIGATIONG_2_SHOOTING
Set false to Flag4Shooting
End if
- Else
Set Event2Post to
ES_NAVIGATIONG_2_COMMUNICATION
End if
- Post Event2Post to the Master SM

End if
If event type is ES_STALL
- Increment ES_Stall_Counts by 1
- If ES_Stall_Counts greater than 10 times
Set NextState as
NavigatingSTATE_AutonomousNavigating_Forward
Move in Forward direction for 5.5 inches
Consume the event
Set 0 to ES_Stall_Counts
Set false to Flag4Shooting
End if
End if
End if
If make state transition is true
Execute exit function for current state
Modify state variables
Execute entry function for new state. This defaults to ES_ENTRY
End if
Return ReturnEvent to upper level state machine

Shooting State Machine


Purpose:
Performing the shooting task for our bot

Module Level Variables:


#define ONE_SEC 1000
#define THREE_SEC 5000
MoveCommand_t ShootInstance;
float RPM = 0.0; // 5900.0;
uint8_t BallIndex = 0;
float ShootingRPM[5] = {4500, 3800, 5000, 5000, 6500};
static Shooting_t CurrentState = ShootingSTATE_Wait4Shooting;

Functions:
ES_Event RunShootingSM( ES_Event CurrentEvent )
Check the status of current state
If CurrentState is ShootingSTATE_Wait4Shooting
Check the event type of current event
If event type is ES_SHOOTING
- Change NextState to
ShootingSTATE_Wait4IRAlignment
- Make state transition to true
- Determine the shooting orientation
- Determine the correct flywheel RPM to shoot the ball
- Rotate towards opponents AD Bucket

- Consume current event


End if
End if
If CurrentState is ShootingSTATE_Wait4IRAlignment
Check the event type of current event
If event type is ES_MOTION_END
- Change NextState to
ShootingSTATE_RunningFlyWheel
- Make state transition to true
- Start running flywheel at designated RPM
- Consume current event
End if
End if
If CurrentState is ShootingSTATE_RunningFlyWheel
Check the event type of current event
If event type is ES_FLYWHEELREADY
- Change NextState to
ShootingSTATE_Waiting4FlyWheel
- Make state transition to true
- Start a timer for 2 seconds to stabilize the RPM of
flywheel
- Consume current event
End if
End if
If CurrentState is ShootingSTATE_Wait4FlyWheel
Check the event type of current event
If event type is ES_TIMEOUT
- Change NextState to ShootingSTATE_Rotating
- Make state transition to true
- Start the servo to launch the ball
- Start a timer for 2 seconds to make sure the ball has
been shoot
- Consume current event
End if
End if
If CurrentState is ShootingSTATE_Rotating
Check the event type of current event
If event type is ES_TIMEOUT
- Change NextState to ShootingSTATE_Shooting
- Make state transition to true
- Set the servo back to its original position
- Stop the flywheel
- Consume current event
End if
End if
If CurrentState is ShootingSTATE_Shooting

Check the event type of current event


If event type is ES_TIMEOUT
- Change NextState to ShootingSTATE_Wait4Shooting
- Make state transition to true
- Self-post ES_SHOOTING_DONE event to Master SM
- Consume current event
End if
End if
If make state transition is true
Execute exit function for current state
Modify state variables
Execute entry function for new state. This defaults to ES_ENTRY
End if
Return ReturnEvent to upper level state machine

TopVOTE (Master) State Machine


Purpose:
The top state machine that controls the state transition between shooting, communication
and navigating.

Module Level Variables:


#define ONE_SEC 100
#define BitsPerNibble 4
#define ALL_BITS (0xff<<2)
#define SSIMODULE_ONE 0x02
uint8_t CurrentTeamColor = BLUE;
// uint8_t MagneticFieldFrequency = 0x03;
float NextCityTargetDistance = 0.0;
uint8_t NextCityTargetIndex = 9;
// Location of Cities
Coordinate_t CitiesLocation[9] = {{6.0,55.8},{9.0,88.5},{29.1,75.8},{33.3,53.8},
{45.3,29.4},{61.8,60.8},{80.8,9.2},{81.5,55.2},{87.6,72.6}};
// Location of Buckets
Coordinate_t BucketsLocation[2] = {{101.0, -5.0},{-5.0, 101.0}};
bool ShootingFlag = true;
#define GAME_ON 1
#define GAME_OFF 0
uint8_t GameStatus = GAME_OFF;
uint8_t NumberOfBalls = 5;
static TopVOTE_t CurrentState = TopVOTESTATE_Wait4StartGame;
static bool Should_Rotate_To_Occupy = 0;

Functions:

bool InitMasterSM ( uint8_t Priority )


Initialize the SSI commnucation with PAC
Initialize hall effect sensor reading
Initialize the PWM module
Initialize the encoder module
Initialize the motor control
Initialize the IR sensor module
Initialize the PID control for flywheel
Initialize the LEDs module
Determine team color based on the switch pin reading PB0
bool PostMasterSM( ES_Event ThisEvent )
Posts an event to this state machines queue
ES_Event RunMasterSM( ES_Event CurrentEvent )
Check the status of current state
If CurrentState is TopVOTESTATE_Wait4GameStart
Check the event type of current event
If event type is ES_TIMEOUT
If current campaign is off
- Set NextState to TopVOTE_Communicating
- Make state transition to true
- Consume current event
- Self-post ES_INQUIRY_PS_STATUS
End if
If current campaign is on
- Set NextState to TopVOTE_Navigating
- Make state transition to true
- Consume current event
- Self-post ES_CALIBRATING
- Change game status to game on
End if
End if
If CurrentState is TopVOTESTATE_Communicating
Execute During function for updating overall polling station status or
occupying polling station. ES_ENTRY & ES_EXIT are processed here
allow the lower level state machines to re-map or consume the event.
Process any events and check the event type of current event
If event type is ES_BLOCKED
- Set NextState to TopVOTE_Shooting
- Make state transition to true
- Consume current event
- Self-post ES_SHOOTING
End if
If event type is ES_PS_STATUS_COMPLETED
If GameStatus equals GAME_OFF
If current campaign is off
- Set NextState to
TopVOTE_Wait4GameStart
- Make state transition to true

- Start a 100 ms timer to update the


overall polling station status
End if
If current campaign is ON and if opponents AD
bucket is not under attacking and the number of
balls is greater than 0
- Set NextState to
TopVOTE_Navigating
- Make state transition to true
- Self-post ES_SHOOTING to Master
SM
- Reduce number of balls by one
End if
Else
- Set NextState to
TopVOTE_Navigating
- Make state transition to true
- Self-post ES_CALIBRATING to
Master SM
- Consume current event
- Set GameStatus to GAME_ON
End if
End if
If GameStatus equals GAME_ON
If current campaign is off
- Set NextState to
TopVOTE_Wait4GameStart
- Make state transition to true
- Start a 100 ms timer to update the
overall polling station status
- Set NumberofBalls to 5
- Set GameStatus to GAME_OFF
End if
If current campaign is ON and if opponents AD
bucket is not under attacking and the number of
balls is greater than 0
- Set NextState to
TopVOTE_Navigating
- Make state transition to true
- Self-post
ES_AUTONOMOUS_NAVIGATION
to Master SM
- Consume current event
- Set GameStatus to GAME_ON
End if
End if
End if
If event type is ES_ABORT_OCCUPYING
- Set NextState to TopVOTE_Navigating
- Make state transition to true

- Consume current event


If Should_Rotate_To_Occupy is true
- Set false to Should_Rotate_To_Occupy
- Change Event2Posts event type to
ES_ROTATING_360
End if
Else
- Change Event2Posts event type to
ES_CALIBRATING
End if
Post Event2Post to Master SM
End if
If CurrentState is TopVOTESTATE_Navigating
Execute During function for controlling the motion of our bot.
ES_ENTRY & ES_EXIT are processed here allow the lower level state
machines to re-map or consume the event.
Process any events and check the event type of current event
If event type is ES_CALIBRATION_COMPLETED
- Set NextState to TopVOTE_Communicating
- Make state transition to true
- Self-post ES_INQUIRY_PS_STATUS to Master SM
- Consume current event
End if
If event type is ES_NAVIGATING_2_COMMUNICATION
- Set NextState to TopVOTE_Communicating
- Make state transition to true
- Self-post ES_OCCUPY_PS to Master SM
- Consume current event
- Set true to Should_Rotate_To_Occupy
End if
If event type is ES_NAVIGATING_2_SHOOTING
- Set NextState to TopVOTE_Shooting
- Make state transition to true
- Self-post ES_SHOOTING to Master SM
- Consume current event
End if
End if
If CurrentState is TopVOTESTATE_Shooting
Execute During function for shooting. ES_ENTRY & ES_EXIT are
processed here allow the lower level state machines to re-map or
consume the event.
Process any events and check the event type of current event
If event type is ES_SHOOTING_DONE
- Set NextState to TopVOTE_Navigating
- Make state transition to true
- Self-post ES_AUTONOMOUS_NAVIGATION to
Master SM
- Consume current event
End if

End if
If make state transition is true
Execute exit function for current state
Modify state variables
Execute entry function for new state. This defaults to ES_ENTRY
End if
Return ReturnEvent to upper level state machine

You might also like