Merge pull request #288 from dastels/master

Code for IrRobotControl guide
This commit is contained in:
Mike Barela 2018-08-07 11:09:33 -04:00 committed by GitHub
commit a06829524e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 195 additions and 0 deletions

82
IrRobotControl/glove.py Normal file
View file

@ -0,0 +1,82 @@
import time
import busio
import board
import pulseio
import adafruit_irremote
import adafruit_lis3dh
# Control codes
STOP = 0x01
ROTATE_LEFT = 0x02
ROTATE_RIGHT = 0x03
FORWARD = 0x04
FORWARD_LEFT = 0x05
FORWARD_RIGHT = 0x06
REVERSE = 0x07
REVERSE_LEFT = 0x08
REVERSE_RIGHT = 0x09
TRANSMIT_DELAY = 0.1
# Setup accelerometer
i2c = busio.I2C(board.ACCELEROMETER_SCL, board.ACCELEROMETER_SDA)
sensor = adafruit_lis3dh.LIS3DH_I2C(i2c, address=0x19)
# Create a 'pulseio' output, to send infrared signals on the IR transmitter @ 38KHz
pwm = pulseio.PWMOut(board.IR_TX, frequency=38000, duty_cycle=2 ** 15)
pulseout = pulseio.PulseOut(pwm)
# Create an encoder that will take numbers and turn them into IR pulses
encoder = adafruit_irremote.GenericTransmit(header=[9500, 4500],
one=[550, 550],
zero=[550, 1700],
trail=0)
# pylint: disable=unused-argument
# pylint: disable=pointless-statement
def log(s):
"""Optionally output some text.
:param string s: test to output
"""
# swap the comments on the next two lines to enable/disable logging output
pass
# print(s)
# pylint: enable=pointless-statement
# pylint: enable=unused-argument
while True:
x, y, z = sensor.acceleration
log("{0: 0.3f} {1: 0.3f} {2: 0.3f}".format(x, y, z))
if z < -5.0 and abs(y) < 3.0: # palm down
if x < -5.0: # tipped counterclockwise
log("ROTATE_LEFT")
encoder.transmit(pulseout, [ROTATE_LEFT] * 4)
elif x > 5.0: # tipped clockwise
log("ROTATE_RIGHT")
encoder.transmit(pulseout, [ROTATE_RIGHT] * 4)
else: # level
log("STOP")
encoder.transmit(pulseout, [STOP] * 4)
elif y > 5.0: # palm facing away
if x < -5.0: # tipped counterclockwise
log("FORWARD_LEFT")
encoder.transmit(pulseout, [FORWARD_LEFT] * 4)
elif x > 5.0: # tipped clockwise
log("FORWARD_RIGHT")
encoder.transmit(pulseout, [FORWARD_RIGHT] * 4)
else: # straight up
log("FORWARD")
encoder.transmit(pulseout, [FORWARD] * 4)
elif y < -5.0: # palm facing toward (hand down)
if x < -5.0: # tipped counterclockwise
log("REVERSE_RIGHT")
encoder.transmit(pulseout, [REVERSE_RIGHT] * 4)
elif x > 5.0: # tipped clockwise
log("REVERSE_LEFT")
encoder.transmit(pulseout, [REVERSE_LEFT] * 4)
else: #straight down
log("REVERSE")
encoder.transmit(pulseout, [REVERSE] * 4)
time.sleep(TRANSMIT_DELAY)

113
IrRobotControl/robot.py Normal file
View file

@ -0,0 +1,113 @@
import board
import pulseio
import adafruit_irremote
from adafruit_crickit import crickit
# Control codes
STOP = 0x01
ROTATE_LEFT = 0x02
ROTATE_RIGHT = 0x03
FORWARD = 0x04
FORWARD_LEFT = 0x05
FORWARD_RIGHT = 0x06
REVERSE = 0x07
REVERSE_LEFT = 0x08
REVERSE_RIGHT = 0x09
left_wheel = crickit.dc_motor_1
right_wheel = crickit.dc_motor_2
# pylint: disable=unused-argument
# pylint: disable=pointless-statement
def log(s):
"""Optionally output some text.
:param string s: test to output
"""
# swap the comments on the next two lines to enable/disable logging output
pass
# print(s)
# pylint: enable=pointless-statement
# pylint: enable=unused-argument
# These allow easy correction for motor speed variation.
# Factors are determined by observation and fiddling.
# Start with both having a factor of 1.0 (i.e. none) and
# adjust until the bot goes more or less straight
def set_right(speed):
right_wheel.throttle = speed * 0.9
def set_left(speed):
left_wheel.throttle = speed
# Uncomment this to find the above factors
# set_right(1.0)
# set_left(1.0)
# while True:
# pass
# Create a 'pulseio' input, to listen to infrared signals on the IR receiver
pulsein = pulseio.PulseIn(board.IR_RX, maxlen=120, idle_state=True)
# Create a decoder that will take pulses and turn them into numbers
decoder = adafruit_irremote.GenericDecode()
while True:
# Listen for incoming IR pulses
pulses = decoder.read_pulses(pulsein)
# Try and decode them
try:
# Attempt to convert received pulses into numbers
received_code = decoder.decode_bits(pulses)
except adafruit_irremote.IRNECRepeatException:
# We got an unusual short code, probably a 'repeat' signal
log("NEC repeat!")
continue
except adafruit_irremote.IRDecodeException as e:
# Something got distorted or maybe its not an NEC-type remote?
log("Failed to decode: {}".format(e.args))
continue
if received_code == [STOP] * 4:
log("STOP")
set_left(0.0)
set_right(0.0)
elif received_code == [ROTATE_LEFT] * 4:
log("ROTATE_LEFT")
set_left(-0.25)
set_right(0.25)
elif received_code == [ROTATE_RIGHT] * 4:
log("ROTATE_RIGHT")
set_left(0.25)
set_right(-0.25)
elif received_code == [FORWARD] * 4:
log("FORWARD")
set_left(0.5)
set_right(0.5)
elif received_code == [FORWARD_LEFT] * 4:
log("FORWARD_LEFT")
set_left(0.0)
set_right(0.5)
elif received_code == [FORWARD_RIGHT] * 4:
log("FORWARD_RIGHT")
set_left(0.5)
set_right(0.0)
elif received_code == [REVERSE] * 4:
log("REVERSE")
set_left(-0.5)
set_right(-0.5)
elif received_code == [REVERSE_LEFT] * 4:
log("REVERSE_LEFT")
set_left(-0.25)
set_right(-0.5)
elif received_code == [REVERSE_RIGHT] * 4:
log("REVERSE_RIGHT")
set_left(-0.5)
set_right(-0.25)
else:
log("UNKNOWN")