You are on page 1of 3

1 DogSM

************************************************************************************
Pseudo-code for the DogSM state machine
This is the state machine that handles the overarching logic for the hovercraft.

InitDogSM
Takes a priority number, returns true

// Initialize deferral queue


// Initialize the MyPriority variable with the passed in parameter.
// Initialize the UART 1 subsystem
// Initialize the Pokemon LEDs to be off
// Set CurrentState to be InitDogState
// Get dogtag number
// Post Event ES_Init to DogSM queue
End of InitDogSM

PostDogSM
Create PostDogSM

RunDogSM
The parameter it takes in will be ThisEvent and the parameter field of
ThisEvent will be the time that the event occurred. Returns ES_NO_Event.

// Create ES_Event ReturnEvent


// Define variables
// Set NextState to CurrentState
// Based on the state of the CurrentState, choose one of the following
// blocks of code:
// If case is InitDogState
// If event type is ES_Init
// Initialize pair status to be unpaired (0)
// Initialize encrypt status to be inactive (0)
// Make sure all fans come out of reset in OFF state
// Initialize pokeball servo to spin until we successfully pair
// Set NextState to Waiting2Pair
// Endif
// If case is WaitingToPair
// If event type is EV_PairRequested
// Set the pair status to be paired (1)
// Store the source address of the XBee that requested to pair to use
// as the destination address for our transmissions
// Post to DogXBeeTransmitService to send an ACK to pair
// Start the unpair timer
// Set the next state to be WaitingForEncrypt
// Else if event type is EV_RetryXmit2Xbee
// (If the transmit status packet indicates the packet was a NACK or a CCA)
// Resend the previous packet
// Endif
// If case is WaitingForEncrypt
// If event type is EV_EncrKeyReceived
// Set encrypt status to be active (i.e. encryption key has been sent)
// Stop the pokeball indicators rotation
// Turn on indicator LEDs to show completely paired status by posting
// EV_BattleStart to ControlService
// Turn on lift fan
// Turn on thrust fan by unmasking interrupt
// Post to DogXBeeTransmitService to send a STATUS packet to the FARMER
// Start the unpair timer again
// Set the next state to be WaitingForControl
// Else if event type is EV_RetryXmit2Xbee
// (If the transmit status packet indicates the packet was a NACK or a CCA)
// Resend the previous packet
// Else if unpair timer times out
// Reset the pair status to be unpaired (0)
// Reset the encrypt status to be inactive (0)
// Turn off lift and thrust fans
// Turn pokeball servo back on

1
// Turn off Pokemon LEDs
// Set the next state to go back to WaitingToPair
// Endif
// If case is WaitingForControl
// If event type is EV_ControlReceived
// Post to DogXBeeTransmitService to send a STATUS packet to the FARMER
// Start the unpair timer again
// If event type is EV_EncryptionReset
// Start the unpair tier again
// Post to DogXBeeTransmitService to send an ENCR_RESET packet to the FARMER
// Else if event type is EV_RetryXmit2Xbee
// (If the transmit status packet indicates the packet was a NACK or a CCA)
// Resend the previous packet
// Else if unpair timer timesout
// Reset the pair status to be unpaired (0)
// Reset the encrypt status to be inactive (0)
// Turn off lift and thrust fans
// Turn pokeball servo back on
// Turn off Pokemon LEDs
// Set the next state to go back to WaitingToPair
// Endif
// Endif
// Set CurrentState to NextState
// Return ES_NO_EVENT
End of RunDogSM

InitUART1
Initializes UART module 1 in order to use asynchronous wired communication with the
on board Xbee

// Enable the clock to the UART module


// Enable the clock to the appropriate GPIO module (Port B) as defined in
// the GPIO pins and Alternate Functions table
// Wait for the UART module (Module 1) to be ready
// Wait for the GPIO module (Port B) to be ready
// Set the bits that will be assigned to digital functions (PB0 and PB1)
// Set the direction of the UART pins (input for receive and output for
// transmit)
// Select the alternate functions for the UART
// Configure the PMCn fields in the GPIOPCTL register to assign the UART
// pins (alternate function value to mux in is 1)
// Disable the UART by clearing the UARTEN bit in the UARTCTL register
// (should be disabled out of reset)
// Write the integer portion of the baud rate divisor (BRD) for 9600 baud
// Write the fractional portion of the BRD for 9600 baud
// Write the desired serial parameters (8N1): 8-bit word length, no parity
// (default), and one stop bit (default)
// Configure the UART operation: enable receive and transmit (default),
// disable high-speed enable (default), and set the EOT interrupt to fire
// when another byte can be written in the data register (default), not
// when the transmission has finished and the last bit has shifted out of
// the shift register.
// Enable the UART
// Enable the receive interrupts locally (do not enable the transmit
// interrupt until ready to transmit)
// Enable the transmit and receive interrupts in the NVIC (only one
// interrupt vector for one UART module): it is interrupt number 6 so it
// appears in EN0 at bit 6 (page 104 and page 142 in the data sheet)
// Make sure interrupts are enabled globally
End of InitUART1

GetDestAddress
// Return the destination address

GetPairStatus
// Return the current pair status

GetEncryptStatus
// Return the encryption status (a formality, since should be the same as the pair
status)

2
GetDogTag
A getter function which reads an analog input pin and computes dog tag number based
on the computed voltage.

// Initialize DogTag variable


// Get current state from input line using ADMulti.c function
// Use value for PE0 (channel 0 in the read array) to back calculate input line
voltage
// Note that a max AD reading of 4095 corresponds to 3.3V
// Determine dog tag by sorting voltage into bins
// If voltage less than the cutoff for dog tag 1
// Set DogTag equal to 1 (i.e. Charmander)
// Else if voltage less than cutoff for dog tag 2
// Set DogTag equal to 2 (i.e. Bulbasaur)
// Else
// Set DogTag equal to 3 (Squirtle)
// Endif
// Return DogTag
End of GetDogTag

FansOff
A function for turning both lift and thrust fans off.

// Turn lift fan off by posting an EV_ToggleLIft event to PIC_UARTService with the
// fan off parameter
// Set thrust fan duty cycles to zero
// Mask thrust fan interrupt
End of FansOff

UART1_ISR
An interrupt service routine which responds to both transmits and receive events
having to do with the Xbee.

// Check which flag caused the UART interrupt to fire (TX or RX):
// If it was transmit:
// Clear the transmit interrupt flag
// Call the UART1 transmit ISR
// Else if it was receive:
// Clear the receive interrupt flag
// Call the UART1 receive ISR
// Endif
End of UART1_ISR

End of DogSM

You might also like