Advertisement
silver2row

Testing the BOT!

Aug 8th, 2019
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.28 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. # Use this idea w/ making motors go in reverse...
  4. # with help from #beagle on Freenode
  5.  
  6. import Adafruit_BBIO.GPIO as GPIO
  7. import Adafruit_BBIO.PWM as PWM
  8.  
  9. class Motor:
  10.     def __init__( self, dir_pin, pwm_pin, pwm_freq ):
  11.         self.dir_pin = dir_pin
  12.         self.pwm_pin = pwm_pin
  13.         self.value = 0
  14.  
  15.         PWM.start( pwm_pin, 0, pwm_freq )
  16.         GPIO.setup( dir_pin, GPIO.OUT )
  17.  
  18.     def set( self, value ):
  19.         assert -100 <= value <= 100
  20.         if ( value < 0 ) != ( self.value < 0 ):
  21.             # changing direction
  22.             PWM.set_duty_cycle( self.pwm_pin, 0 )
  23.             GPIO.output( self.dir_pin, value < 0 )
  24.         PWM.set_duty_cycle( self.pwm_pin, abs( value ) )
  25.         self.value = value
  26.  
  27. motor1 = Motor( dir_pin="P8_18", pwm_pin="P9_16", pwm_freq=2000 )
  28. motor2 = Motor( dir_pin="P8_16", pwm_pin="P9_14", pwm_freq=2000 )
  29. motor3 = Motor( dir_pin="P8_14", pwm_pin="P8_13", pwm_freq=2000 )
  30. motor4 = Motor( dir_pin="P8_26", pwm_pin="P8_19", pwm_freq=2000 )
  31.  
  32.  
  33. # simple test
  34. import time
  35. motor1.set( 100 )  # forward full speed
  36. time.sleep( 1 )
  37. motor1.set( 50 )  # forward half speed
  38. time.sleep( 1 )
  39. motor1.set( 0 )  # stop
  40. time.sleep( 1 )
  41. motor1.set( -50 )  # reverse half speed
  42. time.sleep( 1 )
  43. motor1.set( 0 )  # stop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement