Page 1 of 1

servo control

Posted: Wed Jun 12, 2019 11:14 pm
by lclark2074
Ok I am trying to make two servos to work not together or at the same time with python 3.5. for right now I am to get the first servo to work.
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
servoPin=13 #the pin using
servoPin1=15
GPIO.setup(servoPin, GPIO.OUT)#the pin using
GPIO.setup(servoPin1, GPIO.OUT)
pwm=GPIO.PWM(servoPin,50)#the pin using
pwm1=GPIO.PWM(servoPin1,50)
pwm.start(7)

def green_light():#the pin using
for i in range(0,20):
desiredPosition=input("where do you want the servo? 0-180: ")
x = int(desiredPosition)#put in for virsion3.5
DC=1./18.*(x)+2#put in for virsion3.5
pwm.ChangeDutyCycle(DC)
pwm.stop()
pwm1.start(7)
def red_light():
for i in range(0,20):
desiredPosition=input("where do you want the servo2? 0-180 ")
T = int (desiredPosition)
DC=1./18.*(T)+2
pwm.ChangeDutyCycle(DC)
pwm1.stop()
test=input("do you want to calabrate start servo y or n: ")#selects corect servo
if test[:1] == 'y':
green_light()
elif test[:1] == 'n':
red_light()
GPIO.cleanup()

#xString = input("Enter a number: ")
#x = int(xString)
#yString = input("Enter a second number: ")
#y = int(yString)
#print('The sum of ', x, ' and ', y, ' is ', x+y, '.', sep='')

Re: servo control

Posted: Wed Jun 12, 2019 11:20 pm
by lclark2074
what happened to formatting

Re: servo control

Posted: Wed Jun 12, 2019 11:43 pm
by Andyroo
lclark2074 wrote:
Wed Jun 12, 2019 11:20 pm
what happened to formatting
You have to use the code tags around the text. This is entered by the </> button above the entry box. Pop your code in them and it will keep the spaces.

You can also quote the original message and add code marks around that.

Re: servo control

Posted: Wed Jun 12, 2019 11:44 pm
by Andyroo
lclark2074 wrote:
Wed Jun 12, 2019 11:14 pm
Ok I am trying to make two servos to work not together or at the same time with python 3.5. for right now I am to get the first servo to work.

Code: Select all

import RPi.GPIO as  GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False) 
servoPin=13 #the pin using
servoPin1=15
GPIO.setup(servoPin, GPIO.OUT)#the pin using
GPIO.setup(servoPin1, GPIO.OUT)
pwm=GPIO.PWM(servoPin,50)#the pin using
pwm1=GPIO.PWM(servoPin1,50)
pwm.start(7)

def green_light():#the pin using
    for i in range(0,20):
        desiredPosition=input("where do you want  the servo? 0-180:  ")
        x = int(desiredPosition)#put in for virsion3.5
        DC=1./18.*(x)+2#put in for virsion3.5
        pwm.ChangeDutyCycle(DC)
pwm.stop()
pwm1.start(7)
def red_light():
    for i in range(0,20):
        desiredPosition=input("where do you want  the servo2? 0-180  ")
        T = int (desiredPosition) 
        DC=1./18.*(T)+2
        pwm.ChangeDutyCycle(DC)
pwm1.stop()
test=input("do you want to calabrate start servo y or n:  ")#selects corect servo
if test[:1] == 'y': 
        green_light()   
elif test[:1] == 'n':
        red_light()
GPIO.cleanup()

#xString = input("Enter a number: ")
#x = int(xString)
#yString = input("Enter a second number: ")
#y = int(yString)
#print('The sum of ', x, ' and ', y, ' is ', x+y, '.', sep='')

Re: servo control

Posted: Thu Jun 13, 2019 12:29 pm
by pcmanbob
So the first piece of advice is to not use pwm.stop(), there is a bug in the RPI.GPIO code that results in pwm not working if you use pwm.stop() until you reboot the pi.

So you have told us what you are trying to do but not what your problem is ? unless we have some idea what problem you have we can help.

I have edited your code and added some print statements, I also change your DC= lines as 2.5 is about the minimum setting for your average servo.

Code: Select all

import RPi.GPIO as  GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False) 
servoPin=13 #the pin using
servoPin1=15
GPIO.setup(servoPin, GPIO.OUT)#the pin using
GPIO.setup(servoPin1, GPIO.OUT)
pwm=GPIO.PWM(servoPin,50)#the pin using
pwm1=GPIO.PWM(servoPin1,50)
pwm.start(7)
pwm1.start(7)

def green_light():#the pin using
    for i in range(0,20):
        desiredPosition=input("where do you want  the servo? 0-180:  ")
        x = int(desiredPosition)#put in for virsion3.5
        DC=1./18.*(x)+2.5 #put in for virsion3.5
        print ("Green ",DC)
        pwm.ChangeDutyCycle(DC)


