Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
TX-PI-HAT Class
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
import pigpio
class TXPIMotor():
def set_IN(self, pin, value):
self.pi.write(pin, value)
def get_freq(self):
return self.pi.get_PWM_frequency(self.pwm)
def set_freq(self, value):
self.freq = value
self.pi.set_PWM_frequency(self.pwm, self.freq)
def get_duty(self):
return self.pi.get_PWM_dutycycle(self.pwm)
def set_duty(self, value):
self.dutycycle = value
self.pi.set_PWM_dutycycle(self.pwm, value)
def set_mode(self, mode):
bits = { "Off": [ pigpio.LOW, pigpio.LOW ],
"Left": [ pigpio.HIGH, pigpio.LOW ],
"Right": [ pigpio.LOW, pigpio.HIGH ],
"Brake": [ pigpio.HIGH, pigpio.HIGH ] }
self.pi.write(self.in1, bits[mode][0]);
self.pi.write(self.in2, bits[mode][1]);
def __init__(self, in1, in2, pwm, freq, dutycycle, pi):
self.in1 = in1
self.in2 = in2
self.pwm = pwm
self.freq = freq
self.dutycycle = dutycycle
self.pi = pi
self.set_freq(self.freq)
self.set_duty(self.dutycycle)
self.pi.set_mode(self.in1, pigpio.OUTPUT)
self.pi.set_mode(self.in2, pigpio.OUTPUT)
self.set_IN(self.in1, pigpio.LOW)
self.set_IN(self.in2, pigpio.LOW)
class TXPIOutput():
def __init__(self, pin, pi):
self.pi = pi
self.pin = pin
pi.set_mode(self.pin, pigpio.OUTPUT)
def set_value(self, value):
self.pi.write(self.pin, value)
class TXPIInput():
def __init__(self, pin, pi):
self.pi = pi
self.pin = pin
pi.set_mode(self.pin, pigpio.INPUT)
def get_value(self):
return 100
class TXPICounter():
def __init__(self, pin, pi):
self.counter = pi.callback(pin, pigpio.FALLING_EDGE)
def get_value(self):
pass
def clear(self):
pass
def enabeld(self):
pass
def callback(event, tick):
print(event, tick)
class TXPIHat():
PINS = { "I1": 12, "I2": 16, "I3": 20, "I4": 21,
"STBY": 19,
"AIN1": 23, "AIN2": 22, "PWMA": 18,
"BIN1": 5, "BIN2": 6, "PWMB": 13 }
def motor_stby(self, value):
self.STBY.set_value(value)
def motor_enabled(self, value):
if value:
self.motor_stby(1)
else:
self.motor_stby(0)
def __init__(self):
try:
self.pi = pigpio.pi()
# configure I1..I4 as input
self.I1 = TXPIInput(self.PINS["I1"], self.pi)
self.I2 = TXPIInput(self.PINS["I2"], self.pi)
self.I3 = TXPIInput(self.PINS["I3"], self.pi)
self.I4 = TXPIInput(self.PINS["I4"], self.pi)
# confugure I2 as counter
self.C1 = TXPICounter(self.PINS["I1"], self.pi)
self.C2 = TXPICounter(self.PINS["I2"], self.pi)
# power up h bridge for M1 and M2
self.STBY = TXPIOutput(self.PINS["STBY"], self.pi)
self.STBY.set_value(pigpio.LOW)
# ---------------- M1 -----------------------
# configure h bridge
self.M1 = TXPIMotor(self.PINS["BIN1"], self.PINS["BIN2"], self.PINS["PWMB"], 200, 50, self.pi)
# ---------------- M2 -----------------------
# configure h bridge
self.M2 = TXPIMotor(self.PINS["AIN1"], self.PINS["AIN2"], self.PINS["PWMA"], 200, 50, self.pi)
self.ok = True
except Exception as e:
self.ok = False
self.err = str(e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment