====== Software Setup ======
- Open the VSCode from the Applications directory on the flash drive
- Click on the small green square in the bottom left corner of the screen\\ \\ {{ :pibot:lesson3:softwarewin:s1.png |VSCode Main Screen}}
- Select the "Connect Current Window to Host..." option\\ \\ {{ :pibot:lesson3:softwarewin:s2.png |VSCode Remote-SSH}}
- Type "pi@pibot##" and press enter\\ \\ 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! It may take a couple of minutes while it installs some files on the PiBot {{ :pibot:lesson3:softwarewin:s3.png |VSCode Remote Connection}}
- Click the "Open Folder" button\\ \\ {{ :pibot:lesson3:softwarewin:s4.png |VSCode Open Folder}}
- Select the "pibot" folder and click the "OK" button\\ \\ {{ :pibot:lesson3:softwarewin:s5.png |VSCode Browse}}
- Open a terminal in Visual Studio Code from the Terminal->New Terminal menu
- Type the following command and press enter to download the Python code for this lesson\\ \\ wget http://osoyoo.com/driver/picar/picar-obstacle-avoid.py
- Type the following command and press enter to make the PiBot drive and avoid obstacles\\ \\ The PiBot will first rotate the Ultrasonic Distance Sensor to the front for 3 seconds. If the sensor does not face the front of the PiBot during these three seconds then hold down the control + C keys to end the program. Then remove the M2x4mm screw that holds the Ultrasonic Distance Sensor to the Micro Servo Motor and re-position it so that it faces the front of the PiBot. Then reinstall the M2x4mm screw to hold the Ultrasonic Distance Sensor to the Micro Servo Motor, type the command again, and press enter. python3 picar-obstacle-avoid.py
- Hold down the Ctrl + C keys to stop the PiBot
===== Software Explanation =====
- Select the picar-obstacle-avoid.py file in the list on the left to view the code that makes the PiBot run\\ \\ {{ :pibot:lesson3:softwarewin:s6.png |VSCode picar-line-tracking.py}}
- Find line 33 in the picar-obstacle-avoid.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 [[:pibot:lesson1:softwarewin#software_explanation|Lesson 1]] These variables set the PWM output that tells the micro servo motor what direction to point. If the ultrasonic distance sensor doesn't point straight ahead and you can't get it to point straight ahead by adjusting it, you can fine-tune where it points by changing these values. If you change the servo_ctr value, be sure to change the other two values so that there is the same difference between them and the servo_ctr value.
- Find where the measure function is defined. What is it doing? Compare the steps in the function to the "Timing Diagram" section of the [[https://cdn.sparkfun.com/datasheets/Sensors/Proximity/HCSR04.pdf|datasheet]] for the ultrasonic distance sensor. Does the code match what the datasheet says to do to make a measurement and calculate the distance?\\ \\ If you don't remember what the while statements do, review [[:pibot:lesson2:softwarewin#software_explanation|Lesson 2]] for a refresher. The datasheet for sensors on the robot tell you how to interface with them and get the data you need. If no one else has written software to use the sensor, you can always write your own using the datasheet to know what to do. See the [[:pibot:lesson2#electrical_description|Electrical Description]] section on the previous page for a description of how the sensor works
- Find the main loop in the code. What is the code doing to make the robot sense obstacles and move? What would you do to make it move smoother without stopping? Make those changes to the code and save it with File->Save\\ \\ The [[https://cdn.sparkfun.com/datasheets/Sensors/Proximity/HCSR04.pdf|datasheet]] has a minimum amount of time required between measurements. Make sure that there will be enough time after your changes so that the measurements don't get messed up. Stopping the PiBot while taking measurements ensures that it has enough time to make a decision which direction to go. When you make it drive continuously, you need to ensure that it can take the measurements and make a decision before it moves far enough to run into something.
- Type the following command in the Visual Studio Code terminal and press enter to make the PiBot move with your changes\\ \\ python3 picar-obstacle-avoid.py
- When you're finished testing out your changes type the following command in the Visual Studio Code 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
[[:pibot:lesson3#software|Return to Lesson 3]]