Subversion Repositories NaviCtrl

Rev

Rev 494 | Rev 496 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
41 ingob 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.
41 ingob 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.
41 ingob 31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
360 holgerb 32
// + Software LICENSING TERMS
41 ingob 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.
41 ingob 55
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
490 killagreg 56
#include <ctype.h>
57
#include <stdio.h>
58
#include <stdlib.h>      
41 ingob 59
#include <string.h>
60
#include "91x_lib.h"
61
#include "waypoints.h"
62
#include "uart1.h"
490 killagreg 63
#include "fat16.h"
41 ingob 64
 
65
// the waypoints list
351 holgerb 66
#define MAX_LIST_LEN 101
227 killagreg 67
 
280 killagreg 68
Point_t PointList[MAX_LIST_LEN];
69
u8 WPIndex = 0;         // list index of GPS point representig the current WP, can be maximal WPCount
70
u8 POIIndex = 0;        // list index of GPS Point representing the current POI, can be maximal WPCount
71
u8 WPCount = 0;         // number of waypoints
491 killagreg 72
u8 PointCount = 0;      // number of points in the list can be maximal equal to MAX_LIST_LEN
73
u8 POICount = 0;        // number of point of interest in the list
41 ingob 74
 
296 killagreg 75
u8 WPActive = FALSE;
280 killagreg 76
 
77
u8 PointList_Init(void)
41 ingob 78
{
280 killagreg 79
        return PointList_Clear();
41 ingob 80
}
81
 
280 killagreg 82
u8 PointList_Clear(void)
41 ingob 83
{
84
        u8 i;
227 killagreg 85
        WPIndex = 0;    // real list position are 1 ,2, 3 ...
86
        POIIndex = 0;   // real list position are 1 ,2, 3 ...
280 killagreg 87
        WPCount = 0;    // no waypoints
283 holgerb 88
    POICount = 0;
280 killagreg 89
        PointCount = 0; // no contents
296 killagreg 90
        WPActive = FALSE;
280 killagreg 91
        NaviData.WaypointNumber = WPCount;
92
        NaviData.WaypointIndex = 0;
41 ingob 93
 
280 killagreg 94
        for(i = 0; i < MAX_LIST_LEN; i++)
41 ingob 95
        {
280 killagreg 96
                PointList[i].Position.Status = INVALID;
97
                PointList[i].Position.Latitude = 0;
98
                PointList[i].Position.Longitude = 0;
99
                PointList[i].Position.Altitude = 0;
100
                PointList[i].Heading = 361;             // invalid value
101
                PointList[i].ToleranceRadius = 0;       // in meters, if the MK is within that range around the target, then the next target is triggered
102
                PointList[i].HoldTime = 0;                      // in seconds, if the was once in the tolerance area around a WP, this time defines the delay before the next WP is triggered
490 killagreg 103
                PointList[i].Event_Flag = 0;            // future implementation
104
                PointList[i].Index = 0;
286 killagreg 105
                PointList[i].Type = POINT_TYPE_INVALID;
490 killagreg 106
                PointList[i].WP_EventChannelValue = 0;
286 killagreg 107
                PointList[i].AltitudeRate = 0;          // no change of setpoint
490 killagreg 108
                PointList[i].Speed = 0;
109
                PointList[i].CamAngle = 0;
110
                PointList[i].Name[0] = 0;
41 ingob 111
        }
112
        return TRUE;           
113
}
114
 
280 killagreg 115
u8 PointList_GetCount(void)
41 ingob 116
{
280 killagreg 117
        return PointCount; // number of points in the list
41 ingob 118
}
119
 
295 killagreg 120
Point_t* PointList_GetAt(u8 index)
41 ingob 121
{
295 killagreg 122
        if((index > 0) && (index <= PointCount)) return(&(PointList[index-1])); // return pointer to this waypoint
123
        else return(NULL);
124
}
125
 
126
u8 PointList_SetAt(Point_t* pPoint)
127
{
128
        // if index is in range
296 killagreg 129
        if((pPoint->Index > 0) && (pPoint->Index <= MAX_LIST_LEN))
41 ingob 130
        {
295 killagreg 131
                // check list entry before update
132
                switch(PointList[pPoint->Index-1].Type)
277 killagreg 133
                {
295 killagreg 134
                        case POINT_TYPE_INVALID: // was invalid
135
                                switch(pPoint->Type)
136
                                {
137
                                        default:
138
                                        case POINT_TYPE_INVALID:
139
                                                // nothing to do
140
                                                break;
141
 
142
                                        case POINT_TYPE_WP:
143
                                                WPCount++;
144
                                                PointCount++;
145
                                                break;
146
 
147
                                        case POINT_TYPE_POI:
148
                                                POICount++;
149
                                                PointCount++;
150
                                                break;
151
                                }
152
                                break;
277 killagreg 153
 
490 killagreg 154
                        case POINT_TYPE_WP: // was a waypoint
295 killagreg 155
                                switch(pPoint->Type)
156
                                {
157
                                        case POINT_TYPE_INVALID:
158
                                                WPCount--;
159
                                                PointCount--;
160
                                                break;
277 killagreg 161
 
295 killagreg 162
                                        default:
163
                                        case POINT_TYPE_WP:
164
                                                //nothing to do
165
                                                break;
166
 
167
                                        case POINT_TYPE_POI:
168
                                                POICount++;
169
                                                WPCount--;
170
                                                break;
171
                                }
172
                                break;
173
 
174
                        case POINT_TYPE_POI: // was a poi
175
                                switch(pPoint->Type)
176
                                {
177
                                        case POINT_TYPE_INVALID:
178
                                                POICount--;
179
                                                PointCount--;
180
                                                break;
181
 
182
                                        case POINT_TYPE_WP:
183
                                                WPCount++;
184
                                                POICount--;
185
                                                break;
186
 
187
                                        case POINT_TYPE_POI:
188
                                        default:
189
                                                // nothing to do
190
                                                break;
191
                                }
192
                                break;         
277 killagreg 193
                }
295 killagreg 194
                memcpy(&PointList[pPoint->Index-1], pPoint, sizeof(Point_t)); // copy data to list entry                                                                                
195
                NaviData.WaypointNumber = WPCount;
196
                return pPoint->Index;
41 ingob 197
        }
296 killagreg 198
        else return(0);
41 ingob 199
}
200
 
227 killagreg 201
// returns the pointer to the first waypoint within the list
280 killagreg 202
Point_t* PointList_WPBegin(void)
41 ingob 203
{
277 killagreg 204
        u8 i;
227 killagreg 205
        WPIndex = 0; // set list position invalid
295 killagreg 206
 
283 holgerb 207
        if(WPActive == FALSE) return(NULL);
295 killagreg 208
 
277 killagreg 209
        POIIndex = 0; // set invalid POI
280 killagreg 210
        if(PointCount > 0)
152 killagreg 211
        {
225 killagreg 212
                // search for first wp in list
295 killagreg 213
                for(i = 0; i <MAX_LIST_LEN; i++)
225 killagreg 214
                {
280 killagreg 215
                        if((PointList[i].Type == POINT_TYPE_WP) && (PointList[i].Position.Status != INVALID))
225 killagreg 216
                        {
227 killagreg 217
                                WPIndex = i + 1;
225 killagreg 218
                                break;
219
                        }
220
                }
277 killagreg 221
                if(WPIndex) // found a WP in the list
222
                {
280 killagreg 223
                        NaviData.WaypointIndex = 1;
277 killagreg 224
                        // update index to POI
280 killagreg 225
                        if(PointList[WPIndex-1].Heading < 0) POIIndex = (u8)(-PointList[WPIndex-1].Heading);
296 killagreg 226
                        else POIIndex = 0;                     
277 killagreg 227
                }
228
                else // some points in the list but no WP found
229
                {
280 killagreg 230
                        NaviData.WaypointIndex = 0;
277 killagreg 231
                        //Check for an existing POI
295 killagreg 232
                        for(i = 0; i < MAX_LIST_LEN; i++)
277 killagreg 233
                        {
280 killagreg 234
                                if((PointList[i].Type == POINT_TYPE_POI) && (PointList[i].Position.Status != INVALID))
277 killagreg 235
                                {
236
                                        POIIndex = i + 1;
237
                                        break;
238
                                }
239
                        }
240
                }
152 killagreg 241
        }
277 killagreg 242
        else // no point in the list
152 killagreg 243
        {
277 killagreg 244
                POIIndex = 0;
295 killagreg 245
                NaviData.WaypointIndex = 0;    
227 killagreg 246
        }
295 killagreg 247
 
248
        if(WPIndex) return(&(PointList[WPIndex-1]));
249
        else return(NULL);
41 ingob 250
}
251
 
