lucasgg
Posts: 6
Joined: Mon Mar 26, 2018 2:21 pm

Pi Servo Control - Stops Listening to Input

Wed Apr 11, 2018 3:54 pm

Using this script, pi stops listening to input immediately. The servo continues to hold/readjust its position, however.

Code: Select all

import os
from os import system 
import RPi.GPIO as GPIO
import time
from time import sleep

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(3, GPIO.OUT)
pwm=GPIO.PWM(3, 50)
pwm.start(0)

while True:
    input = GPIO.input(16)
    if (input == True):
         system('/home/pi/Desktop/175Degrees.py')
        duty = 175 / 18 + 2
        GPIO.output(3, True)
        pwm.ChangeDutyCycle(duty)
        sleep(0)
        pwm.ChangeDutyCycle(3, False)
        pwm.changeDutyCycle(0)
    else:
        system('/home/pi/Desktop/90Degrees.py')
        duty = 60 / 18 + 2
        GPIO.output(3, True)
        pwm.ChangeDutyCycle(duty)
        sleep(0)
        pwm.ChangeDutyCycle(3, False)
        pwm.changeDutyCycle(0)


pwm.stop()
GPIO.cleanup()  

*however* when I use this basic input listening script, it never stops listening in the time i have tested it.

Code: Select all

import RPi.GPIO as GPIO
import time
from time import sleep

GPIO.setmode(GPIO.BOARD)
GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)


while True:
    input = GPIO.input(16)
    if (input == True):
        print("input true")
    else:
        print("input false")

I am a beginner programmer
Last edited by lucasgg on Thu Apr 12, 2018 1:33 pm, edited 4 times in total.

saltydog
Posts: 39
Joined: Mon Dec 24, 2012 10:40 am

Re: Pi Servo Control - Stops Listening to I`nput

Wed Apr 11, 2018 10:53 pm

lucasgg wrote:
Wed Apr 11, 2018 3:54 pm
Using this script, pi stops listening to input immediately. The servo continues to hold/readjust its position, however.
If the servo is still working (moving), then it is still listening, isn't it?

Chris

User avatar
joan
Posts: 14472
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Pi Servo Control - Stops Listening to I`nput

Wed Apr 11, 2018 10:59 pm

Python scripts need to be entered within code quotes for clarity, otherwise the formatting is lost.

pcmanbob
Posts: 7264
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Pi Servo Control - Stops Listening to I`nput

Thu Apr 12, 2018 9:45 am

copying your code from the quote screen and placing it between code tags

Code: Select all

import os
from os import system 
import RPi.GPIO as GPIO
import time
from time import sleep

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(3, GPIO.OUT)
pwm=GPIO.PWM(3, 50)
pwm.start(0)

while True:
    input = GPIO.input(16)
    if (input == True):
         system('/home/pi/Desktop/175Degrees.py')
        duty = 175 / 18 + 2
        GPIO.output(3, True)
        pwm.ChangeDutyCycle(duty)
        sleep(0)
        pwm.ChangeDutyCycle(3, False)
        pwm.changeDutyCycle(0)
    else:
        system('/home/pi/Desktop/90Degrees.py')
        duty = 60 / 18 + 2
        GPIO.output(3, True)
        pwm.ChangeDutyCycle(duty)
        sleep(0)
        pwm.ChangeDutyCycle(3, False)
        pwm.changeDutyCycle(0)


pwm.stop()
GPIO.cleanup()  


import RPi.GPIO as GPIO
import time
from time import sleep

GPIO.setmode(GPIO.BOARD)
GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)


while True:
    input = GPIO.input(16)
    if (input == True):
        print("input true")
    else:
        print("input false")
reveals an indent error on your first system line.
my first question is what are the 2 python programs doing that you are calling using system ? if these never completed then your program will never return to continue with the code following them.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

lucasgg
Posts: 6
Joined: Mon Mar 26, 2018 2:21 pm

Re: Pi Servo Control - Stops Listening to I`nput

Thu Apr 12, 2018 1:31 pm

saltydog wrote:
Wed Apr 11, 2018 10:53 pm
lucasgg wrote:
Wed Apr 11, 2018 3:54 pm
Using this script, pi stops listening to input immediately. The servo continues to hold/readjust its position, however.
If the servo is still working (moving), then it is still listening, isn't it?

