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
The Raspberry Pi goes for a fly! With Pi cam
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:
- 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.
- 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.
- 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.
- 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?
Here is a video of the short hover flight:
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 

Top Comments