225 killagreg 252
// returns the last waypoint
280 killagreg 253
Point_t* PointList_WPEnd(void)
151 killagreg 254
{
277 killagreg 255
 
256
        u8 i;
227 killagreg 257
        WPIndex = 0; // set list position invalid
277 killagreg 258
        POIIndex = 0; // set invalid
283 holgerb 259
 
280 killagreg 260
        if(WPActive == FALSE) return(NULL);
277 killagreg 261
 
280 killagreg 262
        if(PointCount > 0)
151 killagreg 263
        {
227 killagreg 264
                // search backward!
295 killagreg 265
                for(i = 1; i <= MAX_LIST_LEN; i++)
225 killagreg 266
                {
295 killagreg 267
                        if((PointList[MAX_LIST_LEN - i].Type == POINT_TYPE_WP) && (PointList[MAX_LIST_LEN - i].Position.Status != INVALID))
225 killagreg 268
                        {      
295 killagreg 269
                                WPIndex = MAX_LIST_LEN - i + 1;
225 killagreg 270
                                break;
271
                        }
272
                }
277 killagreg 273
                if(WPIndex) // found a WP within the list
274
                {
280 killagreg 275
                        NaviData.WaypointIndex = WPCount;
276
                        if(PointList[WPIndex-1].Heading < 0) POIIndex = (u8)(-PointList[WPIndex-1].Heading);
277 killagreg 277
                        else POIIndex = 0;     
278
                }
278 killagreg 279
                else // list contains some points but no WP in the list
277 killagreg 280
                {
281
                        // search backward for a POI!
295 killagreg 282
                        for(i = 1; i <= MAX_LIST_LEN; i++)
277 killagreg 283
                        {
295 killagreg 284
                                if((PointList[MAX_LIST_LEN - i].Type == POINT_TYPE_POI) && (PointList[MAX_LIST_LEN - i].Position.Status != INVALID))
277 killagreg 285
                                {      
295 killagreg 286
                                        POIIndex = MAX_LIST_LEN - i + 1;
277 killagreg 287
                                        break;
288
                                }
289
                        }
295 killagreg 290
                        NaviData.WaypointIndex = 0;    
277 killagreg 291
                }
151 killagreg 292
        }
277 killagreg 293
        else // no point in the list
227 killagreg 294
        {
280 killagreg 295
                POIIndex = 0;
296
                NaviData.WaypointIndex = 0;
227 killagreg 297
        }
295 killagreg 298
        if(WPIndex) return(&(PointList[WPIndex-1]));
299
        else return(NULL);
151 killagreg 300
}
301
 
