Service Modules
IRManager
IR Manager is a service that detects the knight and goal IR emitters. Three distinct phototransistors have input capture timer pins, and an ES_Timer loop checks the frequency of the incoming signals several times a second.
PseudoCode (Technical Overview): Module Interrupt/Run Function Interface
PseudoCode (Technical Overview): Module Interrupt/Run Function Interface
- Use Three Input Compare Interrupts That Increment Tick Counter
- Use An ES_TIMER Library that 10 times a second checks the three counters, determines if the three pulse numbers are appropriate for the knight vs. goal (forward and rear facings sensors detect knight, side sensor detects goal), and resets the counters
- If a sensor detects something, it posts an ES_GOALDETECTED or ES_KNIGHTDETECTED event to the mater state machine to handle if appropriate
TapeDebounce
TapeDebounce contains the event checkers for the tape sensors. It is its own service because it initially was using a debouncing system, however this was removed after further testing revealed there was no noise in the tape sensor signal
PseudoCode (Technical Overview): Event Checker Function For Tape Sensors
PseudoCode (Technical Overview): Event Checker Function For Tape Sensors
- Read the Value of the tape sensor in the AD port
- If the value is above the voltage threshold for black tape and it wasn't the last time checked:
- Post ES_CENTRALTAPEDETECTED or ES_LATERALTAPEDETECTED and post to the master state machine to handle if appropriate
SPIProjectService
SPIProjectService is responsible for periodically querying the JSR and transmitting new stage/gave over messages to the master state machine. The SPI Project service performs the bitwise operations necessary to parse incoming signals and stores game scores, which could be queried by the state chart.
PseudoCode (Technical Overview):
ModuleLevelVariables:
InitServiceModule:
RunServiceModule:
o If 50ms timer: Write CommandType To Register
o If 2ms timer: Write appropriate value to register based on command type and number
SPI Interrupt:
QueryReload/QueryMyScore/QueryTheirScore functions accessible to state charts
PseudoCode (Technical Overview):
ModuleLevelVariables:
- CommandType: whether currently asking for game status or matchscore
- CommandNumber: value 0-3 to depending on place in sequence
- My Score: our knights score
- Their Score: other knights score
- Reload Status: Boolean
InitServiceModule:
- Initialize all of the SPI settings per previous command generator project. Start timer for 50ms
RunServiceModule:
o If 50ms timer: Write CommandType To Register
o If 2ms timer: Write appropriate value to register based on command type and number
SPI Interrupt:
- Module Level Variable: LastGameStatus
- Based on current commandType and CommandNumber:
- Read register and update module level variables appropriate
- If game status changes or head knocked off post event to state chart / update score module level variables
- If CommandNumber is 3:
- Switch command types and reset commandnumber to 0
- Start 50ms timer
- Else:
- Set 2ms timer
- CommandNumber++
QueryReload/QueryMyScore/QueryTheirScore functions accessible to state charts
BallRequestService
BallRequestService is called upon by the Reload state machine when it is an appropriate time to request balls. The service uses an output compare timer to obtain the precise signal necessary to request a ball, and then the ES_Timer library to wait approximately 3 seconds between ball requests.
PseudoCode (Technical Overview):
Run Function:
Interrupt:
PseudoCode (Technical Overview):
Run Function:
- if request ball event: turn on indicator led and program next output compare
- if timeout event:
- if ball 5 over: send finished reload event
- if not ball 5 and three seconds up: turn on indicator led and program next output compare
- else: restart timer to wait a full three seconds
Interrupt:
- Clear Flag
- If the IR emittor is high: put it low, increment cycle number, and set output compare for 30 ms
- If the IR emiittor is low
- If it has been 10 cycles: start timer for big break and turn off LED low
- Else: Set the emitter high, and set output compare for 10 ms
BallShootService
BallShootService responds to two event calls, one of which will turn on the shooting motor early to allow it to warm up, and the second of which controls a servo motor to release the balls into the shooter.
PseudoCode (Technical Overview):
PseudoCode (Technical Overview):
- if event is start the shorter: increase the duty cycle on the PWM pin to 45 from 0
- if event is to shoot balls:
- change pulse width of the servo holding balls back
- start timer
- if event is a timeout:
- set PWM on motor back to zero
- change pulse width of servo to keep new balls back
- post event to move forward HSM to start moving
LanceMotorControl
LanceMotorControl is called to deploy the lance for the 3 second period. There are two servo motors which control the chop and sweep motions of the lance, and this service handles the timings and positions to obtain the deadliest motion.
PseudoCode (Technical Overview):
PseudoCode (Technical Overview):
- if event is deploy the lance: set a timer to timeout
- if event is a timeout:
- switch based on counter
- based on switch number reposition tilt servo or pan servo
- increment counter
- restart timer
- if count is greater than programmed motions: send ES_MOVE event to move forward state chart
MotorService
MotorService is a complex service that receives calls to move straight, backwards, and turn. Using encoders connected to input capture pins, the service can precisely measure distance traveled for turns and finite moves, sending motor response events when these moves are finished. For movements across the track, the service applies a PI control law to the PWM signal on the motors based on encoder readings. In order to keep the head from falling over, the service controls an acceleration and deceleration period. Finally, the service is able to recognize a wall hit when the requested duty cycle hits 100 for both wheels, and then automatically stops the motors and ends the stage.
PseudoCode (Technical Overview):
Left Encoder Interrupt:
Right Encoder Interrupt:
Duty Cycle Interrupt:
Run Function:
PseudoCode (Technical Overview):
Left Encoder Interrupt:
- Increment tick counter
- If currently counting the number of ticks
- Increment special tick counter
- if special tick count is greater that requested ticks: turn both motors off and send motor response event to master state machine
Right Encoder Interrupt:
- Increment tick counter
- If currently counting the number of tick until reaching goal line
- Increment special tick counter
- if special tick count is greater that field distance ticks: turn on timer that will begin process of slowing down speed
Duty Cycle Interrupt:
- if currently moving:
- Determine Error Between Left Encoder and Desired Number of Ticks
- Determine Error Between Right Encoder and Desired Number of Ticks
- If Duty cycle is above 100 on both motors, post motor stop event and post motor response event
- Apply PI Control Laws to both motors
- Apply Linear offset to right motor
- Update Duty Cycles Accordingly
Run Function:
- If Event is TimeOut: increase or decrease set point speed as appropriate to allow for slowing/speeding up
- If Event is motor event, switch based on event parameter. Things to control:
- Direction: Forward, Backward, Turning Left, Turning Right: control relative current directions on wheels
- Speed: How high is DTY Cycle. Different speeds for long vs. short movements
- Moving For Set Distance? If so, program into module so that interrupt can turn off when appropriate
- Traveling across the track? If so, start the timers to speed up duty cycle gradually so headed doesn't fall off