Skip to content Skip to sidebar Skip to footer

I Need Help With A Python Script I Am Trying To Adapt For A Specific Need

I am a total newbie with Python, I normally use Perl. I have an Arduino wired up with some servos I use to control a web cam, the script in question works perfect, the servos recei

Solution 1:

Beside @combatdave's answer, on this part:

try:
        servo = int(sys.argv[1])
        angle = int(sys.argv[2])
except IndexError:
        print ('a servo and angle are required')
sys.exit(2)

# Set up serial baud rate

You should indend sys.exit(2) too. Because of this, the program exits right after getting arguments.


Solution 2:

It looks like your indentation after the function definition is wrong. It should be:

def move(servo, angle):
    '''Moves the specified servo to the supplied angle.

    Arguments:
        servo
          the servo number to command, an integer from 1-4
        angle
          the desired servo angle, an integer from 0 to 180

    (e.g.) >>> servo.move(2, 90)
           ... # "move servo #2 to 90 degrees"'''

    if (0 <= angle <= 180):
        ser.write(chr(255))
        ser.write(chr(servo))
        ser.write(chr(angle))
    else:
        print "Servo angle must be an integer between 0 and 180. You typed:"
        print servo
        print angle

(Note how the if...else block is indented)


Solution 3:

Why adapt the original script? Just use this:

import servo
servo.move(int(argv[1]), int(argv[2]))

I'm not sure whether this happened when pasting code into stackoverflow, but the indenting is flawed on multiple places. Please check this too :)


Solution 4:

Your indentation is wrong basically.

See this snippet:

try:
        servo = int(sys.argv[1])
        angle = int(sys.argv[2])
except IndexError:
        print ('a servo and angle are required')
sys.exit(2)

The sys.exit(2) call is always executed! So the program does nothing.


Post a Comment for "I Need Help With A Python Script I Am Trying To Adapt For A Specific Need"