Skip to content

Instantly share code, notes, and snippets.

@ShawnHymel
Created September 9, 2016 17:25
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 ShawnHymel/d61e838f32fe0bef24a208131c8ed6cc to your computer and use it in GitHub Desktop.
Save ShawnHymel/d61e838f32fe0bef24a208131c8ed6cc to your computer and use it in GitHub Desktop.
Test of the H-Bridge Block with the Intel Edison
import mraa
import time
# Motor parameters. Set to 1 or -1 to change default direction
dirA = 1
dirB = 1
# PWM A (pin 12) is on pin 20 in MRAA
pwmA = mraa.Pwm(20)
pwmA.period_us(1000)
pwmA.enable(True)
# PWM B (pin 13) is on pin 14 in MRAA
pwmB = mraa.Pwm(14)
pwmB.period_us(1000)
pwmB.enable(True)
# Direction pins A1 and A2 are on GPIO 48 and 47 (33 and 46 in MRAA)
a1 = mraa.Gpio(33)
a1.dir(mraa.DIR_OUT)
a1.write(1)
a2 = mraa.Gpio(46)
a2.dir(mraa.DIR_OUT)
a2.write(1)
# Direction pins B1 and B2 are on GPIO 15 and 14 (48 and 36 in MRAA)
b1 = mraa.Gpio(48)
b1.dir(mraa.DIR_OUT)
b1.write(1)
b2 = mraa.Gpio(36)
b2.dir(mraa.DIR_OUT)
b2.write(1)
# Standby pin is GPIO 49 (47 in MRAA)
standby = mraa.Gpio(47)
standby.dir(mraa.DIR_OUT)
standby.write(1)
# Put the motor driver into standby mode
def standby(mode):
if standby:
standby.write(0)
else:
standby.write(1)
# Differential drive. A and B can be -1 to 1.
def diffDrive(speedA, speedB):
# Make sure the speeds are within the bounds
if speedA < -1.0:
speedA = -1.0
if speedA > 1.0:
speedA = 1.0
if speedB < -1.0:
speedB = -1.0
if speedB > 1.0:
speedB = 1.0
# Set motor speeds
speedA = dirA * speedA
speedB = dirB * speedB
if speedA < 0:
a1.write(0)
a2.write(1)
pwmA.write(abs(speedA))
else:
a1.write(1)
a2.write(0)
pwmA.write(speedA)
if speedB < 0:
b1.write(0)
b2.write(1)
pwmB.write(abs(speedB))
else:
b1.write(1)
b2.write(0)
pwmB.write(speedB)
# Test
diffDrive(1.0, 1.0)
time.sleep(1.0)
diffDrive(0.0, 0.6)
time.sleep(1.0)
diffDrive(-0.8, 0.8)
time.sleep(1.0)
diffDrive(-0.7, -0.7)
time.sleep(1.0)
diffDrive(0.0, 0.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment