Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | |||
knowledge_base:programming:treadmill [2022/12/02 14:45] – removed - external edit (Unknown date) 127.0.0.1 | knowledge_base:programming:treadmill [2022/12/02 14:45] (current) – ↷ Page moved from knowledge_base:treadmill to knowledge_base:programming:treadmill George Wayne | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Raspberry Pi to Control Treadmill ====== | ||
+ | ===== Install Python3 ===== | ||
+ | < | ||
+ | apt-get install python3 | ||
+ | </ | ||
+ | ===== Install WiringPi (for Python3) ===== | ||
+ | < | ||
+ | sudo apt-get install python3-dev python3-pip | ||
+ | sudo pip3 install wiringpi | ||
+ | </ | ||
+ | ===== Python3 Code - treadmill.py ===== | ||
+ | < | ||
+ | # | ||
+ | # 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() | ||
+ | wiringpi.pinMode(18, | ||
+ | wiringpi.pwmSetMode(0) | ||
+ | wiringpi.pwmSetClock(938) | ||
+ | wiringpi.pwmSetRange(1024) | ||
+ | wiringpi.pwmWrite(18, | ||
+ | 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)/ | ||
+ | t1 = t2 | ||
+ | print(' | ||
+ | vin = input(' | ||
+ | if is_number(vin): | ||
+ | vinf = float(vin) | ||
+ | if vinf<0 or vinf>7: | ||
+ | print(' | ||
+ | elif vinf< | ||
+ | speed = 0 | ||
+ | wiringpi.pwmWrite(18, | ||
+ | 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 += 1 | ||
+ | time.sleep(0.015) | ||
+ | else: | ||
+ | i = sp1-1 | ||
+ | while i>=sp2: | ||
+ | wiringpi.pwmWrite(18, | ||
+ | i -= 1 | ||
+ | time.sleep(0.035) | ||
+ | sp1 = sp2 | ||
+ | elif vin == ' | ||
+ | break | ||
+ | else: | ||
+ | print(' | ||
+ | except KeyboardInterrupt: | ||
+ | wiringpi.pwmWrite(18, | ||
+ | wiringpi.pinMode(18, | ||
+ | finally: | ||
+ | wiringpi.pwmWrite(18, | ||
+ | wiringpi.pinMode(18, | ||
+ | </ |