Software Setup
- Open the terminal app
Click the magnifying glass in the top right corner of the screen and type “terminal” to find the terminal app
- Type the following command and press enter to connect to the Raspberry Pi terminal via SSH
Replace the text “pibot##” with the label on the PiBot
The password to enter when prompted is “DogsAndCatsAreNice2.” without the quotes. The '.' at the end is important!
ssh pi@pibot##.local
- Type the following command and press enter to change to the directory for the PiBot code
cd ~/pibot
- Type the following command and press enter to download the Python code for this lesson
wget http://osoyoo.com/driver/picar/picar-line-tracking.py
- Make sure that the PiBot is facing along the black tape path
- Type the following command and press enter to make the PiBot follow the black tape path
A set of 5 digits will start scrolling along the screen. These represent the status of each line sensor that the PiBot is “seeing”
python3 picar-line-tracking.py
- Hold down the control + C keys to stop the PiBot
- Type the following command and press enter to close the SSH connection
exit
Software Explanation
- To view the code that makes the PiBot run perform the following steps
- Open the Visual Studio Code from the Applications directory on the flash drive
- Find line 37 in the picar-line-tracking.py file. What is the name of the variables on that line and the ones below it? What do you think they are for based on their names?
If you don't remember what a variable is, review the description from Lesson 1
- Find the places in the code where the sensor# variables are used. What do you think the sts# variables are for? Is that different from what you thought earlier?
The “If” statement allows the code to make decisions based on the result of a test. If the result is “True” or “1” then it executes assigns the first value, else the result was “False” or “0” and it assigns the last value.
The sts# variables are assigned a '0' value if the GPIO pin is a '1' and a '1' value if the GPIO pin is not a '1'.
See the Electrical Description section on the previous page for a description of how the sensors work
- Find the other places in the code where the “If” statements are used. What do you think is happening with those “If” statements?
The second group of “If” statements are standalone and not part of assigning a value to a variable. This is the most common use of the “If” statement. When the expression after the “If” is “True” then it will run the code after the “If” statement, otherwise it will skip that code and go to the next statement
You can check for multiple values of a variable in an “If” statement by combining them with the “or” operator. When any of the values matches, then the result is “True”
The “or” operator is part of boolean logic which allows computers to make decisions based on any input. Boolean logic is the basis for how computers work.
Comments can be added to the code to describe what it is doing. They always start with a “#” character and everything else on the line after that is a comment and not run as part of the code. The comments in the code aren't always correct. Can you spot the errors?
- Look at line 114 in the code. What do you think this line does?
The “While” statement is similar to the “If” statement. It will continue to run the code under it as long as the expression is “True”. We call this a loop because when it gets to the end of the code under the “While” statement it loops back to the beginning. How long do you think this “While” loop will keep running?
- The PiBot moves pretty jerkily as it tries to stay on the line. How do you think you can change the code so that it follows the line more smoothly? Make those changes to the code and save it with File→Save
- Open a terminal in Visual Studio Code from the Terminal→New Terminal menu
- Type the following command and press enter to make the PiBot move with your changes
python3 picar-line-tracking.py
- When you're finished testing out your changes type the following command in the terminal and press enter to shutdown the PiBot
sudo shutdown -h now
- Click on the small green rectangle in the bottom left corner of the screen and select “Close Remote Connection”
- Wait 3-5 minutes for the green light on the Raspberry Pi to stop flashing and turn off the switch on the PiBot battery