简体   繁体   English

将命令行参数传递给Python脚本会导致错误

[英]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! 我有一些来自ADAFRUIT的python示例脚本,它控制伺服,它的工作原理!

I would like to execute it to change the ServoMin and ServoMax values by calling two values from command line. 我想执行它来通过从命令行调用两个值来更改ServoMin和ServoMax值。 The problem I am having is that it seems not to like it when I import sys and set ServoMin = (sys.argv) . 我遇到的问题是,当我import sys并设置ServoMin = (sys.argv)时似乎不喜欢它。 Im not sure why its not accepting int or str. 我不知道为什么它不接受int或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)

sys.argv is an list of arguments, you need to access the arguments, then convert them to Ints. sys.argv是一个参数列表,您需要访问参数,然后将它们转换为Ints。

This should do it: 这应该这样做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM