Skip to content

Instantly share code, notes, and snippets.

@ditwoo
Created May 23, 2022 21:15
Show Gist options
  • Save ditwoo/69858fb23b1e8803ecbcae74ac300f3c to your computer and use it in GitHub Desktop.
Save ditwoo/69858fb23b1e8803ecbcae74ac300f3c to your computer and use it in GitHub Desktop.
import Jetson.GPIO as GPIO
import time
class Motor:
def __init__(self, ena, in1, in2):
self.ena = ena
self.in1 = in1
self.in2 = in2
def __enter__(self):
GPIO.setup(self.ena, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(self.in1, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(self.in2, GPIO.OUT, initial=GPIO.LOW)
GPIO.output(self.ena, GPIO.HIGH)
GPIO.output(self.in1, GPIO.LOW)
GPIO.output(self.in2, GPIO.LOW)
return self
def __exit__(self, exc_type, exc_value, exc_traceback):
GPIO.output(self.ena, GPIO.LOW)
GPIO.output(self.in1, GPIO.LOW)
GPIO.output(self.in2, GPIO.LOW)
def stop(self):
GPIO.output(self.in1, GPIO.LOW)
GPIO.output(self.in2, GPIO.LOW)
def backward(self):
GPIO.output(self.in1, GPIO.LOW)
GPIO.output(self.in2, GPIO.HIGH)
def forward(self):
GPIO.output(self.in1, GPIO.HIGH)
GPIO.output(self.in2, GPIO.LOW)
def main():
GPIO.setwarnings(False)
# set pin numbers to the board's
GPIO.setmode(GPIO.BOARD)
with Motor(ena=37, in1=35, in2=33) as lmoto, Motor(ena=36, in1=38, in2=40) as rmoto:
rmoto.forward()
lmoto.forward()
time.sleep(2.5)
rmoto.backward()
lmoto.backward()
time.sleep(2.5)
GPIO.cleanup()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment