Laboratory 1: Digital Input/Output (GPIO)
Laboratory Description
In this lab, simple digital inputs are used to program the RPI-RSLK to drive in a desired pattern composed of independent segments:
drive and turn left 90°
drive and turn left 45°
drive forward
stop for 1 second.
drive and turn right 45°
drive and turn right 90°
The bumper buttons on the car will be used to input each sequential step while a breadboard wired slide switch will cause the inputted pattern to execute and/or stop. One breadboard wired pushbutton is used to delete the last step inputted while a second wired pushbutton will delete all steps inputted. A BiColor LED will provide indication of if the pattern is running (GREEN), the pattern is complete (RED) or the program is waiting for user input (OFF), and the onboard RGB LED indicates both segment entry and sequence operation.
Laboratory Goals
This laboratory is designed to further a student’s understanding of:
The Code Composer Studio (CCS) Integrated Development Environment
Programming the MSPM0G3507 microcontroller,
The implementation and use of inputs and outputs,
The development of modular C programs towards producing a function system, and
The functionality of the RPI Robotics System Learning Kit (RPI-RSLK).
Preparation
Students should complete Activities 1-5 prior to starting this laboratory. A good understanding of the tasks in Activity 5 is essential for this laboratory.
Hardware and Tools
RPI-RSLK Robotic Car
Breadboard
Components (provided):
2x Pushbutton
1x Slideswitch
1x BiColor LED (BiLED)
Various Resistors ( 1x 510Ω, 3x 1kΩ)
Wire (available in classroom)
Project Template
Part A: (Class 1) Build and Test I/O
The first day of this lab consists of building the hardware required, producing the GPIO initialization code, and implementing tests to verify the correct operation of all inputs and outputs. The circuitry needed is shown in the Figure Lab 1 Schematic. Note that this schematic provides both the circuitry included on the RSLK and that which needs to be built. The circuitry that needs to be built should be done on an external breadboard, not the one mounted on the RSLK, to allow for other teams to use the same car.
Warning
There are no 5V connections within Lab 1 Schematic, only 3.3V! This is because the microcontroller pins are only 3.3V tolerant and will likely fail if 5V is applied to them. Ensure that you never use 5V when connecting to this microcontroller!
Lab 1 Circuit Schematic
Both the left and right drive motors on the RSLK are controlled by two output lines: On/Off and Direction. The On/Off line will turn the motor on (High) or off (Low) whereas the Direction pin indicates if the wheel should rotate forward (Low) or reverse (High).
With the circuitry built, the GPIO pins must be initialized. You must use the Lab 1 template project Lab1_template.zip. This template project has a purpose-built library (lab1lib Driver Library) for interacting with the motors. Within this project, initialize the pins as specified in the schematic within GPIOInit() using the EmCon HAL functions only; do not use registers. We will not be using registers to interface with the MSPM0 for the rest of the course.
Important
The bumpers on the RSLK are connected to ground but do not have a corresponding pullup resistor, like circuit 2 from Activity 4. For this to work, a pullup resistor needs to be provided by the MSPM0G3507 internally via GPIO configuration. To enable the necessary internal pullup resistor for the required pins, use the HAL function:
void GPIO_setInternalResistor(...)
See the associated HAL documentation on how to use this function. Note that this function must be called after any other initialization functions for the associated pin(s).
Finally, finish the code provided within function testIO() to test the inputs and outputs. The GPIO input states should be printed each time a keyboard key is pressed. Further, if the keyboard key pressed is contained within the list below, the specified operation should be completed to test the output. An example on how to complete this is given in the template code.
Keys |
Function |
|---|---|
R/r |
RGB LED RED Ch. ON (R)/OFF (r) |
G/g |
RGB LED GREEN Ch. ON (G)/OFF (g) |
B/b |
RGB LED BLUE Ch. ON (B)/OFF (b) |
q |
BiLED1 Red |
w |
BiLED1 Off |
e |
BiLED1 Green |
Warning
Do not forget to turn the car on to test! The motors will not work without the car being turned on; however, all other components work regardless of the car being on or off.
To turn the car on: Move the slide switch on the rear-left of the car to On. A blue LED near this switch will light to indicate it is on, assuming there are charged batteries in the car. If the car batteries are near dead, a red LED will also light, indicating the batteries should be replaced. If neither light up, the car is either dead or has no batteries.
Ask a course staff member to get charged batteries for the car.
Please remember to turn off the car when you are done with it!
Ask a staff member to check off Part A when complete; then continue with Part B.
Part B: (Class 2) Pattern Input and Running
With the inputs and outputs built, configured, and tested, the pattern control routine may be added to the project. As provided in the description above, each of the bumper buttons are used to input a specific action into the pattern. They should be configured such that:
BMP1 adds the “turn left 90°” segment (-90°)
BMP2 adds the “turn left 45°” segment (-45°)
BMP3 adds the “drive forward” segment (0°)
BMP4 adds the “stop for 1s” segment
BMP5 adds the “turn right 45°” segment (45°)
BMP6 adds the “turn right 90°” segment (90°)
Each time a segment is added to the segment the RGB LED should change color; the sequence of which does not matter as long as it can light at least 3 different colors. This is used to indicate a success input of a segment. If the breadboard pushbutton (push1) is pressed, the last segment input into the sequence should be deleted; whereas the other pushbutton (push2) deletes the whole sequence. Finally, if the breadboard slide switch is toggled from off to on, the program should trigger the pattern to be run and the BiLED lit GREEN. Once the pattern completes, the BiLED should be lit RED until the slide switch is toggled back off. While the BiLED is lit either color, the inputs are ignored.
The simple functional description above can be translated into a flow diagram: Lab 1 Flowchart.
Lab 1 Flowchart. Blocks with red text indicate functionality provided by functions within the lab1lib Driver Library.
Warning
The bumper buttons are prone to bouncing. You will need to debounce your button press/release and slide swith on/off detection to avoid registering multiple button presses for a single physical press.
Implement the control scheme above within the function controlSystem(). The flow chart must be explicitly followed; that is, do not make assumptions about system behavior that are not described by the diagram. You will need to comment out the function call to testIO() and uncomment the call to controlSystem() within the main() function’s while(1) loop. As controlSystem() is called within a while(1), there is no need to have an additional while(1) loop within this function, though other while() or for() loops are required.
Ask a staff member to check off Part B when complete and submit the final code to Gradescope. The code must successfully implement the logic as described above and not be prone to button bouncing. For demonstrational purposes, a simplistic obstacle course may be set up in the classroom for the cars to traverse.
Note
It may be useful for debugging purposes to also have the RGB LED changing colors as the running sequence cycles through segments. This is not required but allowed.
lab1lib Driver Library Functions
The following are functions that are provided by the lab1lib Driver Library and are provided within src/lab1lib.lib. The documentation provided here is also available in inc/lab1lib.h
initSequence
void initSequence(void)
This function initializes the lab1lib Driver.
recordSegment
int8_t recordSegment(int8_t mv)
This function adds a segment to the programmed sequence. The input, int8_t mv, represents the segment to add:
|
Corresponding Segment |
|---|---|
-90 |
Drive and Turn 90° Left |
-45 |
Drive and Turn 45° Left |
0 |
Drive Straight |
45 |
Drive and Turn 45° Right |
90 |
Drive and Turn 90° Right |
-128 |
INVALID |
any other number |
Stop for 1s |
This function will output:
0 if segment was added to the sequence
1 if the segment has reached the maximum length (50), segment was not added
-1 if the value of
mvis invalid
popSegment
uint8_t popSegment(void)
This function removes the most recently added segment. Outputs 0 if a segment was erased or 1 is there is no segments to erase (sequence is empty).
clearSequence
void clearSequence(void)
Erase the currently programmed sequence.
runSequence
uint8_t runSequence(void)
Runs the programmed sequence. Returns 0 if started properly, 1 if the sequence is empty, or 2 if the GPIO pins for the motor are not set up properly. In the case of a return of 2, the program will also print a message indicating the issue.
statusSegment
int8_t statusSegment(void)
Returns the currently running segment type in the sequence (see table for recordSegment) or -128 if the sequence is not running (either hasn’t run or is complete).
statusSequence
int8_t statusSequence(void)
Returns the step that a running sequence is on (1,2,3, etc.) or -128 if the sequence is not running (either hasn’t run or is complete)
getSequenceLength
uint8_t getSequenceLength(void)
Returns the total number of segments currently programmed into the sequence. The maximum length of a sequence is 50.