私も午前に滞在信号を失ったことを考えていませんこのセンサーでうんざりする。私のコードはあなたのものと似たように実行され、それが機能するにはタイムアウトは必要ありません。私は見つけることができます 1つの違いはこれです:
while i< 1:
GPIO.output(GPIO_TRIGGER, False)
time.sleep(C.time['count'])
私は睡眠時間がここにあるどのくらいかわからないが、それはそれが問題を引き起こしている可能性があります。それが鉱山の設定と似ている場合は、トリガーをfalseに設定すると、代わりにin/outピンの設定後になり、ノイズを除去するために2秒待つことになります。あなたの待ち時間はより低いかもしれません、私は伝えることはできません。パルスを送信する直前にトリガを再度falseに設定する必要はありません。わかりませんが、誤った開始を引き起こしている可能性があります。これと同じように変更してwhileループでfalseに設定を削除します。
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)
GPIO.output(GPIO_TRIGGER, False)
print("Waiting for sensor to settle\n")
time.sleep(2)
タイムアウトを必要とせずに問題を解決できるかどうかはわかりませんが、必要ないようです。
センサのオブジェクトを作成するためのモジュールを作成しました。このモジュールは、より読みやすいスクリプトを可能にします。私はまたのpythonに非常に新しいですし、すべての経験豊富なプログラマでとても楽しいエラーがどこかにあるかもしれないではないが、あなたはそれを使用するか、単にコードを比較したい場合には、以下ここにある:
#! /usr/bin/python3
# dist.py this is a module for objectifying an ultrasonic distance sensor.
import RPi.GPIO as GPIO
import time
class Distancer(object):
#init takes an input of one GPIO for trigger and one for echo and creates the object,
#it searches for a calibration file in the working directory (name)Const.txt, if none
#is found it will initiate a calibration
def __init__(self, trig, cho, name):
self.trigger = trig
self.echo = cho
self.name = name
self.filename = self.name + 'Const.txt'
GPIO.setup(self.trigger, GPIO.OUT)
GPIO.setup(self.echo, GPIO.IN)
GPIO.output(self.trigger, False)
print("Waiting for sensor to calm down")
time.sleep(2)
try:
with open(self.filename, "r") as inConst:
self.theConst = int(inConst.read())
except (OSError, IOError) as e:
print("Not calibrated, initializing calibration")
self.calibrate()
with open(self.filename, "r") as inConst:
self.theConst = int(inConst.read())
#Returns the echo time
def measureTime(self):
GPIO.output(self.trigger, True)
time.sleep(0.00001)
GPIO.output(self.trigger, False)
while GPIO.input(self.echo) == 0:
pulse_start = time.time()
while GPIO.input(self.echo) == 1:
pulse_end = time.time()
pulse_duration = pulse_end - pulse_start
return pulse_duration
#Returns a distance in cm
def measure(self):
return self.measureTime() * self.theConst
#Makes you set up the sensor at 3 different distances in order to find the
#relation between pulse time and distance, it creates the file (name)Const.txt
#in the working directory and stores the constant there.
def calibrate(self):
ten = []
thirty = []
seventy = []
print("Make distance 10 cm, enter when ready")
input()
for i in range(30):
ten.append(10/self.measureTime())
time.sleep(0.2)
print("Make distance 30 cm, enter when ready")
input()
for i in range(30):
thirty.append(30/self.measureTime())
time.sleep(0.2)
print("Make distance 70 cm, enter when ready")
input()
for i in range(30):
seventy.append(70/self.measureTime())
time.sleep(0.2)
allTime = ten + thirty + seventy
theOne = 0.0
for i in range(90):
theOne = theOne + allTime[i]
theOne = theOne/90
with open(self.filename, "w") as inConst:
inConst.write(str(round(theOne)))
#Will continually check distance with a given interval until something reaches the
#treshold (cm), takes an argument to set wether it should check for something being
#nearer(near) or farther(far) than the treashold. Returns True when treshold is reached.
def distWarn(self, nearfar, treashold):
if nearfar.lower() == "near":
while True:
if self.measure() < treashold:
return True
break
time.sleep(0.2)
if nearfar.lower() == "far":
while True:
if self.measure() > treashold:
return True
break
time.sleep(0.2)
#Will measure with a second interval and print the distance
def keepGoing(self):
while True:
try:
print(str(round(self.measure())) + ' cm')
time.sleep(1)
except KeyboardInterrupt:
print("Won't keep going")
break
私はそれを実行しましたテストするために以下のコードを書いてください。すべてがうまくいくようです。最初に実行されると、センサーを何かから別の距離に置くことによってセンサーを較正するよう促します。
#! /usr/bin/python3
import RPi.GPIO as GPIO
import time
import dist as distancer
GPIO.setmode(GPIO.BOARD)
TRIG = 16
ECHO = 18
dist = distancer.Distancer(TRIG, ECHO, 'dist')
def main():
global dist
print(str(round(dist.measureTime(),5)) + ' s')
print(str(round(dist.measure())) + ' cm')
dist.distWarn('near', 10)
print('Warning, something nearer than 10 cm at ' + time.asctime(time.localtime(time.time())))
dist.distWarn('far', 10)
print('Warning, something further than 10 cm at ' + time.asctime(time.localtime(time.time())))
dist.keepGoing()
GPIO.cleanup()
print('Fin')
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
GPIO.cleanup()
print("Exiting")
time.sleep(1)
この質問はあまりにも幅広いですが、ここではいくつかの原因が考えられます:マルチメーターを取得する...ピンとグラウンドを測定し、コードが実行されている間にオシレーションを取得していますか?それとも常にゼロですか? '(x == 0の間)'は常に真であると思われます。次に、[ラズベリーマイクロプロセッサ](http://raspberrypi.stackexchange.com/)専用のstackoverflowフォーラムを確認し、 – Bonatti