Skip to content

Commit 26fab61

Browse files
authored
Merge pull request #9 from zlite/master
Added example library
2 parents 337465b + 87b8808 commit 26fab61

File tree

2 files changed

+165
-0
lines changed

2 files changed

+165
-0
lines changed

examples/Robot.py

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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, left_trim=0, right_trim=0, stop_at_exit=True):
20+
"""Create an instance of the robot. Can specify the following optional
21+
parameter
22+
- left_trim: Amount to offset the speed of the left motor, can be positive
23+
or negative and use useful for matching the speed of both
24+
motors. Default is 0.
25+
- right_trim: Amount to offset the speed of the right motor (see above).
26+
- stop_at_exit: Boolean to indicate if the motors should stop on program
27+
exit. Default is True (highly recommended to keep this
28+
value to prevent damage to the bot on program crash!).
29+
"""
30+
31+
self._left_trim = left_trim
32+
self._right_trim = right_trim
33+
if stop_at_exit:
34+
atexit.register(self.stop)
35+
36+
def _left_speed(self, speed):
37+
"""Set the speed of the left motor, taking into account its trim offset.
38+
"""
39+
assert -1 <= speed <= 1, 'Speed must be a value between -1 to 1 inclusive!'
40+
speed += self._left_trim
41+
speed = max(-1, min(1, speed)) # Constrain speed to 0-255 after trimming.
42+
kit.motor1.throttle = speed
43+
44+
def _right_speed(self, speed):
45+
"""Set the speed of the right motor, taking into account its trim offset.
46+
"""
47+
assert -1 <= speed <= 1, 'Speed must be a value between -1 to 1 inclusive!'
48+
speed += self._right_trim
49+
speed = max(-1, min(1, speed)) # Constrain speed to 0-255 after trimming.
50+
kit.motor2.throttle = speed
51+
52+
@staticmethod
53+
def stop():
54+
"""Stop all movement."""
55+
kit.motor1.throttle = 0
56+
kit.motor2.throttle = 0
57+
58+
def forward(self, speed, seconds=None):
59+
"""Move forward at the specified speed (0-255). Will start moving
60+
forward and return unless a seconds value is specified, in which
61+
case the robot will move forward for that amount of time and then stop.
62+
"""
63+
# Set motor speed and move both forward.
64+
self._left_speed(speed)
65+
self._right_speed(speed)
66+
# If an amount of time is specified, move for that time and then stop.
67+
if seconds is not None:
68+
time.sleep(seconds)
69+
self.stop()
70+
71+
def steer(self, speed, direction):
72+
# Move forward at the specified speed (0- 1). Direction is +- 1.
73+
# Full left is -1, Full right is +1
74+
if (speed + direction/2) > 1:
75+
speed = speed - direction/2 # calibrate so total motor output never goes above 1
76+
left = speed + direction/2
77+
right = speed - direction/2
78+
self._left_speed(left)
79+
self._right_speed(right)
80+
81+
def backward(self, speed, seconds=None):
82+
"""Move backward at the specified speed (0-255). Will start moving
83+
backward and return unless a seconds value is specified, in which
84+
case the robot will move backward for that amount of time and then stop.
85+
"""
86+
# Set motor speed and move both backward.
87+
self._left_speed(-1*speed)
88+
self._right_speed(-1*speed)
89+
# If an amount of time is specified, move for that time and then stop.
90+
if seconds is not None:
91+
time.sleep(seconds)
92+
self.stop()
93+
94+
95+
def right(self, speed, seconds=None):
96+
"""Spin to the right at the specified speed. Will start spinning and
97+
return unless a seconds value is specified, in which case the robot will
98+
spin for that amount of time and then stop.
99+
"""
100+
# Set motor speed and move both forward.
101+
self._left_speed(speed)
102+
self._right_speed(0)
103+
# If an amount of time is specified, move for that time and then stop.
104+
if seconds is not None:
105+
time.sleep(seconds)
106+
self.stop()
107+
108+
def left(self, speed, seconds=None):
109+
"""Spin to the left at the specified speed. Will start spinning and
110+
return unless a seconds value is specified, in which case the robot will
111+
spin for that amount of time and then stop.
112+
"""
113+
# Set motor speed and move both forward.
114+
self._left_speed(0)
115+
self._right_speed(speed)
116+
# If an amount of time is specified, move for that time and then stop.
117+
if seconds is not None:
118+
time.sleep(seconds)
119+
self.stop()

examples/RobotTest.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Simple two DC motor robot class usage example.
2+
# Author: Tony DiCola, Chris Anderron
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 -0.05, 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+
27+
robot = Robot.Robot(left_trim=LEFT_TRIM, right_trim=RIGHT_TRIM)
28+
29+
# Now move the robot around!
30+
# Each call below takes two parameters:
31+
# - speed: The speed of the movement, a value from -1.0 to +1.0. The higher the value
32+
# the faster the movement. You need to start with a value around 0.10
33+
# to get enough torque to move the robot.
34+
# - time (seconds): Amount of time to perform the movement. After moving for
35+
# this amount of seconds the robot will stop. This parameter
36+
# is optional and if not specified the robot will start moving
37+
# forever.
38+
39+
robot.left(0.5,1)
40+
robot.right(0.5,1)
41+
robot.steer(0.5,0.2)
42+
time.sleep(3)
43+
robot.stop() # Stop the robot from moving.
44+
45+
46+
# That's it! Note that on exit the robot will automatically stop moving.

0 commit comments

Comments
 (0)