root / quad1 / AeroQuad / GPS.h @ 9240aaa3
History | View | Annotate | Download (4.51 KB)
1 |
/*
|
---|---|
2 |
AeroQuad v2.1 - October 2010
|
3 |
www.AeroQuad.com
|
4 |
Copyright (c) 2010 Ted Carancho. All rights reserved.
|
5 |
An Open Source Arduino based multicopter.
|
6 |
|
7 |
This program is free software: you can redistribute it and/or modify
|
8 |
it under the terms of the GNU General Public License as published by
|
9 |
the Free Software Foundation, either version 3 of the License, or
|
10 |
(at your option) any later version.
|
11 |
|
12 |
This program is distributed in the hope that it will be useful,
|
13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15 |
GNU General Public License for more details.
|
16 |
|
17 |
You should have received a copy of the GNU General Public License
|
18 |
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
19 |
*/
|
20 |
|
21 |
/**************************************************************/
|
22 |
/************************** GPS *******************************/
|
23 |
/**************************************************************/
|
24 |
// This is experimental, it is not yet functional
|
25 |
|
26 |
#include "TinyGPS.h" |
27 |
|
28 |
#ifdef GPS
|
29 |
|
30 |
TinyGPS gps; |
31 |
//NewSoftSerial nss(13, 3);
|
32 |
|
33 |
bool newdata;
|
34 |
unsigned long start; |
35 |
|
36 |
void gpsdump(TinyGPS &gps);
|
37 |
bool feedgps();
|
38 |
void printFloat(double f, int digits = 2); |
39 |
|
40 |
void printFloat(double number, int digits) |
41 |
{ |
42 |
// Handle negative numbers
|
43 |
if (number < 0.0) |
44 |
{ |
45 |
Serial.print('-');
|
46 |
number = -number; |
47 |
} |
48 |
|
49 |
// Round correctly so that print(1.999, 2) prints as "2.00"
|
50 |
double rounding = 0.5; |
51 |
for (uint8_t i=0; i<digits; ++i) |
52 |
rounding /= 10.0; |
53 |
|
54 |
number += rounding; |
55 |
|
56 |
// Extract the integer part of the number and print it
|
57 |
unsigned long int_part = (unsigned long)number; |
58 |
double remainder = number - (double)int_part; |
59 |
Serial.print(int_part); |
60 |
|
61 |
// Print the decimal point, but only if there are digits beyond
|
62 |
if (digits > 0) |
63 |
Serial.print(".");
|
64 |
|
65 |
// Extract digits from the remainder one at a time
|
66 |
while (digits-- > 0) |
67 |
{ |
68 |
remainder *= 10.0; |
69 |
int toPrint = int(remainder); |
70 |
Serial.print(toPrint); |
71 |
remainder -= toPrint; |
72 |
} |
73 |
} |
74 |
|
75 |
void gpsdump(TinyGPS &gps)
|
76 |
{ |
77 |
long lat, lon;
|
78 |
float flat, flon;
|
79 |
unsigned long age, date, time, chars; |
80 |
int year;
|
81 |
byte month, day, hour, minute, second, hundredths; |
82 |
unsigned short sentences, failed; |
83 |
|
84 |
gps.get_position(&lat, &lon, &age); |
85 |
Serial.print("Lat/Long(10^-5 deg): "); Serial.print(lat); Serial.print(", "); Serial.print(lon); |
86 |
Serial.print(" Fix age: "); Serial.print(age); Serial.println("ms."); |
87 |
|
88 |
feedgps(); // If we don't feed the gps during this long routine, we may drop characters and get checksum errors
|
89 |
|
90 |
gps.f_get_position(&flat, &flon, &age); |
91 |
Serial.print("Lat/Long(float): "); printFloat(flat, 5); Serial.print(", "); printFloat(flon, 5); |
92 |
Serial.print(" Fix age: "); Serial.print(age); Serial.println("ms."); |
93 |
|
94 |
feedgps(); |
95 |
|
96 |
gps.get_datetime(&date, &time, &age); |
97 |
Serial.print("Date(ddmmyy): "); Serial.print(date); Serial.print(" Time(hhmmsscc): "); Serial.print(time); |
98 |
Serial.print(" Fix age: "); Serial.print(age); Serial.println("ms."); |
99 |
|
100 |
feedgps(); |
101 |
|
102 |
gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age); |
103 |
Serial.print("Date: "); Serial.print(static_cast<int>(month)); Serial.print("/"); Serial.print(static_cast<int>(day)); Serial.print("/"); Serial.print(year); |
104 |
Serial.print(" Time: "); Serial.print(static_cast<int>(hour)); Serial.print(":"); Serial.print(static_cast<int>(minute)); Serial.print(":"); Serial.print(static_cast<int>(second)); Serial.print("."); Serial.print(static_cast<int>(hundredths)); |
105 |
Serial.print(" Fix age: "); Serial.print(age); Serial.println("ms."); |
106 |
|
107 |
feedgps(); |
108 |
|
109 |
Serial.print("Alt(cm): "); Serial.print(gps.altitude()); Serial.print(" Course(10^-2 deg): "); Serial.print(gps.course()); Serial.print(" Speed(10^-2 knots): "); Serial.println(gps.speed()); |
110 |
Serial.print("Alt(float): "); printFloat(gps.f_altitude()); Serial.print(" Course(float): "); printFloat(gps.f_course()); Serial.println(); |
111 |
Serial.print("Speed(knots): "); printFloat(gps.f_speed_knots()); Serial.print(" (mph): "); printFloat(gps.f_speed_mph()); |
112 |
Serial.print(" (mps): "); printFloat(gps.f_speed_mps()); Serial.print(" (kmph): "); printFloat(gps.f_speed_kmph()); Serial.println(); |
113 |
|
114 |
feedgps(); |
115 |
|
116 |
gps.stats(&chars, &sentences, &failed); |
117 |
Serial.print("Stats: characters: "); Serial.print(chars); Serial.print(" sentences: "); Serial.print(sentences); Serial.print(" failed checksum: "); Serial.println(failed); |
118 |
} |
119 |
|
120 |
bool feedgps()
|
121 |
{ |
122 |
/*while (nss.available())
|
123 |
{
|
124 |
if (gps.encode(nss.read()))
|
125 |
return true;
|
126 |
}
|
127 |
return false;*/
|
128 |
return true; |
129 |
} |
130 |
|
131 |
#endif
|