Helkhoury
Posts: 4
Joined: Sat Nov 26, 2016 1:25 am

Help with pigpio in QtCreator

Sat Nov 26, 2016 1:32 am

I have a Rpi 3 running Qt5 and Qt Creator, all on the Pi itself. All works great. I am able to compile a cpp file with a GUI. Now i am moving to actually doing something with it. I chose to go with the pigpio library found on http://abyz.co.uk/rpi/pigpio/index.html

I followed the installation on http://abyz.co.uk/rpi/pigpio/download.html and got all the way to section "To check the library". All tests pass.

Problem 1: Qt Creator could not locate the header file Solution 1: Added INCLUDEPATH += /usr/local/include to my .pro file Question 1: Is there a better way to add this to my environment so i don't have to repeat with every project?

Problem 2: Although the editor now finds the pigpio.h file when i include it, AND I am able to retrieve the functions out of the file (with intellisense...so it is working), the compile fails with error that the function calls out of the pigpio.h file are not recognized.
Does anyone know a fix for this? Am I missing a compilation parameter in Qt?

So here is what i have so far. I wrote a program, single file to make things easy, then ran
gcc -o main main.cpp -lpigpio -lrt -lpthread at the command prompt which generated the main executable. I then ran sudo ./main in the command prompt, and I see the expected output.

So this works, therefore i can say my environment (the OS and all add-ons) works. But the QtCreator setup does not recognize the pigpio library. If i run any program without the pigpio, it works as expected. so I also know my QtCreator setup works as a development tool on the Pi. Just not with pigpio.

Now here is where my level of incompetence kicks in, where do i add those compile flags into my QtCreator environment?

I am dying to get started on the GUI and hardware programming using the library, but for some odd reason, i cannot figure out how to overcome this one step.

I hope someone here has done that before.

thanks,
H-

User avatar
topguy
Posts: 5773
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Help with pigpio in QtCreator

Wed Nov 30, 2016 11:44 am

If you had to add and "INCLUDEPATH" then you probably need to add a "LIBPATH" too.

Helkhoury
Posts: 4
Joined: Sat Nov 26, 2016 1:25 am

Re: Help with pigpio in QtCreator

Mon Dec 05, 2016 1:49 pm

Topguy, Thanks for the input. I have done it and now i am in the next problem. Here is the update. I just moved the thread here from another forum. Joan was helping, but now the issue is setup, not pigpio. Here is why:

Added the following lines to the QT Creator .pro project:

Code: Select all

    INCLUDEPATH += /usr/include/
    LIBS += -lpigpio -lrt -lpthread
Now my project compiles and runs. But the pigpio is not getting initialized when the project is running from Qt Creator. More on why later, but at least the project runs in my environment. I tried it in the command line, and it works, it actually works as expected. So there is something preventing the library from getting initialized when called from Qt Creator.
Here is the code from the command line.

Code: Select all

    [email protected]:~ $ sudo killall pigpiod
    [email protected]:~ $ gcc -o main main.cpp -lpigpio -lrt -lpthread 
    [email protected]:~ $ sudo ./main
    pigpio initialisation passed
    [email protected]:~ $ 
So there you have it, It is working as expected from the command prompt.
So the issue now is the integration into Qt Creator. When I run the same file, which i have included below, from QtCreator, I get the output "pigpio initialisation failed" which tells me the

Code: Select all

gpioInitialise()
is returning false and the program returns. So it is running, compiling, but the library is not getting initilized. Same main file as the one i did from the command prompt.

Anyone knows what the problem is? It seems from @joan feedback that there is another process running. I tried everything to make sure there are no other processes running the library, and thre weren't. Based on the latest, seems the process is getting started or running when i debug my project in Qt Creator. Also happens when I do a release build.
So something is starting the process, and by the time it gets to it in my code, it flags it as running again. I will keep trying to debug this issue, but I am on day 6 now and no progress on operating the GPIO on my Pi from Qt.
I will try another library just to get me going. I really wanted the pigpio to run. Anyone has that problem? Or the same setup but it works?

Code: Select all

#include <stdio.h>
#include <pigpio.h>