def red_light():
    for i in range(0,20):
        desiredPosition=input("where do you want  the servo2? 0-180  ")
        T = int (desiredPosition) 
        DC=1./18.*(T)+2.5
        print("red ",DC)
        pwm.ChangeDutyCycle(DC)
        

test=input("do you want to calabrate start servo y or n:  ")#selects corect servo
if test[:1] == 'y': 
        green_light()   
elif test[:1] == 'n':
        red_light()
GPIO.cleanup()

#xString = input("Enter a number: ")
#x = int(xString)
#yString = input("Enter a second number: ")
#y = int(yString)
#print('The sum of ', x, ' and ', y, ' is ', x+y, '.', sep='')
so running your code ( no servos attached ) seems to output sensible duty cycle values, so I can't actually see what your problem might be, unless it was just the pwm.stop() preventing your servo's from working.

Re: servo control

Posted: Thu Jun 13, 2019 10:58 pm
by lclark2074
thank you Andyroo for how to properly show me the right formatting.
thank you pcmanbob I tried the changes to script and it worked.
I have 1 servo conected to pin 13. Tomorrow I will try to put the next servo to pin 15. and see if it works.
my problem was that the first servo was not moving and now it is. thank you again

Code: Select all

 import RPi.GPIO as  GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False) 
servoPin=13 #the pin using
servoPin1=15
GPIO.setup(servoPin, GPIO.OUT)#the pin using
GPIO.setup(servoPin1, GPIO.OUT)
pwm=GPIO.PWM(servoPin,50)#the pin using
pwm1=GPIO.PWM(servoPin1,50)
pwm.start(7)
pwm1.start(7)

def green_light():#the pin using
    for i in range(0,20):
        desiredPosition=input("where do you want  the servo? 0-180:  ")
        x = int(desiredPosition)#put in for virsion3.5
        DC=1./18.*(x)+2.5 #put in for virsion3.5
        print ("Green ",DC)
        pwm.ChangeDutyCycle(DC)


def red_light():
    for i in range(0,20):
        desiredPosition=input("where do you want  the servo2? 0-180  ")
        T = int (desiredPosition) 
        DC=1./18.*(T)+2.5
        print("red ",DC)
        pwm.ChangeDutyCycle(DC)
        

test=input("do you want to calabrate start servo y or n:  ")#selects corect servo
if test[:1] == 'y': 
        green_light()   
elif test[:1] == 'n':
        red_light()
GPIO.cleanup()

Re: servo control

Posted: Fri Jun 14, 2019 9:24 pm
by lclark2074
Ok I got the second servo to work and I changed the code a little

Code: Select all

import RPi.GPIO as  GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False) 
servoPin1=13 #servo 1
servoPin2=15 
GPIO.setup(servoPin1, GPIO.OUT) #servo 1
GPIO.setup(servoPin2, GPIO.OUT) #servo 2
pwm1=GPIO.PWM(servoPin1,50) #servo 1
pwm2=GPIO.PWM(servoPin2,50) #servo 2
pwm1.start(7)
pwm2.start(7)

def green_light():#the pin using
    for i in range(0,20):
        desiredPosition=input("where do you want  the servo? 0-180:  ") #servo 1
        x = int(desiredPosition) #servo 1
        DC=1./18.*(x)+2.5 
        print ("Green ",DC)
        pwm1.ChangeDutyCycle(DC)


def red_light():
    for i in range(0,20):
        desiredPosition=input("where do you want  the servo2? 0-180  ") #servo 2
        T = int (desiredPosition) 
        DC=1./18.*(T)+2.5
        print("red ",DC)
        pwm2.ChangeDutyCycle(DC)


test=input("do you want to calabrate start servo1 y or n:  ") #selects servo1
if test[:1] == 'y': 
        green_light()
best=input("do you want to calabrate start servo2 y or n:  ") #selects servo2  
if best[:1] == 'y':
        red_light()




Re: servo control

Posted: Sun Jun 16, 2019 2:47 pm
by lclark2074
Ok I got both servos and a relay to work. But I am having trouble with the inputs with pwm in the same script.
This is a setup script for 1 of the pi"s. For testing I need to make sure all the pin IO's work. This is why extra dead ends in the script.

Code: Select all

import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False) 
servoPin1=13 #servo 1
servoPin2=15 #servo 1
relay=11
BUTTON1=16
BUTTON2=18
GPIO.setup(BUTTON1,GPIO.IN,pull_up_down=GPIO.PUD_DOWN) #BUTTON1
GPIO.setup(BUTTON2,GPIO.IN,pull_up_down=GPIO.PUD_UP) #BUTTON2
BUTTON1=False #BUTTON1
BUTTON2=False #BUTTON2
GPIO.setup(servoPin1, GPIO.OUT) #servo 1
GPIO.setup(servoPin2, GPIO.OUT) #servo 2
GPIO.setup(relay,GPIO.OUT) #relay
pwm1=GPIO.PWM(servoPin1,50) #servo 1
pwm2=GPIO.PWM(servoPin2,50) #servo 2
pwm1.start(7)
pwm2.start(7)

