?lang_form? ?lang_select? ?lang_submit? ?lang_endform?
{HEADER END}
{BLAME START}

library

?curdirlinks? -

Blame information for rev 6

Line No. Rev Author Line
1 6 kaklik <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
3 <title>Procyon AVRlib: nmea.c Source File</title>
4 <link href="dox.css" rel="stylesheet" type="text/css">
5 </head><body>
6 <!-- Generated by Doxygen 1.4.2 -->
7 <div class="qindex"><a class="qindex" href="main.html">Main&nbsp;Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="annotated.html">Data&nbsp;Structures</a> | <a class="qindex" href="dirs.html">Directories</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="functions.html">Data&nbsp;Fields</a> | <a class="qindex" href="globals.html">Globals</a> | <a class="qindex" href="pages.html">Related&nbsp;Pages</a></div>
8 <h1>nmea.c</h1><a href="nmea_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">00001 <span class="comment">/*! \file nmea.c \brief NMEA protocol function library. */</span>
9 00002 <span class="comment">//*****************************************************************************</span>
10 00003 <span class="comment">//</span>
11 00004 <span class="comment">// File Name : 'nmea.c'</span>
12 00005 <span class="comment">// Title : NMEA protocol function library</span>
13 00006 <span class="comment">// Author : Pascal Stang - Copyright (C) 2002</span>
14 00007 <span class="comment">// Created : 2002.08.27</span>
15 00008 <span class="comment">// Revised : 2002.08.27</span>
16 00009 <span class="comment">// Version : 0.1</span>
17 00010 <span class="comment">// Target MCU : Atmel AVR Series</span>
18 00011 <span class="comment">// Editor Tabs : 4</span>
19 00012 <span class="comment">//</span>
20 00013 <span class="comment">// NOTE: This code is currently below version 1.0, and therefore is considered</span>
21 00014 <span class="comment">// to be lacking in some functionality or documentation, or may not be fully</span>
22 00015 <span class="comment">// tested. Nonetheless, you can expect most functions to work.</span>
23 00016 <span class="comment">//</span>
24 00017 <span class="comment">// This code is distributed under the GNU Public License</span>
25 00018 <span class="comment">// which can be found at http://www.gnu.org/licenses/gpl.txt</span>
26 00019 <span class="comment">//</span>
27 00020 <span class="comment">//*****************************************************************************</span>
28 00021
29 00022 <span class="preprocessor">#ifndef WIN32</span>
30 00023 <span class="preprocessor"></span><span class="preprocessor"> #include &lt;avr/io.h&gt;</span>
31 00024 <span class="preprocessor"> #include &lt;avr/interrupt.h&gt;</span>
32 00025 <span class="preprocessor"> #include &lt;avr/pgmspace.h&gt;</span>
33 00026 <span class="preprocessor">#endif</span>
34 00027 <span class="preprocessor"></span><span class="preprocessor">#include &lt;string.h&gt;</span>
35 00028 <span class="preprocessor">#include &lt;stdlib.h&gt;</span>
36 00029 <span class="preprocessor">#include &lt;math.h&gt;</span>
37 00030
38 00031 <span class="preprocessor">#include "<a class="code" href="global_8h.html">global.h</a>"</span>
39 00032 <span class="preprocessor">#include "<a class="code" href="buffer_8h.html">buffer.h</a>"</span>
40 00033 <span class="preprocessor">#include "<a class="code" href="rprintf_8h.html">rprintf.h</a>"</span>
41 00034 <span class="preprocessor">#include "<a class="code" href="gps_8h.html">gps.h</a>"</span>
42 00035
43 00036 <span class="preprocessor">#include "<a class="code" href="nmea_8h.html">nmea.h</a>"</span>
44 00037
45 00038 <span class="comment">// Program ROM constants</span>
46 00039
47 00040 <span class="comment">// Global variables</span>
48 00041 <span class="keyword">extern</span> GpsInfoType GpsInfo;
49 00042 u08 NmeaPacket[NMEA_BUFFERSIZE];
50 00043
51 00044 <span class="keywordtype">void</span> nmeaInit(<span class="keywordtype">void</span>)
52 00045 {
53 00046 }
54 00047
55 00048 u08* nmeaGetPacketBuffer(<span class="keywordtype">void</span>)
56 00049 {
57 00050 <span class="keywordflow">return</span> NmeaPacket;
58 00051 }
59 00052
60 00053 u08 nmeaProcess(<a class="code" href="structstruct__cBuffer.html">cBuffer</a>* rxBuffer)
61 00054 {
62 00055 u08 foundpacket = NMEA_NODATA;
63 00056 u08 startFlag = FALSE;
64 00057 <span class="comment">//u08 data;</span>
65 00058 u16 i,j;
66 00059
67 00060 <span class="comment">// process the receive buffer</span>
68 00061 <span class="comment">// go through buffer looking for packets</span>
69 00062 <span class="keywordflow">while</span>(rxBuffer-&gt;<a class="code" href="structstruct__cBuffer.html#o2">datalength</a>)
70 00063 {
71 00064 <span class="comment">// look for a start of NMEA packet</span>
72 00065 <span class="keywordflow">if</span>(<a class="code" href="group__buffer.html#ga4">bufferGetAtIndex</a>(rxBuffer,0) == <span class="charliteral">'$'</span>)
73 00066 {
74 00067 <span class="comment">// found start</span>
75 00068 startFlag = TRUE;
76 00069 <span class="comment">// when start is found, we leave it intact in the receive buffer</span>
77 00070 <span class="comment">// in case the full NMEA string is not completely received. The</span>
78 00071 <span class="comment">// start will be detected in the next nmeaProcess iteration.</span>
79 00072
80 00073 <span class="comment">// done looking for start</span>
81 00074 <span class="keywordflow">break</span>;
82 00075 }
83 00076 <span class="keywordflow">else</span>
84 00077 <a class="code" href="group__buffer.html#ga2">bufferGetFromFront</a>(rxBuffer);
85 00078 }
86 00079
87 00080 <span class="comment">// if we detected a start, look for end of packet</span>
88 00081 <span class="keywordflow">if</span>(startFlag)
89 00082 {
90 00083 <span class="keywordflow">for</span>(i=1; i&lt;(rxBuffer-&gt;<a class="code" href="structstruct__cBuffer.html#o2">datalength</a>)-1; i++)
91 00084 {
92 00085 <span class="comment">// check for end of NMEA packet &lt;CR&gt;&lt;LF&gt;</span>
93 00086 <span class="keywordflow">if</span>((<a class="code" href="group__buffer.html#ga4">bufferGetAtIndex</a>(rxBuffer,i) == <span class="charliteral">'\r'</span>) &amp;&amp; (<a class="code" href="group__buffer.html#ga4">bufferGetAtIndex</a>(rxBuffer,i+1) == <span class="charliteral">'\n'</span>))
94 00087 {
95 00088 <span class="comment">// have a packet end</span>
96 00089 <span class="comment">// dump initial '$'</span>
97 00090 <a class="code" href="group__buffer.html#ga2">bufferGetFromFront</a>(rxBuffer);
98 00091 <span class="comment">// copy packet to NmeaPacket</span>
99 00092 <span class="keywordflow">for</span>(j=0; j&lt;(i-1); j++)
100 00093 {
101 00094 <span class="comment">// although NMEA strings should be 80 characters or less,</span>
102 00095 <span class="comment">// receive buffer errors can generate erroneous packets.</span>
103 00096 <span class="comment">// Protect against packet buffer overflow</span>
104 00097 <span class="keywordflow">if</span>(j&lt;(NMEA_BUFFERSIZE-1))
105 00098 NmeaPacket[j] = <a class="code" href="group__buffer.html#ga2">bufferGetFromFront</a>(rxBuffer);
106 00099 <span class="keywordflow">else</span>
107 00100 <a class="code" href="group__buffer.html#ga2">bufferGetFromFront</a>(rxBuffer);
108 00101 }
109 00102 <span class="comment">// null terminate it</span>
110 00103 NmeaPacket[j] = 0;
111 00104 <span class="comment">// dump &lt;CR&gt;&lt;LF&gt; from rxBuffer</span>
112 00105 <a class="code" href="group__buffer.html#ga2">bufferGetFromFront</a>(rxBuffer);
113 00106 <a class="code" href="group__buffer.html#ga2">bufferGetFromFront</a>(rxBuffer);
114 00107
115 00108 <span class="preprocessor"> #ifdef NMEA_DEBUG_PKT</span>
116 00109 <span class="preprocessor"></span> rprintf(<span class="stringliteral">"Rx NMEA packet type: "</span>);
117 00110 <a class="code" href="group__rprintf.html#ga3">rprintfStrLen</a>(NmeaPacket, 0, 5);
118 00111 <a class="code" href="group__rprintf.html#ga3">rprintfStrLen</a>(NmeaPacket, 5, (i-1)-5);
119 00112 <a class="code" href="group__rprintf.html#ga5">rprintfCRLF</a>();
120 00113 <span class="preprocessor"> #endif</span>
121 00114 <span class="preprocessor"></span> <span class="comment">// found a packet</span>
122 00115 <span class="comment">// done with this processing session</span>
123 00116 foundpacket = NMEA_UNKNOWN;
124 00117 <span class="keywordflow">break</span>;
125 00118 }
126 00119 }
127 00120 }
128 00121
129 00122 <span class="keywordflow">if</span>(foundpacket)
130 00123 {
131 00124 <span class="comment">// check message type and process appropriately</span>
132 00125 <span class="keywordflow">if</span>(!strncmp(NmeaPacket, <span class="stringliteral">"GPGGA"</span>, 5))
133 00126 {
134 00127 <span class="comment">// process packet of this type</span>
135 00128 nmeaProcessGPGGA(NmeaPacket);
136 00129 <span class="comment">// report packet type</span>
137 00130 foundpacket = NMEA_GPGGA;
138 00131 }
139 00132 <span class="keywordflow">else</span> <span class="keywordflow">if</span>(!strncmp(NmeaPacket, <span class="stringliteral">"GPVTG"</span>, 5))
140 00133 {
141 00134 <span class="comment">// process packet of this type</span>
142 00135 nmeaProcessGPVTG(NmeaPacket);
143 00136 <span class="comment">// report packet type</span>
144 00137 foundpacket = NMEA_GPVTG;
145 00138 }
146 00139 }
147 00140 <span class="keywordflow">else</span> <span class="keywordflow">if</span>(rxBuffer-&gt;<a class="code" href="structstruct__cBuffer.html#o2">datalength</a> &gt;= rxBuffer-&gt;<a class="code" href="structstruct__cBuffer.html#o1">size</a>)
148 00141 {
149 00142 <span class="comment">// if we found no packet, and the buffer is full</span>
150 00143 <span class="comment">// we're logjammed, flush entire buffer</span>
151 00144 <a class="code" href="group__buffer.html#ga7">bufferFlush</a>(rxBuffer);
152 00145 }
153 00146 <span class="keywordflow">return</span> foundpacket;
154 00147 }
155 00148
156 00149 <span class="keywordtype">void</span> nmeaProcessGPGGA(u08* packet)
157 00150 {
158 00151 u08 i;
159 00152 <span class="keywordtype">char</span>* endptr;
160 00153 <span class="keywordtype">double</span> degrees, minutesfrac;
161 00154
162 00155 <span class="preprocessor"> #ifdef NMEA_DEBUG_GGA</span>
163 00156 <span class="preprocessor"></span> rprintf(<span class="stringliteral">"NMEA: "</span>);
164 00157 <a class="code" href="group__rprintf.html#ga2">rprintfStr</a>(packet);
165 00158 <a class="code" href="group__rprintf.html#ga5">rprintfCRLF</a>();
166 00159 <span class="preprocessor"> #endif</span>
167 00160 <span class="preprocessor"></span>
168 00161 <span class="comment">// start parsing just after "GPGGA,"</span>
169 00162 i = 6;
170 00163 <span class="comment">// attempt to reject empty packets right away</span>
171 00164 <span class="keywordflow">if</span>(packet[i]==<span class="charliteral">','</span> &amp;&amp; packet[i+1]==<span class="charliteral">','</span>)
172 00165 <span class="keywordflow">return</span>;
173 00166
174 00167 <span class="comment">// get UTC time [hhmmss.sss]</span>
175 00168 GpsInfo.PosLLA.TimeOfFix.f = strtod(&amp;packet[i], &amp;endptr);
176 00169 <span class="keywordflow">while</span>(packet[i++] != <span class="charliteral">','</span>); <span class="comment">// next field: latitude</span>
177 00170
178 00171 <span class="comment">// get latitude [ddmm.mmmmm]</span>
179 00172 GpsInfo.PosLLA.lat.f = strtod(&amp;packet[i], &amp;endptr);
180 00173 <span class="comment">// convert to pure degrees [dd.dddd] format</span>
181 00174 minutesfrac = modf(GpsInfo.PosLLA.lat.f/100, &amp;degrees);
182 00175 GpsInfo.PosLLA.lat.f = degrees + (minutesfrac*100)/60;
183 00176 <span class="comment">// convert to radians</span>
184 00177 GpsInfo.PosLLA.lat.f *= (M_PI/180);
185 00178 <span class="keywordflow">while</span>(packet[i++] != <span class="charliteral">','</span>); <span class="comment">// next field: N/S indicator</span>
186 00179
187 00180 <span class="comment">// correct latitute for N/S</span>
188 00181 <span class="keywordflow">if</span>(packet[i] == <span class="charliteral">'S'</span>) GpsInfo.PosLLA.lat.f = -GpsInfo.PosLLA.lat.f;
189 00182 <span class="keywordflow">while</span>(packet[i++] != <span class="charliteral">','</span>); <span class="comment">// next field: longitude</span>
190 00183
191 00184 <span class="comment">// get longitude [ddmm.mmmmm]</span>
192 00185 GpsInfo.PosLLA.lon.f = strtod(&amp;packet[i], &amp;endptr);
193 00186 <span class="comment">// convert to pure degrees [dd.dddd] format</span>
194 00187 minutesfrac = modf(GpsInfo.PosLLA.lon.f/100, &amp;degrees);
195 00188 GpsInfo.PosLLA.lon.f = degrees + (minutesfrac*100)/60;
196 00189 <span class="comment">// convert to radians</span>
197 00190 GpsInfo.PosLLA.lon.f *= (M_PI/180);
198 00191 <span class="keywordflow">while</span>(packet[i++] != <span class="charliteral">','</span>); <span class="comment">// next field: E/W indicator</span>
199 00192
200 00193 <span class="comment">// correct latitute for E/W</span>
201 00194 <span class="keywordflow">if</span>(packet[i] == <span class="charliteral">'W'</span>) GpsInfo.PosLLA.lon.f = -GpsInfo.PosLLA.lon.f;
202 00195 <span class="keywordflow">while</span>(packet[i++] != <span class="charliteral">','</span>); <span class="comment">// next field: position fix status</span>
203 00196
204 00197 <span class="comment">// position fix status</span>
205 00198 <span class="comment">// 0 = Invalid, 1 = Valid SPS, 2 = Valid DGPS, 3 = Valid PPS</span>
206 00199 <span class="comment">// check for good position fix</span>
207 00200 <span class="keywordflow">if</span>( (packet[i] != <span class="charliteral">'0'</span>) &amp;&amp; (packet[i] != <span class="charliteral">','</span>) )
208 00201 GpsInfo.PosLLA.updates++;
209 00202 <span class="keywordflow">while</span>(packet[i++] != <span class="charliteral">','</span>); <span class="comment">// next field: satellites used</span>
210 00203
211 00204 <span class="comment">// get number of satellites used in GPS solution</span>
212 00205 GpsInfo.numSVs = atoi(&amp;packet[i]);
213 00206 <span class="keywordflow">while</span>(packet[i++] != <span class="charliteral">','</span>); <span class="comment">// next field: HDOP (horizontal dilution of precision)</span>
214 00207 <span class="keywordflow">while</span>(packet[i++] != <span class="charliteral">','</span>); <span class="comment">// next field: altitude</span>
215 00208
216 00209 <span class="comment">// get altitude (in meters)</span>
217 00210 GpsInfo.PosLLA.alt.f = strtod(&amp;packet[i], &amp;endptr);
218 00211
219 00212 <span class="keywordflow">while</span>(packet[i++] != <span class="charliteral">','</span>); <span class="comment">// next field: altitude units, always 'M'</span>
220 00213 <span class="keywordflow">while</span>(packet[i++] != <span class="charliteral">','</span>); <span class="comment">// next field: geoid seperation</span>
221 00214 <span class="keywordflow">while</span>(packet[i++] != <span class="charliteral">','</span>); <span class="comment">// next field: seperation units</span>
222 00215 <span class="keywordflow">while</span>(packet[i++] != <span class="charliteral">','</span>); <span class="comment">// next field: DGPS age</span>
223 00216 <span class="keywordflow">while</span>(packet[i++] != <span class="charliteral">','</span>); <span class="comment">// next field: DGPS station ID</span>
224 00217 <span class="keywordflow">while</span>(packet[i++] != <span class="charliteral">'*'</span>); <span class="comment">// next field: checksum</span>
225 00218 }
226 00219
227 00220 <span class="keywordtype">void</span> nmeaProcessGPVTG(u08* packet)
228 00221 {
229 00222 u08 i;
230 00223 <span class="keywordtype">char</span>* endptr;
231 00224
232 00225 <span class="preprocessor"> #ifdef NMEA_DEBUG_VTG</span>
233 00226 <span class="preprocessor"></span> rprintf(<span class="stringliteral">"NMEA: "</span>);
234 00227 <a class="code" href="group__rprintf.html#ga2">rprintfStr</a>(packet);
235 00228 <a class="code" href="group__rprintf.html#ga5">rprintfCRLF</a>();
236 00229 <span class="preprocessor"> #endif</span>
237 00230 <span class="preprocessor"></span>
238 00231 <span class="comment">// start parsing just after "GPVTG,"</span>
239 00232 i = 6;
240 00233 <span class="comment">// attempt to reject empty packets right away</span>
241 00234 <span class="keywordflow">if</span>(packet[i]==<span class="charliteral">','</span> &amp;&amp; packet[i+1]==<span class="charliteral">','</span>)
242 00235 <span class="keywordflow">return</span>;
243 00236
244 00237 <span class="comment">// get course (true north ref) in degrees [ddd.dd]</span>
245 00238 GpsInfo.VelHS.heading.f = strtod(&amp;packet[i], &amp;endptr);
246 00239 <span class="keywordflow">while</span>(packet[i++] != <span class="charliteral">','</span>); <span class="comment">// next field: 'T'</span>
247 00240 <span class="keywordflow">while</span>(packet[i++] != <span class="charliteral">','</span>); <span class="comment">// next field: course (magnetic north)</span>
248 00241
249 00242 <span class="comment">// get course (magnetic north ref) in degrees [ddd.dd]</span>
250 00243 <span class="comment">//GpsInfo.VelHS.heading.f = strtod(&amp;packet[i], &amp;endptr);</span>
251 00244 <span class="keywordflow">while</span>(packet[i++] != <span class="charliteral">','</span>); <span class="comment">// next field: 'M'</span>
252 00245 <span class="keywordflow">while</span>(packet[i++] != <span class="charliteral">','</span>); <span class="comment">// next field: speed (knots)</span>
253 00246
254 00247 <span class="comment">// get speed in knots</span>
255 00248 <span class="comment">//GpsInfo.VelHS.speed.f = strtod(&amp;packet[i], &amp;endptr);</span>
256 00249 <span class="keywordflow">while</span>(packet[i++] != <span class="charliteral">','</span>); <span class="comment">// next field: 'N'</span>
257 00250 <span class="keywordflow">while</span>(packet[i++] != <span class="charliteral">','</span>); <span class="comment">// next field: speed (km/h)</span>
258 00251
259 00252 <span class="comment">// get speed in km/h</span>
260 00253 GpsInfo.VelHS.speed.f = strtod(&amp;packet[i], &amp;endptr);
261 00254 <span class="keywordflow">while</span>(packet[i++] != <span class="charliteral">','</span>); <span class="comment">// next field: 'K'</span>
262 00255 <span class="keywordflow">while</span>(packet[i++] != <span class="charliteral">'*'</span>); <span class="comment">// next field: checksum</span>
263 00256
264 00257 GpsInfo.VelHS.updates++;
265 00258 }
266 00259
267 </pre></div><hr size="1"><address style="align: right;"><small>Generated on Sun Oct 29 03:41:07 2006 for Procyon AVRlib by&nbsp;
268 <a href="http://www.doxygen.org/index.html">
269 <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.2 </small></address>
270 </body>
271 </html>
{BLAME END}
{FOOTER START}

Powered by WebSVN v2.8.3