Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. QtonPi
  4. Raspberry PI 4 Tachometer doesn't display data
QtWS25 Last Chance

Raspberry PI 4 Tachometer doesn't display data

Scheduled Pinned Locked Moved Unsolved QtonPi
6 Posts 3 Posters 1.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rooster37
    wrote on last edited by VRonin
    #1

    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_())
    
    JonBJ 1 Reply Last reply
    0
    • R rooster37

      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_())
      
      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #2

      @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 QTimer to a local variable. That gets destroyed immediately after self.showRPM(), and hence there is no longer any timer running. Making it e.g. self.timer = QTimer(self) should fix.

      R 1 Reply Last reply
      1
      • JonBJ JonB

        @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 QTimer to a local variable. That gets destroyed immediately after self.showRPM(), and hence there is no longer any timer running. Making it e.g. self.timer = QTimer(self) should fix.

        R Offline
        R Offline
        rooster37
        wrote on last edited by
        #3

        @JonB I changed timer = QTimer(self) to self.timer = QTimer(self) in def __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.

        jsulmJ JonBJ 2 Replies Last reply
        0
        • R rooster37

          @JonB I changed timer = QTimer(self) to self.timer = QTimer(self) in def __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.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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)...

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • R rooster37

            @JonB I changed timer = QTimer(self) to self.timer = QTimer(self) in def __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.

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by JonB
            #5

            @rooster37
            I you put a print("Called showRPM") as the first statement in the body of def showRPM(self) (i.e. by the looks of it just above self.ui.RPMlcdNumber.display(rpm)) you will know how many times it is called, and therefore whether your QTimer is working.

            For the rest of it please answer @jsulm's questions.

            1 Reply Last reply
            0
            • R Offline
              R Offline
              rooster37
              wrote on last edited by
              #6

              QTimer is working. The problem is that I didn't call the function.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved