We will be creating 3 objects:
|
Startup Python commands: import easygopigo3 as easy |
** How alive are we? ** gpg.volt() |
Returns the current battery voltage. Fresh batteries start out at 12V, then diminish. Motors will stop working when the batteries are drawn down to somewhere between 6 and 8V. Brain death occurs somewhere below 5V. |
print(gpg.volt()) |
** Driving and stopping ** gpg.drive_inches(distance) |
Drives the robot forward (or backward) distance in inches, then stops. | # 2 steps forward, 1 step back: step = 3.5 gpg.forward() gpg.drive_inches(2*step) gpg.backward() gpg.drive_inches(step) |
** Setting wheel speed ** gpg.set_speed(dps) gpg.get_speed() gpg.reset_speed() |
Sets the speed of the
wheels in degrees-per-second (dps).
Will affect the speed of gpg.drive_inches(), gpg.drive_degrees(),
gpg.forward() and gpg.backward(). For normal speeds: -1000 <= dps <= 1000, but you can experiment. Since there are usually 360 degrees in a circle, a speed of 360 turns the wheel one revolution/sec. get_speed() returns the current speed (dps) reset_speed() sets the speed to 300 |
gpg.set_speed(900) # slowing it down current = gpg.get_speed() gpg.set_speed(0.5 * current) |
** Turning and stopping ** gpg.turn_degrees(dg) |
Rotates the robot dg degrees clockwise (if dg >
0), or counter-clockwise (if dg < 0), then stops. Only one wheel turns, with its speed controlled by gpg.set_speed() |
# projectile vomit gpg.turn_degrees(1000) |
** Distance from object ** dist.read_inches() |
Returns the distance from the distance sensor to the nearest object in front of it (in the direction that it's looking), in inches. | # sneak up on someone gpg.set_speed(100) while dist.read_inches() > 2: gpg.drive_inches(0.25) time.sleep(0.5) |
** Controlling the servo ** serv.reset_servo() serv.rotate_servo(dg) |
The servo should point the distance sensor straight ahead when the
robot is off. When the robot is turned on, the servo will turn slightly clockwise (sign of life). serv.reset_servo() should leave the servo pointing straight ahead (dg = 90), and undo the little jerk it made on startup. serv.rotate_servo(dg). if dg = 90: straight ahead; if dg = 0: rotate to 90 degrees clockwise (facing right) if dg = 180: rotate 90 degrees counter-clockwise (facing left) |
|
**Continuous motor control** gpg.forward() gpg.backward() gpg.right() gpg.set_motor_dps(gpg.MOTOR_RIGHT,speed) gpg.left() gpg.set_motor_dps(gpg.MOTOR_LEFT, speed) gpg.stop() |
gpg.forward(): Drive forward without stopping, at previously set
speed. gpg.backward(): Drive backward without stopping, at previously set speed. gpg.right(): Turn right (only right wheel turning) at previously set speed. gpg.set_motor_dps(): Set right motor's speed. gpg.left(): Turn left (only left wheel turning) at previously set speed. gpg.set_motor_dps(): Set left motor's speed. These functions return immediately, after telling the robot to drive continually. This, of course, cries out for the STOP function... gpg.stop() |
# approach wall while showing # distance left gpg.set_speed(300) gpg.forward() while dist.read_inches() > 2.5: print(gpg.read_inches()) gpg.stop() # turn right along a circle (left # wheel turning faster) for 2.5 secs gpg.set_motor_dps(gpg.MOTOR_LEFT,300) gpg.set_motor_dps(gpg.MOTOR_RIGHT,100) time.sleep(2.5) gpg.stop() |
** Working with the motor encoders ** gpg.reset_encoders() gpg.read_encoders() |
Each motor encoder (left
and right) measures how far its wheel has traveled (in degrees). gpg.reset_encoders(): resets the current measurement to 0 the_left_one, the_right_one = gpg.read_encoders(): returns the number of degrees each wheel has traveled since the encoders were reset. |
# turn right along a circle (similar to the
example above) # stopping after the left wheel has rotated twice gpg.reset_encoders() gpg.set_motor_dps(gpg.MOTOR_LEFT,300) gpg.set_motor_dps(gpg.MOTOR_RIGHT,100) lt,rt = gpg.read_encoders() while lt < 720: lt,rt = gpg.read_encoders() gpg.stop() |
** Setting the LEDs' colors ** gpg.set_left_eye_color(color) gpg.set_right_eye_color(color) gpg.set_eye_color(color) |
Sets the color of one or
both of the pair of LEDs one the top board of the robot. The color is encoded as a triplet of integers (R,G,B) between 0 and 255. R=red, G=green, B=blue. Note: since the color is a parenthesized triplet of ints, you'll see 2 opening and closing parentheses when these methods are called. |
# right eye -> light blue # left eye -> bright red # then change right -> mid-yellow gpg.set_right_eye_color((0,0,50)) gpg.set_left_eye_color((255,0,0)) gpg.open_eyes() gpg.set_right_eye_color((100,100,0)) gpg.open_right_eye() |
** Turning the LEDs on/off and
activating their colors ** gpg.open_left_eye() gpg.open_right_eye() gpg.open_eyes() gpg.close_right_eye() gpg.close_left_eye() gpg.close_eyes() |
The 3 open methods
turn on the particular LEDs and/or actually change the color(s). The 3 close methods turn the LEDs off. Note: the open methods also activate the color changes called by the set color methods above -- in other words, you'll see the color change only after a open method is called. |
# see above |