sudo apt-get install python3-dev python3-pip
sudo pip3 install wiringpi
#!/usr/bin/env python3
# Must be run as root
# import wiringpi2 as wiringpi
import wiringpi as wiringpi
import time
def is_number(s):
try:
float(s)
return True
except ValueError:
return False
wiringpi.wiringPiSetupGpio() # using BCM GPIO numbers, only on port 18
wiringpi.pinMode(18,2)
wiringpi.pwmSetMode(0)
wiringpi.pwmSetClock(938) # 19.2MHz/1024/938 = 20Hz, has to be 20Hz
wiringpi.pwmSetRange(1024)
wiringpi.pwmWrite(18, 0)
time0 = time.time()
t1 = time0
t2 = time0
speed = 0
sp1 = 844
sp2 = 844
distance = 0
try:
while 1:
t2 = time.time()
distance = distance + speed*(t2-t1)/3600
t1 = t2
print('Duration: ', round((time.time()-time0)/60,2), 'Min. Speed: ', round(speed, 2), 'MPH. Distance: ', round(distance,2), 'Mi.\n')
vin = input('Speed(0~7mph, q to quit): ')
if is_number(vin):
vinf = float(vin)
if vinf<0 or vinf>7:
print('Please enter a number between 0~7 or q to quit')
elif vinf<0.9:
speed = 0
wiringpi.pwmWrite(18, 0)
else:
speed = vinf
sp2 = int(873.5-58.85*speed+0.5)
if sp1<sp2:
i = sp1+1
while i<=sp2:
wiringpi.pwmWrite(18, i)
i += 1
time.sleep(0.015)
else:
i = sp1-1
while i>=sp2:
wiringpi.pwmWrite(18, i)
i -= 1
time.sleep(0.035)
sp1 = sp2
elif vin == 'q':
break
else:
print('Please enter a number between 0~7, or q to quit')
except KeyboardInterrupt:
wiringpi.pwmWrite(18, 0)
wiringpi.pinMode(18,0)
finally:
wiringpi.pwmWrite(18, 0)
wiringpi.pinMode(18,0)