Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- # Use this idea w/ making motors go in reverse...
- import Adafruit_BBIO.GPIO as GPIO
- import Adafruit_BBIO.PWM as PWM
- class Motor:
- def __init__( self, dir_pin, pwm_pin, pwm_freq ):
- self.dir_pin = dir_pin
- self.pwm_pin = pwm_pin
- self.value = 0
- PWM.start( pwm_pin, 0, pwm_freq )
- GPIO.setup( dir_pin, GPIO.OUT )
- def set( self, value ):
- assert -100 <= value <= 100
- if ( value < 0 ) != ( self.value < 0 ):
- # changing direction
- PWM.set_duty_cycle( self.pwm_pin, 0 )
- GPIO.output( self.dir_pin, value < 0 )
- PWM.set_duty_cycle( self.pwm_pin, abs( value ) )
- self.value = value
- motor1 = Motor( dir_pin = "P8_18", pwm_pin = "P9_16", pwm_freq = 2000 )
- motor2 = Motor( dir_pin = "P8_16", pwm_pin = "P9_14", pwm_freq = 2000 )
- motor3 = Motor( dir_pin = "P8_14", pwm_pin = "P8_13", pwm_freq = 2000 )
- motor4 = Motor( dir_pin = "P8_26", pwm_pin = "P8_19", pwm_freq = 2000 )
- # simple test
- import time
- motor1.set( 100 ) # forward full speed
- time.sleep( 1 )
- motor1.set( 50 ) # forward half speed
- time.sleep( 1 )
- motor1.set( 0 ) # stop
- time.sleep( 1 )
- motor1.set( -50 ) # reverse half speed
- time.sleep( 1 )
- motor1.set( 0 ) # stop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement