element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Sci Fi Your Pi
  • Challenges & Projects
  • Design Challenges
  • Sci Fi Your Pi
  • More
  • Cancel
Sci Fi Your Pi
Blog Quad Cop with Microstack GPS - A Multi-Threaded Approach for C++ Users
  • Blog
  • Forum
  • Documents
  • Files
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: screamingtiger
  • Date Created: 20 May 2015 9:01 PM Date Created
  • Views 1043 views
  • Likes 5 likes
  • Comments 6 comments
  • quadcop_project
  • design_challenge
  • sci_fi_your_pi
Related
Recommended

Quad Cop with Microstack GPS - A Multi-Threaded Approach for C++ Users

screamingtiger
screamingtiger
20 May 2015

Application Information
http://www.element14.com/community/community/design-challenges/sci-fi-your-pi/blog/2015/04/22/some-information-from-my-application

ChipKit Pi Vs Arduino Pro Mini
http://www.element14.com/community/community/design-challenges/sci-fi-your-pi/blog/2015/05/01/quick-update-on-the-quadcop-and-the-chipkit-pi

Quadcopter Assembled (You call that a Quadcopter?)
http://www.element14.com/community/community/design-challenges/sci-fi-your-pi/blog/2015/05/06/quadcopter-assembled

QuadCop -The Control Switch
http://www.element14.com/community/videos/16202/l/control-switch-explanation

Quad Cop with ChipKit Pi - An "Experience" with Innovation Required

http://www.element14.com/community/community/design-challenges/sci-fi-your-pi/blog/2015/05/07/quad-cop-with-chipkit-pi--an-experience-with-innovation

The Raspberry Pi goes for a fly!  With Pi cam

http://www.element14.com/community/community/design-challenges/sci-fi-your-pi/blog/2015/05/11/quadcop-the-raspberry-pi-goes-for-a-fly

 

For the Raspberry Pi Flight System (RPFS) I want the Pi to do all the GPS manipulation, parsing and calculations.  Working with the GPS is only one of several tasks the RPFS will be doing.  Because  Pi 2 now has multiple cores so this make multi-tasking a really good option.

 

The Microstack GPS is connected to the Pi's serial port and spits out NMEA strings.  For more information see http://www.gpsinformation.org/dale/nmea.htm.  NMEA is a common format for exchanging GPS data.

One way to do multi tasking within a single application is to use threads.  In GNU C, there is a thread library called PThreads that is very easy to use.  I decided I would write a multi-threaded object class that can easily be reused in any project with minimal effort.  To scope out what it takes to make this possible, here is what needs to be accomplished:

 

  1. Read the data from the serial port on the Pi, which is /dev/ttyAMA0.  This reading must be interrupt driven and buffered.  We don't want to miss any bytes.
  2. Parse the NMEA data coming from the GPS via the serial port, perform validation and fill out variables with all the information that the GPS can provide.
  3. Provide an easy way to access the variables, that is thread safe.  What this means is that when reading the variables, a flag must be set not to update them in the middle of the read.  This will cause odd results to come through.
  4. An instance of the object can be created, started, and it will begin parsing the data in the backround and provide information as requested.  No more interaction is required by the host program.  Basically a "start and forget" mentality.

 

 

 

After doing some research, I found how to read the serial port with an interrupt as well as a small pre-written library for parsing NMEA data.  The library is called TinyGPS++ and it is actually written for the Arduino.  I ported it over to the Raspberry Pi for use with C and C++.  You can find the TinyGPS++ library here: http://arduiniana.org/libraries/TinyGPS/

 

I then wrapped the serial code and the TinyGPS++ code into a nice multithreaded GPS class.

Here is an example of how to use my new C++ class which has gps.h and gps.cpp as well as the 2 TinyGPS++ code files.

 

 

include <iostream>
using namespace std;
#include "gps.h"

int main(void)
{
        GPS *gps = new GPS();
        gps->Initialize();
        gps->Start();
        while(1)
        {
                sleep(5);
                cout << gps->GetAge() << endl;
                cout << gps->GetLat() << endl;
                cout << gps->GetLong() << endl;
                cout << gps->GetAlt() << endl << endl;
        }
        delete gps;
        return 0;
}

 

 

As you can see, it is as easy as creating the GPS object, initializing and starting it. I have implemented a few functions for testing:

GetAge - How old the GPS coordinates are in seconds.  The Microstack sends new information every 1 second so typically the age of the coordinates is around .3 to .5 seconds.  This is a good thing to check as if the coordinates are too old you may want to wait for an update.

GetLat - Get the current latitude

GetLong - Get the current longitude

GetAlt - Get the current altitude

 

The reason I use functions instead of directly accessing variables is because we need thread safe reads:

double GPS::GetLat()
{
        double l;
        readBlock = true;
        l = currentLat;
        readBlock = false;
        return l;
}

 

The readBlock flag is checked in the threaded update code, and as long as the flag is set to true, it wont update the variables.

 

If anyone is interested in using my GPS object above, please let me know.  I can send the code out as well as how to compile it.

It is designed for the RPi and microstack.  I have some cleanup to do in the code but I am happy to pass it a long.  Once I get it completed I will be putting it up on my personal blog.  But if you want to get started with the GPS now with C++, here is a way to get going.

 

Another update is that I have the ChipKit Pi working completely as a control switch.  Here is a demo flight I did with the control switch reading my radio inputs and sending them to the flight controller.  A small step in the right direction.  I am working hard to get some auto flight code done, and having the GPS working is a HUGE step in that direction.  It doesn't look like much but a lot is going on.  The ChipKit pi is reading the PWM signals from the Rx, and then passing them to the flight controller on the quad via the software PWM library.  A switch on the radio will put the Raspberry Pi into control, and my radio inputs will be ignored except to put it back into manual control at my request.  Its been very windy the last 2 weeks so I had to settle for a simple hover inside, a bit nerve racking.  This is in fact the first test of this.   Did it turn out ok?

 

image

 

Here is a video of the short hover flight:

 

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

 

Here is some rambling about what is going on so far with this, this is a bad video my apologies I just felt I need some "proof" since the flight looks like any other flight  image

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

  • Sign in to reply

Top Comments

  • balearicdynamics
    balearicdynamics over 10 years ago +1
    Always with great compliments, Joey! It is really good. When you have time I have something to ask you about the chipkit that I have already in doubt. But this is another question. You mention to use C…
  • balearicdynamics
    balearicdynamics over 10 years ago in reply to screamingtiger +1
    I see. I was just thinking to how much time need a system based on daemons. Maybe better but it is sure more complex and more difficult to fine tune. If I were you to approach something like this I had…
  • screamingtiger
    screamingtiger over 10 years ago in reply to balearicdynamics +1
    I agree, if I were going for portability or ease of use separate applications that worked together would be a good way to go. I'm crunched for time and lost time due to my ChipKit ignorance, so going to…
  • chalek1992
    chalek1992 over 8 years ago

    Cześć Joey Thompson

    Im próbuje zastosować kod w C ++ do raspbery PI 3, która działa pod Ubuntu kolegi, właśnie kupiłem Adafruit ostateczny gps breakout v3 GPS i mam pewne problemy w kompilacjach, Pomożesz z nim ?? Co należy dokładnie umieścić w oknie polecenia, a które pliki należy używać, może starać się rozmawiać za pośrednictwem poczty elektronicznej lub skype, ja nie chcę spamu tutaj, uważa Mike

    My email: loniewskiguitar92@gmail.com

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • balearicdynamics
    balearicdynamics over 10 years ago in reply to screamingtiger

    Trust me, your quadcop beauty is when there is no ground it is laying on image

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • screamingtiger
    screamingtiger over 10 years ago in reply to balearicdynamics

    I agree, if I were going for portability or ease of use separate applications that worked together would be a good way to go.

    I'm crunched for time and lost time due to my ChipKit ignorance, so going to make things easier for me.

     

    I'm working on some navigation code now...  Things should get interesting.   I also need to find a better quadcopter, I mean one that looks better that is!

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • balearicdynamics
    balearicdynamics over 10 years ago in reply to screamingtiger

    I see. I was just thinking to how much time need a system based on daemons. Maybe better but it is sure more complex and more difficult to fine tune. If I were you to approach something like this I had to provide at lest more than 2 or 3 drones image

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • screamingtiger
    screamingtiger over 10 years ago in reply to balearicdynamics

    The issue with using different programs, is inter-program communication.    For example, if I have the GPS as one program, and another program that performs another task needs GPS information, how do I pass the GPS information between them?  There are ways such as a web service, a port service etc but that is a lot of overhead.

     

    My goal is to create 1 program, called "RasberryPiAutoControl" that is multithreaded.  This way it can do all the tasks needed with threads, but all the threads share memory and code space.  This allow passing of the information to be done in memory and no more parsing, reading etc is required.

    That's the main reason I am doing it that way, otherwise on top of writing the programs I also have to write subprograms and additional code for passing of information AND validation.  Anytime you pass information like that you also need validation.  When passing information in memory space, we assume memory reads are clean otherwise we have a serious problem.

     

    I had looked at using GPSD but after trying it out I realized all it does is return NMEA strings which I end up parsing anyways.  I would still need interrupt driven code to read a TCP port instead of a serial port, so I decided there was no a point and just read data form serial directly.

     

    If GPSD or something like it could read the serial, and parse the data.  Then allow me to read specific information without parsing such as latitude, that would be nice.

     

    I do see what you are saying, to have a few deamons running that work together to fly, that would be a good approach as well.  But given the time frame I don't have time for that type of overhead.

     

    Joey

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2026 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube