This state machine manages the state of the fan motor, responding to events such as those from the fidget spinner SM to turn on or shut off the motor.
State Diagram
Pseudocode
Pseudo-code for the fan motor module (a service that implements a state machine)
Data private to the module: CurrentState
InitMotor
Takes a priority number, returns True if no errors
Initialize the MyPriority variable with the passed in parameter
Call MotorInitialize
Call FanInit
Set CurrentState to MotorOff
Post the initial transition event
End of InitMotor
PostMotor
Posts an event to this state machine's queue (false if the enqueue operation failed, true otherwise)
Return post given event with parameters MyPriority and ThisEvent
End of PostMotor
RunMotor
Takes an event ThisEvent with EventType that is one of ES_INIT, ES_TIMEOUT, SPINNER_START, or GAME_COMPLETED
Returns ES_NO_EVENT if no error ES_ERROR otherwise
Based on the state of the CurrentState variable choose one of the following blocks of code:
CurrentState is MotorOff
If ThisEvent is SPINNER_START
Check the state of the Game state machine
If GameState is not PingPong_Completed
Set the fan PWM according to the potentiometer input
Initialize the motor timer
Change CurrentState to MotorOn
End
End
End MotorOff block
CurrentState is MotorOn
If ThisEvent is SPINNER_STOP
Stop the fan PWM
Change CurrentState to MotorOff
Else If ThisEvent is ES_TIMEOUT
Set the fan PWM according to the potentiometer input
Initialize the motor timer
Else If ThisEvent is GAME_COMPLETED or PP_COMPLETED
Stop the fan PWM
Change CurrentState to MotorOff
End
End MotorOn block
End of RunMotor
QueryMotor
Return the CurrentState
End of QueryMotor
MotorInitialize
Initialize PB4 as the PWM output
End of MotorInitialize
Data private to the module: CurrentState
InitMotor
Takes a priority number, returns True if no errors
Initialize the MyPriority variable with the passed in parameter
Call MotorInitialize
Call FanInit
Set CurrentState to MotorOff
Post the initial transition event
End of InitMotor
PostMotor
Posts an event to this state machine's queue (false if the enqueue operation failed, true otherwise)
Return post given event with parameters MyPriority and ThisEvent
End of PostMotor
RunMotor
Takes an event ThisEvent with EventType that is one of ES_INIT, ES_TIMEOUT, SPINNER_START, or GAME_COMPLETED
Returns ES_NO_EVENT if no error ES_ERROR otherwise
Based on the state of the CurrentState variable choose one of the following blocks of code:
CurrentState is MotorOff
If ThisEvent is SPINNER_START
Check the state of the Game state machine
If GameState is not PingPong_Completed
Set the fan PWM according to the potentiometer input
Initialize the motor timer
Change CurrentState to MotorOn
End
End
End MotorOff block
CurrentState is MotorOn
If ThisEvent is SPINNER_STOP
Stop the fan PWM
Change CurrentState to MotorOff
Else If ThisEvent is ES_TIMEOUT
Set the fan PWM according to the potentiometer input
Initialize the motor timer
Else If ThisEvent is GAME_COMPLETED or PP_COMPLETED
Stop the fan PWM
Change CurrentState to MotorOff
End
End MotorOn block
End of RunMotor
QueryMotor
Return the CurrentState
End of QueryMotor
MotorInitialize
Initialize PB4 as the PWM output
End of MotorInitialize
Header
ES_Configure.h
Source Code
Download
motor.h | |
File Size: | 0 kb |
File Type: | h |
motor.c | |
File Size: | 5 kb |
File Type: | c |