Subversion Repositories NaviCtrl

Rev

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