Subversion Repositories NaviCtrl

Rev

Rev 171 | Go to most recent revision | 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"
215 killagreg 62
#include "uart1.h"
24 StephanB 63
 
64
 
65
//________________________________________________________________________________________________________________________________________
41 ingob 66
// Module name:                 kml.c
24 StephanB 67
// Compiler used:               avr-gcc 3.4.5
85 killagreg 68
// Last Modifikation:   22.03.2009
69
// Version:                             1.03
70
// Authors:                             Stephan Busker, Gregor Stobrawa
24 StephanB 71
// Description:                 Source files to write gps-coordinates to a file in the kml (keyhole markup language) fileformat
72
//                                              Copyright (C) 2007 Stephan Busker
73
//........................................................................................................................................
74
// 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
75
//                                              extern u8       KML_DocumentInit(KML_Document_t *doc)                                                           // initializes the kml-document to resetvalues.
76
//                                              extern u8       KML_DocumentOpen(s8 *, KML_Document_t *);                                                       // opens a new kml document. A filename can be specified.
77
//                                              extern u8       KML_DocumentClose(KML_Document_t *doc);                                                         // closes an open document
85 killagreg 78
//                                              extern u8   KML_PlaceMarkOpen(KML_Document_t *);                                                                // opens a new placemark within the specified document
79
//                                              extern u8       KML_PlaceMarkClose(KML_Document_t *);                                                           // Closes the placemark
24 StephanB 80
//                                              extern u8       KML_LineStringBegin(KML_Document_t *);                                                          // begins a new line within the actual placemark
81
//                                              extern u8       KML_LineStringEnd(KML_Document_t *doc);                                                         // ends the actual linestring
82
//                                              extern u8       KML_LineStringAddPoint(struct str_gps_nav_data, KML_Document_t *);      // adds a new point (gps-coordinates) to the actual linestring
83
//........................................................................................................................................
41 ingob 84
// ext. functions:
85
//
24 StephanB 86
//........................................................................................................................................
87
//
88
// URL:                                 www.Mikro-Control.de
89
// mailto:                              stephan.busker@mikro-control.de
90
//________________________________________________________________________________________________________________________________________
91
 
92
 
93
 
94
//________________________________________________________________________________________________________________________________________
41 ingob 95
// Function:    KML_DocumentInit(KML_Document_t *)
96
//
24 StephanB 97
// Description: This function initializes the kml-document for further use.
98
//
41 ingob 99
//
24 StephanB 100
// Returnvalue: '1' if document was initialized
101
//________________________________________________________________________________________________________________________________________
102
 
103
u8 KML_DocumentInit(KML_Document_t *doc)
85 killagreg 104
{                                                                                                              
105
        doc->state       = KML_DOC_CLOSED;                                                                                                      // state of the kml-document
41 ingob 106
        doc->file        = NULL;
24 StephanB 107
        return(1);
108
}
109
 
110
//________________________________________________________________________________________________________________________________________
41 ingob 111
// Function:    KML_Document_Open(void);
112
//
24 StephanB 113
// Description: This function opens a new KML- document with the specified name and creates the document header within the file.
114
//
41 ingob 115
//
116
// Returnvalue: '1' if the KML- file could be created.
24 StephanB 117
//________________________________________________________________________________________________________________________________________
118
 
119
u8 KML_DocumentOpen(s8 *name, KML_Document_t *doc)
120
{
121
 
122
        u8 retvalue = 0;
123
 
41 ingob 124
        if(doc == NULL) return(0);
125
 
126
        KML_DocumentInit(doc);                                                                                                          // intialize the document with resetvalues
127
        doc->file = fopen_(name,'a');                                                                                           // open a new file with the specified filename on the memorycard.
128
 
24 StephanB 129
        if(doc->file != NULL)                                                                                                           // could the file be opened?
130
        {
131
                retvalue = 1;                                                                                                                   // the document could be created on the drive.
85 killagreg 132
                doc->state = KML_DOC_OPENED;                                                                                            // change document state to opened. At next a placemark has to be opened.
133
                fwrite_((void*)KML_DOCUMENT_HEADER, sizeof(KML_DOCUMENT_HEADER)-1,1,doc->file);// write the KML-header to the document.
24 StephanB 134
        }
135
 
136
        return(retvalue);
137
}
138
 
