Advertisement
silver2row

MakingActuatorsReverseInMotion...

Sep 30th, 2020
1,553
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.27 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. # Use this idea w/ making motors go in reverse...
  4.  
  5. import Adafruit_BBIO.GPIO as GPIO
  6. import Adafruit_BBIO.PWM as PWM
  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.         assert -100 <= value <= 100
  19.         if ( value < 0 ) != ( self.value < 0 ):
  20.             # changing direction
  21.             PWM.set_duty_cycle( self.pwm_pin, 0 )
  22.             GPIO.output( self.dir_pin, value < 0 )
  23.         PWM.set_duty_cycle( self.pwm_pin, abs( value ) )
  24.         self.value = value
  25.  
  26. motor1 = Motor( dir_pin = "P8_18", pwm_pin = "P9_16", pwm_freq = 2000 )
  27. motor2 = Motor( dir_pin = "P8_16", pwm_pin = "P9_14", pwm_freq = 2000 )
  28. motor3 = Motor( dir_pin = "P8_14", pwm_pin = "P8_13", pwm_freq = 2000 )
  29. motor4 = Motor( dir_pin = "P8_26", pwm_pin = "P8_19", pwm_freq = 2000 )
  30.  
  31.  
  32. # simple test
  33. import time
  34. motor1.set( 100 )  # forward full speed
  35. time.sleep( 1 )
  36. motor1.set( 50 )  # forward half speed
  37. time.sleep( 1 )
  38. motor1.set( 0 )  # stop
  39. time.sleep( 1 )
  40. motor1.set( -50 )  # reverse half speed
  41. time.sleep( 1 )
  42. motor1.set( 0 )  # stop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement