(所有代码都在底部)
我正在为一辆汽车制作一个数字仪表板以及运行python的覆盆子pi来操纵和计算所有值。为了获得可视化界面,我使用TKinter。
我已经成功制作了一个python脚本,它使用GPIO事件检测来计算rpm以及基于每个转弯所需时间的速度。目前我正在使用霍尔效应传感器为每次旋转计时。
我还使用TKinter创建了一个简单的界面来显示所有计算值,即rpm,速度等。此时TKinter只显示每个变量的标准值,即0.似乎TKinter的主循环停止所有要运行的其他代码。
所以我遇到的问题是让TKinter和脚本的其余部分同时运行。
根据已完成的研究,似乎存在三种在Tk旁边运行代码的方法:s mainloop
https://gordonlesti.com/use-tkinter-without-mainloop/
我曾尝试使用这些建议来解决它,但我还没有让它发挥作用。
所以我试图实现它,一旦脚本运行TK接口启动并开始GPIO事件检测。一旦检测到GPIO.FALLING就会调用函数get_eng_pulse或get_spd_pulse,具体取决于触发的GPIO引脚。
我想知道是否有人能给我一些可能有用的指示或帮助我让它发挥作用。
我是python的新手所以请原谅我,如果我错过了什么。
使用事件检测的脚本然后计算值并将其打印出来(按预期工作):
import time
import RPi.GPIO as GPIO
tr_dia = 620 #diameter in mm
eng_pulse = 0
eng_rpm = 0.00
eng_elapse = 0.00
eng_start = time.time()
rpm_hall = 16
spd_pulse = 0
spd_elapse = 0.00
spd_start = time.time()
tr_rpm = 0.00
car_speed = 0.00
spd_hall = 18
wat_temp = 0.0
oil_temp = 0.0
oil_ps = 0.0
battery_v = 0.0
lambda_value = 0.0
GPIO.setmode(GPIO.BOARD)
GPIO.setup(rpm_hall, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(spd_hall, GPIO.IN, pull_up_down = GPIO.PUD_UP)
def get_eng_pulse(number):
global eng_elapse,eng_rpm,eng_pulse,eng_start
eng_pulse += 1
if eng_pulse > 0:
eng_elapse = time.time() - eng_start
eng_pulse -= 1
eng_rpm = 1/eng_elapse *60
eng_start = time.time()
def get_spd_pulse(number):
global spd_elapse, tr_rpm, spd_pulse, spd_start, tr_dia, car_speed
spd_pulse += 1
if spd_pulse > 0:
spd_elapse = time.time() - spd_start
spd_pulse -= 1
tr_rpm = 1/spd_elapse * 60
car_speed = tr_rpm * tr_dia / 1000 * 3.1415 / 60 * 3.6
spd_start = time.time()
GPIO.add_event_detect(rpm_hall, GPIO.FALLING, callback = get_eng_pulse, bouncetime = 5)
GPIO.add_event_detect(spd_hall, GPIO.FALLING, callback = get_spd_pulse, bouncetime = 5)
while (1):
print("rpm:{0:.2f} rpm_elapse:{1:.4f} tire_rpm:{2:.2f} speed:{3:.2f} speed_elapse:{4:.2f}" .format(eng_rpm,eng_elapse,tr_rpm, car_speed, spd_elapse))
time.sleep(.2)
使用TKinter显示给定vaiables但事件检测无法正常工作的脚本:
## Imports ##
# coding: utf8
import sys
import time
import RPi.GPIO as GPIO
from Tkinter import *
import tkFont
## Adjustable variables ##
tr_dia = 620 #tire diameter in mm
## Variables ##
eng_pulse = 0
eng_rpm = 0.0
eng_elapse = 0.00
eng_start = time.time()
rpm_hall = 16
spd_pulse = 0
spd_elapse = 0.00
spd_start = time.time()
tr_rpm = 0.0
car_speed = 0.0
spd_hall = 18
## Unimplemented variables ##
wat_temp = 0.0
oil_temp = 0.0
oil_ps = 0.0
#battery_v = 0.0
#lambda_value = 0.0
## GPIO settings ##
GPIO.setmode(GPIO.BOARD)
GPIO.setup(rpm_hall, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(spd_hall, GPIO.IN, pull_up_down = GPIO.PUD_UP)
## call for variable(unimplementd)##
## Logic and calculation behind engine RPM ##
def get_eng_pulse(number):
global eng_elapse,eng_rpm,eng_pulse,eng_start
eng_pulse += 1
if eng_pulse > 0:
eng_elapse = time.time() - eng_start
eng_pulse -= 1
eng_rpm = 1/eng_elapse *60
eng_start = time.time()
## Logic and calculation behind car speed ##
def get_spd_pulse(number):
global spd_elapse, tr_rpm, spd_pulse, spd_start, tr_dia, car_speed
spd_pulse += 1
if spd_pulse > 0:
spd_elapse = time.time() - spd_start
spd_pulse -= 1
tr_rpm = 1/spd_elapse * 60
car_speed = tr_rpm * tr_dia / 1000 * 3.1415 / 60 * 3.6
spd_start = time.time()
root.after(100, get_spd_pulse)
## TKinter start ##
## Configuring the window "root" ##
label_color = "yellow"
value_color = "white"
root = Tk()
root.title("Dashboard")
root.attributes("-fullscreen",True)
root.bind("<1>", exit)
root.configure(background = "black")
## RPM label ##
RPM = Label(root, text = "RPM", fg = label_color, background = "black", font=("Helvetica", 25, "bold"))
RPM.place(x=80, y=140)
## RPM value ##
eng_rpm_var = StringVar()
eng_rpm_var.set(str(eng_rpm)+ " RPM")
eng_rpm_value = Label(root, textvariable = eng_rpm_var, fg = value_color, background = "black", font=("Helvetica", 40, "bold"))
eng_rpm_value.place(x=80, y=180)
## Speed label ##
Speed = Label(root, text = "Speed", fg = label_color, background = "black", font=("Helvetica", 25, "bold"))
Speed.place(x=450, y=140)
## Speed value ##
car_speed_var = StringVar()
car_speed_var.set(str(car_speed)+ " Km/h")
car_speed_value = Label(root, textvariable = car_speed_var, fg = value_color, background = "black", font=("Helvetica", 40, "bold"))
car_speed_value.place(x=450, y=180)
## Water temperature label ##
Water_temp = Label(root, text = "WAT-T", fg = label_color, background = "black", font=("Helvetica", 25, "bold"))
Water_temp.place(x=80, y=310)
## Water temperature value ##
wat_temp_var = StringVar()
wat_temp_var.set(str(wat_temp) + "°C")
Water_temp_value = Label(root, textvariable = wat_temp_var, fg = value_color, background = "black", font=("Helvetica", 40, "bold"))
Water_temp_value.place(x=80, y=350)
## Oil Temperature label ##
Oil_temp = Label(root, text = "OIL-T", fg = label_color, background = "black", font=("Helvetica", 25, "bold"))
Oil_temp.place(x=255, y=310)
## Oil temperature value ##
oil_temp_var = StringVar()
oil_temp_var.set(str(oil_temp) + "°C")
Oil_temp_value = Label(root, textvariable = oil_temp_var, fg = value_color, background = "black", font=("Helvetica", 40, "bold"))
Oil_temp_value.place(x=255, y=350)
## Oil pressure label ##
Oil_press = Label(root, text = "OIL-P", fg = label_color, background = "black", font=("Helvetica", 25, "bold"))
Oil_press.place(x=450, y=310)
## Oil pressure value ##
oil_ps_var = StringVar()
oil_ps_var.set(str(oil_ps) + " Kgf")
Oil_ps_value = Label(root, textvariable = oil_ps_var, fg = value_color, background = "black", font=("Helvetica", 40, "bold"))
Oil_ps_value.place(x=450, y=350)
root.mainloop()
## Defining exit from mouseclick ##
def exit():
root.quit()
## TKinter end ##
## Main signal calls ##
GPIO.add_event_detect(rpm_hall, GPIO.FALLING, callback = get_eng_pulse, bouncetime = 5)
GPIO.add_event_detect(spd_hall, GPIO.FALLING, callback = get_spd_pulse, bouncetime = 5)
## Terminal outprints ##
while (1):
print("rpm:{0:.2f} rpm_elapse:{1:.4f} tire_rpm:{2:.2f} speed:{3:.2f} speed_elapse:{4:.2f}" .format(eng_rpm,eng_elapse,tr_rpm, car_speed, spd_elapse))
time.sleep(.2)