0

After a week of searching...

I have built a robotic arm run by a Pi 3. It has 5 servos, as well as a stepper motor. I have code written see, (Pastebin). And I am not able to get each servo motor to move incrementally when the key press function is called. I can make the servos move to positions, but not move in between whenever I press the button.

Ideally the code would call a servo up function, and the servo would move up by increments of 20 every time the function is called.

This is the main focus:

def shoulder_up():
    for spwm in range(1000, 2000, 20):
        pi.set_servo_pulsewidth(shoulderpin, spwm)
        time.sleep(0.4)
        pi.set_servo_pulsewidth(shoulderpin, 0) 

When this is called, I need the servo to move a small increment to the left. This means closer to a 1000 pwm signal value. I cannot determine how to make it move the increment every time based on the previous movement. The code currently just moves the servo through the defined range.


Update:

I would like to try to use this, but I cannot get the variables to change.

def shoulder_up():
global spwm
global nspwm
spwm = nspwm
pi.set_servo_pulsewidth(shoulderpin, spwm)
nspwm = spwm + 20

Every time the loop runs, I want spwm to change and become 20 higher. For some reason I cant get it to change. It goes to 1520 every time.


Any help on this would be appreciated. Yes my code is in alpha, so it is messy and a little nonuniform.

2
  • 1
    You seriously want us to review over 200 lines of your code? Could you try to isolate the problem to a smaller section of code please?
    – Darth Vader
    Jun 9, 2017 at 14:29
  • Yes. Of course. Jun 9, 2017 at 14:33

2 Answers 2

1

Did more testing, and here is my final code. I determined that I was failing to reference a global variable properly. I have included the final code for those who are in the same boat. PasteBin Robotic Arm Thank you to those who tried to help!

0

Seems like you need to define some persistent servo position variables, and then increment/decrement them (be sure to clamp the positions to limits when inc/dec)

for example something like:

servo_positions=[0,0,0,0,0]

def clamp(n, minn, maxn):
    return max(min(maxn, n), minn)

def inc_servo(nServo):  #nServo 0-4
    global servo_positions
    newPos = servo_positions[nServo]+10
    servo_positions[nServo] = clamp(newPos, -90, +90)

now in your key_loop, when '+1' pressed use inc_servo[1]

2
  • I am trying to follow what you have done. What variables do I need to change to reflect my setup? Jun 12, 2017 at 12:27
  • I have tried to implement but to no avail. Here is what I have done. [pastebin.com/0hG9j2Xb] Jun 12, 2017 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.