def green_light(): #the pin using
    for i in range(0,20):
        desiredPosition=input("where do you want  the servo? 0-180:  ") #servo 1
        x = int(desiredPosition) #servo 1
        DC=1./18.*(x)+2.5 
        print ("Green ",DC)
        pwm1.ChangeDutyCycle(DC)

def red_light():
    for i in range(0,20):
        desiredPosition=input("where do you want  the servo2? 0-180  ") #servo 2
        T = int (desiredPosition) 
        DC=1./18.*(T)+2.5
        print("red ",DC)
        pwm2.ChangeDutyCycle(DC)

def blue_light():#for the relay
    blink_num=4
    for i in range(0,blink_num):
        GPIO.output(11,True)
        print ("blue on")
        sleep(3)
        GPIO.output(11,False)
        print ("blue off")
        sleep(1)
        
def orange_light():#for the BUTTON1

    while (True):
        if GPIO.input (BUTTON1)==1:
            print("on") 
            #GPIO.output(LED1,True)
            sleep(.2)
        else:
            if GPIO.input (BUTTON1)==0:
                #GPIO.output(LED1,False)
                print("off")
                sleep(.2)
       
def brown_light():#for the BUTTON2
    while (True):
        if GPIO.input (BUTTON2)==0:
            #GPIO.output(LED1,True)
            print("on")
            sleep(.2)
        else:
            if GPIO.input (BUTTON2)==1:
                #GPIO.output(LED1,False)
                print("off")
                sleep(.2)
        
test=input("do you want to calabrate start servo1 y or n:  ") #selects servo1
if test[:1] == 'y': 
    green_light()
best=input("do you want to calabrate start servo2 y or n:  ") #selects servo2  
if best[:1] == 'y':
    red_light()
lest=input("do you want to check the relay y or n:  ") #selects relay 
if lest[:1] == 'y':
    blue_light()
lest=input("do you want to check input n y or n:  ") #selects BUTTON1
if lest[:1] == 'y':
    orange_light()
lest=input("do you want to check input p y or n:  ") #selects BUTTON2 
if lest[:1] == 'y':
    brown_light()
    
do you want to check input n y or n: y
Traceback (most recent call last):
File "/home/pi/Desktop/starter_setup.py", line 84, in <module>
orange_light()
File "/home/pi/Desktop/starter_setup.py", line 51, in orange_light
if GPIO.input (BUTTON1)==1:
ValueError: The channel sent is invalid on a Raspberry Pi

Re: servo control

Posted: Sun Jun 16, 2019 2:54 pm
by pcmanbob
Edit.
opps missed the end of your post comes from viewing on a tablet while doing other things.

Edit2.

so back on my pc running your code on a pi reveals the problem.

Code: Select all

BUTTON1=16 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> set variable here to 16
BUTTON2=18
GPIO.setup(BUTTON1,GPIO.IN,pull_up_down=GPIO.PUD_DOWN) #BUTTON1 #>>> set up input here using gpio 16
GPIO.setup(BUTTON2,GPIO.IN,pull_up_down=GPIO.PUD_UP) #BUTTON2
BUTTON1=False #BUTTON1 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> reset variable to false
BUTTON2=False #BUTTON2
So you set up BUTTON1 to be the variable for the gpio your are going to use for that input , gpio16 in this case, then you set up the but then you use the BUTTON1 variable again and you set it to false so when you later call your function orange_light() and you r program attempts to read a gpio pin instead of your BUTTON1 = 16 for gpio 16 it equal false, hence your error.

Re: servo control

Posted: Sun Jun 16, 2019 3:10 pm
by B.Goode

Code: Select all

Traceback (most recent call last):
File "/home/pi/Desktop/starter_setup.py", line 84, in <module>
orange_light()
File "/home/pi/Desktop/starter_setup.py", line 51, in orange_light
if GPIO.input (BUTTON1)==1:
ValueError: The channel sent is invalid on a Raspberry Pi


"The channel sent is invalid"

GPIO.input() needs to be told which gpio pin to get the input level from. So it needs to be a number referring to a valid gpio pin.

What do you think is represented by BUTTON1 when you use it to refer to a pin? Perhaps debug it by printing its value just before you use it. Maybe it isn't what you think? Perhaps something has changed it from your expectations?

Re: servo control

Posted: Sun Jun 16, 2019 3:42 pm
by lclark2074
GPIO.input() needs to be told which gpio pin to get the input level from. So it needs to be a number referring to a valid gpio pin.

What do you think is represented by BUTTON1 when you use it to refer to a pin? Perhaps debug it by printing its value just before you use it. Maybe it isn't what you think? Perhaps something has changed it from your expectations?

THANK YOU B.Goode I am being an Idiot. I puled line 9 and line 10

Code: Select all

BUTTON1=False #BUTTON1
BUTTON2=False #BUTTON2
and the script works now.