2016-07-05 53 views
2

当我使用以下代码运行以下代码时,我的树莓派重新启动:sudo python robot.py 我的树莓派pi通过robohat控制器连接到4个电机,一个云台组件和一个超声波传感器RobohatRasperry pi在运行程序时重启

import robohat, time, random 
dist_l = 0 
dist_r = 0 
speed = 80 
pan = 1 
tilt = 0 
tVal = 25 # 20 degrees is centre 
pVal = 20 # 25 degrees is centre 

robohat.init() 
def doServos():# Set servo to wanted degrees 
    robohat.setServo(pan, pVal) 
    robohat.setServo(tilt, tVal) 

end = "False" 
while end == "False": 

    def dist_check(): #gets distance 
     pVal = 20 
     tVal = 25 
    doServos() #Centre the servos  
def servoPosGo(): 
    pVal2 = (90) 
    robohat.setServo(pan, pVal2) 
    robohat.setServo(tilt, tVal) 
    dist_l = robohat.getDistance() 
servoPosGo() 
#Take right reading at 90 degrees 


def servoPosGo2(): 
    pVal3 = (-60) 
    robohat.setServo(pan, pVal3) 
    robohat.setServo(tilt, tVal) 
    dist_r = robohat.getDistance() 
servoPosGo2() 

doServos() #Centres the servos 
dist_check() 
if dist_r < 100 or dist_l < 100: #Is the distance greater than 100, and go which is greater but less than 100 
    if dist_l > dist_r: 
     robohat.spinLeft(speed) 
     robohat.forward(speed) 
     time.sleep(dist_l - 10) 
     robohat.stop() 
    elif dist_r > dist_l: 
     robohat.spinRight(speed) 
     robohat.forward(speed) 
     time.sleep(dist_r - 10) 
     robohat.stop() 
    elif dist_l == dist_r: #If the two distnaces are the same, choose a random one 
     ran = random.randrange(1, 2) 
     if ran == (1): 
      robohat.spinLeft(speed) 
      robohat.forward(speed) 
      time.sleep(dist_l - 10) 
      robohat.stop() 
     elif ran = (2): 
      robohat.spinRight(speed) 
      robohat.forward(speed) 
      time.sleep(dist_r - 10) 
      robohat.stop() 
elif dist_l > 100 or dist_r > 100: #If distance IS greater than 100, go forward and set end to true 
    end = "True" 
    robohat.forward(speed) 
    if dist_r > dist_l: 
     time.sleep(dist_r - 10) 
    elif dist_l > dist_r: 
     time.sleep(dist_l - 10) 
+0

你用什么来为你的树莓供电?访问外部电机和/或传感器可能需要比您的信号源更多的能量,导致重启,因为您的Raspberry一秒钟没有获得足够的能量。 – Heinrich

+0

重新启动可能是由电气故障造成的。没有附加任何设备,它会重新启动吗? –

+0

看起来你的dist_check函数从未被定义过。你所拥有的只是dist_check(),但从来没有确定dist_check()。 – David

回答

0

给你的RPi一个更强的交流适配器。它应该至少有1.2 A.

此外,请确保您的伺服器没有从RPi供电,地必须是相同的,只有伺服命令行连接到RPi引脚。还有一个事实,即RPi为其GPIO提供3.5V输出,而大多数伺服器在功率和cmd线路上需要5V电源。虽然其中一些可以在cmd线上使用较低的电压正常工作。

如果你不直接连接你的马达GPIO,你必须重新检查接地等

有时将有助于增加电源输入大电容,这样突如其来的瞬间不会震撼你的RPI为重启。