Pseudocode-Project 1
Pseudocode-Project 1
PinAssignments
Interrupts/Priorities
Files:
Master_SM.c
PACLogic_SM.c
Capture_PS_SM
Request_SM.c
SendingCMD_SM.c
SendingByte_SM.c
AttackStrategy_SM.c
Attack_SM.c
Fire_SM.c
HallEffect_SM.c
PositionLogic_Service.c
PhotoTransitor_Service.c
GameInfo.c
Strategy_SM.c
Definitions.h
ControlLaws(DriveTrainControl_Service.c,CannonControl_Service.c)
PinAssignments
Pin#
SpecialFunction
Connectedto
PortA
A2
SSI_0Clock
PACModule
A3
SSI_0Fss
PACModule
A4
SSI_0Rx
PACModule
A5
SSI_0Tx
PACModule
A6
M1PWM2
NotusingforPWM
A7
M1PWM3
NotusingforPWM
PortB
B0
Red/BlueSwitch
B1
GameStatusIndicator(output)
B2
PACErrorIndicator
B3
B4
SSI_2Clock,M0PWM2
PeriscopeMotorPWM
B5
SSI_2Fss,M0PWM3
CannonPWM
B6
SSI_2Rx,M0PWM0
HopperServoPWM
B7
SSI_2Tx,M0PWM1
PeriscopeLatchPWM
PortC
C4
M0PWM6,WT0CCP0
PhotoTransistor
C5
M0PWM7,WT0CCP1
CannonEncoder
C6
WT1CCP0
PeriscopeEncoder1
C7
WT1CCP1
PeriscopeEncoder2
PortD
D0
SSI_1Clock,M0PWM6,M1PWM0,
WT2CCP0
HallSensor1
D1
SSI_1FSS,M0PWM7,M1PWM1,
WT2CCP1
HallSensor2
D2
SSI_1RX,WT3CCP0
HallSensor3
D3
SSI_1TX,WT3CCP1
HallSensor4
D4
WT4CCP0
NOCONNECTION!!
DriveControlLaw
D5
WT4CCP1
NOCONNECTION!!
CannonControlLaw
D6
WT5CCP0
DriveEncoderLeft
D7
WT5CCP1
DriveEncoderRight
PortE
E0
HBridgeL(Direction)
E1
HBridgeR(Direction)
E2
DriveEncoderLeft(forPolling)
E3
DriveEncoderRight(forPolling)
E4
M1PWM2
HBriidgeL(PWM)
E5
M1PWM3
HBridgeR(PWM)
PortF
F0
SSI_1_RX
F1
SSI_1_TX
F2
SSI_1CLK
F3
SSI_1FSS
F4
Interrupts/Priorities
InterruptName
Function
Priority
DriveEncoderLeft
DriveEncoderRight
PeriscopeEncoder
OrientPhototransistors
CannonEncoder
ControlSpeedof
PhotoTransistorLeft
IdentifyBeacon
PhotoTransistorRight
IdentifyBeacon
SPIInterrupt
ReceiveCommands
HallSensors
PollingStation
Master_SM.c
boolInitMasterSM(uint8_tPriority)
DeclareEvent
Setprioritytoequalourpriority
SeteventtobeES_Entry
CalltheStartMasterSM
returnTrue
boolPostMasterSM(ESEventThisEvent)
returnES_PostToService
ES_EventRunMasterSM(ES_EventCurrentEvent)
CurrentEvent=CallourDuringMasterfunctionpassingtheCurrentEvent
SettheCurrentEventtoES_NO_Event
returntheCurrentEvent
voidStartMasterSM(Es_EventCurrentEvent)
SetavolatileESEventtobealocalEvent=CurrentEvent(fordebugging)
RunMasterSMwithourlocalevent
staticES_EventDuringMaster(EsEventEvent)
iftheEventisanEntryEvent
StartStrategySMpassingtheEntryevent
StartPACSMpassingtheEntryevent
StartAttackStrategySMpassingtheEntryevent
StartHallEffectSMpassingtheEntryevent
iftheeventisanES_Exit
RunStrategySMpassingtheExitevent
RunPACSMpassingtheExitevent
RunAttackStrategySMpassingtheExitevent
RunHallEffectSMpassingtheExitevent
else
RunStrategySMpassingtheevent
RunPACSMpassingtheevent
RunAttackStrategySMpassingtheevent
RunHallEffectSMpassingtheevent
PACLogic_SM.c
ES_EventRunPAC_SM(ES_EventCurrentEvent)
setMakeTransitiontoFalse
SettheNextState=CurrentState
SettheEntryEventKindto0,iethedefault
Setourreturneventtothecurrentevent
Switch
CurrentState=Waiting4Command
SetCurrentEvent=RunDuringWaiting4CommandFunction
CurrentEvent.EventType=ES_Timeout&CurrentEvent.EventParam=
CheckCampaignStatus
SetNextStatetoCampaignStatus
SetMaketransitiontotrue
CurrentEvent.EventType=ES_PS_Detected
SetNextStatetoCapture_PS
SetMaketransitiontotrue
CurrentState=CampaignStatus
SetCurrentEvent=RuntheDuringCampaignStatusFunction
CurrentEvent.EventType=ES_Transaction_Complete
SetNextStatetoWaiting4Command
SetMaketransitiontotrue
CurrentEvent.EventType=ES_PS_Detected
DefertheEvent
CurrenState=Capture_PS
SetCurrentEvent=RuntheDuringcapturePSFunction
SetCurrentEvent=ES_PS_Captured
SetNextStatetoWaiting4Command
SetMaketransitiontotrue
IfMakeTransition=True
CurrentEvent=ES_Exit
CalltheRunPAC_SM
SettheCurrentState=NextState
SettheCurrentEvent.EventType=ES_Entry
RunthePAC_SMagainwiththeES_Entryevent
voidStartPAC_SM(ES_EventCurrentEvent)
CreateavolatileES_EventLocalEventequaltotheCurrentEvent
SettheCurrentState=Waiting4CommandState,ourentrystate
RunthePAC_SMwiththeCurrentEvent
PAC_State_tQueryPacSM(void)
returntheCurrentState
staticES_EventDuringWaiting4Command(ES_EventEvent)
SetvolatileNewEventfordebugging
iftheEventisanES_EntryEvent
Startthetimerforthecampaignstatus
Calltherecalldeferredeventsfunction
return(event)
staticES_EventDuringCampaignStatus(ES_EventEvent)
SetvolatileNewEventfordebugging
iftheEventisanES_EntryEvent
CreateanES_EventforSendCMd
SettheParametertoCampaignStatus_SPI_Command_BYTE
PostanES_SendCMD
iftheEventisanES_ExitEvent
UpdatetheEnemyQueueusingourQueueDataStructure
else
RunSendingCMD_SM()
return(event)
staticES_EventDuringCapture(ES_EventEvent)
SetvolatileNewEventfordebugging
iftheEventisanES_EntryEvent
iftheEventisanES_ExitEvent
UpdatetheEnemyQueueusingourQueueDataStructure
else
RunSendingCMD_SM()
return(event)
Capture_PS_SM
ES_EventRunCapturePS_SM(ES_EventCurrentEvent)
setMakeTransitiontoFalse
SettheNextState=CurrentState
SettheEntryEventKindto0,iethedefault
Setourreturneventtothecurrentevent
Switch
CurrentState=Measuring1
SetCurrentEvent=RunDuringMeasuring1Function
CurrentEvent.EventType=EV_PS_Detected
SetNextStatetoRequest1
SetMaketransitiontotrue
CurrentState=Request1
SetCurrentEvent=RunDuringRequest1Function
CurrentEvent.EventType=ES_Transaction_Complete
IfResponseisReady,WeareAcknowledged,andtheLocationiscorrect
SetNextStatetoMeasuring2
SetMaketransitiontotrue
elseifResponseisReady
SetNextStatetoMeasuring1_t
SetMaketransitiontotrue
CurrentState=Measuring2
SetCurrentEvent=RunDuringRequest1Function
CurrentEvent.EventType=EV_PS_Detected
SetNextStatetoRequest2
SetMaketransitiontotrue
CurrentState=Request2
SetCurrentEvent=RunDuringRequest2Function
IfResponseisReady,WeareAcknowledged,andtheLocationiscorrec
PostanES_PS_CapturedEvent
Setmaketransitiontofalseaswearentactuallytransitioningout
elseifResponseisReady
SetNextStatetoMeasuring1
SetMaketransitiontotrue
IfMakeTransition=True
CurrentEvent=ES_Exit
CalltheRunCapturePS_SM
SettheCurrentState=NextState
SettheCurrentEvent.EventType=ES_Entry
RuntheCapturePS_SMagainwiththeES_Entryevent
voidStartCapturePS_SM(ES_EventCurrentEvent)
CreateavolatileES_EventLocalEventequaltotheCurrentEvent
SettheCurrentState=Request1State,ourentrystate
RuntheCapturePS_SMwiththeCurrentEvent
CapturePS_State_tQueryCapturePS_SM(void)
returntheCurrentState
staticES_EventDuringMeasuring#1(ES_EventEvent)
SetvolatileNewEventfordebugging
iftheEventisanES_EntryEvent
iftheEventisanES_ExitEvent
else
return(event)
staticES_EventDuringRequest#1(ES_EventEvent)
SetvolatileNewEventfordebugging
iftheEventisanES_EntryEvent
iftheEventisanES_ExitEvent
CreateanES_EventforES_PS_Measuring
PosttheEvent
else
RunRequest_SM()
return(event)
staticES_EventDuringMeasuring#2(ES_EventEvent)
SetvolatileNewEventfordebugging
iftheEventisanES_EntryEvent
iftheEventisanES_ExitEvent
else
return(event)
staticES_EventDuringRequest#2(ES_EventEvent)
SetvolatileNewEventfordebugging
iftheEventisanES_EntryEvent
iftheEventisanES_ExitEvent
CreateanES_EventforES_PS_Measuring
PosttheEvent
else
RunRequest_SM()
return(event)
Request_SM.c
ES_EventRunRequest_SM(ES_EventCurrentEvent)
setMakeTransitiontoFalse
SettheNextState=CurrentState
SettheEntryEventKindto0,iethedefault
Setourreturneventtothecurrentevent
Switch
CurrentState=Request
SetCurrentEvent=RunDuringRequestFunction
CurrentEvent.EventType=ES_Transaction_Complete
SetNextStatetoQuery
SetMaketransitiontotrue
CurrentState=Query
SetCurrentEvent=RunDuringQueryFunction
CurrentEvent.EventType=ES_Transaction_Complete
IfResponseisNotReadyhelperfunction
SetMaketransitiontotruetoleavethestateandrenterit
voidStartRequest_SM(ES_EventCurrentEvent)
CreateavolatileES_EventLocalEventequaltotheCurrentEvent
SettheCurrentState=RequestState,ourentrystate
RuntheRequest_SMwiththeCurrentEvent
Request_State_tQueryRequest_SM(void)
returntheCurrentState
staticES_EventDuringRequestState(ES_EventEvent)
SetvolatileNewEventfordebugging
iftheEventisanES_EntryEvent
CreateanES_EventforSendCMD
SettheParametertoRequest_SPI_Command_BYTE
PostanES_SendCMD
iftheEventisanES_ExitEvent
else
Run_SendingCMD_SM
return(event)
staticES_EventDuringQueryState(ES_EventEvent)
SetvolatileNewEventfordebugging
iftheEventisanES_EntryEvent
CreateanES_EventforSendCMD
SettheParametertoQuery_SPI_Command_BYTE
PostanES_SendCMD
iftheEventisanES_ExitEvent
else
Run_SendingCMD_SM
return(event)
SendingCMD_SM.c
ModuleLevelVariables
CreateResponseArray[5]uint8_twhichisupdatedineachevents
ES_EventRunSendingCMD_SM(ES_EventCurrentEvent)
setMakeTransitiontoFalse
SettheNextState=CurrentState
SettheEntryEventKindto0,iethedefault
Setourreturneventtothecurrentevent
Switch
CurrentState=Waiting
SetCurrentEvent=RunDuringWaitingFunction
CurrentEvent.EventType=ES_SendCMD
CreateanES_EventforSendByte
SettheParametertofromSendCMDEvent(ie.theEventwepassedin)
PostanES_SendByte
SetNextStatetoSendingByte1_t
SetMaketransitiontotrue
CurrentState=SendingByte1_t
SetCurrentEvent=RunDuringSendingByte1_tFunction
CurrentEvent.EventType=ESTimeout&Param=SSI
CreateanES_EventforSendByte
SettheParameterto0x00
PostanES_SendByte
SetNextStatetoSendingByte1_2
SetMaketransitiontotrue
CurrentState=SendingByte2_t
SetCurrentEvent=RunDuringSendingByte2_tFunction
CurrentEvent.EventType=ESTimeout&Param=SSI
CreateanES_EventforSendByte
SettheParameterto0x00
PostanES_SendByte
SetNextStatetoSendingByte1_3
SetMaketransitiontotrue
CurrentState=SendingByte3_t
SetCurrentEvent=RunDuringSendingByte3_tFunction
CurrentEvent.EventType=ESTimeout&Param=SSI
SetNextStatetoSendingByte1_4
CreateanES_EventforSendByte
SettheParameterto0x00
PostanES_SendByte
SetMaketransitiontotrue
CurrentState=SendingByte4_t
SetCurrentEvent=RunDuringSendingByte3_tFunction
CurrentEvent.EventType=ESTimeout&Param=SSI
SetNextStatetoSendingByte1_5
CreateanES_EventforSendByte
SettheParameterto0x00
PostanES_SendByte
SetMaketransitiontotrue
CurrentState=SendingByte5_t
SetCurrentEvent=RunDuringSendingByte5_tFunction
CurrentEvent.EventType=ESTimeout&Param=SSI
SetNextStatetoWaiting
Post(ES_Trasaction_Complete)event
SetMaketransitiontotrue
voidStartSendCMD_SM(ES_EventCurrentEvent)
CreateavolatileES_EventLocalEventequaltotheCurrentEvent
SettheCurrentState=WaitingState,ourentrystate
RuntheSendCMD_SMwiththeCurrentEvent
SendCMD_State_tQuerySendCMD_SM(void)
returntheCurrentState
staticES_EventDuringWaitingState_t(ES_EventEvent)
SetvolatileNewEventfordebugging
iftheEventisanES_EntryEvent
iftheEventisanES_ExitEvent
else
return(event)
staticES_EventDuringSendingByte1State_t(ES_EventEvent)
SetvolatileNewEventfordebugging
iftheEventisanES_EntryEvent
iftheEventisanES_ExitEvent
ReadtheDataRegisterandStoretheuint8_tinthe0throwofour
ResposneArray
else
RunSendByteSM()
return(event)
staticES_EventDuringSendingByte2State_t(ES_EventEvent)
SetvolatileNewEventfordebugging
iftheEventisanES_EntryEvent
iftheEventisanES_ExitEvent
ReadtheDataRegisterandStoretheuint8_tinthe1throwofour
ResposneArray
else
RunSendByteSM()
return(event)
staticES_EventDuringSendingByte3State_t(ES_EventEvent)
SetvolatileNewEventfordebugging
iftheEventisanES_EntryEvent
iftheEventisanES_ExitEvent
ReadtheDataRegisterandStoretheuint8_tinthe2throwofour
ResposneArray
else
RunSendByteSM()
return(event)
staticES_EventDuringSendingByte4State_t(ES_EventEvent)
SetvolatileNewEventfordebugging
iftheEventisanES_EntryEvent
iftheEventisanES_ExitEvent
ReadtheDataRegisterandStoretheuint8_tinthe3throwofour
ResposneArray
else
RunSendByteSM()
return(event)
staticES_EventDuringSendingByte5State_t(ES_EventEvent)
SetvolatileNewEventfordebugging
iftheEventisanES_EntryEvent
iftheEventisanES_ExitEvent
ReadtheDataRegisterandStoretheuint8_tinthe4throwofour
ResposneArray
else
RunSendByteSM()
return(event)
uint8_tArraygetResponse()
returnResponseArray
*Note:SendingByte_SMwasremovedtomakeourPAC
interfacecompliantwiththebehaviordetailedinthespec.
SendingByte_SM.c
ES_EventRunSendingByte_SM(ES_EventCurrentEvent)
setMakeTransitiontoFalse
SettheNextState=CurrentState
SettheEntryEventKindto0,iethedefault
Setourreturneventtothecurrentevent
Switch
CurrentState=WaitingtoSend
SetCurrentEvent=RunDuringWaitingtoSendFunction
CurrentEvent.EventType=ES_SendByte
WriteToOurDataRegsiterwiththeES_SendByteParameter
EnableXmitInterrupt?<doweeverdisablethis?
SetNextStatetoWaitingforEOT
SetmakeTransitiontotrue
CurrentState=WaitingforEOT
SetCurrentEvent=RunDuringWaitingtoSendFunction
CurrentEvent.EventType=ES_EOT
SetNextStatetoWaitingforTimeout
SetmakeTransitiontotrue
CurrentState=WaitingforTimeout
SetCurrentEvent=RunDuringWaitingtoSendFunction
CurrentEvent.EventType=ES_TimeoutandParameter=SSI
SetNextStatetoWaitingtoSend
SetmakeTransitiontotrue
voidStartSendByte_SM(ES_EventCurrentEvent)
CreateavolatileES_EventLocalEventequaltotheCurrentEvent
SettheCurrentState=WaitingtoSendState,ourentrystate
RuntheSendByte_SMwiththeCurrentEvent
SendByte_State_tQuery_SendByte_SM(void)
returntheCurrentState
staticES_EventDuringWaiting2SendState_t(ES_EventEvent)
SetvolatileNewEventfordebugging
iftheEventisanES_EntryEvent
iftheEventisanES_ExitEvent
else
return(event)
staticES_EventDuringWaiting4EOTState_t(ES_EventEvent)
SetvolatileNewEventfordebugging
iftheEventisanES_EntryEvent
iftheEventisanES_ExitEvent
else
return(event)
staticES_EventDuringWaiting4TimeoutState_t(ES_EventEvent)
SetvolatileNewEventfordebugging
iftheEventisanES_EntryEvent
Start2msTimer
iftheEventisanES_ExitEvent
else
return(event)
AttackStrategy_SM.c
ES_EventRunAttackStrategy_SM(ES_EventCurrentEvent)
setMakeTransitiontoFalse
SettheNextState=CurrentState
SettheEntryEventKindto0,iethedefault
Setourreturneventtothecurrentevent
Switch
CurrentState=Wait4AttackPhase_t
SetCurrentEvent=RunDuringWaiting4AttackPhaseFunction
CurrentEvent.EventType=ES_Timeout,StartAttackingTimer
SetNextStatetoAttack_t
SetmakeTransitiontotrue
CurrentState=Attack_t
SetCurrentEvent=RunDuringAttack_tFunction
CurrentEvent.EventType=ES_TimeoutwithParameterfor
AttackComplete/AttackDuration
SetNextStatetoWait4NextAttack_t
SetmakeTransitiontotrue
CurrentState=Wait4NextAttack_t
SetCurrentEvent=RunDuringWait4NextAttack_tFunction
CurrentEvent.EventType=ES_TimeoutandParameter=NextAttackTimer
SetNextStatetoAttack_t
SetmakeTransitiontotrue
voidStartAttackStrategy_SM(ES_EventCurrentEvent)
CreateavolatileES_EventLocalEventequaltotheCurrentEvent
SettheCurrentState=Wait4AttackPhase_t,ourentrystate
RuntheAttackStrategy_SMwiththeCurrentEvent
AttackStrategy_State_tQueryAttackStrategy_SM(void)
returntheCurrentState
staticES_EventDuringWait4AttackPhase_t(ES_EventEvent)
SetvolatileNewEventfordebugging
iftheEventisanES_EntryEvent
StartStartAttackingTimerTimer
iftheEventisanES_ExitEvent
else
return(event)
staticES_EventDuringAttack_t(ES_EventEvent)
SetvolatileNewEventfordebugging
iftheEventisanES_EntryEvent
GettheOldPowerTrainDestinationandsavetoastaticvariable
Setthenewdestinationtonull
iftheEventisanES_ExitEvent
Setthedestinationtooursavedpowertraindestination
else
RuntheAttack_SM
return(event)
staticES_EventDuringNextAttack_t(ES_EventEvent)
SetvolatileNewEventfordebugging
iftheEventisanES_EntryEvent
StarttheNextAttackTimer
iftheEventisanES_ExitEvent
else
return(event)
Attack_SM.c
ES_EventRunAttackStrategy_SM(ES_EventCurrentEvent)
setMakeTransitiontoFalse
SettheNextState=CurrentState
SettheEntryEventKindto0,iethedefault
Setourreturneventtothecurrentevent
Switch
CurrentState=Align&StartCannon_t
SetCurrentEvent=RunDuringAlign&StartCannont_tFunction
CurrentEvent.EventType=CannonFullSpeed
SetNextStatetoFullSpeed_t
SetmakeTransitiontotrue
CurrentEvent.EventType=ES_CannonAligned
SetNextStatetoAligned
SetmakeTransitiontotrue
CurrentState=Aligned_t
SetCurrentEvent=RunDuringAligned_tFunction
CurrentEvent.EventType=FullSpeed_t
SetNextStatetoFiring_t
SetmakeTransitiontotrue
CurrentState=FullSpeed_t
SetCurrentEvent=RunDuringCannonAngledFunction
CurrentEvent.EventType=ES_CannonAligned
SetNextStatetoFiring_t
SetmakeTransitiontotrue
CurrentState=Firing_t
SetCurrentEvent=RunDuringFiring_tFunction
CurrentEvent.EventType=ES_CannonAligned
SetNextStatetoFiring_t
SetmakeTransitiontotrue
voidStartAttack_SM(ES_EventCurrentEvent)
CreateavolatileES_EventLocalEventequaltotheCurrentEvent
SettheCurrentState=Wait4AttackPhase_t,ourentrystate
RuntheAttackStrategy_SMwiththeCurrentEvent
Attack_State_tQueryAttack_SM(void)
returntheCurrentState
staticES_EventDuringAlign&StartCannonState_t(ES_EventEvent)
SetvolatileNewEventfordebugging
iftheEventisanES_EntryEvent
StartTimerforCannonAngleReached
StartTimerforCheckingAlignmentofCannon
GetourdesiredCannonangle
SettheSetpointofthecannonanglemotortobeequaltoourdesiredbeacon
RotatethePowerTrain/Robottodesiredalignment
iftheEventisanES_ExitEvent
else
return(event)
staticES_EventDuringAlignedState_t(ES_EventEvent)
SetvolatileNewEventfordebugging
iftheEventisanES_EntryEvent
iftheEventisanES_ExitEvent
else
return(event)
staticES_EventDuringCannonAngled_t(ES_EventEvent)
SetvolatileNewEventfordebugging
iftheEventisanES_EntryEvent
iftheEventisanES_ExitEvent
else
return(event)
staticES_EventDuringFiring_t(ES_EventEvent)
SetvolatileNewEventfordebugging
iftheEventisanES_EntryEvent
iftheEventisanES_ExitEvent
StartNextShotTimer
else
RunFireSM()
return(event)
*Note:thefunctionalityofthisstatemachinewasincorporatedinto
Attack_SM.c
Fire_SM.c
ES_EventRunFire_SM(ES_EventCurrentEvent)
setMakeTransitiontoFalse
SettheNextState=CurrentState
SettheEntryEventKindto0,iethedefault
Setourreturneventtothecurrentevent
Switch
CurrentState=OpeningHopper
SetCurrentEvent=RunDuringOpeningHopperFunction
CurrentEvent.EventType=ES_Timeout,BallLoaded
SetNextStatetoDriveHammer
SetmakeTransitiontotrue
CurrentState=DriveHammer
SetCurrentEvent=RunDuringDriveHammerFunction
voidStartFiring_SM(ES_EventCurrentEvent)
CreateavolatileES_EventLocalEventequaltotheCurrentEvent
SettheCurrentState=OpeningHopper,ourentrystate
RuntheFire_SMwiththeCurrentEvent
Fire_State_tQueryFire_SM(void)
returntheCurrentState
staticES_EventDuringOpeningHopper_t(ES_EventEvent)
SetvolatileNewEventfordebugging
iftheEventisanES_EntryEvent
PostES_Open_Hoppersothatthehopperservicerotatestheservo
StarttheBallLoadedTimer
iftheEventisanES_ExitEvent
else
return(event)
staticES_EventDuring
DriveHammer
_t(ES_EventEvent)
SetvolatileNewEventfordebugging
iftheEventisanES_EntryEvent
PostES_Open_Hoppersothatthehopperservicerotatestheservo
StarttheBallLoadedTimer
iftheEventisanES_ExitEven
PostES_Close_Hoppersothatthehopperservicerotatestheservo
PostanES_StopCannontorevdownthecannon
else
return(event)
HallEffect_SM.c
PoundDefines
#definePERIOD_MEASURING_ERROR_TOLERANCE10
#defineNUMBER_PULSES_TO_STOP
#defineNUMBER_HALL_EFFECT_SENSORS
#defineNUMBER_FREQUENCIES16
PoundDefineIndexinArrayCorrespondingtoHallEffectPosition
#defineLEFT_OUTER_INDEX0
#defineLEFT_INNER_INDEX1
#defineRIGHT_INNER_INDEX2
#defineRIGHT_OUTER_INDEX3
ModuleLevelVariables
CreateModuleLevelVariableforPossiblePeriods(inmicroseconds)
staticuint8_tPS_Possible_Periods={1000,947,893,840,787,733,680,627,573,520,467,
413,360,307,253,200}
CreateModuleLevelvariableforEachHallSensorsLastCapturedPeriods(inus)
staticuint8_tHallSensor_LastPeriods[NUMBER_PULSES_TO_STOP]
[NUMBER_HALL_EFFECT_SENSORS]
ES_EventRunHallEffect_SM(ES_EventCurrentEvent)
setMakeTransitiontoFalse
SettheNextState=CurrentState
SettheEntryEventKindto0,iethedefault
Setourreturneventtothecurrentevent
Switch
CurrentState=Measure_t
SetCurrentEvent=RunDuringMeasure_tFunction
CurrentEvent.EventType=EV_PS_Detected
SetNextStatetoRequesting_t
SetmakeTransitiontotrue
CurrentState=Requesting_t
SetCurrentEvent=RunDuringRequesting_tFunction
CurrentEvent.EventType=EV_PS_Measuring
SetNextStatetoMeasure_t
SetmakeTransitiontotrue
voidStartHallEffectSM_SM(ES_EventCurrentEvent)
CreateavolatileES_EventLocalEventequaltotheCurrentEvent
SettheCurrentState=Measure_t,ourentrystate
CallOurInitializeHallEffectInterrupts()Function
RuntheHallEffect_SMwiththeCurrentEvent
HallEffect_State_tQueryHallEffect_SM(void)
returntheCurrentState
staticES_EventDuringMeasure_t(ES_EventEvent)
SetvolatileNewEventfordebugging
iftheEventisanES_EntryEvent
EnableAllHallSensorInterrupts
iftheEventisanES_ExitEvent
DisableAllHallSensorInterrupts
else
return(event)
staticES_EventDuringRequesting_t(ES_EventEvent)
SetvolatileNewEventfordebugging
iftheEventisanES_EntryEvent
iftheEventisanES_ExitEvent
else
return(event)
InitializeHallEffectInterrupts()
initializeInterrupt(HallEffectLeft_OuterTimer)
RepeatforAllHallEffectSensors
DisableAllHallSensorInterrupts()
LocallyDisableHallSensorInterruptLeft_Outer
LocallyDisableHallSensorInterruptLeft_Inner
LocallyDisableHallSensorInterruptRight_Inner
LocallyDisableHallSensorInterruptRight_Outer
EnableAllHallSensorInterrupts()
LocallyEnableHallSensorInterruptLeft_Outer
LocallyEnableHallSensorInterruptLeft_Inner
LocallyEnableHallSensorInterruptRight_Inner
LocallyEnableHallSensorInterruptRight_Outer
InterruptResponseforEachHallSensor
HallSensor_Left_Outer_InterruptResponse()
CleartheSourceoftheInterrupt
DefineThisCapture
Grabthecapturedvalue
Calculatetheperiodinmicroseconds
StorethelastCapturevalue
callthe
updatePeriodArrays
functionwithLEFT_OUTER_INDEX
callthedetectPollingStationfunctionwithSensor_Index
updatePeriodArraysfunction(uint8_tsensor_index,uint32CurrentPeriod)
IftheNUMBER_PULSES_TO_STOP>2
forloopcountingdownfromNUMBER_PULSES_TO_STOP1to(>)0
HallSensor_LastPeriod[i][sensor_index]=
HallSensor_LastPeriod[i1][sensor_index]
SettheHallSensor_LastPeriod[0][sensor_index]=CurrentPeriod
detectPollingStationfunction(uint8_tsensor_index)
setbooleansignalingvariablePS_detected=false
firstcheckifthelastperiodiswithinthetolerance
periodMatch=PS_Period_Comparison(HallSensor_LastPeriod[0][sensor_index])
Checkiftheperiodsbeforethatwerealsowithinthesametolerance
IftheNUMBER_PULSES_TO_STOP>1
initializenumGoodPulses=1
forloopcountingfrom1to(>=)NUMBER_PULSES_TO_STOP1
if(boolcontained=toleranceCheck(
HallSensor_LastPeriod[i][sensor_index],
periodMatch,
PERIOD_MEASURING_ERROR_TOLERANCE))
incrementnumGoodPulses
ifnumGoodPulsesequalsNUMBER_PULSES_TO_STOP
PostES_PS_detectedevent
uint32_tperiodMatchPS_Period_Comparison(uint32_tactualPeriod)
setreturnPeriodto0as0willbereturnedifnomatchesarefound
for0:NUMBER_PS_FREQUENCIES1
boolcontained=toleranceCheck(actualPeriod,PS_Possible_Periods[i],
PERIOD_MEASURING_ERROR_TOLERANCE)
if(contained)
returnPeriod=PS_Possible_Periods[i]
returnthereturnPeriod
PhotoTransitor_Service.c
PoundDefines
#definePERIOD_MEASURING_ERROR_TOLERANCE10
#defineNUMBER_CONSECUTIVE_PULSES_2STORE3
#defineNUMBER_PULSES_TO_BE_ALIGNED2
#defineNUMBER_PHOTOTRANSISTORS2
#defineNUMBER_BEACON_FREQUENCIES4
#defineBEACON_F_NW1450
#defineBEACON_F_NE1700
#defineBEACON_F_SE1250
#defineBEACON_F_SW1950
#defineBEACON_INDEX_NW0
#defineBEACON_INDEX_NE1
#defineBEACON_INDEX_SE2
#defineBEACON_INDEX_SW3
#defineDIRECTION1(use1ifCWand1ifCCW)
PoundDefineIndexinArrayCorrespondingtoPhototransistor
#defineLEFT_PHOTOTRANSISTOR_INDEX0
#defineRIGHT_PHOTOTRANSISTOR_INDEX1
ModuleLevelVariables
structBeacon{
uint16_tPeriod,
uint16_tlastEncoderAngle,
uint16_tlastUpdateTime
}
staticstructbeacon_structs[4]
CreateModuleLevelforEachPhotoTransitorsPeriods
staticuint8_tPhotoTransistor_LastPeriods[NUMBER_CONSECUTIVE_PULSES_2STORE]
[NUMBER_PHOTOTRANSISTORS]
InitializeBeaconValues
CreateourStructures
ie.structbeaconbeacon_NW
CalculateourBeaconPeriodsinMicroseconds
ie.beacon_NW.period=1000000/BEACON_F_NW
CreateanemptystructcallednoBeacon
setthenoBeaconsperiodequaltozero
Setthevaluesofourbeaconsarray
beacon_structs[0]=beacon_NWmakingsurethatthisisthesameorderasthe
enum
InitPhotoTransistorInterrupts
InitPhotoTransistor(Left)
InitPhotoTransisor(Right)
PhotoTransistorLeft_InterruptResponse()
CleartheSourceoftheInterrupt
DefineThisCapture
Grabthecapturedvalue
Calculatetheperiodinmicroseconds
StorethelastCapturevalue
callupdateLastPeriodfunctionandpass(LeftIndex,Period)
checkforabeacon(LeftIndex)
voidupdateLastPeriod(uint8_tIndex,uint32_tperiod)
forloopfromi=NUMBER_CONSECUTIVE_PULSES_2STORE1to(>)0
PhotoTransistor_LastPeriods[i][index]=PhotoTransistor_LastPeriods[i1][index]
Updatethemostrecentperiodby:
PhotoTransistor_LastPeriods[0][index]=period
voidcheckforabeacon(uint8_tindex)
setourtargetbeaconsstructureequaltotheresultofwhichBeacon(index)
ifthetargetbeaconisnotequaltothenullstruct(seeifthefrequency=0)
createcountervariablenumCorrect
iteratethroughallofourPhotoTransistor_LastPeriodsarrayandseehowmany
arewithinourtolerancebounds
ifnumCorrect=NUMBER_PULSES_TO_BE_ALIGNED
getthecurrentangleoftherotatingMotorandstoreinthestruct
getthecurrenttimeandstoreitinourstruct
postES_Beacon_Aligned
uint16_twhichBeacon(uint8_tindex)andreturnastructBeacon
getthecurrentPeriodLocally,ie.PhotoTransistor_LastPeriods[0][index]
CheckiftheperiodiswithinthetoleranceboundsforanyofourbeaconPeriodseg.
if(toleranceCheck(currentPeriod,beacon_NW.period,PERIOD_TOLERANCE)
ifitis,returnthatstructureeg.
returnbeacon_NW
iftheperioddoesntmatchanything,returnthenoBeaconstructure
uint16_tgetBeaconAngle(beacon_namesbeacon)
returnbeacon_array[
beacon].a
ngle
uint16_tgetBeaconUpdate(uint8_tbeacon)
returnbeacon_array[
beacon].t
ime
GameInfo.c
uint8_tArrayofsize9Current_Status_ofPollingStations
booleanAdStatusforRed
booleanAdStatusforBlue
booleanGameStatus
queueforEnemyCaptures
priorityqueuefordestinations
EnemyCaptureQueue.c
Queueimplementationusingastaticarrayofsize9
boolIsEmpty()
Returntrueiffhead==tail
VoidEnqueue(uint8_tstation)
Iftailisattheendofthearray,settail=0
Insertstationintothearrayattail
Incrementtail
uint8_tDequeue()
Ifhead==tail(thequeueisempty)returnEMPTYconstant
Else,setreturnstationbasedonthestationlocatedatindexheadinthearray
Incrementhead
Ifheadisattheendofthearray,sethead=0
uint8_tPositionInQueue(uint8_tstation)
Seti=head
Setposition=0
While(i!=tail)
Ifthearrayatindexiisequaltostation,returnposition
Incrementi
Incrementposition
Ifiistheendofthearray,seti=0
ReturnNOT_IN_QUEUE
Helpers.c:
(abunchofstaticstructs)
voidInitPWM(
uint8_tmodule,
uint8_tblock,
uint8_tgenerator,
uint32_tperiod,
uint32_tclock_div_constant,
uint8_tgpio_rcgc,
uint8_tgpio_base,
uint8_tgpio_pin,
uint8_tgpio_afsel_clear_mask,
uint8_tgpio_afsel_newval)
EnableclocktoPWM
enableclocktoport
SetPWMclockbasedonsystemclockanddivider
waitforpwmclock
disablepwm
programgeneratorA
programgeneratorB
Setperiod
enablethePWMoutputs
EnablealternatefunctionalityofGPIOpins
SelectPWMasalternatefunction
EnablepinsasdigitalI/O
Enablepinsasoutput
Setthedirectionpinslow
Selectupdowncounting,setlocalsynchronization,andenablegenerator
posttheinitialtransitionevent
booleangetResponseReadyStatus(responseArray)
returns(Array[1]==0xAA)
booleangetIsAcknowledged(responseArray)
booleanisOurs(resposneArray)
uint8_tgetLocation(responseArray)
booleantoleranceCheck(uint32_tvalue,uint32_ttarget,uint32_ttol)
setreturnVal=false
if((value>targettol)&&(value<target+tol))
setreturnValtrue
returnthereturnVal
//InterruptDefinitions
#defineSYSCTL_NUM(x)SYSCTL_RCGCWTIMER_R##x##
#defineWTIMER_NUM(x)WTIMER##x##_BASE
#defineTIMER_CTL(x)TIMER_CTL_T##x##EN
#defineILR(x)TIER_O_T##x##ILR
#defineCAPTURE_MODE(x)
#define
#define
#defineSOURCE(x)TIMER_ICR_T##x##TOCINT
#defineENABLE(x)TIMER_IMR_C##x##EIM
InitInputCapture(uint8_ttimer_num,uint8_ttimer_letter,uint8_tport_def,uint8_tport_base,
uint8_tpin,uint8_tnvic_priority,uint8_tnvic_enable,uint8_tpriority,uint8_tnvic_enable_bit)
Startbyenablingtheclocktothetimer,
SYSCTL_NUM(
timer_num)
EnabletheclocktoPort
port_def
SinceweaddedthisPortCclockinit,wecanimmediatelystartintoconfiguringthe
timer,noneedforfurtherdelaymakesurethattimer(
timer_letter)
isdisabledbefore
configuring
Setitupin32bitwide(individual,notconcatenated)mode
Theconstantnamederivesfromthe16/32bittimer,butthisisa32/64bittimersowe
aresettingthe32bitmode
Wewanttousethefull32bitcount,soinitializetheIntervalLoadRegisterto0xffff.ffff
(itsdefaultvalue:)
SetuptimerAincapturemode(TAMR=3,TAAMS=0),Foredgetime(TACMR=1)
andupcounting(TACDIR=1)
Tosettheeventtorisingedge,weneedtomodifytheTAEVENTbitsinGPTMCTL.
Risingedge=00,sowecleartheTAEVENTbits
NowSetuptheporttodothecapture(clockwasenabledearlier)startbysettingthe
alternatefunctionforPortCbit4(WT0CCP0)
Then,mapbit4'salternatefunctiontoWT0CCP07isthemuxvaluetoselect
WT0CCP0,16toshiftitovertotherightnibbleforbit4(4bits/nibble*4bits)
EnablepinonfordigitalI/O
Makepinintoaninput
Backtothetimertoenablealocalcaptureinterrupt
SetthePrioritysothatcommandgeneratorisprioritized
EnabletheTimerAinWideTimer0interruptintheNVIC
Itisinterruptnumber94soappearsinEN2atbit30
Makesureinterruptsareenabledglobally
Nowkickthetimeroffbyenablingitandenablingthetimertostallwhilestoppedbythe
debugger
ClearInputCapture(chartimernum,chartimer_letter)
HWREG(base+TIMER_O_ICR)=SOURCE(timer_letter)
DisableInterrupt(chartimernum,chartimer_letter)
HWREG(
WTIMER_NUM(
timer_num
)
+TIMER_O_IMR)&=~
ENABLE(
timer_letter
)
EnableInterrupt(chartimer_num,chartimer_letter)
HWREG(
WTIMER_NUM(
timer_num
)
+TIMER_O_IMR)|=
ENABLE(
timer_letter
)
voidInitPeriodic(
uint8_ttimer_num,
uint8_ttimer_letter,
uint8_tport_def,
uint8_tport_base,
uint8_tpin,
uint32_tnvic_priority,
uint32_tnvic_enable,
uint8_tpriority,
uint32_tpriority_clear_mask,
uint8_tnvic_enable_bit,
uint8_ttime_length,
uint8_tgpio_afsel_clear_mask,
uint8_tgpio_afsel_newval)
startbyenablingtheclocktothetimer
startbyenablingtheclocktothetimer
Setitupin32bitwide(individual,notconcatenated)mode
Setuptimerinperiodicmodesothatitrepeatsthetimeouts
Settimeouttotimelength
Enablealocaltimeoutinterrupt
SetPrioritytohigherthanzero(1)sothatitdoesnotconflictwithourotherinterrupt.
EnabletheTimerinWideTimerinterruptintheNVIC
Makesureinterruptsareenabledglobally
Nowkickthetimeroffbyenablingitandenablingthetimerto
Stallwhilestoppedbythedebugger
StrategyFunctions:
Updateprioritieswhen:
theycaptureastation
collisiondetected
wecaptureastation
ChooseDestination:
Dequeuepriorityqueueandreturnthestation
ChooseDestination():
uint8_ttarget
Foreachstation:
Ifweownthestation
Setprioritytomaxval
Elseifthestationisobstructed
Setprioritytomaxval1
Elsepriorityis:
Distancemultiplier*currentdistancetostation
+capturemultiplier*positionincapturequeue
+retrymultiplier*numberofretries
+intrinsicprioritizationofastation
SettargettobethetargetwithLOWESTpriorityvalue
RMS(floatx1,floaty1,floatx2,floaty2)
returnsqrt((x2x1)^2+(y2y1)^2)
left(target)
while(ownership[target]==myTeam)
target
Whataboutdestinationselectionwhenwecollidewiththeirrobot?
Basically,whenwedetectacollisionnearatarget,rulethattargetoutUNTILweseethat
thatlocation(oranotherlocation)getscaptured.Thenreattempt.
Definitions.h
[Modulewhichcontainsgeneraldefinedconstantsfortheprogram]
ControlLaws
PIDControllers:
CalculateRPMerrorasRPMTargetCurrentRPM
IncrementIntegralTermbyiGain*RPMError
ClampIntegralTermto[0,100](antiwindup)
SetDutyaspGain*(RPMError+IntegralTerm+(dGain*(RPMErrorLastError)))
StoreRPMErrorasLastError
ReturnDuty