raid23
Posts: 24
Joined: Tue Dec 19, 2017 7:26 am

gpio.cleanup() not clean enough?

Wed Jan 03, 2018 8:09 pm

I'm trying to control a linear actuator with my Pi, but having some trouble with managing output signal.
The program itself is as follows:

Code: Select all

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import RPi.GPIO as gpio
import time

actuator1_in1 = 17
actuator1_in2 = 22

def gpio_init():
    gpio.setmode(gpio.BCM)
    gpio.setup(actuator1_in1, gpio.OUT)
    gpio.setup(actuator1_in2, gpio.OUT)
    
def open_hatch(sec):
    gpio.output(actuator1_in1, gpio.LOW)
    gpio.output(actuator1_in2, gpio.LOW)
    cnt = 1
    while cnt < 2:
        cnt += 1
        gpio.output(actuator1_in2, gpio.HIGH)
        time.sleep(sec)

def main():
      sec = input("Keep it running for (seconds): ", )
      open_hatch(sec)
      
if __name__ == "__main__":
	try:
                gpio_init()
		main()
	except:
		print("Something went wrong")
	finally:
                gpio.cleanup()


What I'm trying to do here: ask from the user for how many seconds to run the motor, in function open_hatch make sure pins are set to False before setting gpio22 to True, hold it for a certain count of seconds, clean up the pins, exit program.

What really happens: when the program is executed, a numerical input is prompted, after I insert, say, 3, engine runs for 3 seconds and program stops. So far, so good. But when I execute it for the second time, while I'm prompted for numerical input, the engine starts running backward without me entering anything.

Why so? I expected that gpio.cleanup() takes care of setting all used pins to default again. How come it starts running at once? And where comes the knowledge to move in opposite direction?

As I'm new to both RPi and programming, in general, all other suggestions how to operate my motor more politely are more than welcome.

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

Re: gpio.cleanup() not clean enough?

Wed Jan 03, 2018 8:54 pm

Without knowing the operation specifications of your actuator my advice would be to replace the actuator with 2 LED's connected gpio output pin -470 ohm resistor - LED - ground pin. One each of your outputs.

Then run your program and watch the LED's and see if they do as you expect.
If you see the expected output each time you run the program then its going to be the actuator that's responding to your program in a way you did not expect.
In which case you need to do some testing with manual inputs on you actuator to find out why it responds in this way
Please only ask questions in the forum I will not answer questions sent to me directly via PM
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

raid23
Posts: 24
Joined: Tue Dec 19, 2017 7:26 am

Re: gpio.cleanup() not clean enough?

Wed Jan 03, 2018 9:57 pm

Unfortunately, I do not have any LEDs handy at the moment but I'll try to do that.

But I'm still suspicious it has something to do with output control by Pi.. When I execute the program once, reboot Pi and then execute once more, there's no such phenomenon - the actuator moves both times for the inserted count of seconds in the expected direction.

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

Re: gpio.cleanup() not clean enough?

Wed Jan 03, 2018 10:13 pm

When you set the GPIO as outputs in gpio.init() they will be set to the last requested level. Either set them both to low (or high) in gpio.init() or set them both to low (or high) before gpio.cleanup().

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

Re: gpio.cleanup() not clean enough?

Wed Jan 03, 2018 10:56 pm

Having tested your code I added the line to turn in2 back to low after your time.sleep so things work correctly, but I still can't see how this would make your actuator run backwards as you were still giving it the command to run forwards !

Code: Select all

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import RPi.GPIO as gpio
import time

actuator1_in1 = 17
actuator1_in2 = 22

def gpio_init():
    gpio.setmode(gpio.BCM)
    gpio.setup(actuator1_in1, gpio.OUT)
    gpio.setup(actuator1_in2, gpio.OUT)
    
def open_hatch(sec):
    gpio.output(actuator1_in1, gpio.LOW)
    gpio.output(actuator1_in2, gpio.LOW)
    cnt = 1
    while cnt < 2:
        cnt += 1
        gpio.output(actuator1_in2, gpio.HIGH)
        time.sleep(sec)
        gpio.output(actuator1_in2, gpio.LOW)

def main():
    sec = input("Keep it running for (seconds): ", )
    open_hatch(sec)
      
if __name__ == "__main__":
    try:
        gpio_init()
        main()
    except:
        print("Something went wrong")
    finally:
        gpio.cleanup()
        
Please only ask questions in the forum I will not answer questions sent to me directly via PM
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

raid23
Posts: 24
Joined: Tue Dec 19, 2017 7:26 am

Re: gpio.cleanup() not clean enough?

Thu Jan 04, 2018 8:14 am

Thanks!

HInts from both replies are useful and make my program working as expected.
Although, I still do not understand what exactly is the purpose of cleanup() function if the next time gpio pins are set, last requested level is used again. I'd say pins are not "clean enough" :)
What concerns actuator's weird behavior (going backward for some reason), it's still a mystery (when switching the direction, actuator doesn't move backward, but all the way to the end on the next execution), but setting inputs to LOW again works well for me.

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

Re: gpio.cleanup() not clean enough?

Thu Jan 04, 2018 1:34 pm

I agree with you regarding GPIO.cleanup() I always thought it reset the gpio back to default as if the pi had just rebooted, don't see the point of it if it does not, all it will stop is the message about gpio already in use which can be silenced anyway.
Please only ask questions in the forum I will not answer questions sent to me directly via PM
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

supra
Posts: 845
Joined: Thu Feb 28, 2013 9:21 am
Location: Ontario, Canada

Re: gpio.cleanup() not clean enough?

Fri Jan 05, 2018 2:33 am

@raid23: Which linear actuator model are you using?

User avatar
Rbert
Posts: 11
Joined: Mon May 01, 2017 8:57 pm
Location: Amsterdam

Re: gpio.cleanup() not clean enough?

Sun Jan 07, 2018 9:23 pm

How is the actuator exactly connected? Was there any scematic provided to you which you used as a reference?

Return to “Python”

Who is online

Users browsing this forum: No registered users and 10 guests