Professional Documents
Culture Documents
Command Module HSM Pseudocode
Command Module HSM Pseudocode
MasterMachine
InitMasterSM:
Configure pins A2, A3, and A4 as digital inputs and enable pull downs
Configure pin B4 as digital input and enable pull down
Configure SPI1 using SPI HAL
Set SPI module as master
Set pin A0 as SS output
Set pin A1 as SD output
Set idle state for clock high (CKP = 1)
Set CKE equal to 0
Setup 8 bit communication
Set BRG register to 999
Disable enhanced buffer mode
Clear SPI buffer
Enable SPI
Start Master SM
RunMasterSM:
Set MakeTransition to false
Set NextState to CurrentState
StartMasterSM:
Set CurrentState to NotPlaying
Call RunMasterSM with CurrentEvent
CheckStartButton:
Set ReturnVal to false
Read current state of button pin
If CurrentButtonState is not equal to LastButtonState:
Set ReturnVal to true
If button state is 1 (pressed down):
Post START_BUTTON event to MasterSM
Set LastButtonState to CurrentButtonState
Return ReturnVal
CheckLeftBumper:
Set ReturnVal to false
Read current state of left bumper pin
If CurrentLeftBumper is not equal to LastLeftBumper:
If left bumper is pressed and right bumper has been pressed:
Reset bumper values
Post BACK_BUMPER event to Master SM
Set ReturnVal to true
Else
Set Left bumper value
Set LastLeftBumper to CurrentLeftBumper
Return ReturnVal
CheckRightBumper:
Set ReturnVal to false
Read current state of right bumper pin
If CurrentRightBumper is not equal to LastRightBumper:
If right bumper is pressed and left bumper has been pressed:
Reset bumper values
Post BACK_BUMPER event to Master SM
Set ReturnVal to true
Else
Set Right bumper value
Set LastRightBumper to CurrentRightBumper
Return ReturnVal
DuringPlaying function:
Set ReturnEvent to Event
If EventType is ES_ENTRY:
Call StartPlayingSM
If EventType is ES_EXIT:
Call RunPlayingSM
Else:
Do the during function for the Playing SM
Return ReturnEvent
PlayingSM
RunPlayingSM:
Set MakeTransition to false
Set NextState to CurrentState
StartPlayingSM:
Set CurrentState to InitialAlign
Call RunPlayingSM with CurrentEvent
During5Point function:
Set ReturnEvent to Event
If EventType is ES_ENTRY:
Call StartPlayingSM
If EventType is ES_EXIT:
Call RunPlayingSM
Else:
Do the during function for the Playing SM
Return ReturnEvent
5PointSM
Run5PointSM:
Set MakeTransition to false
Set NextState to CurrentState
Start5PointSM:
Set CurrentState to Moving
Set moving timer to move forward time
Send move forward command to motors
TurretService
InitTurretService:
Disable Timer 3 for PWM
Choose internal clock as source
Disable gate
Set prescaler to 1:8
Set period appropriate for 50 Hz
Disable Output Compare 1 for PWM
Set PWM pin as output
Map pin to OC1
Choose Timer 3 as source
Set OC1 in PWM mode with no fault detect
Set OC1RS and OC1R to MID_POINT to start turret in center
Turn on OC1
Start Timer 3
Set CurrentPosition to MID_POINT
RunTurretService:
Switch for Event Type
Case for ES_COMMAND
Switch for Event Parameter (command)
Case for Turn CW (0x08)
Start TURRET_TIMER for timeStep
Set isMoving to true
Set moving direction to CW
End turn CW case
IC2Service
RunIC2Service:
Switch for Event Type
Case for LOOK_FOR_BEACON
Set current state to AlignWithBeacon
End look for beacon case
ISR_PIC32:
InitializeIC2:
Turn Timer 2 off
Base Timer 2 on PBClk
Disable gate
Set in 16 bit mode
Set 1:16 prescaler
Set period to max time
Turn off IC2 for beacon sensor
Capture every rising edge
Use 16 bit timer
Interrupt on every capture
Use Timer 2
Map input pin to IC2
Set IC2R to 0
Set in multivector mode
Set priority
Clear interrupt flag
Set interrupt bit
Start timer 2
Enable global interrupts
Turn on IC2
IC2Handler:
Read IC2BUF into InterruptTime
Increment interrupt counter
If InterruptTime is greater than previous
Calculate delta
Else
Calculate delta accounting for rollover
End if
If delta is larger than max for uint16
Send overflow event
Else (delta is good)
Send ES_RISING_EDGE event with delta as parameter to IC2Service
End if
Clear source of interrupt