Motor Service Pseudocode
Motor Service Pseudocode
CheckWheelTurn, TakeMoveStep
Data private to module: uint8_t MyPriority, uint8_t CurrentState, uint16_t CurrentPosition,
uint32_t ConversionResults[2], static uint16_t cwLimit, static uint16_t ccwLimit, const float
factor, static uint16_t LastPos, static uint16_t timeStep, static bool isMoving, static bool
moveDirection, static uint8_t PrevEvent
InitializeMotorService
RunMotorService
If CurrentState is Waiting2Begin
If EventType is ES_INIT
Read initial position of potentiometer
Set LastPos to this initial position
Set NextState to Steering
If Current State is Steering
If EventType is ES_WHEEL_TURN
If EventParam is Left (left turn)
Initialize WHEEL_TIMER with timeStep
Set isMoving to true
Set moveDirection to CCW
If EventParam is Right (right turn)
Initialize WHEEL_TIMER with timeStep
Set isMoving to true
Set moveDirection to CW
If EventParam is Middle (wheel is centered)
Set PulseWidth on Channel 1 to CurrentPosition
Set isMoving to false
If EventType is ES_TIMEOUT
If isMoving is true
Make servo take step by calling TakeMoveStep
If isMoving is true (servo is still in movement)
Restart WHEEL_TIMER with timeStep for next step
If EventType is ES_REQUEST_SERVO_POS
Define event SendServoPos
Set EventType to ES_RECEIVE_SERVO_POS
Set EventParam to CurrentPosition
Send the current position update event to DrivingService
Set CurrentState to NextState
Return ReturnEvent
CheckWheelTurn
If moveDirection is DIRECTION_CCW
Increment CurrentPosition by PWM ticks per step
If CurrentPosition is below the CCW limit
Set PulseWidth on Channel 1 to CurrentPosition
Else
Decrement CurrentPosition by PWM ticks per step
Set isMoving to false
If moveDirection is DIRECTION_CW
Decrement CurrentPosition by PWM ticks per step
If CurrentPosition is below the CW limit
Set PulseWidth on Channel 1 to CurrentPosition
Else
Increment CurrentPosition by PWM ticks per step
Set isMoving to false
Define send servo position event (ES_RECEIVE_SERVO_POS)
Set EventParam to CurrentPosition
Post send servo position event to DrivingService
Return