Programming Autonomy - Robotics with Python Raspberry Pi and GoPiGo p.6




While remote control is cool, autonomy is cooler! In this robotics programming with Python, the Raspberry Pi, and the GoPiGo tutorial, we'll be covering how to begin to program our robot to interact with the physical world all on its own.

To start:

from gopigo import *
import time
import random

min_distance = 70

def autonomy():
    no_problem = True
    while no_problem:

To start, we grab the GoPiGo software, import time, and import random. We set "min_distance" to a value that we want to be the "minimum" distance between our robot and an object it detects. You should choose a number much larger than you really think you want. For example, as you will see, setting 70, for 70cm, usually results in us stopping just barely short of smashing into the wall, because of our logic, which lets us move blindly for "x" amount of time.

Next, we just begin to build the logic for handling issues that we cannot figure out. We start this while loop, under the condition that there is no problem. Later on, we might have the robot just disable itself if there is a problem, or, better yet, enter some sort of problem solving phase that attempts to extract itself from the issue. Carrying on:

        servo(70)
        time.sleep(1)
        dist = us_dist(15)
        if dist > min_distance:
            print('Forward is fine with me', dist)
            fwd()
            time.sleep(1)

We set the servo to "forward" position. For me, that's 70. Your "center" may vary. Next, we grab the current distance between us and the nearest object in front of us. If that distance is greater than the minimum distance that we require, then we're fine to just proceed forward. If not, however:

        else:
            print('Stuff is in the way', dist)
            stop()
            servo(28)
            time.sleep(1)
            left_dir = us_dist(15)
            time.sleep(1)
            servo(112)
            right_dir = us_dist(15)
            time.sleep(1)

Here, we have detected that moving forward is a bad idea. Next, we use the servo to look left, and look right, gathering distance measurements.

            if left_dir > right_dir and left_dir > min_distance:
                print('Choose left!')
                left()
                time.sleep(1)
            elif left_dir < right_dir and right_dir > min_distance:
                print('Choose Right!')
                right()
                time.sleep(1)
            else:
                print('No good option, REVERSE!')
                bwd()
                time.sleep(2)
                rot_choices = [right_rot, left_rot]
                rotation = rot_choices[random.randrange(0,2)]
                rotation()
                time.sleep(1)

            stop()

After having gathered the distance measurements of the left and right directions, we can then make some educated choices. If one of the directions is greater than the min_distance value, then we go that way. If both are greater, then we go the direction that is the largest. If neither direction is greater than the minimum, then we decide our last choice is to just move backwards. Since we came from "backwards," we're assuming this will be safe, but we're doing this totally blind. When we're done with the reversing, we're also going to spin in a random left or right direction.

When all this is done, we use stop(). We do this so last actions are not just repeated. The only action that will continue is moving forward.

Now all we need to do is call things to run:

stop()
enable_servo()
servo(70)
time.sleep(3)
autonomy()

This puts our full script to:

from gopigo import *
import time
import random

min_distance = 70

def autonomy():
    no_problem = True
    while no_problem:
        servo(70)
        time.sleep(1)
        dist = us_dist(15)
        if dist > min_distance:
            print('Forward is fine with me', dist)
            fwd()
            time.sleep(1)
        else:
            print('Stuff is in the way', dist)
            stop()
            servo(28)
            time.sleep(1)
            left_dir = us_dist(15)
            time.sleep(1)
            servo(112)
            right_dir = us_dist(15)
            time.sleep(1)

            if left_dir > right_dir and left_dir > min_distance:
                print('Choose left!')
                left()
                time.sleep(1)
            elif left_dir < right_dir and right_dir > min_distance:
                print('Choose Right!')
                right()
                time.sleep(1)
            else:
                print('No good option, REVERSE!')
                bwd()
                time.sleep(2)
                rot_choices = [right_rot, left_rot]
                rotation = rot_choices[random.randrange(0,2)]
                rotation()
                time.sleep(1)

            stop()
                
stop()
enable_servo()
servo(70)
time.sleep(3)
autonomy()

Not too shabby for such rudimentary code! Now, our robot has "eyes," but only we can see through them. Let's give the gift of vision to our robot next.


There exists 4 quiz/question(s) for this tutorial. for access to these, video downloads, and no ads.

The next tutorial:






  • Robotics with Python Raspberry Pi and GoPiGo Introduction
  • Supplies Needed - Robotics with Python Raspberry Pi and GoPiGo p.2
  • Programming Robot Basics - Robotics with Python Raspberry Pi and GoPiGo p.3
  • Programming Remote Control - Robotics with Python Raspberry Pi and GoPiGo p.4
  • Weaponizing our Robot - Robotics with Python Raspberry Pi and GoPiGo p.5
  • Programming Autonomy - Robotics with Python Raspberry Pi and GoPiGo p.6
  • OpenCV with Raspberry Pi Camera Face Detection Tutorial - Robotics with Python Raspberry Pi and GoPiGo p.7