3.1. Driving Around¶
Driving the GoPiGo3 around is only a matter of choosing the right methods from the
In all of our examples, the
easygopigo3 is required and needs to be imported and a
can be instantiated this way:
from easygopigo3 import EasyGoPiGo3 # importing the EasyGoPiGo3 class gpg = EasyGoPiGo3() # instantiating a EasyGoPiGo3 object
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
We first need to import the
easygopigo3 and the
time modules and instantiate a EasyGoPiGo3 object.
# import the time library for the sleep function import time from easygopigo3 import EasyGoPiGo3 # 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.
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,
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
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
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.
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
3.1.5. Going Forward at Increasing Speed¶
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()