silver2row

Here Number Two

Feb 2nd, 2021 (edited)
859
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.12 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. from flask import Flask, render_template
  4. import Adafruit_BBIO.GPIO as GPIO
  5. import Adafruit_BBIO.PWM as PWM
  6. from time import sleep
  7.  
  8. class Motor:
  9.     def __init__(self, dir_pin, pwm_pin, pwm_freq):
  10.         self.dir_pin = dir_pin
  11.         self.pwm_pin = pwm_pin
  12.         self.value = 0
  13.  
  14.         PWM.start(pwm_pin, 0, pwm_freq)
  15.         GPIO.setup(dir_pin, GPIO.OUT)
  16.  
  17.     def set(self, value):
  18.         if value == self.value:
  19.             return
  20.  
  21.         assert -100 <= value <= 100
  22.  
  23.         if (value < 0) != (self.value < 0):
  24.             # changing direction
  25.             PWM.set_duty_cycle(self.pwm_pin, 0)
  26.             GPIO.output(self.dir_pin, value < 0)
  27.  
  28.         PWM.set_duty_cycle(self.pwm_pin, abs(value))
  29.         self.value = value
  30.  
  31. motor1 = Motor(dir_pin="P8_18", pwm_pin="P9_16", pwm_freq=1000)
  32. # motor2 = Motor(dir_pin="P8_16", pwm_pin="P9_14", pwm_freq=1000)
  33. # motor3 = Motor(dir_pin="P8_14", pwm_pin="P8_13", pwm_freq=1000)
  34. # motor4 = Motor(dir_pin="P8_26", pwm_pin="P8_19", pwm_freq=1000)
  35.  
  36. def set_motorOne(v1):
  37.     motor1.set(v1)
  38.  
  39. ...
  40.  
  41. v1("0",   0)
  42. v1('40', 40)
  43. v1('50', 50)
  44.  
  45. ...
Add Comment
Please, Sign In to add comment