139
//________________________________________________________________________________________________________________________________________
41 ingob 140
// Function:    DocumentClose(KML_Document_t *doc);
24 StephanB 141
//
41 ingob 142
// Description: This function closes the document specified by doc.
143
//
144
//
145
// Returnvalue: '1' if the KML- file could be closed.
24 StephanB 146
//________________________________________________________________________________________________________________________________________
147
 
148
u8 KML_DocumentClose(KML_Document_t *doc)
149
{
150
 
41 ingob 151
        u8 retvalue = 1;
24 StephanB 152
 
41 ingob 153
        if(doc == NULL) return(0);
154
 
85 killagreg 155
        while(doc->state != KML_DOC_CLOSED)                                                             // close linestring, placemark and document before closing the file on the memorycard
41 ingob 156
        {
24 StephanB 157
                switch(doc->state)
158
                {
85 killagreg 159
                        case KML_DOC_LINESTRING_OPENED:
24 StephanB 160
                                KML_LineStringEnd(doc);                                                 // write terminating tag to end linestring.
41 ingob 161
                                break;
24 StephanB 162
 
85 killagreg 163
                        case KML_DOC_PLACEMARK_OPENED:                                                  // write terminating tag to close placemark.
24 StephanB 164
                                KML_PlaceMarkClose(doc);
41 ingob 165
                                break;
166
 
85 killagreg 167
                        case KML_DOC_OPENED:                                                                    // close the file on the memorycard
24 StephanB 168
                                if(doc->file != NULL)
169
                                {
41 ingob 170
                                        fwrite_((void*)KML_DOCUMENT_FOOTER, sizeof(KML_DOCUMENT_FOOTER)-1,1,doc->file); // write the KML- footer to the document.
171
                                        fclose_(doc->file);
24 StephanB 172
                                        retvalue = 1;
173
                                }
85 killagreg 174
                                doc->state = KML_DOC_CLOSED;
41 ingob 175
                                break;
176
 
177
                        default:
85 killagreg 178
                                doc->state = KML_DOC_CLOSED;
41 ingob 179
                                break;
180
 
24 StephanB 181
                }
41 ingob 182
        }
24 StephanB 183
        return(retvalue);
184
}
185
 
186
//________________________________________________________________________________________________________________________________________
85 killagreg 187
// Function:    u8 KML_PlaceMarkOpen(KML_Document_t *doc);
41 ingob 188
//
24 StephanB 189
// Description: This function adds a placemark to the document.
190
//
41 ingob 191
//
192
// Returnvalue: '1' if the PlaceMark could be opened
24 StephanB 193
//________________________________________________________________________________________________________________________________________
194
 
85 killagreg 195
u8 KML_PlaceMarkOpen(KML_Document_t *doc)
24 StephanB 196
{
197
        u8 retvalue = 0;
85 killagreg 198
        if(doc->state == KML_DOC_OPENED)
24 StephanB 199
        {
85 killagreg 200
                if(doc->file != NULL)
201
                {
202
                        doc->state = KML_DOC_PLACEMARK_OPENED;
203
                        retvalue = 1;
204
                        fwrite_((void*)KML_PLACEMARK_HEADER, sizeof(KML_PLACEMARK_HEADER)-1,1,doc->file);
205
                }
24 StephanB 206
        }
207
        return(retvalue);
208
}
209
 
210
//________________________________________________________________________________________________________________________________________
41 ingob 211
// Function:    u8 PlaceMarkClose(KML_PlaceMark_t *place, File *file);
212
//
24 StephanB 213
// Description: This function ends the placemark opened before.
214
//
41 ingob 215
//
216
// Returnvalue: 1' if the PlaceMark could be closed
24 StephanB 217
//________________________________________________________________________________________________________________________________________
218
 
