Skip to content

Instantly share code, notes, and snippets.

@LarsBergqvist
Last active December 15, 2016 07:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LarsBergqvist/0577927090341eb2c7f38c68ded2e367 to your computer and use it in GitHub Desktop.
Save LarsBergqvist/0577927090341eb2c7f38c68ded2e367 to your computer and use it in GitHub Desktop.
A MicroPython car bot
"""
A bot with servos for an Adafruit Feather Huzzah
Requires the Adafruit PWM servo library:
https://github.com/adafruit/Adafruit-PWM-Servo-Driver-Library
Uses a FeatherWing servo board attached with i2C
"""
import machine
import servo
import utime
from wheels import Wheels
from hammer import Hammer
from claws import Claws
class Bot(object):
"""
A robot car with two continuous servos as wheels
and three standard servos as weapons (hammer and claws)
"""
def __init__(self):
# Setup pins to use for i2c and create the servos object
i2c = machine.I2C(machine.Pin(5), machine.Pin(4))
servos = servo.Servos(i2c)
# Create the the servo controlled parts of the bot
# and assign the control pins to use
self.wheels = Wheels(servos, 0, 1)
self.hammer = Hammer(servos, 5)
self.claws = Claws(servos, 7, 6)
# A mapping between each command and the
# corresponding method on the servo objects
self.action_methods = {
'F' : self.wheels.fw_step,
'B' : self.wheels.back_step,
'L' : self.wheels.turn_left,
'R' : self.wheels.turn_right,
'S' : self.wheels.stop,
'U' : self.hammer.up,
'D' : self.hammer.down,
'O' : self.claws.open,
'C' : self.claws.close
}
def run(self, sequence):
"""
Input is a string where each character
represents an action. Each action is executed according
to the defined action method mapping
"""
for action in sequence:
if action in self.action_methods:
self.action_methods[action]()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment