3.1. Driving Around

Driving the awesome GoPiGo3 around is only a matter of choosing the right methods from the EasyGoPiGo3 class. In all of our examples, the easygopigo3 is required and needs to be imported and a EasyGoPiGo3 object can be instantiated this way:

from easygopigo3 import EasyGoPiGo3 # importing the EasyGoPiGo3 class
gpg = EasyGoPiGo3() # instantiating a EasyGoPiGo3 object

Instantiating EasyGoPiGo3 object can fail if the GoPiGo3 is not present or there’s a firmware mismatch. Check the __init__ constructor of this class to see what exceptions can be raised.

3.1.1. Forward, R/L, Backward

The most basic commands don’t offer control over how much distance the GoPiGo3 travels. They only get called once and then it’s the responsability of the user to stop the GoPiGo3 from moving, regularly by using the stop() command.

We first need to import the EasyGoPiGo3 class and the time module and instantiate a EasyGoPiGo3 object.

# import the time library for the sleep function
from easygopigo3 import EasyGoPiGo3
import time

# create an instance of the GoPiGo3 class.
# gpg will be the GoPiGo3 object.
gpg = EasyGoPiGo3()

Next, move the robot forward for exactly one second and right after that continue to the next block of code.

print("Move the motors forward freely for 1 second.")
gpg.forward()
time.sleep(1)
gpg.stop()

Stop the motors for a second.

print("Stop the motors for 1 second.")
time.sleep(1)

And then move forward again, but this time for 50 centimeters and once the moving function ends, wait a second until the next block of code gets executed. Notice that drive_cm() is a blocking function in this case.

print("Drive the motors 50 cm and then stop.")
gpg.drive_cm(50, True)
time.sleep(1)

Then right for a second.

print("Turn right 1 second.")
gpg.right()
time.sleep(1)

And likewise to the left.

print("Turn left 1 second.")
gpg.left()
time.sleep(1)

Finally, stop the robot.

print("Stop!")
gpg.stop()
print("Done!")

If you want to run this by yourself, here’s the script on github.

../_images/driving1.gif

3.1.2. Describing a Square

To make the GoPiGo3 describe a square by moving itself, you need to run the following script. To do this, drive_cm() and turn_degrees() methods are required. A square with the side length of 30cm is drawn. The square is drawn clockwise.

from easygopigo3 import EasyGoPiGo3

gpg = EasyGoPiGo3()
length = 30

for i in range(4):
  gpg.drive_cm(length) # drive forward for length cm
  gpg.turn_degrees(90) # rotate 90 degrees to the right
../_images/driving2.gif

3.1.3. Making Circular Moves

Driving straight in one direction is one thing, but rotating around a center axis at a specific radius is something entirely different. In this example, the GoPiGo3 draws half a circle and then returns on the same track by spinning itself on the spot.

The radius of the circle is set at 50 centimeters and the robot will move for half of the circle (aka 180 degrees).

from easygopigo3 import EasyGoPiGo3

gpg = EasyGoPiGo3()

gpg.orbit(180, 50) # draw half a circle
gpg.turn_degrees(180) # rotate the GoPiGo3 around
gpg.orbit(-180, 50) # return on the initial path
gpg.turn_degrees(180) # and put it in the initial position
../_images/driving3.gif

3.1.4. Drawing an 8 Shape

Let’s say we want to draw an 8 shape with the GoPiGo3 and at the end have the GoPiGo3 reach the same position it initially left from.

To do this, we have to use orbit() and drive_cm() methods.

from easygopigo3 import EasyGoPiGo3

gpg = EasyGoPiGo3()
radius = 30

gpg.orbit(-270, radius) # to rotate to the left
gpg.drive_cm(radius * 2) # move forward
gpg.orbit(270, radius) # to rotate to the right
gpg.drive_cm(radius * 2) # move forward
../_images/driving4.gif

3.1.5. Going Forward at Increasing Speed

In this example, we make the GoPiGo3 go forward at an ever increasing speed. We start of with a speed of 50 and end up going at 300. forward(), set_speed() and stop() methods are used.

Warning

This example will not work with versions released before November 2018. Do an update before running it.

from easygopigo3 import EasyGoPiGo3
from time import time, sleep

gpg = EasyGoPiGo3()

# setting speed to lowest value and
# calculating the step increase in speed
current_speed = 50
end_speed = 400
step = (end_speed - current_speed) / 20
gpg.set_speed(current_speed)

# start moving the robot at an ever increasing speed
gpg.forward()
while current_speed <= end_speed:
  sleep(0.1)
  gpg.set_speed(current_speed)
  current_speed += step

# and then stop it
gpg.stop()
../_images/driving5.gif

3.1.6. Other Examples

There are also other examples you can look at, namely the projects in the GoPiGo3 repository. Also, to see all methods for moving around the GoPiGo3, check the GoPiGo3 movement API.