Software Setup

  1. Open the Visual Studio Code from the Applications directory on the flash drive
  2. Click on the small green square in the bottom left corner of the screen

    VSCode Main Screen

  3. Select the “Connect Current Window to Host…” option

    VSCode Remote-SSH

  4. Type “pi@pibot##.local” 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

    VSCode Remote Connection

  5. Click the “Open Folder” button

    VSCode Open Folder

  6. Select the “pibot” folder and click the “OK” button

    VSCode Browse

  7. Open a terminal in Visual Studio Code from the Terminal→New Terminal menu
  8. 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
  9. 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
  10. Hold down the control + C keys to stop the PiBot

Software Explanation

  1. Select the picar-obstacle-avoid.py file in the list on the left to view the code that makes the PiBot run

    VSCode picar-line-tracking.py

  2. 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 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.

  3. Find where the measure function is defined. What is it doing? Compare the steps in the function to the “Timing Diagram” section of the 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 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 Electrical Description section on the previous page for a description of how the sensor works

  4. 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 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.

  5. 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
  6. 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
  7. Click on the small green rectangle in the bottom left corner of the screen and select “Close Remote Connection”
  8. Wait 3-5 minutes for the green light on the Raspberry Pi to stop flashing and turn off the switch on the PiBot battery

Return to Lesson 3

"For behold, he who forms the mountains and creates the wind, and declares to man what is his thought, who makes the morning darkness, and treads on the heights of the earth—the LORD, the God of hosts, is his name! - Amos 4:13"