41 ingob 302
// returns a pointer to the next waypoint or NULL if the end of the list has been reached
280 killagreg 303
Point_t* PointList_WPNext(void)
41 ingob 304
{
227 killagreg 305
        u8 wp_found = 0;
280 killagreg 306
        if(WPActive == FALSE) return(NULL);
227 killagreg 307
 
295 killagreg 308
        if(WPIndex < MAX_LIST_LEN) // if there is a next entry in the list
41 ingob 309
        {
227 killagreg 310
                u8 i;
295 killagreg 311
                for(i = WPIndex; i < MAX_LIST_LEN; i++) // start search for next at next list entry
225 killagreg 312
                {
280 killagreg 313
                        if((PointList[i].Type == POINT_TYPE_WP) && (PointList[i].Position.Status != INVALID)) // jump over POIs
225 killagreg 314
                        {
227 killagreg 315
                                wp_found = i+1;
225 killagreg 316
                                break;
317
                        }
318
                }
41 ingob 319
        }
227 killagreg 320
        if(wp_found)
321
        {
322
                WPIndex = wp_found; // update list position
280 killagreg 323
                NaviData.WaypointIndex++;
324
                if(PointList[WPIndex-1].Heading < 0) POIIndex = (u8)(-PointList[WPIndex-1].Heading);
227 killagreg 325
                else POIIndex = 0;
280 killagreg 326
                return(&(PointList[WPIndex-1]));        // return pointer to this waypoint
227 killagreg 327
        }
277 killagreg 328
        else
280 killagreg 329
        {  // no next wp found
330
                NaviData.WaypointIndex = 0;    
277 killagreg 331
                POIIndex = 0;
332
                return(NULL);
333
        }
41 ingob 334
}      
280 killagreg 335
 
336
void PointList_WPActive(u8 set)
337
{
295 killagreg 338
        if(set)
339
        {      
340
                WPActive = TRUE;
491 killagreg 341
                PointList_WPBegin(); // updates POI index
295 killagreg 342
        }
343
        else
344
        {
345
                WPActive = FALSE;
346
                POIIndex = 0;  // disable POI also
347
        }
280 killagreg 348
}
41 ingob 349
 
280 killagreg 350
Point_t* PointList_GetPOI(void)
224 killagreg 351
{
280 killagreg 352
        return PointList_GetAt(POIIndex);      
224 killagreg 353
}
354
 
