Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python、Webiopi和Raspberry Pi_Python_Raspberry Pi_Robot_Webiopi - Fatal编程技术网

Python、Webiopi和Raspberry Pi

Python、Webiopi和Raspberry Pi,python,raspberry-pi,robot,webiopi,Python,Raspberry Pi,Robot,Webiopi,我试图通过无线和webiopi控制我的Raspberry Pi汽车。基本功能运行良好-有界面,我点击前进,汽车将前进,当我释放按钮,它将停止 我现在想集成超声波距离传感器,这样当我向前行驶时,当前方有东西时,汽车应该停止。我有它去哪里,当我点击前进按钮,汽车将驾驶和停止时,有东西在范围内,但它只会停止时,有东西在范围内,而不是当我释放按钮。我的while循环不知何故正在循环(卡住),并且没有从webiopi读取release按钮函数 有人能帮忙吗?已经干了好几天了,还不知道我哪里出了问题 以下是

我试图通过无线和webiopi控制我的Raspberry Pi汽车。基本功能运行良好-有界面,我点击前进,汽车将前进,当我释放按钮,它将停止

我现在想集成超声波距离传感器,这样当我向前行驶时,当前方有东西时,汽车应该停止。我有它去哪里,当我点击前进按钮,汽车将驾驶和停止时,有东西在范围内,但它只会停止时,有东西在范围内,而不是当我释放按钮。我的while循环不知何故正在循环(卡住),并且没有从webiopi读取release按钮函数

有人能帮忙吗?已经干了好几天了,还不知道我哪里出了问题

以下是我的python脚本中的循环:

def go_forward(arg):
  global motor_stop, motor_forward, motor_backward, get_range
  print "Testing"
  print mousefwd()
  while (arg) == "fwd":
      print (arg)
      direction = (arg)
      dist = get_range()
      print "Distance %.1f " % get_range()
      if direction == "fwd" and get_range() < 30:
          motor_stop()
          return
      else:
          motor_forward()
这就是我现在拥有它的方式,但仍然无法工作(我是一个彻头彻尾的傻瓜:-):

def前进(arg):
全局电机停止,电机前进,电机后退,获取范围
打印“测试”
方向=arg
而方向==“前进”:
打印参数
dist=get_range()
打印“距离%.1f”%get\u range()
如果get_range()小于30:
电机停止()
elif方向==“前进”:
马达正转()
其他:
电机停止()
也许向前迈出一小步。请参阅webipi使用自己的“循环”,我添加了循环代码以检查电机GPIO的状态和距离,如果电机正在运行,如果距离太短,则停止。现在,当我按下前进按钮时,汽车会移动,当我松开按钮时,汽车会停止,当向前移动且距离小于30厘米时,汽车会停止。唯一的问题是,当距离太短且我多次快速按下前进按钮时,我现在会得到一个“GPIO.output”(回波,1) _webiopi.GPIO.InvalidDirectionException:GPIO通道不是输出“错误:-(

代码现在如下所示:

 def go_forward(direction):
   motor_forward()

 def loop():
   if get_range() < 30 and GPIO.digitalRead(M1) == GPIO.HIGH:
     stop()
     print "stop"
   sleep(1)
def前进(方向):
马达正转()
def loop():
如果get_range()小于30且GPIO.digitalRead(M1)=GPIO.HIGH:
停止()
打印“停止”
睡眠(1)

试试这个-您总是在范围内停车,但可能想在方向上停车

  if get_range() < 30:
      motor_stop()
  elif direction == "fwd":
      motor_forward()
  else:
      motor_stop()
如果get_range()<30:
电机停止()
elif方向==“前进”:
马达正转()
其他:
电机停止()
顺便说一句,当你引用它时,你不需要(arg)只需要arg

打印参数


调用签名中的参数是用来表示函数的参数的

我将在一分钟内回答您的主要问题,但首先我想提到您作为Python初学者正在做的事情,这使您的生活变得比需要的更复杂

函数开始时不需要使用
global
语句。在Python中,如果要写入全局变量,只需要使用
global
语句。如果只需要读取变量或调用函数,则不需要使用
global
语句。在您的函数,无论是变量名还是函数名,都将按以下顺序进行搜索:局部、封闭、全局、内置。“局部”是指在函数中定义的名称,如
方向
变量。“封闭”是在另一个函数中定义函数时使用的规则。这在许多情况下都很有用,但这是一个高级主题,您现在不必担心。“全局”表示(变量、函数或类的)名称在程序的顶层定义,比如你的
motor\u stop
函数等等。最后,“builtin”是指Python的内置名称,比如
str
int
文件

因此,如果省略了
global
语句,Python将搜索名称
motor\u stop
(以及其他函数名),如下所示:

  • 本地:此功能中是否定义了
    电机停止装置
    ?否。继续
  • 封闭:此函数是在另一个函数中定义的吗?否,因此“封闭”规则不适用。继续
  • 全局:是否在程序的顶层定义了
    电机停止装置
    ?是。找到了
  • (内置):根本不检查此规则,因为名称是由“全局”规则找到的
  • 您的所有函数都是在程序的顶层定义的,因此“全局”规则将在不需要
    global
    语句的情况下找到它们。这将使您的生活更加简单

    现在回答你的主要问题:为什么你的代码会永远循环。这是因为你的
    while
    循环的测试条件永远不会变为false。你在
    while direction==“fwd”上循环
    ,而
    while
    循环中的代码不会更改
    方向
    变量。因此,每次它到达循环末尾时,它都会返回以再次测试条件。
    方向
    变量仍然包含字符串
    “fwd”
    ,while循环再次运行。然后
    方向
    变量仍然包含字符串
    “fwd”
    ,因此它将运行第三次。依此类推

    当您希望退出
    while
    循环时,退出
    循环的一种方法是在您想要停止时,将
    方向
    变量设置为其他变量,如
    “停止”
    。(例如,在您调用
    motor\u stop()
    之后)。另一种方法是使用
    return
    语句在该点退出函数。但我的建议是使用
    break
    语句,这意味着“此时,立即退出我所处的循环。”它既适用于
    while
    循环,也适用于
    for
    循环,这是一个非常有用的语句。它看起来像是希望您的
    while
    循环在调用
    motor\u stop()
    时退出,因此有两个
     def go_forward(direction):
       motor_forward()
    
     def loop():
       if get_range() < 30 and GPIO.digitalRead(M1) == GPIO.HIGH:
         stop()
         print "stop"
       sleep(1)
    
      if get_range() < 30:
          motor_stop()
      elif direction == "fwd":
          motor_forward()
      else:
          motor_stop()
    
    def go_forward(arg):
      print "Testing"
      direction = arg
      while direction == "fwd":
          print arg
          dist = get_range()
          print "Distance %.1f " % get_range()
          if get_range() < 30:
             motor_stop()
             break
          elif direction == "fwd":
             motor_forward()
             # No break statement here, so the while loop continues
          else:
             motor_stop()
             break
    
    def go_forward(arg):
      print "Testing"
      direction = arg
      while direction == "fwd":
          print arg
          dist = get_range()
          print "Distance %.1f " % dist
          if dist < 30:
             motor_stop()
             break
          elif direction == "fwd":
             motor_forward()
             # No break statement here, so the while loop continues
          else:
             motor_stop()
             break
    
    def go_forward(arg):
      print "Testing"
      direction = arg
      while direction == "fwd":
          print arg
          dist = get_range()
          print "Distance %.1f " % dist
          if dist < 30:
             motor_stop()
             break
          else:
             motor_forward()
    
    def go_forward(direction):
      print "Testing"
      while direction == "fwd":
          print direction
          dist = get_range()
          print "Distance %.1f " % dist
          if dist < 30:
             motor_stop()
             break
          else:
             motor_forward()
    
     def motor_forward():
         GPIO.output(M1, GPIO.HIGH)
         GPIO.output(M2, GPIO.LOW)
         string = "echo 0=130 > /dev/servoblaster"
         os.system(string)