Functions in this module: InitializeMotorService, PostMotorService, RunMotorService,
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
Setup PWM basic configuration
Set frequency on timer (50 Hz on _Timer_3)
Assign Timer to Channel (_Timer3 to channel 1)
Set PulseWidth on Channel to starting point (midpoint)
Map channel to output pin (Channel 1 to PWM_RPB3)
RunMotorService
Define ReturnEvent variable with EventType ES_NO_EVENT
Define NextState variable
Set NextState to CurrentState
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
Define bool ReturnVal as false
Define uint16_t variable CurrentPos
Read current state of potentiometer and set to CurrentPos
If CurrentPos is less than 300 (out of 1023) i.e. left turn
Create ES_WHEEL_TURN event
Set EventParam to Left
If the previous event (PrevEvent) is not equal to EventParam
Post wheel turn event to this service
Set PrevEvent to EventParam
Set ReturnVal to true
If CurrentPos is greater than 723 (out of 1023) i.e. right turn
Create ES_WHEEL_TURN event
Set EventParam to Right
If the previous event (PrevEvent) is not equal to EventParam
Post wheel turn event to this service
Set PrevEvent to EventParam
Set ReturnVal to true
If CurrentPos is greater than 400 and less than 623 (out of 1023) i.e. centered wheel
Create ES_WHEEL_TURN event
Set EventParam to Middle
If the previous event (PrevEvent) is not equal to EventParam
Post wheel turn event to this service
Set PrevEvent to EventParam
Set ReturnVal to true
Return ReturnVal
TakeMoveStep (private function)
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