int main(int argc, char *argv[])
{
//QApplication a(argc, argv);
//MainWindow w;
//w.show();

double start;

if (gpioInitialise() < 0)
   {
      fprintf(stderr, "pigpio initialisation failed\n");
      return 1;
   }
else
{
    fprintf(stderr, "pigpio initialisation passed\n");
}

   /* Set GPIO modes */
   gpioSetMode(4, PI_OUTPUT);
   gpioSetMode(17, PI_OUTPUT);
   gpioSetMode(18, PI_OUTPUT);
   gpioSetMode(23, PI_INPUT);
   gpioSetMode(24, PI_OUTPUT);

   /* Start 1500 us servo pulses on GPIO4 */
   gpioServo(4, 1500);

   /* Start 75% dutycycle PWM on GPIO17 */
   gpioPWM(17, 192); /* 192/255 = 75% */

   start = time_time();

   while ((time_time() - start) < 60.0)
   {
      gpioWrite(18, 1); /* on */

      time_sleep(0.5);

      gpioWrite(18, 0); /* off */

      time_sleep(0.5);

      /* Mirror GPIO24 from GPIO23 */
      gpioWrite(24, gpioRead(23));
   }

   /* Stop DMA, release resources */
   gpioTerminate();

   //w.hide();
return 0; //a.exec();
}
Thanks, H-

User avatar
joan
Posts: 14260
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Help with pigpio in QtCreator

Mon Dec 05, 2016 3:50 pm

pigpio should print an explanatory message to stderr if initialisation fails.

Helkhoury
Posts: 4
Joined: Sat Nov 26, 2016 1:25 am

Re: Help with pigpio in QtCreator

Wed Dec 07, 2016 5:18 am

joan wrote:pigpio should print an explanatory message to stderr if initialisation fails.
I am trying to find out how to get the output of stderr on Qt output window so i can get you the info. I have posted on the Qt forum. As soon as i can get it fixed, i will post the output i am getting here.

Thanks,
H-

zer02pi
Posts: 18
Joined: Thu Dec 01, 2016 6:39 pm

Re: Help with pigpio in QtCreator

Wed Dec 07, 2016 4:09 pm

Helkhoury wrote:
joan wrote:pigpio should print an explanatory message to stderr if initialisation fails.
I am trying to find out how to get the output of stderr on Qt output window so i can get you the info. I have posted on the Qt forum. As soon as i can get it fixed, i will post the output i am getting here.

Thanks,
H-
probably you are not running program as root. try this:

1. go to tools - options - enviorement on qtcreator.
2. in tab named "general" there is a terminal option. write /usr/bin/gnome-terminal -x sudo in there. apply it and close option window.
3. then press to projects in the main screen. it is in left under debug. there has to be this entry: "run in terminal" check if it is not checked. (i do not remember exactly. it can be output instead of projects. check both of them )

now you can run your program as root on qt creator.

Helkhoury
Posts: 4
Joined: Sat Nov 26, 2016 1:25 am

Re: Help with pigpio in QtCreator

Thu Dec 08, 2016 3:56 am

zer02pi,
Thank you for the help. I took your input and tried it, and first it did not work.
I then followed your lead and googled a few things, and ended up putting /usr/bin/x-terminal-emulator -e sudo

I actually have no idea what the difference is, but it worked.
So the fix is based on your input:
1. go to tools - options - environment on qtcreator.
2. in tab named "general" there is a terminal option. write /usr/bin/x-terminal-emulator -e sudo in there. apply it and close option window.
3. then press to projects in the main screen. it is in left under debug. Then on the top, click on Run instead of Build. Check "run in terminal"

My project runs and the terminal now shows the pigpio is initialized. Next step is to actually connect some hardware and get it working. I will report back and let everyone know this is the fix for sure. But it is a great step in the right direction.

thanks for all the help here. It's great.

H-

User avatar
joan
Posts: 14260
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Help with pigpio in QtCreator

Thu Dec 08, 2016 9:05 am

As you are linked against libpigpio you need to use sudo as the library needs root privileges to run.

An alternative would be to link against libpigpiod_if2 as that has a couple of advantages. Firstly you don't need to use sudo to run your program, secondly you can compile and run your program on a networked Linux/Mac box. I.e. You can control your Pi from the network. The libpigpiod_if2 library is pretty much a C equivalent of the pigpio Python module.

I would recommend using libpigpiod_if2 if possible.

http://abyz.co.uk/rpi/pigpio/pdif2.html

Return to “C/C++”