490 killagreg 355
#define LINE_MAX 70
356
#define WP_FILE_VERSION_COMPATIBLE 3
357
// save actual point list to SD card
495 killagreg 358
u8 PointList_SaveToFile(WPL_Store_t * pWPL_Store)
490 killagreg 359
{
360
        File_t *fp;
495 killagreg 361
        s8 wpline[LINE_MAX];
362
        u8 retval = WPL_ERROR;
490 killagreg 363
        // user absolute path, i.e. leading /
495 killagreg 364
 
365
        sprintf(wpline, "/list_%03d.wpl", pWPL_Store->Index);
490 killagreg 366
 
367
        UART1_PutString("\n\r Save WPL...");
368
 
369
        if(Fat16_IsValid())
370
        {       // check if wpl file is existing
495 killagreg 371
                if(fexist_(wpline))
372
                {       //file is existent
373
                        if(!(pWPL_Store->OverwriteFile))
374
                        {
375
                                UART1_PutString("Error: file exist!\r\n");
376
                                return(WPL_FILEEXIST);
377
                        }      
378
                }
490 killagreg 379
                fp = fopen_(wpline, 'w');               // try to open the file
380
                if(fp == NULL)
381
                {
382
                        UART1_PutString("ERROR: Creating waypoint file!\r\n");
383
                        return(retval);
384
                }
385
                // Create general section and key entries
386
                fputs_("[General]\r\n", fp);
495 killagreg 387
                sprintf(wpline, "Name=%s\r\n",  pWPL_Store->Name);
494 killagreg 388
                fputs_(wpline, fp);
495 killagreg 389
                sprintf(wpline, "PositionType=%d\r\n",  pWPL_Store->Type);
390
                fputs_(wpline, fp);
490 killagreg 391
                sprintf(wpline, "FileVersion=%d\r\n", WP_FILE_VERSION_COMPATIBLE);
392
                fputs_(wpline, fp);
393
                sprintf(wpline, "NumberOfWaypoints=%d\r\n", PointCount);
394
                fputs_(wpline, fp);
395
                // dump all points if existent
396
                if(PointCount)
397
                {
398
                        u8 i, u8_1;
399
                        s32 i32_1, i32_2;
400
 
401
                        for (i = 0; i < PointCount; i++)
402
                        {
403
                                sprintf(wpline, "[Point%d]\r\n",PointList[i].Index);
404
                                fputs_(wpline, fp);
405
                                // write latitude in deg
406
                                if(PointList[i].Position.Latitude < 0) u8_1 = '-';
407
                                else u8_1 = '+';
408
                                i32_1 = abs(PointList[i].Position.Latitude)/10000000L;
409
                                i32_2 = abs(PointList[i].Position.Latitude)%10000000L;
410
                                sprintf(wpline, "Latitude=%c%ld.%07ld\r\n", u8_1, i32_1, i32_2);
411
                                fputs_(wpline, fp);
412
                                // write longitude in deg
413
                                if(PointList[i].Position.Longitude < 0) u8_1 = '-';
414
                                else u8_1 = '+';
415
                                i32_1 = abs(PointList[i].Position.Longitude)/10000000L;
416
                                i32_2 = abs(PointList[i].Position.Longitude)%10000000L;
417
                                sprintf(wpline, "Longitude=%c%ld.%07ld\r\n", u8_1, i32_1, i32_2);
418
                                fputs_(wpline, fp);
419
                                // write tolerace radius in m
420
                                sprintf(wpline, "Radius=%d\r\n", PointList[i].ToleranceRadius);
421
                                fputs_(wpline, fp);
422
                                // write altitude in m
423
                                if(PointList[i].Position.Altitude < 0) u8_1 = '-';
424
                                else u8_1 = '+';
425
                                if(PointList[i].Type == POINT_TYPE_POI)
426
                                {
427
                                        i32_1 = abs(PointList[i].Position.Altitude)/100L;  // cm --> m
428
                                        i32_2 = abs(PointList[i].Position.Altitude)%100L;
429
                                }
430
                                else
431
                                {
432
                                        i32_1 = abs(PointList[i].Position.Altitude)/10L; // dm --> m
433
                                        i32_2 = abs(PointList[i].Position.Altitude)%10L;
434
                                }
435
                                sprintf(wpline, "Altitude=%c%ld.%01ld\r\n",  u8_1, i32_1, i32_2);
436
                                fputs_(wpline, fp);
437
                                // write climb rate in 0.1 m/s
438
                                sprintf(wpline, "ClimbRate=%d\r\n", PointList[i].AltitudeRate);
439
                                fputs_(wpline, fp);
440
                                // write hold time in s
441
                                sprintf(wpline, "DelayTime=%d\r\n", PointList[i].HoldTime);
442
                                fputs_(wpline, fp);
443
                                // write event channel value
444
                                sprintf(wpline, "WP_Event_Channel_Value=%d\r\n", PointList[i].WP_EventChannelValue);
445
                                fputs_(wpline, fp);
446
                                // write heading in deg (0= nothing, neg. values index to poi)
447
                                sprintf(wpline, "Heading=%d\r\n", PointList[i].Heading);
448
                                fputs_(wpline, fp);
449
                                // write speed in 0.1 m/s
450
                                sprintf(wpline, "Speed=%d\r\n", PointList[i].Speed);
451
                                fputs_(wpline, fp);
452
                                // write cam angle in degree (255 -> POI-Automatic)
453
                                sprintf(wpline, "CAM-Nick=%d\r\n", PointList[i].CamAngle);
454
                                fputs_(wpline, fp);
455
                                // write point type
456
                                sprintf(wpline, "Type=%d\r\n", PointList[i].Type + 1);
457
                                fputs_(wpline, fp);    
458
                                // write prefix 
459
                                //if(PointList[i].Type == POINT_TYPE_WP) u8_1 = 'P';
460
                                //else u8_1 = '0';
461
                                sprintf(wpline, "Prefix=%s\r\n", PointList[i].Name);
462
                                fputs_(wpline, fp);
463
                        } // EOF loop over all points
464
                } // EOF if(PointCount)
465
                if(EOF == fclose_(fp))
466
                {
467
                        UART1_PutString("failed!\r\n");
468
                }
469
                else
470
                {
471
                        UART1_PutString("ok\r\n");
495 killagreg 472
                        retval = WPL_OK;
490 killagreg 473
                }                                
474
        } // EOF if(Fat16_IsValid())
492 killagreg 475
        else UART1_PutString("no file system found!\r\n");     
490 killagreg 476
        return(retval);
477
}
478
 
