Raspberry PI 4 Tachometer doesn't display data
-
I've made a QT GUI to display RPM data from hall sensors. The Python program that I embedded in my program works with print statements. But the Main Window displays a zero that does not change. Can anyone help get the data to display. The following is the code I used:
import sys #import spidev import time, math import os import RPi.GPIO as GPIO from PyQt5.QtCore import QTime, QTimer from PyQt5.QtWidgets import QApplication, QMainWindow, QLCDNumber from tach import Ui_MainWindow gpio_pin_number = 12 # GPIO Pin used by sensor wheel_diameter_in = 10 # wheel diameter in inches adjustment = 1 # adjustment for gear ratio or number of magnets seconds = 1 # time to wait between printing values rpm = 0 mph = 0 elapsed_time = 0 # amount of time a full revolution takes number_interrupts = 0 # counts interrupts (triggered by sensor) previous_number_interrupts = 0 start_timer = time.time() class tach(QMainWindow): def __init__(self, parent=None): super(tach, self).__init__(parent) self.ui = Ui_MainWindow() self.ui.setupUi(self) timer = QTimer(self) timer.timeout.connect(self.showRPM) timer.start(1000) self.showRPM() def showRPM(self): def init_GPIO(): # initialize GPIO GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(gpio_pin_number,GPIO.IN,GPIO.PUD_UP) def calculate_elapsed_time(channel): global number_interrupts, start_timer, elapsed_time, signal number_interrupts+=1 # increase by 1 whenever an interrupt occurs elapsed_time = time.time() - start_timer # time each rotation takes start_timer = time.time() # Set start_time to current time def calculate_speed(wheel_diameter): global number_interrupts, elapsed_time, rpm, mph if elapsed_time !=0: # avoid DivisionByZero error rpm = (1/elapsed_time * 60) * adjustment wheel_circumf_in = math.pi * wheel_diameter_in # wheel circumference in inches mph = (rpm * wheel_circumf_in) / 1056 def init_interrupt(): GPIO.add_event_detect(gpio_pin_number, GPIO.FALLING, callback = calculate_elapsed_time, bouncetime = 20) self.ui.RPMlcdNumber.display(rpm) if __name__ == '__main__': import sys app = QApplication(sys.argv) tach = tach() tach.show() sys.exit(app.exec_()) -
I've made a QT GUI to display RPM data from hall sensors. The Python program that I embedded in my program works with print statements. But the Main Window displays a zero that does not change. Can anyone help get the data to display. The following is the code I used:
import sys #import spidev import time, math import os import RPi.GPIO as GPIO from PyQt5.QtCore import QTime, QTimer from PyQt5.QtWidgets import QApplication, QMainWindow, QLCDNumber from tach import Ui_MainWindow gpio_pin_number = 12 # GPIO Pin used by sensor wheel_diameter_in = 10 # wheel diameter in inches adjustment = 1 # adjustment for gear ratio or number of magnets seconds = 1 # time to wait between printing values rpm = 0 mph = 0 elapsed_time = 0 # amount of time a full revolution takes number_interrupts = 0 # counts interrupts (triggered by sensor) previous_number_interrupts = 0 start_timer = time.time() class tach(QMainWindow): def __init__(self, parent=None): super(tach, self).__init__(parent) self.ui = Ui_MainWindow() self.ui.setupUi(self) timer = QTimer(self) timer.timeout.connect(self.showRPM) timer.start(1000) self.showRPM() def showRPM(self): def init_GPIO(): # initialize GPIO GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(gpio_pin_number,GPIO.IN,GPIO.PUD_UP) def calculate_elapsed_time(channel): global number_interrupts, start_timer, elapsed_time, signal number_interrupts+=1 # increase by 1 whenever an interrupt occurs elapsed_time = time.time() - start_timer # time each rotation takes start_timer = time.time() # Set start_time to current time def calculate_speed(wheel_diameter): global number_interrupts, elapsed_time, rpm, mph if elapsed_time !=0: # avoid DivisionByZero error rpm = (1/elapsed_time * 60) * adjustment wheel_circumf_in = math.pi * wheel_diameter_in # wheel circumference in inches mph = (rpm * wheel_circumf_in) / 1056 def init_interrupt(): GPIO.add_event_detect(gpio_pin_number, GPIO.FALLING, callback = calculate_elapsed_time, bouncetime = 20) self.ui.RPMlcdNumber.display(rpm) if __name__ == '__main__': import sys app = QApplication(sys.argv) tach = tach() tach.show() sys.exit(app.exec_())@rooster37
Hello and welcome.Firstly please take the trouble to enclose blocks of code like yours with a line of
```before and after it, which is what the Code (</>) toolbar button above where you type in does. This is especially important in the case of Python code, where leading whitespace is vital.But the Main Window displays a zero that does not change.
timer = QTimer(self)You assign the
QTimerto a local variable. That gets destroyed immediately afterself.showRPM(), and hence there is no longer any timer running. Making it e.g.self.timer = QTimer(self)should fix. -
@rooster37
Hello and welcome.Firstly please take the trouble to enclose blocks of code like yours with a line of
```before and after it, which is what the Code (</>) toolbar button above where you type in does. This is especially important in the case of Python code, where leading whitespace is vital.But the Main Window displays a zero that does not change.
timer = QTimer(self)You assign the
QTimerto a local variable. That gets destroyed immediately afterself.showRPM(), and hence there is no longer any timer running. Making it e.g.self.timer = QTimer(self)should fix. -
@JonB I changed
timer = QTimer(self)toself.timer = QTimer(self)indef __init__(self, parent=None):and It still does not display the RPM. It displays a steady 0. I am a newbie when it come to python and QT.@rooster37 I don't understand your "def showRPM(self)" - does it really contain other functions like "def init_GPIO()"? If so you do not call them! Instead you always set "rpm" which never changes (because "def calculate_speed(wheel_diameter)" is never called)...
-
@JonB I changed
timer = QTimer(self)toself.timer = QTimer(self)indef __init__(self, parent=None):and It still does not display the RPM. It displays a steady 0. I am a newbie when it come to python and QT.@rooster37
I you put aprint("Called showRPM")as the first statement in the body ofdef showRPM(self)(i.e. by the looks of it just aboveself.ui.RPMlcdNumber.display(rpm)) you will know how many times it is called, and therefore whether yourQTimeris working.For the rest of it please answer @jsulm's questions.