219
u8 KML_PlaceMarkClose(KML_Document_t *doc)
220
{
221
 
222
        u8 retvalue = 0;                                                                                                                        // close the Placemark-tag of the corosponding document.
223
 
85 killagreg 224
        if(doc->state == KML_DOC_PLACEMARK_OPENED)
24 StephanB 225
        {
226
                if(doc->file != NULL)
227
                {
85 killagreg 228
                        doc->state = KML_DOC_OPENED;
41 ingob 229
                        fwrite_((void*)KML_PLACEMARK_FOOTER, sizeof(KML_PLACEMARK_FOOTER)-1,1,doc->file);
24 StephanB 230
                        retvalue = 1;
231
                }
232
        }
41 ingob 233
 
24 StephanB 234
        return(retvalue);
235
}
236
 
237
//________________________________________________________________________________________________________________________________________
41 ingob 238
// Function:    u8 LineStringBegin(KML_Document_t *doc);
239
//
24 StephanB 240
// Description: This function ends the placemark opened before.
241
//
41 ingob 242
//
243
// Returnvalue: '1' if the LineString could be started
24 StephanB 244
//________________________________________________________________________________________________________________________________________
245
 
246
u8 KML_LineStringBegin(KML_Document_t *doc)
247
{
248
 
249
        u8 retvalue = 0;
41 ingob 250
 
85 killagreg 251
        if(doc->state == KML_DOC_PLACEMARK_OPENED)
24 StephanB 252
        {
85 killagreg 253
                if(doc->file != NULL)
254
                {
255
                        doc->state = KML_DOC_LINESTRING_OPENED;
256
                        fwrite_((void*)KML_LINESTRING_HEADER, sizeof(KML_LINESTRING_HEADER)-1,1,doc->file);
257
                        retvalue = 1;
258
                }
24 StephanB 259
        }
260
        return(retvalue);
261
}
262
 
263
//________________________________________________________________________________________________________________________________________
85 killagreg 264
// Function:    u8 KML_LineStringEnd(KML_Document_t *doc)
41 ingob 265
//
24 StephanB 266
// Description: This function ends the placemark opened before.
267
//
41 ingob 268
//
269
// Returnvalue: '1' if the LineString could be terminated
24 StephanB 270
//________________________________________________________________________________________________________________________________________
271
 
272
u8 KML_LineStringEnd(KML_Document_t *doc)
273
{
274
 
275
        u8 retvalue = 0;
41 ingob 276
 
85 killagreg 277
        if(doc->state == KML_DOC_LINESTRING_OPENED)
24 StephanB 278
        {
85 killagreg 279
                if(doc->file != NULL)
280
                {
281
                        doc->state = KML_DOC_PLACEMARK_OPENED;
282
                        fwrite_((void*)KML_LINESTRING_FOOTER, sizeof(KML_LINESTRING_FOOTER)-1,1,doc->file);
283
                        retvalue = 1;
284
                }
24 StephanB 285
        }
286
        return(retvalue);
287
}
288
 
289
//________________________________________________________________________________________________________________________________________
41 ingob 290
// Function:    u8 LineStringAddPoint(gps_data_t, KML_Document_t *doc)
291
//
24 StephanB 292
// Description: This function adds a point to the specified document.
293
//
41 ingob 294
//
295
// Returnvalue: '1' if a ppoint was added could be started
24 StephanB 296
//________________________________________________________________________________________________________________________________________
297
 
