0

i want to programming my robotic arm (5 servo motors) with python ,so i wrote a python code but my problem is that when i try the code lines separately from terminal window it works fine and robot moves like i want , but when run it directly as a python code ,robot doesn't move correctly (the movement is very simple it just pick up an object and put it in a container ) .So can you please help me and check my code if there any thing wrong ,here is my code

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)

GPIO.setup(11,GPIO.OUT)
GPIO.setup(15,GPIO.OUT)
GPIO.setup(16,GPIO.OUT)
GPIO.setup(22,GPIO.OUT)
GPIO.setup(7,GPIO.OUT)
p11=GPIO.PWM(11,50)
p15=GPIO.PWM(15,50)
p16=GPIO.PWM(16,50)
p22=GPIO.PWM(22,50)
p7=GPIO.PWM(7,50)
p11.start(5)
p15.start(5)
p16.start(5)
p22.start(5)

p7.start(5)
time.sleep(1)
try:
  while True:

    p11.ChangeDutyCycle(10)
    p15.ChangeDutyCycle(10)
    p16.ChangeDutyCycle(5)
    p22.ChangeDutyCycle(7.5)

    p7.ChangeDutyCycle(5)
    p7.ChangeDutyCycle(10)
    p22.ChangeDutyCycle(10)

    p16.ChangeDutyCycle(10)
    p11.ChangeDutyCycle(5)
    p16.ChangeDutyCycle(5)
    p22.ChangeDutyCycle(7.5)
    p7.ChangeDutyCycle(5)
    p7.ChangeDutyCycle(10)

    p15.ChangeDutyCycle(5)

    time.sleep(1)
except KeyboardInterrupt:
  p11.stop()
  p15.stop()
  p16.stop()
  p22.stop()
  p7.stop()
GPIO.cleanup()
  • RPi.GPIO is really not suitable for servos. It's PWM (servo control pulses) is software timed. You might find one servo acceptable using RPi.GPIO. Why not use a module which provides hardware timed PWM like RPIO.GPIO or my pigpio? – joan Aug 10 '17 at 7:43

Your Answer

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

Browse other questions tagged or ask your own question.