如何使用raw_input来控制pwm

时间:2015-11-01 18:32:46

标签: python pygame raw-input pwm

我正在尝试使用raw_input函数键入lr这样的字母,它将是某个pwm值,如0.4或{ {1}}。这是我的代码:

1.6

我还有另一个用import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.cleanup() enable_pin = 18 coil_A_1_pin = 27 coil_A_2_pin = 17 coil_B_1_pin = 23 coil_B_2_pin = 24 coil_A_x1_pin = 22 coil_A_x2_pin = 5 coil_B_x1_pin = 6 coil_B_x2_pin = 13 s = 12 GPIO.setup(enable_pin, GPIO.OUT) GPIO.setup(coil_A_1_pin, GPIO.OUT) GPIO.setup(coil_A_2_pin, GPIO.OUT) GPIO.setup(coil_B_1_pin, GPIO.OUT) GPIO.setup(coil_B_2_pin, GPIO.OUT) GPIO.setup(coil_A_x1_pin, GPIO.OUT) GPIO.setup(coil_A_x2_pin, GPIO.OUT) GPIO.setup(coil_B_x1_pin, GPIO.OUT) GPIO.setup(coil_B_x2_pin, GPIO.OUT) GPIO.setup(s, GPIO.OUT) GPIO.output(enable_pin, 1) def forward(delay, steps): for i in range(0, steps): setStep(1, 1, 0, 0) time.sleep(delay) setStep(0, 1, 1, 0) time.sleep(delay) setStep(0, 0, 1, 1) time.sleep(delay) setStep(1, 0, 0, 1) time.sleep(delay) def backwards(delay, steps): for i in range(0, steps): setStep(1, 0, 0, 1) time.sleep(delay) setStep(0, 0, 1, 1) time.sleep(delay) setStep(0, 1, 1, 0) time.sleep(delay) setStep(1, 1, 0, 0) time.sleep(delay) def setStep(w1, w2, w3, w4): GPIO.output(coil_A_1_pin, w1) GPIO.output(coil_A_x1_pin, w1) GPIO.output(coil_A_2_pin, w2) GPIO.output(coil_A_x2_pin, w2) GPIO.output(coil_B_1_pin, w3) GPIO.output(coil_B_x1_pin, w3) GPIO.output(coil_B_2_pin, w4) GPIO.output(coil_B_x2_pin, w4) p = GPIO.PWM(s,7.5) try: while True: l = 0.3 r = 1.7 n = 1.1 pos = raw_input("Left(l), right(r) or neutral(n)? ") p.ChangeDutyCycle(float(pos)) delay = raw_input("Delay between steps (milliseconds)? ") steps = raw_input("How many steps forward? ") forward(int(delay) / 1000.0, int(steps)) steps = raw_input("How many steps backwards? ") backwards(int(delay) / 1000.0, int(steps)) except KeyboardInterrupt: GPIO.cleanup() 执行此操作的程序,但我无法弄清楚如何在开发模式chromebook上使用ssh,所以我放弃了。另外不要介意我正在运行的步进电机的所有pygame和其他东西。步进电机工作正常,因为我直接输入数字而不是变量,但我无法弄清楚如何使它与变量一起工作。如果有人告诉我如何在chromebook上运行pygame,这会让它变得更容易,那就太棒了!

1 个答案:

答案 0 :(得分:0)

对于变量pos,您应该使用input而不是raw_input
基本上s = input('Say: ')

做同样的事情
s = eval( raw_input('Say: ') )

即。给定的输入字符串将评估。然后你甚至不需要漂​​浮它。