85 killagreg 298
u8 KML_LineStringAddPoint(KML_Document_t *doc)
24 StephanB 299
{
300
 
301
        u8 retvalue = 0;
302
        s8 string[50];
41 ingob 303
        s32 rel_altitude = 0;
304
 
85 killagreg 305
        if(doc == NULL) return(0);
41 ingob 306
 
215 killagreg 307
        if((GPSData.Position.Status != INVALID) && (NaviData.HomePosition.Status != INVALID))
24 StephanB 308
        {
85 killagreg 309
                if(doc->state == KML_DOC_LINESTRING_OPENED)
24 StephanB 310
                {
41 ingob 311
                        if(doc->file != NULL)
312
                        {
313
                                s32 i1, i2;
51 killagreg 314
                                u8 sign;
85 killagreg 315
                                if(GPSData.Position.Longitude < 0) sign = '-';
51 killagreg 316
                                else sign = '+';
85 killagreg 317
                                i1 = abs(GPSData.Position.Longitude)/10000000L;
318
                                i2 = abs(GPSData.Position.Longitude)%10000000L;
51 killagreg 319
                                sprintf(string,"\r\n%c%ld.%07ld,",sign, i1, i2);
41 ingob 320
                                fputs_(string, doc->file);
85 killagreg 321
                                if(GPSData.Position.Latitude < 0) sign = '-';
51 killagreg 322
                                else sign = '+';
85 killagreg 323
                                i1 = abs(GPSData.Position.Latitude)/10000000L;
324
                                i2 = abs(GPSData.Position.Latitude)%10000000L;
51 killagreg 325
                                sprintf(string,"%c%ld.%07ld,",sign, i1, i2);
41 ingob 326
                                fputs_(string, doc->file);
327
                                // calculate relative altitude with respect to the altitude of the home position
215 killagreg 328
                                rel_altitude = GPSData.Position.Altitude - NaviData.HomePosition.Altitude;
41 ingob 329
                                if(rel_altitude < 0) rel_altitude = 0; // avoid negative altitudes in log
330
                                i1 = rel_altitude/1000L;
53 killagreg 331
                                i2 = rel_altitude%1000L;
41 ingob 332
                                sprintf(string," %ld.%03ld",i1, i2);
333
                                fputs_(string, doc->file);
334
                                retvalue = 1;
335
                        }
24 StephanB 336
                }
337
        }
338
 
339
        return(retvalue);
340
}
341
 
342
//________________________________________________________________________________________________________________________________________
41 ingob 343
// Function:    u8 KML_LoggGPSCoordinates(gps_data_t *, KML_Document_t *)
344
//
24 StephanB 345
// Description: This function opens adds gpscoordinates to an KML-Document. The document will be opened, if not already done
346
//
41 ingob 347
//
348
// Returnvalue: '1' if an gps coordinate was logged
24 StephanB 349
//________________________________________________________________________________________________________________________________________
350
 
85 killagreg 351
u8 KML_LoggGPSCoordinates(KML_Document_t *doc)
24 StephanB 352
{
41 ingob 353
        u8 retval = 0;
85 killagreg 354
        while(doc->state != KML_DOC_LINESTRING_OPENED)                                  // automatic create document with default filename on the card.
41 ingob 355
        {
24 StephanB 356
                switch(doc->state)
357
                {
85 killagreg 358
                        case KML_DOC_CLOSED:                                                                    // document hasn't been opened yet therefore it will be initialized automatically
41 ingob 359
                                retval = KML_DocumentOpen("default.kml",doc);   // open the kml-document with a standardname.
24 StephanB 360
                        break;
361
 
85 killagreg 362
                        case KML_DOC_OPENED:                                                                    // if a document has been opened before but no placemark exists:
363
                                retval = KML_PlaceMarkOpen(doc);
24 StephanB 364
                        break;
365
 
85 killagreg 366
                        case KML_DOC_PLACEMARK_OPENED:                                                  // add linestring to the placemark
41 ingob 367
                                retval = KML_LineStringBegin(doc);
24 StephanB 368
                        break;
369
 
41 ingob 370
                        default:
85 killagreg 371
                                retval = 0;
41 ingob 372
                        break;
373
 
24 StephanB 374
                }
41 ingob 375
                if(retval != 1) return(retval); // stop on error
376
        }
24 StephanB 377
 
85 killagreg 378
        if(doc->state == KML_DOC_LINESTRING_OPENED)                                             // if the document was opened add coordinates to the document.
24 StephanB 379
        {
85 killagreg 380
                retval = KML_LineStringAddPoint(doc);
24 StephanB 381
        }
41 ingob 382
        return(retval);
24 StephanB 383
}
27 holgerb 384