Subversion Repositories NaviCtrl

Rev

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