479
// load actual point list from SD card
495 killagreg 480
u8 PointList_ReadFromFile(WPL_Store_t * pWPL_Store)
490 killagreg 481
{
482
        File_t *fp;
483
        s8 wpline[LINE_MAX], retval = 0;
484
 
485
        s8 *name, *value;
486
        u8 i;
487
 
488
        u8 IsGeneralSection = 0;
489
        u8 IsPointSection  = 0;
490
        u8 WPNumber = 0;
491
 
492
        // user absolute path, i.e. leading /
495 killagreg 493
        sprintf(wpline, "/list_%03d.wpl", pWPL_Store->Index);
490 killagreg 494
 
495
        UART1_PutString("\n\r Read ");
496
        UART1_PutString(wpline);
497
        UART1_PutString("...");
498
 
499
        if(Fat16_IsValid())
500
        {       // check if wpl file is existing
501
                fp = fopen_(wpline, 'r');               // try to open the file
502
                if(fp == NULL)
503
                {
504
                        UART1_PutString("ERROR: Reading waypoint file!\r\n");
505
                        return(0);
506
                }
507
                // clear point list first
508
                PointList_Clear();
509
                // read all lines from file
510
                while(fgets_(wpline, LINE_MAX, fp) != 0)
511
                {
512
                        if ( // ignorelines starting with \r,\n,' ',';','#'
513
                                (wpline[0] != '\n') &&
514
                                (wpline[0] != '\r') &&
515
                                (wpline[0] != ' ' ) &&
516
                                (wpline[0] != ';' ) &&
517
                                (wpline[0] != '#' )
518
                                )
519
                        {
520
                                // check for section line found
521
                                if(wpline[0] == '[')
522
                                {
523
                                        // next section found
524
                                        IsGeneralSection = 0;
525
                                        IsPointSection = 0;
526
 
527
                                        name  = strtok(&wpline[1], "]");
528
                                        if(name != NULL)   // if section name
529
                                        {
530
                                                // check section type
531
                                                for(i=0; name[i]; i++) name[i] = toupper(name[i]); // convert to upper case
532
 
533
                                                if(strncmp(name, "POINT", 5) == 0)
534
                                                {
535
                                                        IsPointSection = (u8)atoi(&name[5]);
536
                                                        PointCount++;
537
                                                }
538
                                                else if(strcmp(name, "GENERAL") == 0)
539
                                                {
540
                                                        IsGeneralSection = 1;
541
                                                }
542
                                                else
543
                                                {
544
                                                        UART1_PutString("Unknown section: ");
545
                                                        UART1_PutString(name);
546
                                                        UART1_PutString("\r\n");
547
                                                }
548
                                        }
549
                                } // EOF section line
550
                                else
551
                                {       // look for key entrys of each sections
552
                                        name  = strtok(wpline, "="); // get name
553
                                        value = strtok(NULL, "="); // get value
554
                                        if ((name != NULL) && (value != NULL))
555
                                        {
556
                                                for(i=0; name[i]; i++) name[i] = toupper(name[i]); // convert to upper case
557
                                                if(IsPointSection &&  (IsPointSection <= WPNumber))
558
                                                {
559
                                                        if(strcmp(name, "LATITUDE") == 0)
560
                                                        {
561
                                                                PointList[IsPointSection-1].Position.Latitude = (s32)(atof(value) * 1E7);
562
                                                        }
563
                                                        else if(strcmp(name, "LONGITUDE") == 0)
564
                                                        {
565
                                                                PointList[IsPointSection-1].Position.Longitude = (s32)(atof(value) * 1E7);
566
                                                        }
567
                                                        else if(strcmp(name, "RADIUS") == 0)
568
                                                        {
569
                                                                PointList[IsPointSection-1].ToleranceRadius = (u8)atoi(value);
570
                                                        }
571
                                                        else if(strcmp(name, "ALTITUDE") == 0)
572
                                                        {
573
                                                                PointList[IsPointSection-1].Position.Altitude = (s32)(atof(value) * 100.0);      // in cm
574
                                                                PointList[IsPointSection-1].Position.Status = NEWDATA;
575
                                                        }
576
                                                        else if(strcmp(name, "CLIMBRATE") == 0)
577
                                                        {
578
                                                                PointList[IsPointSection-1].AltitudeRate = (u8)atoi(value);
579
                                                        }
580
                                                        else if(strcmp(name, "DELAYTIME") == 0)
581
                                                        {
582
                                                                PointList[IsPointSection-1].HoldTime = (u8)atoi(value);
583
                                                        }
584
                                                        else if(strcmp(name, "WP_EVENT_CHANNEL_VALUE") == 0)
585
                                                        {
586
                                                                PointList[IsPointSection-1].WP_EventChannelValue = (u8)atoi(value);
587
                                                        }
588
                                                        else if(strcmp(name, "HEADING") == 0)
589
                                                        {
590
                                                                PointList[IsPointSection-1].Heading = (s16)atoi(value);
591
                                                        }
592
                                                        else if(strcmp(name, "SPEED") == 0)
593
                                                        {
594
                                                                PointList[IsPointSection-1].Speed = (u8)atoi(value);
595
                                                        }
596
                                                        else if(strcmp(name, "CAM-NICK") == 0)
597
                                                        {
598
                                                                PointList[IsPointSection-1].CamAngle = (u8)atoi(value);
599
                                                        }
600
                                                        else if(strcmp(name, "TYPE") == 0)
601
                                                        {
602
                                                                PointList[IsPointSection-1].Type = (u8)atoi(value);
603
                                                                if(PointList[IsPointSection-1].Type > 0) PointList[IsPointSection-1].Type--;  // index shift
604
                                                                else PointList[IsPointSection-1].Type = POINT_TYPE_INVALID;
605
 
606
                                                                switch(PointList[IsPointSection-1].Type)
607
                                                                {
608
                                                                        case POINT_TYPE_WP:
609
                                                                                // this works only if altitude key is set before point type key in WPL file     !!
610
                                                                                PointList[IsPointSection-1].Position.Altitude /= 10; // dm only for WPs 
611
                                                                                WPCount++;
612
                                                                                break;
613
 
614
                                                                        case POINT_TYPE_POI:
615
                                                                                POICount++;
616
                                                                                break;
617
                                                                }
618
                                                        }
619
                                                        else if(strcmp(name, "PREFIX") == 0)
620
                                                        {
621
                                                                strncpy(PointList[IsPointSection-1].Name, value, 4);
622
                                                                PointList[IsPointSection-1].Name[3] = 0; // Terminate string
623
                                                        }
624
                                                        else
625
                                                        {
626
                                                                UART1_PutString("Unknown key: ");
627
                                                                UART1_PutString(name);
628
                                                                UART1_PutString("\r\n");
629
                                                        }      
630
                                                } // EOF point section
631
                                                else if(IsGeneralSection)
632
                                                {
633
                                                        if(strcmp(name, "NUMBEROFWAYPOINTS") == 0)
634
                                                        {      
635
                                                                WPNumber = (u8)atoi(value);
636
                                                                if(!WPNumber) // no waypoints in file
637
                                                                {
638
                                                                        return(1); // we are done here  
639
                                                                }
640
                                                                else if(WPNumber > MAX_LIST_LEN) // number o points larger than ram list
641
                                                                {
642
                                                                        UART1_PutString("To much points!");
643
                                                                        return(0);
644
                                                                }
645
                                                        }
646
                                                        else if (strcmp(name, "FILEVERSION") == 0)
647
                                                        {
648
                                                                if((u8)atoi(value) != WP_FILE_VERSION_COMPATIBLE)
649
                                                                {
650
                                                                        UART1_PutString("Bad file version!\r\n");
651
                                                                        return(0);     
652
                                                                }
653
                                                        }
495 killagreg 654
                                                        else if (strcmp(name, "NAME") == 0)
655
                                                        {
656
                                                                u8 len = strlen(value);
657
                                                                if(len > 11)
658
                                                                value[11] = 0;
659
                                                                memcpy(pWPL_Store->Name, value, 12);
660
                                                        }
661
                                                        else if (strcmp(name, "POSITIONTYPE") == 0)
662
                                                        {
663
                                                                pWPL_Store->Type = (u8)atoi(value);
664
                                                        }
490 killagreg 665
                                                        else
666
                                                        {
667
                                                                UART1_PutString("Unknown key: ");
668
                                                                UART1_PutString(name);
669
                                                                UART1_PutString("\r\n");
670
                                                        }
671
                                                } // EOF general section
672
                                        } // EOF valid key entry
673
                                } // EOF key entry line
674
                        } // valid line
675
                } // EOF loop over all lines
676
                fclose_(fp);
677
                NaviData.WaypointNumber = WPCount;
678
                UART1_PutString("ok\r\n");
679
                retval = 1;                              
680
        } // EOF if(Fat16_IsValid())
681
        else UART1_PutString("no file system found!\r\n");     
682
        return(retval);
491 killagreg 683
}
490 killagreg 684
 
