I am using a raspberry pi to track information from an animals running wheel such as distance travelled and speed.
I am using a script (source: https://drive.google.com/file/d/1kKWoHWWLt008tQB-H_9C2gjgoBFygKR4/view?usp=sharing)
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)
I get the following error when running the code:
Traceback (most recent call last): File "/home/pi/wheel.py", line 55, in miles_traveled = float (feet_traveled) / 5280 NameError: name 'feet_traveled' is not defined
In the script I have feet_traveled defined as 2.09 because that is the distance traveled during one wheel rotation.
I am not sure why I am getting the error but any help is appreciated.
credit for writing original script: https://www.reddit.com/r/RASPBERRY_PI_PROJECTS/comments/g4xkrd/made_a_speedometer_odometer_for_my_cat_wheel/
Hi feet_traveled
it's only defined and living inside the increase_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:
feet_traveled += 2.095
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
feet_traveled = 2.095
and your error will go away.