Motor Service

This service is responsible for controlling the rotation of the motor based on the state machine. If the IR sensor or capacitive touch switch is triggered, start or stop the motor depending on the previous state.

Constants and Module Variables

Module Defines

static uint8_t MyPriority, static uint32_t ConversionResults[1], static bool Toggle, static uint16_t LastPulseWidth, static MotorState_t CurrentMotorState

Constants

THREE_SEC 3000

AD_CHANNEL BIT5HI

TWENTY_SEC 20000

Module Functions

CheckSliderStatus

No input

Declare int p to (100*ConversionResults[0])/1023, ReturnVal to be false

Call ADC_MultiRead(ConversionResults)

Declare uint16_t CurrentPulseWidth to be p*50000/100


If CurrentPulseWidth is different from LastPulseWidth

Set ReturnVal to true

Create ES_Event_t newEvent, EventType as ES_NEW_POT_VAL, EventParam as CurrentPulseWidth

Post newEvent to PostMotorDriverService

Init Timer for TWENTY_SEC

Update LastPulseWidth to be CurrentPulseWidth

Return ReturnVal


PostMotorDriverService

Input: ES_Event_t

Post to service(priority, event)

Service Initialization

InitMotorDriverService

Inputs: priority


Set motor pin, IN1, IN2 to be low

Set toggle to false

Set potentiometer pins to input, and analog 

Set boolean x variable to ADC_ConfigAutoScan(AD_CHANNEL)

Call ADC_MultiRead(ConversionResults)

Set p to (100*ConversionResults[0])/1023

Configure PWM_Setup, AssignChannelToTimer, Set Period and Map Channel

Declare LastPulseWidth to be p*50000/100

Set CurrentMotorState to 0


Post initial ES_INIT

Service Run

RunMotorDriverService

Input: Event ThisEvent


Declare ReturnEvent to be ES_NO_EVENT

Declare PostEvent to be ES_NO_EVENT


Switch statement for CurrentMotorState

Case INIT:

switch statement for currentEvent:

Case ES_MOTOR_INIT:

Declare int p to (100*ConversionResults[0])/1023

Call ADC_MultiRead(ConversionResults)

Declare uint16_t init_pVal to be p*50000/100

if init_pVal > 25000:

set motor IN1 pin to 1

set motor IN2 pin to 0

declare uint16_t bwdPulseWidth as ((init_pVal - 25000)*50000)/25000;

Call PWMOperate_SetPulseWidthOnChannel(bwdPulseWidth, 3);

else:

set motor IN1 pin to 0

set motor IN2 pin to 1

declare uint16_t fwdPulseWidth as ((25000 - init_pVal)*50000)/25000;

Call PWMOperate_SetPulseWidthOnChannel(fwdPulseWidth, 3);

Break;

Case ES_NEW_POT_VAL:

Declare uint16_t pVal as currentEvent’s eventParam

if pVal > 25000:

set motor IN1 pin to 1

set motor IN2 pin to 0

declare uint16_t bwdPulseWidth as ((init_pVal - 25000)*50000)/25000;

Call PWMOperate_SetPulseWidthOnChannel(bwdPulseWidth, 3);

else:

set motor IN1 pin to 0

set motor IN2 pin to 1

declare uint16_t fwdPulseWidth as ((25000 - init_pVal)*50000)/25000;

Call PWMOperate_SetPulseWidthOnChannel(fwdPulseWidth, 3);

Break;

Case ES_MOTOR_STOP:

set motor IN1 pin to 0

set motor IN2 pin to 0

PWMOperate_SetPulseWidthOnChannel(0, 3);

CurrentMotorState to 0

Break;

Break;

Case STOPPED:

switch statement for currentEvent:

Case ES_MOTOR_INIT:

Declare int p to (100*ConversionResults[0])/1023

Call ADC_MultiRead(ConversionResults)

Declare uint16_t init_pVal to be p*50000/100

if init_pVal > 25000:

set motor IN1 pin to 1

set motor IN2 pin to 0

declare uint16_t bwdPulseWidth as ((init_pVal - 25000)*50000)/25000;

Call PWMOperate_SetPulseWidthOnChannel(bwdPulseWidth, 3);

else:

set motor IN1 pin to 0

set motor IN2 pin to 1

declare uint16_t fwdPulseWidth as ((25000 - init_pVal)*50000)/25000;

Call PWMOperate_SetPulseWidthOnChannel(fwdPulseWidth, 3);

set CurrentMotorState to INIT

Break;

Case ES_MOTOR_STOP:

set motor IN1 pin to 0

set motor IN2 pin to 0

PWMOperate_SetPulseWidthOnChannel(0, 3);

CurrentMotorState to STOPPED

Break;

Break;

Return ReturnEvent