pinstripebob
Posts: 6
Joined: Mon Feb 19, 2018 4:23 pm
Location: Indiana, USA

Programming an emergency stop button

Mon Feb 19, 2018 4:40 pm

Hi all,

I'm making a little automated process that activates a processor, lowers parts into it, waits for a period, deactivates the processor, and raises the parts. It is activated by pressing a switch (in real life it's actually 2 buttons and a safety switch), which is GPIO 2.

I'd like to add a physical stop button local to the process in case I want to interrupt it and change some parameters. I have the stop button wired to ground and GPIO 17 right now. I've added code at line 31 for the stop button, and I've tweaked it a few times, but it doesn't seem to want to work for me. The button I purchased I'm using is an e-stop button I had laying around, so it is normally open, and when pressed is closed. The button stays closed until you twist it and it resets.

The problem I'm having is it doesn't seem to work correctly, and triggers the process to stop when it shouldn't. Is it possible to get some guidance on how to properly implement this? The code is below. Thank you!

Code: Select all

from gpiozero import LED, Button
import RPi.GPIO as GPIO
from time import sleep
import sys

signal = LED(26)		##This is signal to turn on processor.  !!THIS WORKS BACKWARDS!!
down = LED(20)		##This is signal to move parts into bath.
switch = Button(2)		##This is safety switch & 2 buttons in series.

GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN)
while True:
    signal.on()		##Makes sure processor is off.
    stop_state = GPIO.input(17)		##State of the stop button, true or false.
    process = 480		##This is the process time.
    print("Ready to begin next batch")
    switch.wait_for_press()		##Wait and activate process.
    print("Process has begun")
    signal.off()		##Turn on processor.
    print("Power supply activating")
    print("Please wait 3 seconds")
    sleep(3)		##Delay before lowering parts.
    down.on()		##Lower parts.
    while process > 10:		##Two while loops are for visual timer.
        m, s = divmod(process, 60)
        h, m = divmod(m, 60)
        time_left = str(h).zfill(2) + ":" + str(m).zfill(2) + ":" + str(s).zfill(2)
        print(time_left + " remaining")
        counter1 = 0
        while counter1 < 10:
            if GPIO.input(17):
                signal.on()		##Deactivate processor.
                sleep(6)		##Delay.
                down.off()		##Raise parts.
                print("Process stopped, please reset to continue")
                quit
                sys.exit()
            else:
                sleep(1)
                counter1 += 1
        process -= 10
    while process > 0:
        m, s = divmod(process, 60)
        h, m = divmod(m, 60)
        time_left = str(h).zfill(2) + ":" + str(m).zfill(2) + ":" + str(s).zfill(2)
        print(time_left + " remaining")
        sleep(1)
        process -= 1
    signal.on()		##Turn off processor.
    sleep(6)		##Delay.
    down.off()		##Raise parts.
    print("Process is complete")
    print("Please wait 5 seconds before restarting")
    sleep(5)		##Delay.


pinstripebob
Posts: 6
Joined: Mon Feb 19, 2018 4:23 pm
Location: Indiana, USA

Re: Programming an emergency stop button

Mon Feb 19, 2018 8:21 pm

I made some progress in the right direction. I put the entire code in a try/except loop. The issue I'm having with this is my "except" isn't working properly. If I code except KeyboardInterrupt: it works, but GPIO 17 isn't triggering it.

Code: Select all

from gpiozero import LED, Button
import RPi.GPIO as GPIO
from time import sleep
import sys

signal = LED(26)       ##Signal to turn on the processor.  !!THIS WORKS BACKWARDS!!
down = LED(20)       ##Signal to move the parts into the bath.
switch = Button(2)  ##This is the safety switch & 2 buttons in series.

GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
try:
    while True:
        signal.on()                                  ##Makes sure processor is off.
        process = 480                            ##This is the process time.
        print("Ready to begin next batch")
        switch.wait_for_press()          ##Wait and activate process.
        print("Process has begun")
        signal.off()                                 ##Turn on processor.
        print("Power supply activating")
        print("Please wait 3 seconds")
        sleep(3)                                     ##Delay before lowering parts.
        down.on()                                 ##Lower parts.
        while process > 10:                 ##Printed timer, 10s interval.
            m, s = divmod(process, 60)
            h, m = divmod(m, 60)
            time_left = str(h).zfill(2) + ":" + str(m).zfill(2) + ":" + str(s).zfill(2)
            print(time_left + " remaining")
			sleep(10)
            process -= 10
        while process > 0:                  ##Printed timer, 1s interval.
            m, s = divmod(process, 60)
            h, m = divmod(m, 60)
            time_left = str(h).zfill(2) + ":" + str(m).zfill(2) + ":" + str(s).zfill(2)
            print(time_left + " remaining")
            sleep(1)
            process -= 1
        signal.on()                             ##Turn off processor.
        sleep(6)                                 ##Delay.
        down.off()                             ##Raise parts.
        print("Process is complete")
        print("Please wait 5 seconds before restarting")
        sleep(5)                                 ##Delay.
except GPIO.input(17) == True:
    signal.on()                                ##Deactivate processor.
    sleep(6)                                    ##Delay.
    down.off()                                ##Raise parts.
    print("Process stopped, please reset to continue")
    quit
    GPIO.cleanup()
    sys.exit()
finally:
    GPIO.cleanup()
    

Idahowalker
Posts: 297
Joined: Wed Jan 03, 2018 5:43 pm

Re: Programming an emergency stop button

Tue Feb 20, 2018 1:09 pm

Code: Select all

