Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. adding qtimer with wiringPiISR

adding qtimer with wiringPiISR

Scheduled Pinned Locked Moved Solved Mobile and Embedded
raspberrycross-compilingwiringpithreadstimer
12 Posts 3 Posters 1.6k 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.
  • J jsulm
    30 Mar 2021, 11:00

    @amina said in timer couldn't work with wiringPiISR:

    and now I want to calculate the number of impulse detected per second

    Then the timeout should be 1000 not 500.
    Also, wouldn't it be enough to use the number of calls of your isrCallback()? I don't know why you are doing this complex calculations inside onTimeout. You should simply have a counter incremented inside isrCallback() which you read in onTimeout to get the number of calls and then set to 0 for the next timeout.

    A Offline
    A Offline
    amina
    wrote on 30 Mar 2021, 11:52 last edited by amina
    #3

    @jsulm
    I just wanted to test that's why there is 500.
    my problem is that in the timeout function it never executes the instructions
    under this condition I don't know why
    if(m_pin==Pin_vitesse)
    {
    int vitesse;
    calcul_vitesse=m_impulsion-calcul_vitesse;
    vitesse= int (calcul_vitesse/4*2.166);
    emit vitesse_Changed(vitesse);
    qDebug()<<"VITESSEEEEEEEE"<<vitesse;
    }
    in the isrCallback I am incrementing m_impulsion so I am counting the number of impulses and in my onTimeout fonction I am calculating the numbre of impulses each timeout.
    and I am trying to calculate a speed m/s , each turn I have 4 impulses and 2.166 is the perimeter of the wheel

    J 1 Reply Last reply 30 Mar 2021, 11:54
    0
    • A amina
      30 Mar 2021, 11:52

      @jsulm
      I just wanted to test that's why there is 500.
      my problem is that in the timeout function it never executes the instructions
      under this condition I don't know why
      if(m_pin==Pin_vitesse)
      {
      int vitesse;
      calcul_vitesse=m_impulsion-calcul_vitesse;
      vitesse= int (calcul_vitesse/4*2.166);
      emit vitesse_Changed(vitesse);
      qDebug()<<"VITESSEEEEEEEE"<<vitesse;
      }
      in the isrCallback I am incrementing m_impulsion so I am counting the number of impulses and in my onTimeout fonction I am calculating the numbre of impulses each timeout.
      and I am trying to calculate a speed m/s , each turn I have 4 impulses and 2.166 is the perimeter of the wheel

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 30 Mar 2021, 11:54 last edited by
      #4

      @amina said in adding qtimer with wiringPiISR:

      if(m_pin==Pin_vitesse)

      Why do you need this?

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

      A 1 Reply Last reply 30 Mar 2021, 11:58
      0
      • J jsulm
        30 Mar 2021, 11:54

        @amina said in adding qtimer with wiringPiISR:

        if(m_pin==Pin_vitesse)

        Why do you need this?

        A Offline
        A Offline
        amina
        wrote on 30 Mar 2021, 11:58 last edited by amina
        #5

        @jsulm said in adding qtimer with wiringPiISR:

        Why do you need this?

        because I have two other instances of this class, so in the slot onTimeout I just want to calculate the speed of this instance and not for the others

        static Capteur_Input vitesse(Pin_vitesse,PUD_OFF,INT_EDGE_RISING,isrInput_vitesse);
        
        Capteur_Input frein(PinFrein,PUD_UP,NO_INTERRUPT);
            Capteur_Input clignotant_G(PinClignotnat_G,PUD_DOWN,NO_INTERRUPT);
        

        maybe I should create 2 classes one for inputs and another for interruptions

        J 1 Reply Last reply 30 Mar 2021, 12:02
        0
        • A amina
          30 Mar 2021, 11:58

          @jsulm said in adding qtimer with wiringPiISR:

          Why do you need this?

          because I have two other instances of this class, so in the slot onTimeout I just want to calculate the speed of this instance and not for the others

          static Capteur_Input vitesse(Pin_vitesse,PUD_OFF,INT_EDGE_RISING,isrInput_vitesse);
          
          Capteur_Input frein(PinFrein,PUD_UP,NO_INTERRUPT);
              Capteur_Input clignotant_G(PinClignotnat_G,PUD_DOWN,NO_INTERRUPT);
          

          maybe I should create 2 classes one for inputs and another for interruptions

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 30 Mar 2021, 12:02 last edited by
          #6

          @amina put another qDebug before the if() - do you see its output?

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

          A 1 Reply Last reply 30 Mar 2021, 12:12
          0
          • J jsulm
            30 Mar 2021, 12:02

            @amina put another qDebug before the if() - do you see its output?

            A Offline
            A Offline
            amina
            wrote on 30 Mar 2021, 12:12 last edited by amina
            #7

            @jsulm said in adding qtimer with wiringPiISR:

            void Capteur_Input::onTimeout()
            { 
                qDebug()<<"time is up";
            
                if(m_pin== Pin_vitesse)
                {
                    qDebug()<<"m_pin== Pin_vitesse";
                int vitesse;
            
                vitesse= int (calcul_vitesse/4*2.166);//*0.001/3600;
                qDebug()<<"VITESSEEEEEEEE"<<m_pin <<readPin()<<vitesse;
            
                emit vitesse_Changed(vitesse);
                qDebug()<<"VITESSEEEEEEEE"<<m_pin <<readPin()<<vitesse;
            
            
                }
                else{
            
                    emit digital_inputChanged(readPin());
                    qDebug()<<"signal DIGITAL emitted m_pin"<<m_pin <<"value"<<readPin();
            
                }
            }
            

            and this is the output so there is an output for the other two instances(frein,clignotant_G) and not the one that is an interrupt (Vitesse)

            time is up
            signal DIGITAL emitted m_pin 0 value 1
            time is up
            signal DIGITAL emitted m_pin 2 value 0
            

            the funny thing is that I used this same condition in the isrCallback and it is working this why i said maybe it is a thread problem ..

            P 1 Reply Last reply 30 Mar 2021, 12:31
            0
            • A amina
              30 Mar 2021, 12:12

              @jsulm said in adding qtimer with wiringPiISR:

              void Capteur_Input::onTimeout()
              { 
                  qDebug()<<"time is up";
              
                  if(m_pin== Pin_vitesse)
                  {
                      qDebug()<<"m_pin== Pin_vitesse";
                  int vitesse;
              
                  vitesse= int (calcul_vitesse/4*2.166);//*0.001/3600;
                  qDebug()<<"VITESSEEEEEEEE"<<m_pin <<readPin()<<vitesse;
              
                  emit vitesse_Changed(vitesse);
                  qDebug()<<"VITESSEEEEEEEE"<<m_pin <<readPin()<<vitesse;
              
              
                  }
                  else{
              
                      emit digital_inputChanged(readPin());
                      qDebug()<<"signal DIGITAL emitted m_pin"<<m_pin <<"value"<<readPin();
              
                  }
              }
              

              and this is the output so there is an output for the other two instances(frein,clignotant_G) and not the one that is an interrupt (Vitesse)

              time is up
              signal DIGITAL emitted m_pin 0 value 1
              time is up
              signal DIGITAL emitted m_pin 2 value 0
              

              the funny thing is that I used this same condition in the isrCallback and it is working this why i said maybe it is a thread problem ..

              P Offline
              P Offline
              Pl45m4
              wrote on 30 Mar 2021, 12:31 last edited by Pl45m4
              #8

              @amina

              So it seems that your if condition is never true.
              Is m_pin coming from your wiringPiISR interrupt function or is it set to a certain value? Print these values before the if clause. What value is Pin_vitesse?

              Edit:

              Have you seen this?

              • http://wiringpi.com/reference/priority-interrupts-and-threads/
              • int wiringPiISR (int pin, int edgeType, void (*function)(void)) ;

              The function will be called when the interrupt triggers. When it is triggered, it’s cleared in the dispatcher before calling your function, so if a subsequent interrupt fires before you finish your handler, then it won’t be missed. (However it can only track one more interrupt, if more than one interrupt fires while one is being handled then they will be ignored)

              Could be possible that all your interrupts on pin_vitesse are getting ignored. What happens in your interrupt function?


              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              A 1 Reply Last reply 30 Mar 2021, 12:53
              0
              • P Pl45m4
                30 Mar 2021, 12:31

                @amina

                So it seems that your if condition is never true.
                Is m_pin coming from your wiringPiISR interrupt function or is it set to a certain value? Print these values before the if clause. What value is Pin_vitesse?

                Edit:

                Have you seen this?

                • http://wiringpi.com/reference/priority-interrupts-and-threads/
                • int wiringPiISR (int pin, int edgeType, void (*function)(void)) ;

                The function will be called when the interrupt triggers. When it is triggered, it’s cleared in the dispatcher before calling your function, so if a subsequent interrupt fires before you finish your handler, then it won’t be missed. (However it can only track one more interrupt, if more than one interrupt fires while one is being handled then they will be ignored)

                Could be possible that all your interrupts on pin_vitesse are getting ignored. What happens in your interrupt function?

                A Offline
                A Offline
                amina
                wrote on 30 Mar 2021, 12:53 last edited by amina
                #9

                @Pl45m4
                m_pin is one of the attributes of this class
                Capteur_Input::Capteur_Input(int pin,int pud,int edge,void (*isrInput)(),QObject *parent):Capteur_(pin,parent)

                and it contains the gpio pin of my sensor I devined them in my capteur_Input.h

                #define Pin_vitesse 3
                #define PinFrein 0
                #define PinClignotnat_G 2
                
                void Capteur_Input::onTimeout()
                {
                    qDebug()<<"time is up"<<m_pin;
                
                    if(m_pin== Pin_vitesse)
                    {
                

                I added this before the condition and this is what I got as an output

                time is up 0
                signal DIGITAL emitted 0 1
                time is up 2
                signal DIGITAL emitted 2 0
                
                time is up 0
                signal DIGITAL emitted 0 1
                time is up 2
                

                so the instance static Capteur_Input vitesse(Pin_vitesse,PUD_OFF,INT_EDGE_RISING,isrInput_vitesse); is never executing the slot onTimeout ..

                the interrupts are not getting ingored, The interrupt function is incrementing an attribut m_impulsion and I am acually showing this number of pulses in the qml part so each interruption the number changes ..

                J 1 Reply Last reply 30 Mar 2021, 12:57
                0
                • A amina
                  30 Mar 2021, 12:53

                  @Pl45m4
                  m_pin is one of the attributes of this class
                  Capteur_Input::Capteur_Input(int pin,int pud,int edge,void (*isrInput)(),QObject *parent):Capteur_(pin,parent)

                  and it contains the gpio pin of my sensor I devined them in my capteur_Input.h

                  #define Pin_vitesse 3
                  #define PinFrein 0
                  #define PinClignotnat_G 2
                  
                  void Capteur_Input::onTimeout()
                  {
                      qDebug()<<"time is up"<<m_pin;
                  
                      if(m_pin== Pin_vitesse)
                      {
                  

                  I added this before the condition and this is what I got as an output

                  time is up 0
                  signal DIGITAL emitted 0 1
                  time is up 2
                  signal DIGITAL emitted 2 0
                  
                  time is up 0
                  signal DIGITAL emitted 0 1
                  time is up 2
                  

                  so the instance static Capteur_Input vitesse(Pin_vitesse,PUD_OFF,INT_EDGE_RISING,isrInput_vitesse); is never executing the slot onTimeout ..

                  the interrupts are not getting ingored, The interrupt function is incrementing an attribut m_impulsion and I am acually showing this number of pulses in the qml part so each interruption the number changes ..

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 30 Mar 2021, 12:57 last edited by
                  #10

                  @amina said in adding qtimer with wiringPiISR:

                  static Capteur_Input vitesse(Pin_vitesse,PUD_OFF,INT_EDGE_RISING,isrInput_vitesse);

                  Where exactly is this code located? You really need to debug your code to see whether vitesse is really created and what happens there.

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

                  A 1 Reply Last reply 30 Mar 2021, 13:14
                  0
                  • J jsulm
                    30 Mar 2021, 12:57

                    @amina said in adding qtimer with wiringPiISR:

                    static Capteur_Input vitesse(Pin_vitesse,PUD_OFF,INT_EDGE_RISING,isrInput_vitesse);

                    Where exactly is this code located? You really need to debug your code to see whether vitesse is really created and what happens there.

                    A Offline
                    A Offline
                    amina
                    wrote on 30 Mar 2021, 13:14 last edited by amina
                    #11

                    @jsulm
                    it is located in my main.cpp

                    static void isrInput_vitesse();
                    static Capteur_Input vitesse(Pin_vitesse,PUD_OFF,INT_EDGE_RISING,isrInput_vitesse);
                    static void isrInput_vitesse()
                    {
                        vitesse.isrCallback();
                    }
                    
                    int main(int argc, char *argv[])
                    {
                        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
                    
                        QGuiApplication app(argc, argv);
                    
                        QQmlApplicationEngine engine;
                        Capteur_Input frein(PinFrein,PUD_UP,NO_INTERRUPT);
                        Capteur_Input clignotant_G(PinClignotnat_G,PUD_DOWN,NO_INTERRUPT);
                        Capteur_ADC essence(-1,ADRESS,AIN1,DEVICE_ID);
                         Capteur_ADC temperature(-1,ADRESS,AIN2,DEVICE_ID);
                        QQmlContext* ctx = engine.rootContext();
                        ctx->setContextProperty("frein", &frein);
                        ctx->setContextProperty("vitesse", &vitesse);
                        ctx->setContextProperty("essence", &essence);
                        ctx->setContextProperty("clignotant_G", &clignotant_G);
                        ctx->setContextProperty("temperature", &temperature);
                    
                        const QUrl url(QStringLiteral("qrc:/main.qml"));
                        QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                                         &app, [url](QObject *obj, const QUrl &objUrl) {
                            if (!obj && url == objUrl)
                                QCoreApplication::exit(-1);
                        }, Qt::QueuedConnection);
                        engine.load(url);
                        return app.exec();
                    }
                    

                    I tried to debbug , and vitesse is created and is creating the timer , this is my constructor and it is executing the commands that starts a timer ..

                    Capteur_Input::Capteur_Input(int pin,int pud,int edge,void (*isrInput)(),QObject *parent):Capteur_(pin,parent)
                    {
                        m_pud=pud;
                        m_impulsion=0;
                        m_typeIO=INPUT;
                        calcul_vitesse=0;
                        wiringPiSetup();
                    
                    
                        switch (edge) {
                        case NO_INTERRUPT:
                        {
                            pinMode(m_pin,INPUT);
                            pullUpDnControl (m_pin, m_pud) ;
                        }            break;
                        case INT_EDGE_SETUP:
                        {
                            pinMode(m_pin,INPUT);
                            pullUpDnControl (m_pin, m_pud) ;
                            wiringPiISR(m_pin, INT_EDGE_SETUP, isrInput);
                        } break;
                    
                        case INT_EDGE_FALLING:
                        {
                            pinMode(m_pin,INPUT);
                            pullUpDnControl (m_pin, m_pud) ;
                            wiringPiISR(m_pin, INT_EDGE_FALLING, isrInput);
                        } break;
                    
                        case INT_EDGE_RISING:
                        {
                    
                            pinMode(m_pin,INPUT);
                            pullUpDnControl (m_pin, m_pud) ;
                            wiringPiISR(m_pin, INT_EDGE_RISING, isrInput);
                        } break;
                    
                        case INT_EDGE_BOTH:
                        {
                            pinMode(m_pin,INPUT);
                            pullUpDnControl (m_pin, m_pud) ;
                            wiringPiISR(m_pin, INT_EDGE_BOTH, isrInput);
                        }
                            break;
                        }
                        m_timer =new QTimer (this);
                        m_timer->setTimerType(Qt::PreciseTimer);
                        QObject::connect(m_timer, &QTimer::timeout, this, &Capteur_Input::onTimeout, Qt::DirectConnection);
                        m_timer->start(1000);
                    }
                    
                    A 1 Reply Last reply 31 Mar 2021, 10:09
                    0
                    • A amina
                      30 Mar 2021, 13:14

                      @jsulm
                      it is located in my main.cpp

                      static void isrInput_vitesse();
                      static Capteur_Input vitesse(Pin_vitesse,PUD_OFF,INT_EDGE_RISING,isrInput_vitesse);
                      static void isrInput_vitesse()
                      {
                          vitesse.isrCallback();
                      }
                      
                      int main(int argc, char *argv[])
                      {
                          QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
                      
                          QGuiApplication app(argc, argv);
                      
                          QQmlApplicationEngine engine;
                          Capteur_Input frein(PinFrein,PUD_UP,NO_INTERRUPT);
                          Capteur_Input clignotant_G(PinClignotnat_G,PUD_DOWN,NO_INTERRUPT);
                          Capteur_ADC essence(-1,ADRESS,AIN1,DEVICE_ID);
                           Capteur_ADC temperature(-1,ADRESS,AIN2,DEVICE_ID);
                          QQmlContext* ctx = engine.rootContext();
                          ctx->setContextProperty("frein", &frein);
                          ctx->setContextProperty("vitesse", &vitesse);
                          ctx->setContextProperty("essence", &essence);
                          ctx->setContextProperty("clignotant_G", &clignotant_G);
                          ctx->setContextProperty("temperature", &temperature);
                      
                          const QUrl url(QStringLiteral("qrc:/main.qml"));
                          QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                                           &app, [url](QObject *obj, const QUrl &objUrl) {
                              if (!obj && url == objUrl)
                                  QCoreApplication::exit(-1);
                          }, Qt::QueuedConnection);
                          engine.load(url);
                          return app.exec();
                      }
                      

                      I tried to debbug , and vitesse is created and is creating the timer , this is my constructor and it is executing the commands that starts a timer ..

                      Capteur_Input::Capteur_Input(int pin,int pud,int edge,void (*isrInput)(),QObject *parent):Capteur_(pin,parent)
                      {
                          m_pud=pud;
                          m_impulsion=0;
                          m_typeIO=INPUT;
                          calcul_vitesse=0;
                          wiringPiSetup();
                      
                      
                          switch (edge) {
                          case NO_INTERRUPT:
                          {
                              pinMode(m_pin,INPUT);
                              pullUpDnControl (m_pin, m_pud) ;
                          }            break;
                          case INT_EDGE_SETUP:
                          {
                              pinMode(m_pin,INPUT);
                              pullUpDnControl (m_pin, m_pud) ;
                              wiringPiISR(m_pin, INT_EDGE_SETUP, isrInput);
                          } break;
                      
                          case INT_EDGE_FALLING:
                          {
                              pinMode(m_pin,INPUT);
                              pullUpDnControl (m_pin, m_pud) ;
                              wiringPiISR(m_pin, INT_EDGE_FALLING, isrInput);
                          } break;
                      
                          case INT_EDGE_RISING:
                          {
                      
                              pinMode(m_pin,INPUT);
                              pullUpDnControl (m_pin, m_pud) ;
                              wiringPiISR(m_pin, INT_EDGE_RISING, isrInput);
                          } break;
                      
                          case INT_EDGE_BOTH:
                          {
                              pinMode(m_pin,INPUT);
                              pullUpDnControl (m_pin, m_pud) ;
                              wiringPiISR(m_pin, INT_EDGE_BOTH, isrInput);
                          }
                              break;
                          }
                          m_timer =new QTimer (this);
                          m_timer->setTimerType(Qt::PreciseTimer);
                          QObject::connect(m_timer, &QTimer::timeout, this, &Capteur_Input::onTimeout, Qt::DirectConnection);
                          m_timer->start(1000);
                      }
                      
                      A Offline
                      A Offline
                      amina
                      wrote on 31 Mar 2021, 10:09 last edited by
                      #12

                      @amina

                      I posted this in an other forum because it has been a while that I am stuck and it has to be fixed .. I am going to share the solution it may help someone https://stackoverflow.com/questions/66869600/timer-couldnt-work-with-wiringpiisr-qtquick-application/66873812?noredirect=1#comment118230663_66873812

                      static instances of QObject are also not supported

                      so the instance vitesse should be created after QApplication.
                      this will fix the problem :

                      static void isrInput_vitesse();
                      
                      static Capteur_Input *vitesse = nullptr;
                      
                      static void isrInput_vitesse()
                      {
                          if(!vitesse) //not initialized yet
                               return; 
                          
                          QMetaObject::invokeMethod( vitesse, "isrCallback", Qt::QueuedConnection ); //or blockingQueue if you need to handle it directly in Qt way.
                      }
                      
                      and in the main fucntion  int main(int argc, char *argv[])
                       {
                             QApplication a(argc, argv);
                      
                             //.....  your main application body  
                      
                             vitesse = new  Capteur_Input(Pin_vitesse,PUD_OFF,INT_EDGE_RISING,isrInput_vitesse);
                          ctx->setContextProperty("vitesse", vitesse);
                      
                      
                             //...
                       } 
                      
                      

                      the function isrCallback should be a slot

                      1 Reply Last reply
                      3

                      12/12

                      31 Mar 2021, 10:09

                      • Login

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