Chris
When the script is running, if I change the input, the servo does not respond or change its angle based off of the new input value.

lucasgg
Posts: 6
Joined: Mon Mar 26, 2018 2:21 pm

Re: Pi Servo Control - Stops Listening to I`nput

Thu Apr 12, 2018 1:41 pm

pcmanbob wrote:
Thu Apr 12, 2018 9:45 am
copying your code from the quote screen and placing it between code tags

Code: Select all

import os
from os import system 
import RPi.GPIO as GPIO
import time
from time import sleep

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(3, GPIO.OUT)
pwm=GPIO.PWM(3, 50)
pwm.start(0)

while True:
    input = GPIO.input(16)
    if (input == True):
         system('/home/pi/Desktop/175Degrees.py')
        duty = 175 / 18 + 2
        GPIO.output(3, True)
        pwm.ChangeDutyCycle(duty)
        sleep(0)
        pwm.ChangeDutyCycle(3, False)
        pwm.changeDutyCycle(0)
    else:
        system('/home/pi/Desktop/90Degrees.py')
        duty = 60 / 18 + 2
        GPIO.output(3, True)
        pwm.ChangeDutyCycle(duty)
        sleep(0)
        pwm.ChangeDutyCycle(3, False)
        pwm.changeDutyCycle(0)


pwm.stop()
GPIO.cleanup()  


import RPi.GPIO as GPIO
import time
from time import sleep

GPIO.setmode(GPIO.BOARD)
GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)


while True:
    input = GPIO.input(16)
    if (input == True):
        print("input true")
    else:
        print("input false")
reveals an indent error on your first system line.
my first question is what are the 2 python programs doing that you are calling using system ? if these never completed then your program will never return to continue with the code following them.

Please describe what a system line is. This will be a long journey of asking dumb questions for the sake of learning.

I probably should mention that I do not completely understand the from "x" import "x". I have figured out that it brings in a "part" of the operating system so that the code can interact properly. A brief explanation would be appreciated.

User avatar
B.Goode
Posts: 8987
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Pi Servo Control - Stops Listening to Input

Thu Apr 12, 2018 1:59 pm

Please describe what a system line is.
It is the line beginning system in your code...

Code: Select all

    input = GPIO.input(16)
    if (input == True):
         system('/home/pi/Desktop/175Degrees.py')
        duty = 175 / 18 + 2
        GPIO.output(3, True)

User avatar
B.Goode
Posts: 8987
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Pi Servo Control - Stops Listening to Input

Thu Apr 12, 2018 2:39 pm

I do not completely understand the from "x" import "x". I have figured out that it brings in a "part" of the operating system so that the code can interact properly. A brief explanation would be appreciated.
As a brief explanation what you have written is probably sufficient.

Rather than getting half-truths, if you need more detail it is probably best to refer to the official python documentation:
https://docs.python.org/3/tutorial/modules.html

It is unlikely that you will understand everything there at a first reading. (I certainly don't, after several attempts...)

lucasgg
Posts: 6
Joined: Mon Mar 26, 2018 2:21 pm

Re: Pi Servo Control - Stops Listening to Input

Thu Apr 12, 2018 3:06 pm

B.Goode wrote:
Thu Apr 12, 2018 1:59 pm
Please describe what a system line is.
It is the line beginning system in your code...

Code: Select all

    input = GPIO.input(16)
    if (input == True):
         system('/home/pi/Desktop/175Degrees.py')
        duty = 175 / 18 + 2
        GPIO.output(3, True)
Oh, shoot! I forgot to remove that. I was experimenting if I could call on a separate python script to change the servo angle. It didn't work.

pcmanbob
Posts: 7264
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Pi Servo Control - Stops Listening to Input

Thu Apr 12, 2018 3:10 pm

lucasgg wrote:
Thu Apr 12, 2018 3:06 pm
B.Goode wrote:
Thu Apr 12, 2018 1:59 pm
Please describe what a system line is.
It is the line beginning system in your code...

Code: Select all

    input = GPIO.input(16)
    if (input == True):
         system('/home/pi/Desktop/175Degrees.py')
        duty = 175 / 18 + 2
        GPIO.output(3, True)
Oh, shoot! I forgot to remove that. I was experimenting if I could call on a separate python script to change the servo angle. It didn't work.
Then you may want to remove the second one as well that further down your code.

Code: Select all

    else:
        system('/home/pi/Desktop/90Degrees.py')
        
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
B.Goode
Posts: 8987
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Pi Servo Control - Stops Listening to Input

Thu Apr 12, 2018 3:12 pm

You could add some lines similar to:

Code: Select all

print("Switch was True")
at appropriate places to track the execution of your script.

Or you could try running it with the Thonny IDE/editor which has some built in tracing to assist with debugging.

What is the intended purpose of the sleep(0) statements?

pcmanbob
Posts: 7264
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Pi Servo Control - Stops Listening to Input

Thu Apr 12, 2018 3:26 pm

So now I have had time to test your code I have removed all the errors including the lines that were not required or in error.

Code: Select all


import RPi.GPIO as GPIO
import time
from time import sleep

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(3, GPIO.OUT)
pwm=GPIO.PWM(3, 50)
pwm.start(0)

while True:
    input = GPIO.input(16)
    if (input == True):
        
        duty = 175 / 18 + 2 
        pwm.ChangeDutyCycle(duty)

    else:
        
        duty = 60 / 18 + 2
        pwm.ChangeDutyCycle(duty)
    sleep (0.1)
the code now works in that you have 2 servo positions selected by pressing the switch.

by way of explanation.

pwm.ChangeDutyCycle(3, False) you only specify the new duty cycle with this command it has no true or false, removed lines.

GPIO.output(3, True) seeing as you are using this output for PWM you don't want to be setting it high or low as well as it will interfere with your servo pwm signal, lines removed.

sleep(0) pointless command so removed, but you do need a sleep to slow down your code so it does not run the processor to 100% usage, so added a single sleep (0.1) command in the while true loop.

pwm.stop(), GPIO.cleanup() removed these line as they would never be executed because you program had a while true loop.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

lucasgg
Posts: 6
Joined: Mon Mar 26, 2018 2:21 pm

Re: Pi Servo Control - Stops Listening to Input

Mon Apr 16, 2018 8:49 pm

B.Goode wrote:
Thu Apr 12, 2018 3:12 pm
You could add some lines similar to:

Code: Select all

print("Switch was True")
at appropriate places to track the execution of your script.

Or you could try running it with the Thonny IDE/editor which has some built in tracing to assist with debugging.

What is the intended purpose of the sleep(0) statements?
Truly, I was experimenting with what affect they had on the code.

lucasgg
Posts: 6
Joined: Mon Mar 26, 2018 2:21 pm

Re: Pi Servo Control - Stops Listening to Input

Mon Apr 16, 2018 8:57 pm

pcmanbob wrote:
Thu Apr 12, 2018 3:26 pm
So now I have had time to test your code I have removed all the errors including the lines that were not required or in error.

Code: Select all


import RPi.GPIO as GPIO
import time
from time import sleep

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(3, GPIO.OUT)
pwm=GPIO.PWM(3, 50)
pwm.start(0)

while True:
    input = GPIO.input(16)
    if (input == True):
        
        duty = 175 / 18 + 2 
        pwm.ChangeDutyCycle(duty)

    else:
        
        duty = 60 / 18 + 2
        pwm.ChangeDutyCycle(duty)
    sleep (0.1)
the code now works in that you have 2 servo positions selected by pressing the switch.

by way of explanation.

pwm.ChangeDutyCycle(3, False) you only specify the new duty cycle with this command it has no true or false, removed lines.

GPIO.output(3, True) seeing as you are using this output for PWM you don't want to be setting it high or low as well as it will interfere with your servo pwm signal, lines removed.

sleep(0) pointless command so removed, but you do need a sleep to slow down your code so it does not run the processor to 100% usage, so added a single sleep (0.1) command in the while true loop.

pwm.stop(), GPIO.cleanup() removed these line as they would never be executed because you program had a while true loop.
I implemented your kind changes and the code is working exactly as desired. Now I will move into edge-detection to ease up on my processor. After all, I want this script to be running for long periods at a time.

Return to “Python”