0

I am writing a Python function to turn my servo motor left and right every time either one of the 2 images (left and right) is clicked.

This is my python script:

    #!/usr/bin/python


def turnCamera (**data):

    import pigpio
    import time

    servos=4
    key = data['direction']

    m=1500
    while (m >= 500 and m <= 2500):
        if (key =="left"):
            m=m+100
        elif (key =="right"):
            m=m-100

    pigpio.start()

    pigpio.set_servo_pulsewidth(servos, m) 
    servostatus= "Servo {} {} micro pulses".format(servos[0], key, m)
    print servostatus
    time.sleep(1)

    pigpio.stop()

    return servostatus

I know this code will not work because everytime the function is called, the value of m (servo position) will get reset to 1500, which is unwanted.

It works when m=1500, without the if and else statement. But when I run this code (with Flask server), it gave me this error: http://pastebin.com/eGgVQnML

What have I done wrong?

This is my jQuery script:

$('#left_button').click(function(){
            $.post("{{ url_for('turn_servo_ajax') }}", {direction:"left"}).done(function (reply) {
                $('#camerapos').empty().append(reply);
                alert("left button clicked");});

        });
R. Barzell
  • 666
  • 5
  • 24
yvonnezoe
  • 7,129
  • 8
  • 30
  • 47
  • Redesign to use an object that encapsulates the current m value and a method to change it. But it's unclear how you're running this with Flask; http is stateless. – Wooble Mar 03 '14 at 13:53
  • how i run with flask: http://stackoverflow.com/questions/22141570/accessing-flask-server-from-my-web-page/22146262?noredirect=1#comment33608180_22146262 can you explain more about the encapsulation and method? – yvonnezoe Mar 03 '14 at 13:56
  • The errors seem to be coming from `pigpio.set_servo_pulsewidth(servos, m)` (you're sending pulsewidth values out of range). Nothing to do with your question. – Ricardo Cárdenes Mar 03 '14 at 14:23
  • @RicardoCárdenes But it was working ok when it only has `m=1500`. Anyway it could have run the script up to the point till `m=1500` right? – yvonnezoe Mar 03 '14 at 15:04
  • @RicardoCárdenes btw, i reload the code and get this error` Unhandled exception in thread started by sys.excepthook is missing lost sys.stderr ` – yvonnezoe Mar 03 '14 at 15:05
  • I'm not even sure what are you trying to do here. Your while loop could be substituted with this: `m = (400 if key == 'right' else 2600)` - and those values *are* out of range – Ricardo Cárdenes Mar 03 '14 at 15:10
  • i'm trying to move the servo bit by bit (m=1500 is at neutral position) so if the user click on `left`, m=1500+100=1600; click on right should give m=1600-100=1500 (but in this case, it will get reset to 1500, so m=1500-100=1400) – yvonnezoe Mar 03 '14 at 15:30
  • Check your logic. You want `if`, not `while`, and you want to check for `>` and `<`, not for `>=` and `<=`` – Ricardo Cárdenes Mar 03 '14 at 17:40
  • @RicardoCárdenes hmm yes, my logic is wrong. I wanted to make the servo to move by 100 everytime i click the button. However, there is a limit (only values between 500-2500 are allowed). So how can i write this algorithm? – yvonnezoe Mar 04 '14 at 01:52

0 Answers0