Professional Documents
Culture Documents
Followerpseudocode
Followerpseudocode
c
Data private to module
Constants: LOST_LED, LEFT_MOTOR_DIRECTION, RIGHT_MOTOR_DIRECTION,
BLUE_RGB, RED_RGB, GREEN_RGB, INTERACT_LED, FRONT_LIMIT_SWITCH,
SERVO_LOW, SERVO_HIGH, leftAdjust
InitFollowerService
Set MyPriority to parameter passed in
Set direction and testDir to zero
Set tapeFollowSlow and tapeFollowFast to target duty cycles for tape
following (60% and 61% respectively)
Set forward to true
Disable global interrupts
Call configure SPI function
Call configure PWM function
Call configure SERVO PWM function
Enable global interrupts
Initialize the following pins as digital outputs: left motor non-pwm
pin, right motor non-pwm pin, interact LED, RBG LED (red, green, and
blue pins), lost LED.
Turn off all LEDs
Set both motor duty cycles to zero
Set inGamePlay to zero
Set servoState to zero
Post ES_INIT event
Return true if ES_INIT successfully posted, false otherwise
PostFollowerService
Post the parameter event to this service.
Return true if event successfully posted, false otherwise
RunFollowerService
Select one of the following blocks of code depending on the event
passed as a parameter:
If ThisEvent is ES_INIT, no actions needed
If ThisEvent is COMMAND_RECEIVED:
Execute the actions corresponding to the command in the event
parameter, as outlined
in Table 1 below
If ThisEvent is ES_TIMEOUT:
Change the servo state to the opposite of what it is.
Restart ServoTimer for half a second
If ThisEvent is LIMIT_SWITCH_HIT:
Set tapeFollowSlow to 99% duty cycle
Set tapeFollowFast to 100% duty cycle
Return true
ConfigSPI(void)
Disable global interrupts
Disable analog function on all pins
Call BasicConfig to configure SPI1 (disables SPI1)
Set SPI1 to follower
Map RA2 as SDO
Set DISSDO to zero
Map RB5 to SDI
Set DISSDI to zero
Map RB3 to SS input
Clear receive buffer
Disable enhanced buffer
Set active edge to second edge
Initialize CKP
Set transfer width to 16 bit
Set baud rate to 917.4 Hz
Clear SPI receive bits
Set interrupt priority to 7
Enable SPI interrupts
Clear SPIROV bit
Set FRMPOL bit to zero
Enable SPI operation
Void ConfigPWM
(all timer configurations in this function are for timer 3)
Turn off timer 3
Disable gated time mode
Clear TCS control bit
Set pre-scale to 4
Set SIDL bit to zero
Clear TMR3 register
Load PR3 so that the drive frequency is 5000 Hz
Clear TMR3 interrupt flag
__SPI1_ISR
Read SPI1 buffer into local variable command
Post COMMAND_RECEIVED event to FollowerService with parameter command
Clear SPI1 receive bit
RunCommand
Select action based on command passed in as a parameter (per the table
below). If the command is not in the table below, print “Error,
unrecognized command”
0x0001 Stop motors, turn off Set left motor duty cycle to
indicators, exit gameplay zero
Set right motor duty cycle to
zero
Turn off RGB LED
Turn off interact LED
Set inGamePlay to zero
0x0027 Tape following: slow left Set left motor duty cycle of
motor forwards tapeFollowSlow forwards
Set right motor duty cycle of
tapeFollowFast forwards
0x0023 Tape following: slow left Set left motor duty cycle of
motor reverse tapeFollowSlow reverse
Set right motor duty cycle of
tapeFollowFast reverse
0x0021 Tape following: slow right Set left motor duty cycle of
motor forwards tapeFollowFast forwards
Set right motor duty cycle of
tapeFollowSlow forwards
Set forwards to true
0x0025 Tape following: slow right Set left motor duty cycle of
motor backwards tapeFollowFast backwards
Set right motor duty cycle of
tapeFollowSlow backwards
ConfigServoPWM
(all timer initialization steps below are for timer 2)
Turn off timer
Disable gated time mode
Clear TCS control bit and select clock source as internal clock
Set pre-scale to 64
Set SIDL bit to zero
Load and clear timer register
Set PR2 to 20ms period
Clear T2IS interrupt flag
Timer2RollOver
Disable global interrupts
If T2IF is pending: increment rollover counter and clear rollover
interrupt
Enable global interrupts
SetLeftDC
If DC parameter is less than zero, set DC to zero
If DC parameter is more than 100, set DC to 100
If direction is zero: set OC2RS to PWMPeriodTicks * DC / 100
If direction is one: set OC2RS to PWMPeriodTicks * (100 - DC) / 100
If direction is not zero or one: print error message for invalid
direction
SetRightDC
If DC parameter is less than zero, set DC to zero
If DC parameter is more than 100, set DC to 100
If direction is zero: set OC1RS to PWMPeriodTicks * DC / 100
If direction is one: set OC1RS to PWMPeriodTicks * (100 - DC) / 100
If direction is not zero or one: print error message for invalid
direction