Python Forum
Halting if command if gpio triggered event triggered
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Halting if command if gpio triggered event triggered
#6
(Jun-13-2021, 01:50 PM)deanhystad Wrote: Only allow motion when the associated limit switch is not pressed.
stepper1 = None
stepper2 = None
while True:
    if keyboard.is_pressed('f') and not button12.is_pressed:
        stepper1 = stepper.BACKWARD

    elif keyboard.is_pressed('b') and not button13.is_pressed:
        stepper1 = stepper.FORWARD
    else:
        stepper1 = None

    if keyboard.is_pressed('r') and not button27.is_pressed:
        stepper2 = stepper.BACKWARD

    elif keyboard.is_pressed('l') and not button5.is_pressed:
        stepper2 = stepper.FORWARD
    else:
        stepper2 = None


    if stepper1:
       kit1.stepper1.onestep(stepper1, style=stepper.DOUBLE)

    if stepper2:
       kit1.stepper2.onestep(stepper2, style=stepper.DOUBLE)
I'm not sure if I have the stepper/key/button mapping correct. It would be a lot easier to keep things straight if you added a little structure to the code. In this example I use a DOF class to to keep the pertinent information for a stepper motor organized.
class DOF():
    """A single degree of freedom.
    stepper - The stepper motor I control
    forward - Function returns True if forward motion is requested
    backward - Function returns True if backward motion is requested
    fwd_limit - Function returns True if forward limit is reached
    bwd_limit - Function returns True if backward limit is reached
    """
    def __init__(self, stepper, forward, backward, fwd_limit=None, bwd_limit=None):
        self.stepper = stepper
        self.fowrard = forward
        self.backward = backward
        self.forward_limit = fwd_limit
        self.backward_limit = bwd_limit

    def move(self):
        """Move the stepper if move is requested and not prevented by limits"""
        if self.forward():
            if self.forward_limit is None or not self.forward_limit():
                self.stepper.onestep(direction=stepper.FORWARD, style=stepper.DOUBLE)

        if self.backward():
            if self.backward_limit is None or not self.backward_limit():
                self.stepper.onestep(direction=stepper.BACKWARD, style=stepper.DOUBLE)

arm = DOF(
    stepper=kit.stepper1,
    forward=lambda:keyboard.is_pressed('f'),
    backward=lambda:keyboard.is_pressed('b'),
    fwd_limit=lambda:Button(12).is_pressed,
    bwd_limit=lambda:Button(13).is_pressed
)

leg = DOF(
    stepper=kit.stepper1,
    forward=lambda:keyboard.is_pressed('l'),
    backward=lambda:keyboard.is_pressed('r'),
    fwd_limit=lambda:Button(27).is_pressed,
    bwd_limit=lambda:Button(5).is_pressed
)

while True:
    arm.move()
    leg.move()

The only way to move things is to use a for I in loop. The command syntax is very specific.
https://learn.adafruit.com/adafruit-dc-a...spberry-pi
Basically you are commanding a pca9685 chip using a library interface over i2c and it only responds a certain way using the library command framework as input.

It took me a while to figure that out for multiple controls and many more to find a compatible gpio control setup that actually worked. Too much in the python arena is changing and some stuff that worked once is not working now. Plus the pi OS updates for the 4 and whatever else is coming down the pike also throws a wrench in things if I go to recreate this months or years later. Then I throw a monkey wrench as I do not have proper schooling and just read what I can get a hold of and try to stick to a syntax that works for me. Once I get it working completely I then add loads of comments for documentation.

The idea is sound though and I could do a rewrite next go around and see what I can make work. I will try to get a picture of things so you get a better picture. As the script piece is just a control interface for the hardware that I am basically fabricating and adapting to make work for my setup...
Reply


Messages In This Thread
RE: Halting if command if gpio triggered event triggered - by knoxvilles_joker - Jun-13-2021, 09:18 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  GPIO Bug? Rchrd 6 1,306 Nov-17-2024, 10:46 PM
Last Post: Rchrd
  function return boolean based on GPIO pin reading caslor 2 2,189 Feb-04-2023, 12:30 PM
Last Post: caslor
  class Update input (Gpio pin raspberry pi) caslor 2 1,863 Jan-30-2023, 08:05 PM
Last Post: caslor
  Webhook, post_data, GPIO partial changes DigitalID 2 2,043 Nov-10-2022, 09:50 PM
Last Post: deanhystad
  How would I use Watchdog to get triggered when DVD is inserted? Daring_T 12 7,514 Aug-17-2021, 01:49 PM
Last Post: Daring_T
  Seemingly unstable GPIO output while executing from RetroPie LouF 6 5,991 Feb-19-2021, 06:29 AM
Last Post: LouF
  Process halting JarredAwesome 0 2,193 Dec-24-2020, 11:46 PM
Last Post: JarredAwesome
  Picture changing triggered by GPIO q_nerk 2 3,540 Dec-14-2020, 03:32 PM
Last Post: DeaD_EyE
  GPIO high if network IP has good ping duckredbeard 3 3,311 Oct-12-2020, 10:41 PM
Last Post: bowlofred
  raspberry pi tank gpio help jatgm1 1 3,483 May-06-2020, 09:00 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020