Subversion Repositories NaviCtrl

Rev

Rev 434 | 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
// + www.MikroKopter.com
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
360 holgerb 7
// + Software Nutzungsbedingungen (english version: see below)
8
// + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt -
9
// + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den
10
// + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool 
11
// + - nachfolgend Software genannt - nur für private Zwecke zu nutzen.
12
// + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig.
24 StephanB 13
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
360 holgerb 14
// + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im
15
// + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu.
16
// + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie
17
// + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden.
18
// + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren
19
// + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
20
// + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren
21
// + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand 
22
// + des Mitverschuldens offen.
23
// + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet.
24
// + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
25
// + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern.
26
// + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang
27
// + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt.
28
// + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software.
29
// + #### ENDE DER NUTZUNGSBEDINGUNGEN ####'
30
// +  Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar.
24 StephanB 31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
360 holgerb 32
// + Software LICENSING TERMS
24 StephanB 33
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
360 holgerb 34
// + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor -
35
// + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware 
36
// + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*.
37
// + The Software may only be used with the Licensor's products.
38
// + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this
39
// + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this
40
// + agreement shall be the property of the Licensor.
41
// + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other
42
// + features that can be used to identify the program may not be altered or defaced by the customer.
43
// + The customer shall be responsible for taking reasonable precautions
44
// + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the
45
// + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and
46
// + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product
47
// + liability. However, the Licensor shall be entitled to the defense of contributory negligence.
48
// + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test
49
// + the software for his purpose before any operational usage. The customer will backup his data before using the software.
50
// + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data
51
// + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations.
52
// + *) The territory aspect only refers to the place where the Software is used, not its programmed range.
53
// + #### END OF LICENSING TERMS ####
54
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
24 StephanB 55
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
41 ingob 56
#include <stdlib.h>
24 StephanB 57
#include <stdio.h>
41 ingob 58
#include "91x_lib.h"
24 StephanB 59
#include "kml.h"
60
#include "kml_header.h"
215 killagreg 61
#include "uart1.h"
380 holgerb 62
#include "logging.h"
24 StephanB 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);
380 holgerb 256
                        Logged_KML_Counter = 0;
85 killagreg 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
 
434 holgerb 307
        if(GPSData.Position.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;
352 holgerb 332
                                sprintf(string,"%ld.%03ld",i1, i2);
41 ingob 333
                                fputs_(string, doc->file);
380 holgerb 334
                                Logged_KML_Counter++;
41 ingob 335
                                retvalue = 1;
336
                        }
24 StephanB 337
                }
338
        }
434 holgerb 339
        else
340
        {
341
                if(doc->state == KML_DOC_LINESTRING_OPENED)
342
                {
343
                        if(doc->file != NULL)
344
                        {
345
                                sprintf(string,"\r\n");
346
                                fputs_(string, doc->file);
347
                                Logged_KML_Counter++;
348
                                retvalue = 1;
349
                        }
350
                }
351
        }
24 StephanB 352
        return(retvalue);
353
}
354
 
355
//________________________________________________________________________________________________________________________________________
41 ingob 356
// Function:    u8 KML_LoggGPSCoordinates(gps_data_t *, KML_Document_t *)
357
//
24 StephanB 358
// Description: This function opens adds gpscoordinates to an KML-Document. The document will be opened, if not already done
359
//
41 ingob 360
//
361
// Returnvalue: '1' if an gps coordinate was logged
24 StephanB 362
//________________________________________________________________________________________________________________________________________
363
 
85 killagreg 364
u8 KML_LoggGPSCoordinates(KML_Document_t *doc)
24 StephanB 365
{
41 ingob 366
        u8 retval = 0;
85 killagreg 367
        while(doc->state != KML_DOC_LINESTRING_OPENED)                                  // automatic create document with default filename on the card.
41 ingob 368
        {
24 StephanB 369
                switch(doc->state)
370
                {
85 killagreg 371
                        case KML_DOC_CLOSED:                                                                    // document hasn't been opened yet therefore it will be initialized automatically
41 ingob 372
                                retval = KML_DocumentOpen("default.kml",doc);   // open the kml-document with a standardname.
24 StephanB 373
                        break;
374
 
85 killagreg 375
                        case KML_DOC_OPENED:                                                                    // if a document has been opened before but no placemark exists:
376
                                retval = KML_PlaceMarkOpen(doc);
24 StephanB 377
                        break;
378
 
85 killagreg 379
                        case KML_DOC_PLACEMARK_OPENED:                                                  // add linestring to the placemark
41 ingob 380
                                retval = KML_LineStringBegin(doc);
24 StephanB 381
                        break;
382
 
41 ingob 383
                        default:
85 killagreg 384
                                retval = 0;
41 ingob 385
                        break;
386
 
24 StephanB 387
                }
41 ingob 388
                if(retval != 1) return(retval); // stop on error
389
        }
24 StephanB 390
 
85 killagreg 391
        if(doc->state == KML_DOC_LINESTRING_OPENED)                                             // if the document was opened add coordinates to the document.
24 StephanB 392
        {
85 killagreg 393
                retval = KML_LineStringAddPoint(doc);
24 StephanB 394
        }
41 ingob 395
        return(retval);
24 StephanB 396
}
27 holgerb 397