Qt World Summit: Register Today!

Raspberry pi Qt plotting with high CPU usage

  • I use Qt creator cross-compile on Raspberry pi B+ to develope an App that plot data from file. The program work fine on my laptop but the plot use hi processing on pi B+. when I run the program on my laptop it work with less than 1% of CPU and on pi with 1-2% CPU, when I start the plotting on my Laptop it take about 13% CPU but on pi it take about 95-99% cpu!!!. Plotting on my laptop rich more than 60 frame per second but on pi just 14-16 frame per second and when I overclocked the pi I got about 35 frame per second. my laptop run Ubuntu 14.04 with gnome shell and the CPU is intel centrino core 2 Dou 2 GHz with 4 GB RAM and NVIDIA. This is the source code of my Qt App

    #include "mainwindow.h"
        #include "ui_mainwindow.h"
        ///*********** STARTING THE CLASS **************************///
        MainWindow::MainWindow(QWidget *parent) :
            ui(new Ui::MainWindow)
        //********************* Read File **************************//
            QFile inputFile("ECG_Data.txt");
            if (inputFile.open(QIODevice::ReadOnly))
               QTextStream in(&inputFile);
               int i=0;
               while (!in.atEnd())
                  QString line = in.readLine();
        ///**************** START BUTTON *************************///
        void MainWindow::on_pushButton_clicked()
            stop= true;
        ///***************** STOP BUTTON **************************///
        void MainWindow::on_pushButton_2_clicked()
            stop = false;
        ///**************** PLOTTING *******************************///
        void MainWindow::plotting()
        //***** set plot parameters *******************************//
            ui->customPlot->graph(0)->setPen(QPen(Qt::yellow)); //set plot line color
        //***** set plot background color ****************************//
            QLinearGradient plotGradient;
            plotGradient.setStart(0, 0); //set start point
            plotGradient.setFinalStop(0, 350); //set final point
            plotGradient.setColorAt(1, QColor(100, 100, 100)); // set background color
        //***** creat connection for plot Axes *************************//
            // make left and bottom axes transfer their ranges to right and top axes:
            connect(ui->customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->xAxis2, SLOT(setRange(QCPRange)));
            connect(ui->customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->yAxis2, SLOT(setRange(QCPRange)));
        //***** creat connection for plot founction *********************//
            // setup a timer that repeatedly calls MainWindow::realtimeDataSlot();
            connect(&dataTimer, SIGNAL(timeout()), this, SLOT(readResponse()));
            dataTimer.start(10); // Interval 0 means to refresh as fast as possible
    ///**************** REAL TIME PLOTTING SLOT *********************///
        void MainWindow::readResponse()
        { if(j==500){j=0;}
            double value0 = fileData[j];
            if (stop)
        //***** calculate two new data points: **************************//
            double key = QDateTime::currentDateTime().toMSecsSinceEpoch()/1000.0; //first data point of plot from time
            static double lastPointKey = 0;
            if (key-lastPointKey > 0.01) // at most add point every 10 ms
            { //emit plottingData(rx);
            //  double value0 = rxplot; // convert string data of serial port to Double to be the second data point of plot
        //***** add data to lines: *****************************//
              ui->customPlot->graph(0)->addData(key, value0);
              // remove data of lines that's outside visible range:
              lastPointKey = key;
    //make key axis range scroll with the data (at a constant range size of 8):
            ui->customPlot->xAxis->setRange(key+0.25, 2, Qt::AlignRight);
    //***** calculate frames per second: *********************************//
            static double lastFpsKey;
            static int frameCount;
             if (key-lastFpsKey > 2) // average fps over 2 seconds
             { ui->statusBar->showMessage(QString("%1 FPS, Total Data points: %2").arg(frameCount/(key-lastFpsKey), 0, 'f', 0).arg(ui->customPlot->graph(0)->data()->count()), 0 );
              lastFpsKey = key;
              frameCount = 0;
        ///****************** DESTRUCTOR ****************************///
            delete ui;

    The App work with full screen on pi "this is the second problem" and don't receive touch event from the touchscreen. I use Qt 5.4 and on pi the operating system is Raspbian Wheezy. I fllowed this instractions to configure the cross-compile on pi here.

    This is the photo of App window on pi screen

    Please help me :(

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Don't get me wrong but are you aware that you are comparing a Dual Core 2GHz (so 2 Core at 2GHz) with 4GB of RAM and a low power ARM processor at 700MHz with 512MB or RAM ? Not to mention the GPU power levels that are also pretty different.

    The first thing I'd check is how much data you are trying to process and show and optimize that.