Overview
Two PICS control MPLabron's operation.
The first "Leader" PIC controls the overall game progression, and interfaced with both control buttons, all three servos, and the IR beacon detection.
It communicated to the "Follower" PIC game state changes and beacon alignment status.
The "Follower" PIC controls both wheel motors, interfaces with the Ultrasonic Sensors, the limit switches, and the encoders, and communicates to the "Leader" PIC when its tasks are complete.
The first "Leader" PIC controls the overall game progression, and interfaced with both control buttons, all three servos, and the IR beacon detection.
It communicated to the "Follower" PIC game state changes and beacon alignment status.
The "Follower" PIC controls both wheel motors, interfaces with the Ultrasonic Sensors, the limit switches, and the encoders, and communicates to the "Leader" PIC when its tasks are complete.
Leader PIC
Leader State Machine
In the Leader PIC, LeaderService.c controls the highest-level of game operation. This state machine controls whether the bot is in an Idle, Loading, or Dunking state. This state machine handles the core game functions of detecting the beacon, turning the game indicator eyebrow servos on/off, and changing the team indicator eye colors on/off, and operating the dunk servo that releases all the balls during a dunk. In addition, this state machine contains event checkers for button presses and sends directions to the follower service via SPI communication to control the motion of the bot.
Leader State Machine Pseudocode
LeaderService.c
LeaderService.h
Leader State Machine Pseudocode
LeaderService.c
LeaderService.h
SPI Query Leader Service
SPIQueryLeaderService.c contains a simple state machine that constantly queries the Follower PIC every 10Hz for relevant commands. This service also initializes SPI1 module as leader on the Leader PIC. In addition, this service contains an Interrupt Service Routine (ISR) to handle SPI interrupts received from the Follower PIC. This service will post an event to the main LeaderService.c when it receives a HW_TARGETREACHED (0x04) from the Follower PIC.
SPIQueryLeaderService Pseudocode
SPIQueryLeaderService.c
SPIQueryLeaderService.h
SPIQueryLeaderService Pseudocode
SPIQueryLeaderService.c
SPIQueryLeaderService.h
Servo Service
ServoService.c handles the three servos: the left eyebrow, right eyebrow, and dunk servo. The LeaderService.c will then post HW_TOGGLE_SERVO events to ServoService.c with the EventParam indicating which servo should be toggled.
Servo Pseudocode
ServoService.c
ServoService.h
Servo Pseudocode
ServoService.c
ServoService.h
Load Button Module and Dunk Button State Machine
In the Leader PIC, there are two buttons: the Load button, which tells the bot to quickly leave and re-enter to the reload zone, and the Dunk button, which tells the bot to drive all the way to the basket and dunk the balls. Due to differences in the hardware of the two different buttons, we had to handle these two buttons differently. The Load button presses are detected using an event checker that detects a rising edge on the digital input pin. The Dunk button presses are debounced using a state machine to handle mechanical switch bouncing we observed during testing.
Load Button Module and Dunk Button State Machine Pseudocode
LoadButton.c
LoadButton.h
DunkButtonService.c
DunkButtonService.h
Load Button Module and Dunk Button State Machine Pseudocode
LoadButton.c
LoadButton.h
DunkButtonService.c
DunkButtonService.h
Beacon Service
In the Leader PIC, BeaconService.c is responsible for detecting the beacon, changing the eye color for the appropriate team, and posting HW_BEACON and HW_MISALIGNED events to the main LeaderService.c. This service contains a state machine that allows the service to initialize a timer and timeout to constantly query for misalignment events. This service also utilizes and initializes IC3 and TMR3 to detect the rising edges of the beacon.
Beacon Pseudocode
BeaconService.c
BeaconService.h
Beacon Pseudocode
BeaconService.c
BeaconService.h
Eye Color Module
EyeColor.c is a module that contains functions that initialize the two eye color pins as digital outputs and functions that allow the eye color to be changed or cleared by writing to the corresponding LATCH of the red and blue eye I/O pins. These functions are all called from the main LeaderService.c and BeaconService.c.
EyeColor Pseudocode
EyeColor.c
EyeColor.h
EyeColor Pseudocode
EyeColor.c
EyeColor.h
Test Harness Service
TestHarnessService.c is a test harness that detects user keyboard events and allows modular testing of specific functionality of all sub-components of the Leader PIC with a significant focus on testing SPI communication to the Follower PIC. This test harness does not change the actual state of the main Leader service state machine.
TestHarnessService Pseudocode
TestHarnessService.c
TestHarnessService.h
TestHarnessService Pseudocode
TestHarnessService.c
TestHarnessService.h
Follower PIC
Motor State Machine
In the Follower PIC, MotorSM.c controls the two wheel motors. It receives direction from FollowerService.c and events from UltrasonicService.c and SwitchChecker.c to switch between GameStart, Loading, Dunking, Unloading, Returning, and GameEnd states. Events received from FollowerService.c, sent from Leader PIC's beacon detection, direct the state machine through realignment with the beacon if needed during Dunking state.
MotorStateMachine Pseudocode MotorSM.c MotorSM.h Video shows MPLabron driving with adjusted PID gains. This control law was used in competition. |
|
Follower SPI Service
FollowerService.c posts events to the motor state machine based on the received byte (which is interpreted based on an enum). This service also initializes SPI1 module as follower on the Follower PIC. In addition, this service contains an Interrupt Service Routine (ISR) to handle SPI interrupts received from the Leader PIC. This service will post an event to MotorSM.c based on the byte received from the Leader PIC.
FollowerService Pseudocode
FollowerService.c
FollowerService.h
FollowerService Pseudocode
FollowerService.c
FollowerService.h
Switch Checker
SwitchChecker.c is a checker that reads in the limit switch value from a digital input pin.
It checks if the new value has changed , and if the change was from not pushed to pushed, posts the wall detected event to the motor state machine.
SwitchChecker Pseudocode
SwitchChecker.c
SwitchChecker.h
It checks if the new value has changed , and if the change was from not pushed to pushed, posts the wall detected event to the motor state machine.
SwitchChecker Pseudocode
SwitchChecker.c
SwitchChecker.h
Ultrasonic Service
Ultrasonic.c service triggers the ultrasonic sensor in the front and the back and reads in the measured distance.
It lowers the TRIG pin when ready for an ultrasonic burst, and then measures the pulse on the ECHO pin, using the measurement to calculate the robot's distance from the front and back wall, then sending events to MotorSM.c at the desired distances from the wall.
Ultrasonic Pseudocode
Ultrasonic.c
Ultrasonic.h
It lowers the TRIG pin when ready for an ultrasonic burst, and then measures the pulse on the ECHO pin, using the measurement to calculate the robot's distance from the front and back wall, then sending events to MotorSM.c at the desired distances from the wall.
Ultrasonic Pseudocode
Ultrasonic.c
Ultrasonic.h
Test Harness
TestHarnessService0 is a simple service based on Ed's test harness service from Lab 3 ME218a. It posts events to services and state machines depending on the user keyboard input. This facilitated debugging individual modules and allowed for each component to be tested before integration with each other and before Follower PIC was integrated with Leader PIC.
TestHarnessService0 Pseudocode
TestHarnessService0.c
TestHarnessService0.h
TestHarnessService0 Pseudocode
TestHarnessService0.c
TestHarnessService0.h