0

I have written a small program that controls two servos: to start the recording process one servo opens a shutter (servo1), then the second one lifts a sky camera out of a housing (servo2). To stop the recording process, the described process has to be completed in the reverse direction.

My issue with code is, that if I only run open_s or close_s, it works fine, but if I run both, the second function does not run correctly (servo1 no longer closes the shutter), but there's also no error message. Any idea why this could be?

Many thanks for any hints!

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
#set up servos
GPIO.setup(12,GPIO.OUT)
servo1 = GPIO.PWM(12,50)

GPIO.setup(13,GPIO.OUT)
servo2 = GPIO.PWM(13,50)

def open_s():
    #run servos
    servo1.start(0)
    servo2.start(0)

    servo1.ChangeDutyCycle(6.1)
    time.sleep(2)
    servo1.stop()

    servo2.ChangeDutyCycle(6.5)
    time.sleep(2)

def close_s():
    servo1.start(0)
    servo2.start(0)

    servo2.ChangeDutyCycle(5.5)
    time.sleep(2)
    servo2.stop()

    servo1.ChangeDutyCycle(3.4)
    time.sleep(2)
    servo1.stop()

open_s()
close_s() #<- This function no longer runs servo1, when I run it in combination with open_s()

GPIO.cleanup()
1

I have no idea why the code does not work. Perhaps look through the issues to see if it is a known problem.

Perhaps use pigpio.

sudo pigpiod # start daemon

pigs s 12 1220 mils 2000 s 13 1300 # open

pigs s 13 1100 mils 2000 s 12 680  # close
3
  • Thanks for your feedback. Can I start the daemon using python or does it always to be done in the terminal?
    – VBA Pete
    Aug 7 at 11:15
  • You could start the daemon in Python. You could start it as a service at boot with the one off command sudo systemctl enable pigpiod
    – joan
    Aug 7 at 11:26
  • I couldn"t get the deamon to work, because I already have another deamon running. However, splitting up the two functions into separate python files and calling them from there fixed it. Thanks again for your help!
    – VBA Pete
    Aug 7 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.