首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用raw_input控制脉宽调制

如何用raw_input控制脉宽调制
EN

Stack Overflow用户
提问于 2015-11-02 02:32:46
回答 1查看 197关注 0票数 1

我尝试使用raw_input函数输入一个字母,如lr,它将是一个特定的PWM值,如0.41.6。下面是我的代码:

代码语言:javascript
复制
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()

我也有另一个程序可以在pygame上做到这一点,但我不知道如何在开发模式的chromebook上通过ssh使用它,所以我放弃了它。也不要介意我的步进电机的所有def和其他东西,我也在运行。步进电机工作得很好,因为我直接输入数字,而不是变量,但我不知道如何让它与变量一起工作。另外,如果有人告诉我如何在chromebook上通过ssh运行pygame,那就太棒了,这会让它变得容易得多!

EN

回答 1

Stack Overflow用户

发布于 2015-11-02 03:18:04

对于变量pos,您应该使用input而不是raw_input

基本上,s = input('Say: ')做的事情与

代码语言:javascript
复制
s = eval( raw_input('Say: ') )

即给定的输入字符串将被求值。那么你甚至不需要对它做一个浮动。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33465689

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档