Jun-13-2021, 09:18 PM
(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...