Professional Documents
Culture Documents
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
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
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
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
#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
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
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
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
Functions:
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