覆盆子PI速度表
我正在使用Raspberry Pi来跟踪动物运行轮的信息,例如行进距离和速度。
我正在使用一个脚本(来源:)
import RPi.GPIO as GPIO
import time
from time import sleep
GPIO.setmode(GPIO.BCM)
GPIO.setup(6, GPIO.IN, pull_up_down=GPIO.PUD_UP)
#get current data from file
file = open("/home/pi/current_count","r")
feet_traveled = float(file.readline())
high_mph = float(file.readline())
max_mph_time = file.readline()
prev_time = 0
mark_time = 0
prev_feet_traveled = 0
mph = 0
def increase_count(channel):
global prev_time
global feet_traveled
global prev_feet_traveled
global high_mph
global max_mph_time
global mph
feet_traveled += 2.095
mark_time = time.time()
elapsed_time = mark_time - prev_time
prev_time = mark_time
fps = (feet_traveled - prev_feet_traveled) / elapsed_time
prev_feet_traveled = feet_traveled
mph = fps / 0.6818182
if mph > high_mph:
high_mph = mph
max_mph_time = time.asctime(time.localtime(time.time()))
print 'Feet per second = ' + str(fps)
print 'MPH = ' + str(mph)
print 'Highest MPH ' + str(high_mph)
# write mph log
with open('/home/pi/mph_log', 'a+') as writer:
localtime = time.asctime(time.localtime(time.time()))
mph_log = localtime + ' MPH {0} fps {1}'.format(mph,fps) + '\n'
writer.write(mph_log)
GPIO.add_event_detect(6, GPIO.RISING, callback=increase_count, bouncetime=300)
while True:
miles_traveled = float(feet_traveled)/5280
print "Distance traveled is {0:,.1f} feet or {1:.3f} miles".format(feet_traveled,miles_traveled)
sleep(10)
distance = "<h1>Distance traveled is {0:,.1f} feet or {1:.3f} miles".format(feet_traveled,miles_traveled)
max_mph = '<h1>Peak MPH was {0:.2f} at '.format(high_mph) + str(max_mph_time)
current_mph = '<h1>Last speed measured was {0:.2f}mph'.format(mph)
# write web page
with open('/var/www/html/index.html', 'w') as writer:
writer.write('<html><body>' + distance)
writer.write('</h1>')
writer.write(current_mph)
writer.write('</h1>')
writer.write(max_mph + '</body></html>')
# write current count to disk
with open('/home/pi/current_count', 'w') as writer:
writer.write(str(feet_traveled) + '\n')
writer.write(str(high_mph) + '\n')
writer.write(max_mph_time)
# write to log file
with open('/home/pi/log_file', 'a+') as writer:
localtime = time.asctime(time.localtime(time.time()))
distance = localtime + ' Distance traveled is {0:,.1f} feet or {1:.3f} miles'.format(feet_traveled,miles_traveled) + '\n'
writer.write(distance)
我在运行代码时会遇到以下错误:
trackback(最近的最新通话):文件“/home/pi/wheel.py”,line 55,in Miles_traveled = float(teet_traveled) / 5280名称:name'feet_traveled'未定义< / p>
在脚本中定义我的teet_travel定义为2.09,因为那是一个轮旋转期间的距离。
我不确定为什么会遇到错误,但对任何帮助表示赞赏。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
hi
teet_traveled
它仅定义并生活在good_count()
函数中。如果您需要在功能范围之外需要它,请考虑将其返回。编辑
关键是
foet_traveled
变量确实定义为全局,它也将在函数的范围之外可用,但是您只有更新变量:不先初始化它。换句话说,您正在将2.095添加到甚至无法浮动的东西中。
因此,我的建议是:使用变量初始化
,您的错误将消失。
Hi
feet_traveled
it's only defined and living inside theincrease_count()
function. Consider returning it if you need it outside of the scope of the function.EDIT
The point is that the
feet_traveled
variable is indeed defined as global and it will be available outside of the function's scope as well, but you are only updating the variable:without having it initialized first. You are adding 2.095 to something that can even not be float, in other words.
So my advice is: initialize the variable with
and your error will go away.