Skip to content

Commit 3b49e06

Browse files
author
Chris Anderson
committed
Added example library
1 parent 337465b commit 3b49e06

File tree

2 files changed

+169
-0
lines changed

2 files changed

+169
-0
lines changed

examples/Robot.py

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# Simple two DC motor robot class. Exposes a simple LOGO turtle-like API for
2+
# moving a robot forward, backward, and turning. See RobotTest.py for an
3+
# example of using this class.
4+
# Author2: Tony DiCola, Chris Anderson
5+
# License: MIT License https://opensource.org/licenses/MIT
6+
7+
8+
# This assumes the Left motor is on Motor 1 and the Right motor is on Motor 2
9+
10+
11+
12+
import time
13+
import atexit
14+
from adafruit_motorkit import MotorKit
15+
16+
kit = MotorKit()
17+
18+
class Robot(object):
19+
def __init__(self, addr=0x60, left_id=1, right_id=2, left_trim=0, right_trim=0,
20+
stop_at_exit=True):
21+
"""Create an instance of the robot. Can specify the following optional
22+
parameters:
23+
- addr: The I2C address of the motor HAT, default is 0x60.
24+
- left_id: The ID of the left motor, default is 1.
25+
- right_id: The ID of the right motor, default is 2.
26+
- left_trim: Amount to offset the speed of the left motor, can be positive
27+
or negative and use useful for matching the speed of both
28+
motors. Default is 0.
29+
- right_trim: Amount to offset the speed of the right motor (see above).
30+
- stop_at_exit: Boolean to indicate if the motors should stop on program
31+
exit. Default is True (highly recommended to keep this
32+
value to prevent damage to the bot on program crash!).
33+
"""
34+
35+
self._left_trim = left_trim
36+
self._right_trim = right_trim
37+
if stop_at_exit:
38+
atexit.register(self.stop)
39+
40+
def _left_speed(self, speed):
41+
"""Set the speed of the left motor, taking into account its trim offset.
42+
"""
43+
assert -1 <= speed <= 1, 'Speed must be a value between -1 to 1 inclusive!'
44+
speed += self._left_trim
45+
speed = max(-1, min(1, speed)) # Constrain speed to 0-255 after trimming.
46+
kit.motor1.throttle = speed
47+
48+
def _right_speed(self, speed):
49+
"""Set the speed of the right motor, taking into account its trim offset.
50+
"""
51+
assert -1 <= speed <= 1, 'Speed must be a value between -1 to 1 inclusive!'
52+
speed += self._right_trim
53+
speed = max(-1, min(1, speed)) # Constrain speed to 0-255 after trimming.
54+
kit.motor2.throttle = speed
55+
56+
def stop(self):
57+
"""Stop all movement."""
58+
kit.motor1.throttle = 0
59+
kit.motor2.throttle = 0
60+
61+
def forward(self, speed, seconds=None):
62+
"""Move forward at the specified speed (0-255). Will start moving
63+
forward and return unless a seconds value is specified, in which
64+
case the robot will move forward for that amount of time and then stop.
65+
"""
66+
# Set motor speed and move both forward.
67+
self._left_speed(speed)
68+
self._right_speed(speed)
69+
# If an amount of time is specified, move for that time and then stop.
70+
if seconds is not None:
71+
time.sleep(seconds)
72+
self.stop()
73+
74+
def steer(self, speed, direction):
75+
"""Move forward at the specified speed (0- 1). Direction is +- 1. Full left is -1, Full right is +1.
76+
"""
77+
if (speed + direction/2) > 1:
78+
speed = speed - direction/2 # calibrate so total motor output never goes above 1
79+
left = speed + direction/2
80+
right = speed - direction/2
81+
self._left_speed(left)
82+
self._right_speed(right)
83+
84+
def backward(self, speed, seconds=None):
85+
"""Move backward at the specified speed (0-255). Will start moving
86+
backward and return unless a seconds value is specified, in which
87+
case the robot will move backward for that amount of time and then stop.
88+
"""
89+
# Set motor speed and move both backward.
90+
self._left_speed(-1*speed)
91+
self._right_speed(-1*speed)
92+
# If an amount of time is specified, move for that time and then stop.
93+
if seconds is not None:
94+
time.sleep(seconds)
95+
self.stop()
96+
97+
98+
def right(self, speed, seconds=None):
99+
"""Spin to the right at the specified speed. Will start spinning and
100+
return unless a seconds value is specified, in which case the robot will
101+
spin for that amount of time and then stop.
102+
"""
103+
# Set motor speed and move both forward.
104+
self._left_speed(speed)
105+
self._right_speed(0)
106+
# If an amount of time is specified, move for that time and then stop.
107+
if seconds is not None:
108+
time.sleep(seconds)
109+
self.stop()
110+
111+
def left(self, speed, seconds=None):
112+
"""Spin to the left at the specified speed. Will start spinning and
113+
return unless a seconds value is specified, in which case the robot will
114+
spin for that amount of time and then stop.
115+
"""
116+
# Set motor speed and move both forward.
117+
self._left_speed(0)
118+
self._right_speed(speed)
119+
# If an amount of time is specified, move for that time and then stop.
120+
if seconds is not None:
121+
time.sleep(seconds)
122+
self.stop()

examples/RobotTest.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Simple two DC motor robot class usage example.
2+
# Author: Tony DiCola
3+
# License: MIT License https://opensource.org/licenses/MIT
4+
import time
5+
6+
# Import the Robot.py file (must be in the same directory as this file!).
7+
import Robot
8+
9+
10+
# Set the trim offset for each motor (left and right). This is a value that
11+
# will offset the speed of movement of each motor in order to make them both
12+
# move at the same desired speed. Because there's no feedback the robot doesn't
13+
# know how fast each motor is spinning and the robot can pull to a side if one
14+
# motor spins faster than the other motor. To determine the trim values move the
15+
# robot forward slowly (around 100 speed) and watch if it veers to the left or
16+
# right. If it veers left then the _right_ motor is spinning faster so try
17+
# setting RIGHT_TRIM to a small negative value, like -5, to slow down the right
18+
# motor. Likewise if it veers right then adjust the _left_ motor trim to a small
19+
# negative value. Increase or decrease the trim value until the bot moves
20+
# straight forward/backward.
21+
LEFT_TRIM = 0
22+
RIGHT_TRIM = 0
23+
24+
25+
# Create an instance of the robot with the specified trim values.
26+
# Not shown are other optional parameters:
27+
# - addr: The I2C address of the motor HAT, default is 0x60.
28+
# - left_id: The ID of the left motor, default is 1.
29+
# - right_id: The ID of the right motor, default is 2.
30+
robot = Robot.Robot(left_trim=LEFT_TRIM, right_trim=RIGHT_TRIM)
31+
32+
# Now move the robot around!
33+
# Each call below takes two parameters:
34+
# - speed: The speed of the movement, a value from 0-255. The higher the value
35+
# the faster the movement. You need to start with a value around 100
36+
# to get enough torque to move the robot.
37+
# - time (seconds): Amount of time to perform the movement. After moving for
38+
# this amount of seconds the robot will stop. This parameter
39+
# is optional and if not specified the robot will start moving
40+
# forever.
41+
42+
robot.right(0.5,1)
43+
time.sleep(3)
44+
robot.stop() # Stop the robot from moving.
45+
46+
47+
# That's it! Note that on exit the robot will automatically stop moving.

0 commit comments

Comments
 (0)