switch = Button(2)  ##This is the safety switch & 2 buttons in series.
From reading the above, by wiring 2 buttons in series, 2 buttons have to be pressed for the safety to activate; an AND gate. Why not wire the buttons in parallel, an OR gate?
Being a programmer: She says go to the store get 1 bottle of milk, if they have eggs bring back 6. She gets mad cause I brought back 6 bottles of milk.

pinstripebob
Posts: 6
Joined: Mon Feb 19, 2018 4:23 pm
Location: Indiana, USA

Re: Programming an emergency stop button

Tue Feb 20, 2018 2:46 pm

That function acts as the start button and works well, the three physical switches/buttons require a very explicit start to begin the process.

I'm looking to incorporate an additional stop button, I need help with that implementation.

User avatar
bensimmo
Posts: 3120
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Programming an emergency stop button

Tue Feb 20, 2018 3:12 pm

Why are you useing RPi.GPIO when using gpiozero ?

pinstripebob
Posts: 6
Joined: Mon Feb 19, 2018 4:23 pm
Location: Indiana, USA

Re: Programming an emergency stop button

Tue Feb 20, 2018 3:15 pm

This is my first time using a Raspberry Pi. Based on what I read from some tutorials and some forums, gpiozero got the code working in its simpler state to start the process, and now from examples I have seen RPi.GPIO was used to generate a stop button.

I can switch to one or the other if that would improve the code and make my stop button functional.

User avatar
bensimmo
Posts: 3120
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Programming an emergency stop button

Tue Feb 20, 2018 3:27 pm

Stick to one and learn the methods, it just means you don't cross and mess up each others 'setup states' especially if you're not sure what ti is doing.

https://gpiozero.readthedocs.io/en/stab ... tml#button

All about gpiozero buttons.
gpiozero does a lot of the hard work for you.
use

stop_button=Button(17)

stop_button.is_pressed

to see if has been pressed (for "GPIO - push to make circuit - GND" buttons)

then either run it in Thonny full and monitor and 'Step by step debug' through the process, you can keep track of all the variables in a side window.
you'll learn when you can use step over (things liek print if they work)

Or print out your variables and counters as you go.

pinstripebob
Posts: 6
Joined: Mon Feb 19, 2018 4:23 pm
Location: Indiana, USA

Re: Programming an emergency stop button

Tue Feb 20, 2018 5:15 pm

That was some pretty good advice, I went ahead and stuck with the gpiozero since that worked for the main portion of the code. I added a bunch of print() statements so I could follow where things were while I tried a few different logic scenarios.

I had issues with the try/except/finally not working as expected, and the program has to explicitly check to see if the stop button has been pressed, however it does now work. Luckily this is not a safety-related button.

I've attached the code below for reference in case someone does a Google search and wants to see what I've done. I defined a program in the main code, and I do an if logic test to see if the button is active (stop.is_active). If it is, I run the defined program called stopprogram().

Code: Select all

from gpiozero import LED, Button
from time import sleep
import sys

signal = LED(26)       ##Signal to turn on the processor.  !!THIS WORKS BACKWARDS!!
down = LED(20)       ##Signal to move the parts.
switch = Button(2)  ##This is the start button.
stop = Button(17)  ##This is the stop button.

def stopprogram():
    print("Stop pressed, please wait 6 seconds")
    signal.on()                                ##Turn off processor.
    sleep(6)                                    ##Delay to let processor quit before raising parts.
    print("Raising parts")
    down.off()                                ##Raise parts.
    print("Please close this window and run the program again")
    sys.exit()                                ##Quit entire .py program

if stop.is_active == False:
    while stop.is_active == False:
        signal.on()                                  ##Makes sure processor is off.
        var = input('Please enter number of minutes to run process: ')
        process = int(var) * 60            ##This is the process time.
        print("Ready to begin, please lower sash and press the start buttons")
        switch.wait_for_press()          ##Wait and activate process.
        print("Process has begun")
        signal.off()                                 ##Turn on processor.
        print("Power supply activating, please wait 3 seconds")
        sleep(3)                                     ##Delay to let processor boot up before lowering parts.
        down.on()                                 ##Lower parts.
        while process > 10:                 ##Printed timer, 10s interval.
            m, s = divmod(process, 60)
            h, m = divmod(m, 60)
            time_left = str(h).zfill(2) + ":" + str(m).zfill(2) + ":" + str(s).zfill(2)
            print(time_left + " remaining")
            counter1 = 0
            while counter1 < 10:
                if stop.is_active == True:
                    stopprogram()
                sleep(1)
                counter1 += 1
            process -= 10
        while process > 0:                  ##Printed timer, 1s interval, final 10 seconds.
            m, s = divmod(process, 60)
            h, m = divmod(m, 60)
            time_left = str(h).zfill(2) + ":" + str(m).zfill(2) + ":" + str(s).zfill(2)
            print(time_left + " remaining")
            if stop.is_active == True:
                stopprogram()
            sleep(1)
            process -= 1
        signal.on()                             ##Turn off processor.
        print("Please wait 6 seconds")
        sleep(6)                                 ##Delay to let processor quit before raising parts.
        down.off()                             ##Raise parts.
        print("Process is complete, please wait 5 seconds before restarting")
        sleep(5)                                 ##Delay.
else:
    stopprogram()


Idahowalker
Posts: 297
Joined: Wed Jan 03, 2018 5:43 pm

Re: Programming an emergency stop button

Tue Feb 20, 2018 5:31 pm

Raspberry Pi- GPIO Events in Python
https://stackoverflow.com/questions/161 ... -in-python
Being a programmer: She says go to the store get 1 bottle of milk, if they have eggs bring back 6. She gets mad cause I brought back 6 bottles of milk.

Return to “Python”

Who is online

Users browsing this forum: Baidu [Spider] and 18 guests