I want to control 2 servos (Tower Pro SG90) with Python and Adafruit_BBIO (v. 0.20) on Debian 3.8.13-bone50.
I’ve made a script that continuously moves the servo. Everything works well with either of the servos (assuming you only work with one servo). But when I try to make movements with both servos in the same script, the first servo makes moves, and the script crashes after it tries to control the second servo. I get: “RuntimeError: You must start() the PWM channel first”.
Anyone knows what could be wrong?
`
import Adafruit_BBIO.PWM as PWM
import sys, time
servo_pin = “P8_13”
servo2_pin = “P8_19”
duty_min = 83.5
duty_max = 95.5
duty_span = duty_max - duty_min
PWM.start(servo_pin, (100-duty_min), 60.0)
PWM.start(servo2_pin, (100-duty_min), 60.0)
def cleanup():
print “Cleaning up”
PWM.stop(servo_pin)
PWM.stop(servo2_pin)
PWM.cleanup()
sys.exit(0)
def move_to(servo, angle):
angle_f = float(angle)
duty = 100 - ((angle_f / 180) * duty_span + duty_min)
print "servo: " + servo + " duty: " + str(duty)
PWM.set_duty_cycle(servo, duty)
time.sleep(2)
while True:
try:
print “start”
move_to(servo_pin, 180)
move_to(servo_pin, 90)
move_to(servo_pin, 0)
move_to(servo_pin, 90)
move_to(servo2_pin, 180)
move_to(servo2_pin, 90)
move_to(servo2_pin, 0)
move_to(servo2_pin, 90)
except KeyboardInterrupt as e:
print e
cleanup()
`
root@beaglebone:/home/debian/servo# python 1.py
start
servo: P8_13 duty: 4.5
servo: P8_13 duty: 10.5
servo: P8_13 duty: 16.5
servo: P8_13 duty: 10.5
servo: P8_19 duty: 4.5
Traceback (most recent call last):
File “1.py”, line 36, in
move_to(servo2_pin, 180)
File “1.py”, line 25, in move_to
PWM.set_duty_cycle(servo, duty)
RuntimeError: You must start() the PWM channel first
root@beaglebone:/home/debian/servo#