Sie sind auf Seite 1von 5

Car.

c
\\ Implements SM that controls the car motion via the key notes

Module level variables: MyPriority, CurrentState, LastInput_CarPlace,


LastInput_LimitHome, LastInput_LimitEnd, ResetEarly,

Module defines: PWM_DUTY_Cycle, PWM_DUTY_CYCLE_REVERSE, PWM_FREQ,


CAR_FORWARD_TIME

Module Functions: Car_Init, Car_Forward, Car_Reverse, Car_Stop

******************************************************************************
****************************
InitCar
Takes a priority number, returns True

Sets the priority of the service


Initialize the hardware (Car motor PWM channels, button pins, limit switches)
Sample the port lines and use it to initialize the LastInput variables
Set the CurrentState to InitializingCar
Post an ES_Init event to this state machine
Output and error if unsuccessful
END of InitCar

PostCar
Takes Event, returns True

Posts an event to Car service


END of PostCar

RunCar
Event Types: ES_Init, CAR_PLACED, MoveForward, LimitEnd, ES_TIMEOUT, VICTORY,
WELCOME_AUDIO, LIMIT_END, LIMIT_HOME, EARLY_RESET, CORRECT_NOTE, TIMEOUT_POT,
HEADPHONES_RESET

Returns ES_NO_EVENT
Local Variables: NextState

Switch based on CurrentState


CurrentState is InitializingCar
If ThisEvent is ES_Init
Set NextState to WelcomeCar
EndIf
End InitializingCar Block

CurrentState is WelcomeCar
If ThisEvent is WELCOME_AUDIO
Set NextState to WaitingForPlacement
EndIf
If ThisEvent is LIMIT_END
Stop car
EndIf
If ThisEvent is LIMIT_HOME
Stop car
EndIf
End WelcomeCar Block

CurrentState is WaitingForPlacement
If ThisEvent is CAR_PLACED
Set NextState to MusicSequence
PostEvent Car_Placed to all services
EndIf
If ThisEvent is EARLY_RESET
Set NextState to WelcomeCar
EndIf
If ThisEvent is LIMIT_END
Stop car
EndIf
If ThisEvent is LIMIT_HOME
Stop car
EndIf
End WaitingForPlacement Block

CurrentState is MusicSequence
If ThisEvent is CORRECT_NOTE
Start car timer
Write motor drive forward
Set NextState to MusicSequence
EndIf

If ThisEvent is LimitEnd
Write motor to stop
Write motor to reverse
Set NextState to CarResetting
EndIf

If ThisEvent is ES_TIMEOUT & parameter is car timer


Write motor to stop
Set NextState to MusicSequence
EndIf

If ThisEvent is TIMEOUT_POT or VICTORY


Write motor drive forward
Set NextState to MovingToEnd
ElseIf ThisEvent is HEADPHONES_RESET or EARLY_RESET
Set ResetEarly to 1
Write motor drive forward
Set NextState to MovingToEnd
EndIf

CurrentState is MovingToEnd
If ThisEvent is LimitEnd
Write motor stop
Write motor drive reverse
Set NextState to CarResetting
EndIf
If ThisEvent is HEADPHONES_RESET or EARLY_RESET
Set ResetEarly to 1
EndIf
EndMovingtoEndBlock

CurrentState is CarResetting
If ThisEvent is LimitHome
Write motor stop
Set NextState to WaitingForHeadphones
EndIf
If ThisEvent is HEADPHONES_RESET or EARLY_RESET
Set ResetEarly to 1
EndIf
EndCarResettingBlock

CurrentState is WaitingForHeadphones
If ThisEvent is HEADPHONES_RESET or ResetEarly equals 1
Set ResetEarly to 0
Set NextState to WelcomeCar
EndIf
EndWaitingForHeadphonesBlock
Set CurrentState to NextState
Return ES_No_Event
END of RunCar

Functions

CheckCarPlace
Takes no parameters, returns true if an event was posted
Local Variable: CurrentInput_CarPlace

Sample the port line and use it to initialize the CurrentInput_CarPlace variable
If CurrentInput_CarPlace not equals to LastInput_Car Place and
Current_Input_CarPlace is lo
PostEvent CAR_PLACED to this service
Set ReturnVal to true
EndIf
Set LastInput_CarPlace to CurrentInput_CarPlace
Return ReturnVal
CheckLimitEnd
Takes no parameters, returns True if an event was posted
Local Variable: CurrentInput_LimitEnd

Sample the port line and use it to initialize the CurrentInput_LimitEnd variable
If CurrentInput_LimitEnd not equals to LastInput_LimitEnd and
Current_Input_LimitEnd is hi
PostEvent LIMIT_END to this service
Set ReturnVal to true
EndIf
Set LastInput_LimitEnd to CurrentInput_LimitEnd
Return ReturnVal

CheckLimitHome
Takes no parameters, returns True if an event was posted
Local Variable: CurrentInput_LimitHome

Sample the port line and use it to initialize the CurrentInput_LimitHome variable
If CurrentInput_LimitHome not equals to LastInput_LimitHome and
Current_Input_LimitHome is hi
PostEvent LIMIT_HOME to this service
Set ReturnVal to true
EndIf
Set LastInput_LimitHome to CurrentInput_LimitHome
Return ReturnVal

Car_Init
Takes no parameters, returns nothing
Initializes the car ports.

Car_Forward
Takes no parameters, returns nothing
Local Variables: dutyCycle_PWM0, channel_PWM0 , dutyCycle_PWM1, channel_PWM1,
reqFreq, group

Set the duty cyles for PWM0 and PWM1


Set the required frequency for the group

Car_Stop
Takes no parameters, returns nothing
Local Variables: dutyCycle_PWM0, channel_PWM0 , dutyCycle_PWM1, channel_PWM1,
reqFreq, group

Set the duty cyles for PWM0 and PWM1


Set the required frequency for the group

Car_Reverse
Takes no parameters, returns nothing
Local Variables: dutyCycle_PWM0, channel_PWM0 , dutyCycle_PWM1, channel_PWM1,
reqFreq, group

Set the duty cyles for PWM0 and PWM1


Set the required frequency for the group

Das könnte Ihnen auch gefallen