0% found this document useful (0 votes)
100 views3 pages

Motor Service Pseudocode

This document describes functions and data for a motor service module. It includes functions to initialize the motor service, run the service, check the wheel position, and take steps to move the motor. Private data includes variables to track the service state, position, conversion results, and movement direction. Key functions initialize PWM configuration, run the state machine responding to events, check the wheel position sensor, and increment/decrement the motor position during movement.

Uploaded by

api-581249153
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views3 pages

Motor Service Pseudocode

This document describes functions and data for a motor service module. It includes functions to initialize the motor service, run the service, check the wheel position, and take steps to move the motor. Private data includes variables to track the service state, position, conversion results, and movement direction. Key functions initialize PWM configuration, run the state machine responding to events, check the wheel position sensor, and increment/decrement the motor position during movement.

Uploaded by

api-581249153
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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

You might also like