Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

how can I create a signal to use from inside an rt_task?



  • I have a rt_task which runs on a xenomai kernel. I wanted to add a GUI, for it so I created a MainWindow project in qtcreator (qt5) and started playing with it.
    The problem I have is that when something happens inside my rt_task, I have to change something in the GUI, and I know the correct way is to use signal/slot, but I don't know how to create or emit a signal from a rt_thread.
    in the main.cpp the relevant part is this:

    void ethercat_process_run(void *arg)
    {
        RTIME now, previous=0;
        int32_t toff=0;
        RTIME rt_ts = rt_timer_read();
    
        while (run)
        {
    ...bla bla...
        }
    }
    
    int main(int argc, char *argv[])
    {
    ...cut...
        rt_task_create(&ethercat_task, "ethercat_proccess_task", 0, 99, 0);
        rt_task_set_affinity(&ethercat_task, &cpu_set_ecat); 		//CPU affinity forEtherCAT task
        rt_task_start(&ethercat_task, &ethercat_process_run, NULL);
    ...cut...
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        return a.exec();
    }
    


  • Signals are public methods. Just create a QObject containing your signals and call those methods.

    class rt_taskMessanger : public QObject{
    Q_OBJECT
    Q_DISABLE_COPY(rt_taskMessanger)
    public:
    explicit rt_taskMessanger(QObject* parent = nullptr) : QObject(parent){}
    signals:
    void somethingHappened();
    };
    
    rt_taskMessanger * rtMessanger;
    int main(int argc, char *argv[])
    {
    rt_taskMessanger msg;
    rtMessanger = &msg;
    ...cut...
        rt_task_create(&ethercat_task, "ethercat_proccess_task", 0, 99, 0);
        rt_task_set_affinity(&ethercat_task, &cpu_set_ecat); 		//CPU affinity forEtherCAT task
        rt_task_start(&ethercat_task, &ethercat_process_run, NULL);
    ...cut...
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    QObject::connect(rtMessanger,&rt_taskMessanger::somethingHappened,[](){qDebug("Something Happened");});
        return a.exec();
    }
    
    void ethercat_process_run(void *arg)
    {
        RTIME now, previous=0;
        int32_t toff=0;
        RTIME rt_ts = rt_timer_read();
    
        while (run)
        {
    ...bla bla...
    rtMessanger->somethingHappened();
        }
    }
    

    P.S.
    make sure your while (run) doesn't block the main event loop otherwise you'll probably need to move that part to a new thread



  • @VRonin Thank you for your quick answer.
    however I have an error on qDebug, saying: expected unqualified-id.
    I added #include <QDebug>, but I can't find around what I'm missing..



  • Does it matter? that's just placeholder code, don't waste your time on it. replace it with std::cout if need be



  • @VRonin The class you suggested works ok.
    Thanks for your help.



  • @spiderdab said in how can I create a signal to use from inside an rt_task?:

    The class you suggested works ok.

    great, so please don't forget to mark your post as solved!


Log in to reply