silver2row

From books to turning!

Jul 31st, 2021
759
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # From the book, "The BeagleBone Black Primer, (McLaughlin 2016)."
  2. # Trying currently to set up Step_PIN2 and Dir_PIN2 for usage on one Stepper...
  3.  
  4. import Adafruit_BBIO.GPIO as GPIO
  5. from time import sleep
  6. import ctypes
  7.  
  8. libc = ctypes.CDLL('libc.so.6')
  9.  
  10. Step_PIN1 = 'P9_16'
  11. Dir_PIN1  = 'P8_18'
  12.  
  13. Step_PIN2 = 'P9_14'
  14. Dir_PIN2  = 'P8_16'
  15.  
  16. MicroSteps = 8
  17.  
  18. def move(steps, speed):
  19.     if steps > 0:
  20.         GPIO.output(Dir_PIN1, GPIO.HIGH)
  21.     else:
  22.         GPIO.output(Dir_PIN1, GPIO.LOW)
  23.  
  24.     steps = abs(steps)
  25.  
  26.     delay = ((float(speed) / 2) / MicroSteps) * 1000
  27.  
  28.     steps_remaining = steps
  29.     while steps_remaining > 0:
  30.         GPIO.output(Step_PIN1, GPIO.HIGH)
  31.         libc.usleep(int(delay))
  32.  
  33.         GPIO.output(Step_PIN1, GPIO.LOW)
  34.         libc.usleep(int(delay))
  35.  
  36.         steps_remaining = steps_remaining - 1
  37.  
  38. def move_degrees(degrees, speed, degrees_per_step=1.8, usteps_per_step=8):
  39.     steps = degrees / (degrees_per_step / usteps_per_step)
  40.     move(steps, speed)
  41.  
  42. if __name__=='__main__':
  43.     GPIO.setup(Step_PIN1, GPIO.OUT)
  44.     GPIO.output(Step_PIN1, GPIO.LOW)
  45.  
  46.     GPIO.setup(Dir_PIN1, GPIO.OUT)
  47.     GPIO.output(Dir_PIN1, GPIO.HIGH)
  48.  
  49.     try:
  50.         move_degrees(90, 100)
  51.         sleep(2)
  52.         move_degrees(-90, 100)
  53.         sleep(2)
  54.         move_degrees(360, 10)
  55.         sleep(2)
  56.         move_degrees(-360, 2.64)
  57.         sleep(2)
  58.         move_degrees(360 * 2, 3)
  59.         sleep(2)
  60.     except KeyboardInterrupt:
  61.         pass
  62.  
  63.     finally:
  64.         GPIO.output(Step_PIN1, GPIO.LOW)
  65.         GPIO.cleanup()
RAW Paste Data