Subversion Repositories NaviCtrl

Rev

Rev 109 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
24 StephanB 1
/*#######################################################################################*/
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
3
/*#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + Copyright (c) 2008 Ingo Busker, Holger Buss
171 ingob 6
// + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY
24 StephanB 7
// + FOR NON COMMERCIAL USE ONLY
8
// + www.MikroKopter.com
9
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
41 ingob 10
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
11
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
12
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
13
// + bzgl. der Nutzungsbedingungen aufzunehmen.
24 StephanB 14
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
15
// + Verkauf von Luftbildaufnahmen, usw.
16
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
41 ingob 17
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
24 StephanB 18
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
20
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
21
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
22
// + eindeutig als Ursprung verlinkt werden
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
25
// + Benutzung auf eigene Gefahr
26
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
27
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
171 ingob 28
// + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
24 StephanB 29
// + mit unserer Zustimmung zulässig
30
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
32
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
41 ingob 33
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
24 StephanB 34
// + this list of conditions and the following disclaimer.
35
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
36
// +     from this software without specific prior written permission.
41 ingob 37
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permitted
24 StephanB 38
// +     for non-commercial use (directly or indirectly)
41 ingob 39
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
24 StephanB 40
// +     with our written permission
41 ingob 41
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
42
// +     clearly linked as origin
171 ingob 43
// +   * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed
24 StephanB 44
//
45
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
46
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
49
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
51
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
52
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 ingob 55
// +  POSSIBILITY OF SUCH DAMAGE.
24 StephanB 56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
41 ingob 57
#include <stdlib.h>
24 StephanB 58
#include <stdio.h>
41 ingob 59
#include "91x_lib.h"
24 StephanB 60
#include "kml.h"
61
#include "kml_header.h"
62
 
63
 
64
//________________________________________________________________________________________________________________________________________
41 ingob 65
// Module name:                 kml.c
24 StephanB 66
// Compiler used:               avr-gcc 3.4.5
85 killagreg 67
// Last Modifikation:   22.03.2009
68
// Version:                             1.03
69
// Authors:                             Stephan Busker, Gregor Stobrawa
24 StephanB 70
// Description:                 Source files to write gps-coordinates to a file in the kml (keyhole markup language) fileformat
71
//                                              Copyright (C) 2007 Stephan Busker
72
//........................................................................................................................................
73
// Functions:                   extern u8       KML_LoggGPSCoordinates(struct str_gps_nav_data , KML_Document_t *);     // intializes the kml-document with standard filename and adds points to the file
74
//                                              extern u8       KML_DocumentInit(KML_Document_t *doc)                                                           // initializes the kml-document to resetvalues.
75
//                                              extern u8       KML_DocumentOpen(s8 *, KML_Document_t *);                                                       // opens a new kml document. A filename can be specified.
76
//                                              extern u8       KML_DocumentClose(KML_Document_t *doc);                                                         // closes an open document
85 killagreg 77
//                                              extern u8   KML_PlaceMarkOpen(KML_Document_t *);                                                                // opens a new placemark within the specified document
78
//                                              extern u8       KML_PlaceMarkClose(KML_Document_t *);                                                           // Closes the placemark
24 StephanB 79
//                                              extern u8       KML_LineStringBegin(KML_Document_t *);                                                          // begins a new line within the actual placemark
80
//                                              extern u8       KML_LineStringEnd(KML_Document_t *doc);                                                         // ends the actual linestring
81
//                                              extern u8       KML_LineStringAddPoint(struct str_gps_nav_data, KML_Document_t *);      // adds a new point (gps-coordinates) to the actual linestring
82
//........................................................................................................................................
41 ingob 83
// ext. functions:
84
//
24 StephanB 85
//........................................................................................................................................
86
//
87
// URL:                                 www.Mikro-Control.de
88
// mailto:                              stephan.busker@mikro-control.de
89
//________________________________________________________________________________________________________________________________________
90
 
91
 
92
 
93
//________________________________________________________________________________________________________________________________________
41 ingob 94
// Function:    KML_DocumentInit(KML_Document_t *)
95
//
24 StephanB 96
// Description: This function initializes the kml-document for further use.
97
//
41 ingob 98
//
24 StephanB 99
// Returnvalue: '1' if document was initialized
100
//________________________________________________________________________________________________________________________________________
101
 
102
u8 KML_DocumentInit(KML_Document_t *doc)
85 killagreg 103
{                                                                                                              
104
        doc->state       = KML_DOC_CLOSED;                                                                                                      // state of the kml-document
41 ingob 105
        doc->file        = NULL;
24 StephanB 106
        return(1);
107
}
108
 
109
//________________________________________________________________________________________________________________________________________
41 ingob 110
// Function:    KML_Document_Open(void);
111
//
24 StephanB 112
// Description: This function opens a new KML- document with the specified name and creates the document header within the file.
113
//
41 ingob 114
//
115
// Returnvalue: '1' if the KML- file could be created.
24 StephanB 116
//________________________________________________________________________________________________________________________________________
117
 
118
u8 KML_DocumentOpen(s8 *name, KML_Document_t *doc)
119
{
120
 
121
        u8 retvalue = 0;
122
 
41 ingob 123
        if(doc == NULL) return(0);
124
 
125
        KML_DocumentInit(doc);                                                                                                          // intialize the document with resetvalues
126
        doc->file = fopen_(name,'a');                                                                                           // open a new file with the specified filename on the memorycard.
127
 
24 StephanB 128
        if(doc->file != NULL)                                                                                                           // could the file be opened?
129
        {
130
                retvalue = 1;                                                                                                                   // the document could be created on the drive.
85 killagreg 131
                doc->state = KML_DOC_OPENED;                                                                                            // change document state to opened. At next a placemark has to be opened.
132
                fwrite_((void*)KML_DOCUMENT_HEADER, sizeof(KML_DOCUMENT_HEADER)-1,1,doc->file);// write the KML-header to the document.
24 StephanB 133
        }
134
 
135
        return(retvalue);
136
}
137
 
138
//________________________________________________________________________________________________________________________________________
41 ingob 139
// Function:    DocumentClose(KML_Document_t *doc);
24 StephanB 140
//
41 ingob 141
// Description: This function closes the document specified by doc.
142
//
143
//
144
// Returnvalue: '1' if the KML- file could be closed.
24 StephanB 145
//________________________________________________________________________________________________________________________________________
146
 
147
u8 KML_DocumentClose(KML_Document_t *doc)
148
{
149
 
41 ingob 150
        u8 retvalue = 1;
24 StephanB 151
 
41 ingob 152
        if(doc == NULL) return(0);
153
 
85 killagreg 154
        while(doc->state != KML_DOC_CLOSED)                                                             // close linestring, placemark and document before closing the file on the memorycard
41 ingob 155
        {
24 StephanB 156
                switch(doc->state)
157
                {
85 killagreg 158
                        case KML_DOC_LINESTRING_OPENED:
24 StephanB 159
                                KML_LineStringEnd(doc);                                                 // write terminating tag to end linestring.
41 ingob 160
                                break;
24 StephanB 161
 
85 killagreg 162
                        case KML_DOC_PLACEMARK_OPENED:                                                  // write terminating tag to close placemark.
24 StephanB 163
                                KML_PlaceMarkClose(doc);
41 ingob 164
                                break;
165
 
85 killagreg 166
                        case KML_DOC_OPENED:                                                                    // close the file on the memorycard
24 StephanB 167
                                if(doc->file != NULL)
168
                                {
41 ingob 169
                                        fwrite_((void*)KML_DOCUMENT_FOOTER, sizeof(KML_DOCUMENT_FOOTER)-1,1,doc->file); // write the KML- footer to the document.
170
                                        fclose_(doc->file);
24 StephanB 171
                                        retvalue = 1;
172
                                }
85 killagreg 173
                                doc->state = KML_DOC_CLOSED;
41 ingob 174
                                break;
175
 
176
                        default:
85 killagreg 177
                                doc->state = KML_DOC_CLOSED;
41 ingob 178
                                break;
179
 
24 StephanB 180
                }
41 ingob 181
        }
24 StephanB 182
        return(retvalue);
183
}
184
 
185
//________________________________________________________________________________________________________________________________________
85 killagreg 186
// Function:    u8 KML_PlaceMarkOpen(KML_Document_t *doc);
41 ingob 187
//
24 StephanB 188
// Description: This function adds a placemark to the document.
189
//
41 ingob 190
//
191
// Returnvalue: '1' if the PlaceMark could be opened
24 StephanB 192
//________________________________________________________________________________________________________________________________________
193
 
85 killagreg 194
u8 KML_PlaceMarkOpen(KML_Document_t *doc)
24 StephanB 195
{
196
        u8 retvalue = 0;
85 killagreg 197
        if(doc->state == KML_DOC_OPENED)
24 StephanB 198
        {
85 killagreg 199
                if(doc->file != NULL)
200
                {
201
                        doc->state = KML_DOC_PLACEMARK_OPENED;
202
                        retvalue = 1;
203
                        fwrite_((void*)KML_PLACEMARK_HEADER, sizeof(KML_PLACEMARK_HEADER)-1,1,doc->file);
204
                }
24 StephanB 205
        }
206
        return(retvalue);
207
}
208
 
209
//________________________________________________________________________________________________________________________________________
41 ingob 210
// Function:    u8 PlaceMarkClose(KML_PlaceMark_t *place, File *file);
211
//
24 StephanB 212
// Description: This function ends the placemark opened before.
213
//
41 ingob 214
//
215
// Returnvalue: 1' if the PlaceMark could be closed
24 StephanB 216
//________________________________________________________________________________________________________________________________________
217
 
218
u8 KML_PlaceMarkClose(KML_Document_t *doc)
219
{
220
 
221
        u8 retvalue = 0;                                                                                                                        // close the Placemark-tag of the corosponding document.
222
 
85 killagreg 223
        if(doc->state == KML_DOC_PLACEMARK_OPENED)
24 StephanB 224
        {
225
                if(doc->file != NULL)
226
                {
85 killagreg 227
                        doc->state = KML_DOC_OPENED;
41 ingob 228
                        fwrite_((void*)KML_PLACEMARK_FOOTER, sizeof(KML_PLACEMARK_FOOTER)-1,1,doc->file);
24 StephanB 229
                        retvalue = 1;
230
                }
231
        }
41 ingob 232
 
24 StephanB 233
        return(retvalue);
234
}
235
 
236
//________________________________________________________________________________________________________________________________________
41 ingob 237
// Function:    u8 LineStringBegin(KML_Document_t *doc);
238
//
24 StephanB 239
// Description: This function ends the placemark opened before.
240
//
41 ingob 241
//
242
// Returnvalue: '1' if the LineString could be started
24 StephanB 243
//________________________________________________________________________________________________________________________________________
244
 
245
u8 KML_LineStringBegin(KML_Document_t *doc)
246
{
247
 
248
        u8 retvalue = 0;
41 ingob 249
 
85 killagreg 250
        if(doc->state == KML_DOC_PLACEMARK_OPENED)
24 StephanB 251
        {
85 killagreg 252
                if(doc->file != NULL)
253
                {
254
                        doc->state = KML_DOC_LINESTRING_OPENED;
255
                        fwrite_((void*)KML_LINESTRING_HEADER, sizeof(KML_LINESTRING_HEADER)-1,1,doc->file);
256
                        retvalue = 1;
257
                }
24 StephanB 258
        }
259
        return(retvalue);
260
}
261
 
262
//________________________________________________________________________________________________________________________________________
85 killagreg 263
// Function:    u8 KML_LineStringEnd(KML_Document_t *doc)
41 ingob 264
//
24 StephanB 265
// Description: This function ends the placemark opened before.
266
//
41 ingob 267
//
268
// Returnvalue: '1' if the LineString could be terminated
24 StephanB 269
//________________________________________________________________________________________________________________________________________
270
 
271
u8 KML_LineStringEnd(KML_Document_t *doc)
272
{
273
 
274
        u8 retvalue = 0;
41 ingob 275
 
85 killagreg 276
        if(doc->state == KML_DOC_LINESTRING_OPENED)
24 StephanB 277
        {
85 killagreg 278
                if(doc->file != NULL)
279
                {
280
                        doc->state = KML_DOC_PLACEMARK_OPENED;
281
                        fwrite_((void*)KML_LINESTRING_FOOTER, sizeof(KML_LINESTRING_FOOTER)-1,1,doc->file);
282
                        retvalue = 1;
283
                }
24 StephanB 284
        }
285
        return(retvalue);
286
}
287
 
288
//________________________________________________________________________________________________________________________________________
41 ingob 289
// Function:    u8 LineStringAddPoint(gps_data_t, KML_Document_t *doc)
290
//
24 StephanB 291
// Description: This function adds a point to the specified document.
292
//
41 ingob 293
//
294
// Returnvalue: '1' if a ppoint was added could be started
24 StephanB 295
//________________________________________________________________________________________________________________________________________
296
 
85 killagreg 297
u8 KML_LineStringAddPoint(KML_Document_t *doc)
24 StephanB 298
{
299
 
300
        u8 retvalue = 0;
301
        s8 string[50];
41 ingob 302
        s32 rel_altitude = 0;
303
 
85 killagreg 304
        if(doc == NULL) return(0);
41 ingob 305
 
85 killagreg 306
        if((GPSData.Position.Status != INVALID) && (GPS_HomePosition.Status != INVALID))
24 StephanB 307
        {
85 killagreg 308
                if(doc->state == KML_DOC_LINESTRING_OPENED)
24 StephanB 309
                {
41 ingob 310
                        if(doc->file != NULL)
311
                        {
312
                                s32 i1, i2;
51 killagreg 313
                                u8 sign;
85 killagreg 314
                                if(GPSData.Position.Longitude < 0) sign = '-';
51 killagreg 315
                                else sign = '+';
85 killagreg 316
                                i1 = abs(GPSData.Position.Longitude)/10000000L;
317
                                i2 = abs(GPSData.Position.Longitude)%10000000L;
51 killagreg 318
                                sprintf(string,"\r\n%c%ld.%07ld,",sign, i1, i2);
41 ingob 319
                                fputs_(string, doc->file);
85 killagreg 320
                                if(GPSData.Position.Latitude < 0) sign = '-';
51 killagreg 321
                                else sign = '+';
85 killagreg 322
                                i1 = abs(GPSData.Position.Latitude)/10000000L;
323
                                i2 = abs(GPSData.Position.Latitude)%10000000L;
51 killagreg 324
                                sprintf(string,"%c%ld.%07ld,",sign, i1, i2);
41 ingob 325
                                fputs_(string, doc->file);
326
                                // calculate relative altitude with respect to the altitude of the home position
85 killagreg 327
                                rel_altitude = GPSData.Position.Altitude - GPS_HomePosition.Altitude;
41 ingob 328
                                if(rel_altitude < 0) rel_altitude = 0; // avoid negative altitudes in log
329
                                i1 = rel_altitude/1000L;
53 killagreg 330
                                i2 = rel_altitude%1000L;
41 ingob 331
                                sprintf(string," %ld.%03ld",i1, i2);
332
                                fputs_(string, doc->file);
333
                                retvalue = 1;
334
                        }
24 StephanB 335
                }
336
        }
337
 
338
        return(retvalue);
339
}
340
 
341
//________________________________________________________________________________________________________________________________________
41 ingob 342
// Function:    u8 KML_LoggGPSCoordinates(gps_data_t *, KML_Document_t *)
343
//
24 StephanB 344
// Description: This function opens adds gpscoordinates to an KML-Document. The document will be opened, if not already done
345
//
41 ingob 346
//
347
// Returnvalue: '1' if an gps coordinate was logged
24 StephanB 348
//________________________________________________________________________________________________________________________________________
349
 
85 killagreg 350
u8 KML_LoggGPSCoordinates(KML_Document_t *doc)
24 StephanB 351
{
41 ingob 352
        u8 retval = 0;
85 killagreg 353
        while(doc->state != KML_DOC_LINESTRING_OPENED)                                  // automatic create document with default filename on the card.
41 ingob 354
        {
24 StephanB 355
                switch(doc->state)
356
                {
85 killagreg 357
                        case KML_DOC_CLOSED:                                                                    // document hasn't been opened yet therefore it will be initialized automatically
41 ingob 358
                                retval = KML_DocumentOpen("default.kml",doc);   // open the kml-document with a standardname.
24 StephanB 359
                        break;
360
 
85 killagreg 361
                        case KML_DOC_OPENED:                                                                    // if a document has been opened before but no placemark exists:
362
                                retval = KML_PlaceMarkOpen(doc);
24 StephanB 363
                        break;
364
 
85 killagreg 365
                        case KML_DOC_PLACEMARK_OPENED:                                                  // add linestring to the placemark
41 ingob 366
                                retval = KML_LineStringBegin(doc);
24 StephanB 367
                        break;
368
 
41 ingob 369
                        default:
85 killagreg 370
                                retval = 0;
41 ingob 371
                        break;
372
 
24 StephanB 373
                }
41 ingob 374
                if(retval != 1) return(retval); // stop on error
375
        }
24 StephanB 376
 
85 killagreg 377
        if(doc->state == KML_DOC_LINESTRING_OPENED)                                             // if the document was opened add coordinates to the document.
24 StephanB 378
        {
85 killagreg 379
                retval = KML_LineStringAddPoint(doc);
24 StephanB 380
        }
41 ingob 381
        return(retval);
24 StephanB 382
}
27 holgerb 383