491 killagreg 685
// move actual point list to ref pos., the point in the list marked by index gets the RefPos afterwards
686
u8 PointList_Move(u8 RefIndex, GPS_Pos_t* pRefPos)
687
{
688
        u8 retval = 0;
689
        GPS_Pos_t RefPos_old;
690
        GPS_Pos_Deviation_t RefDeviation;
691
 
692
        // check inputs for plausibility;
693
        if((RefIndex == 0) || (RefIndex > PointCount)) return(retval); 
694
        if(pRefPos == NULL) return(retval);
695
        if(pRefPos->Status == INVALID) return(retval);
696
 
697
        // try to copy the old reference in point list to a local buffer
698
        if(GPSPos_Copy(&(PointList[RefIndex-1].Position), &RefPos_old))
699
        {
700
                u8 i;
701
                // for each point position in the list
702
                for(i = 0; i < PointCount; i++)
703
                {
704
                        retval = 0;
705
                    // calculate deviation form old ref, i.e the north and east shift of each point in the list from the reference position
706
                        if(!GPSPos_Deviation(&(PointList[i].Position), &RefPos_old, &RefDeviation)) break;
707
                        // copy of the new reference position into this list place
708
                        if(!GPSPos_Copy(pRefPos, &(PointList[i].Position))) break;
709
                        // move new reference according to the deviation of the old reference
710
                        retval = GPSPos_ShiftCartesian(&(PointList[i].Position), RefDeviation.North, RefDeviation.East);
711
                        if(!retval) break;             
712
                }
713
        } // else ref pos old not copied!
714
        if(!retval) PointList_Clear();
715
        return(retval);
716
}
717
 
718
 
719