user1955162
user1955162

Reputation: 829

Passing a command line argument into a Python Script causes error

I have some python example script from ADAFRUIT which controls a servo and it works!

I would like to execute it to change the ServoMin and ServoMax values by calling two values from command line. The problem I am having is that it seems not to like it when I import sys and set ServoMin = (sys.argv). Im not sure why its not accepting int or str. Any ideas?

Here is the code body:

#!/usr/bin/python

from Adafruit_PWM_Servo_Driver import PWM
import time
import sys

# ===========================================================================
# Example Code
# ===========================================================================

# Initialise the PWM device using the default address
# bmp = PWM(0x40, debug=True)
pwm = PWM(0x50, debug=True)

servoMin = 150  # Min pulse length out of 4096
servoMax = 600  # Max pulse length out of 4096

def setServoPulse(channel, pulse):
  pulseLength = 1000000                   # 1,000,000 us per second
  pulseLength /= 60                       # 60 Hz
  print "%d us per period" % pulseLength
  pulseLength /= 4096                     # 12 bits of resolution
  print "%d us per bit" % pulseLength
  pulse *= 1000
  pulse /= pulseLength
  pwm.setPWM(channel, 0, pulse)

pwm.setPWMFreq(60)                        # Set frequency to 60 Hz
while (True):
  # Change speed of continuous servo on channel O
  pwm.setPWM(0, 0, servoMin)
  time.sleep(1)
  pwm.setPWM(0, 0, servoMax)
  time.sleep(1)

Upvotes: 0

Views: 226

Answers (1)

Nicolas Cortot
Nicolas Cortot

Reputation: 6701

sys.argv is an list of arguments, you need to access the arguments, then convert them to Ints.

This should do it:

servoMin = int(sys.argv[1])
servoMax = int(sys.argv[2])

Upvotes: 2

Related Questions