Software
Our software follows the general structure of Arduino programs. In the beginning, we define various constants and input/output pins. We also define custom states for both our FSM states and the tape color.
In setup, we initialize the motor output pins and hook up the servos.
In loop, we read the tape sensor values. We then navigate the logic of the FSM through switch statements. While this handles the logic, the robot itself is controlled through a splattering of helper functions outside of loop. These are organized by:
Finally, our code has debug print statements which can be turned on or off by flags. We also handle key presses to manually control the robot, e.g. pressing f will call forwardMotors(), which we used while testing only the drive component. One lesson learned: do not put delay()s in your debug print statements.
For the full code, please check out our Github repo.
In setup, we initialize the motor output pins and hook up the servos.
In loop, we read the tape sensor values. We then navigate the logic of the FSM through switch statements. While this handles the logic, the robot itself is controlled through a splattering of helper functions outside of loop. These are organized by:
- Line sensor functions
- readTape() reads from the tape sensors. We do some algorithmic correction because our tape sensors are super sensitive. Instead of taking the raw value as the color of the line we are on, we convert the value into a percentage of the largest number the tape sensor has seen. This way, we accommodate individual manufacturing differences and resistor tolerances in our tape sensor values. For example, our side sensor outputs an analog value of 20 for grey tape, while our front one outputs 30. However, these are both around 25% of the value these sensors read while on white tape (80 and 120, respectively.) We implement hysteresis filtering for our tape color thresholds, defining a grey crossing as 10% and a white one as 40%.
- updateTapeValues() is a helper function to readTape() which updates our global tape sensor variables with their appropriate colors, which the robot then reads to line follow.
- Motor movement functions
- forwardMotors() and backwardMotors() make the robot move forward or backwards by setting both motor speeds to be the same, either positive (forward) or negative (backward).
- adjustLeft() and adjustRight() slightly turn the robot by setting one motor speed faster than the other one. We use these while line following.
- turnRightMotors() executes a right turn, which is required for the robot to get through the startup garage. The robot pivots about its center and then moves forward for a set period of time, enough to get to the other team's garage. We increase the robot's speed as we were having trouble getting up a small bump in the playing field before.
- Servo movement functions
- openLeftServo() and openRightServo() open the servos. They then begin a timer which calls closeLeft() or closeRight() after 2 seconds.
- Functions called on a timer for the FSM
- When the robot crosses a grey line, whether it's funding round A, B, or the patent office, we wait half a second before changing the state of the machine back to "moving". This is because the
Finally, our code has debug print statements which can be turned on or off by flags. We also handle key presses to manually control the robot, e.g. pressing f will call forwardMotors(), which we used while testing only the drive component. One lesson learned: do not put delay()s in your debug print statements.
For the full code, please check out our Github repo.