Pseudocode for DOG_FSM.c InitDOG_FSM Set CurrentState to WaitingToPair Initialize UART for communication with XBee Initialize UART for communication with TREAT Initialize PE0 for ADC Kill Lift fan Put LEDs into the initial alternatively flashing state Post the initial transition event end InitDOG_FSM RunDOG_FSM if the event type is ES_TIMEOUT if the event param is BLUE_LED_STRIP_TIMER toggle blue LED strip restart blue LED strip timer else if the event param is RED_LED_STRIP_TIMER toggle the red LED strip restart red LED strip timer end if end if switch on CurrentState case WaitingToPair state if Event type is ES_PACKET_RX AND Event param is REQ_2_PAIR start LOST_CONN_TIMER (1s) Post ES_BEGIN_TXMIT with event param of PAIR_ACK to DOGTXmitSM Set Current State to PairedAndWaitingForKey break case PairedAndWaitingForKey state if Event type is ES_PACKET_RX AND Event param is ENCR_KEY (Store encryption key is already done in DOGRxSM) Set encryption index to 0 Start LOST_CONN_TIMER (1s) Post ES_BEGIN_TXMIT with event param of STATUS to DOGTXmitSM Set CurrentState to Paired Start Paired LED Behavior Open Shark Mouth Post ES_PAIRED to DOGSharkMotions else if Event type is ES_TIMEOUT AND Event param is LOST_CONN_TIMER Start Unpaired LED Behavior Kill lift fan Close Shark Mouth Post ES_UNPAIRED to DOGSharkMotions Set CurrentState to WaitingToPair break case Paired state if the Event type is ES_PACKET_RX Start LOST_CONN_TIMER (1s) if the decrypted header is CTRL Post the decrypted ctrl bytes to MotionService Evaluate effects of the digital brake and peripheral commands If brake is not to be applied turn lift fan on else (If brake is to be applied) turn lift fan off end if If the peripheral bit is high Start Chomp LED Behavior Close Shark Mouth Start chomp timer end if Clear decrypted ctrl so that the 'if' statement above works when a new packet comes in Post ES_BEGIN_TXMIT with event param of STATUS to DOGTXmitSM else (Decrypted header is garbage thus decryption index is off) Post ES_BEGIN_TXMIT with event param of ENCR_RESET to DOGTXmitSM Set encryption index to 0 end if else if the Event Type is ES_TIMEOUT if the event param is LOST_CONN_TIMER Start Unpaired LED Behavior Close Shark Mouth Kill Thrust turn lift fan off Post ES_UNPAIRED to DOGSharkMotions Set CurrentState to WaitingToPair else if the event param is MOUTH_CHOMP_TIMER Open Shark Mouth Start Paired LED Behavior end if end if break End switch on CurrentState return ReturnEvent end RunDOG_FSM ReadDogTag Do ADC multiread if the DogTagReading is between the upper and lower limits for DOG_1 return DOG_1 else if DogTagReading is between the upper and lower limits for DOG_2 return DOG_2 else if DogTagReading is between the upper and lower limits for DOG_3 return DOG_3 else return INVALID_DOGTAG end if end ReadDogTag StartUnpairedLEDBehavior (Puts LEDs into an alternatively flashing state and kicks off relevant timers) turn on blue LEDs turn off red LEDs set blue LED strip period to unpaired period set red LED strip period to unpaired period start blue LED strip timer start red LED strip timer end StartUnpairedLEDBehavior StartPairedLEDBehavior (Puts LEDs into a alternatively flashing state and kicks off relevant timers) turn on blue LEDs turn off red LEDs set blue LED strip period to paired period set red LED strip period to paired period start blue LED strip timer start red LED strip timer end StartPairedLEDBehavior StartChompLEDBehavior (Puts LEDs into a alternatively flashing state and kicks off relevant timers) turn on blue LEDs turn off red LEDs set blue LED strip period to chomp period set red LED strip period to chomp period start blue LED strip timer start red LED strip timer end StartChompLEDBehavior