Subversion Repositories NaviCtrl

Rev

Rev 577 | Rev 642 | 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
516 holgerb 10
// + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool 
360 holgerb 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
516 holgerb 21
// + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand 
360 holgerb 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 -
516 holgerb 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 
360 holgerb 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>
516 holgerb 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"
504 holgerb 64
#include "main.h"
513 killagreg 65
#include "spi_slave.h"
41 ingob 66
 
496 killagreg 67
 
68
WPL_Store_t WPL_Store;
69
 
280 killagreg 70
Point_t PointList[MAX_LIST_LEN];
71
u8 WPIndex = 0;         // list index of GPS point representig the current WP, can be maximal WPCount
72
u8 POIIndex = 0;        // list index of GPS Point representing the current POI, can be maximal WPCount
73
u8 WPCount = 0;         // number of waypoints
491 killagreg 74
u8 PointCount = 0;      // number of points in the list can be maximal equal to MAX_LIST_LEN
75
u8 POICount = 0;        // number of point of interest in the list
601 holgerb 76
u8 FsPointCnt = 0;
517 holgerb 77
s16 HeadingOld = -1;
577 holgerb 78
u32 SD_WaypointTimeout = 5; // Setting on SD-Card
41 ingob 79
 
296 killagreg 80
u8 WPActive = FALSE;
280 killagreg 81
 
82
u8 PointList_Init(void)
41 ingob 83
{
516 holgerb 84
        return PointList_Clear();
41 ingob 85
}
86
 
280 killagreg 87
u8 PointList_Clear(void)
41 ingob 88
{
89
        u8 i;
227 killagreg 90
        WPIndex = 0;    // real list position are 1 ,2, 3 ...
91
        POIIndex = 0;   // real list position are 1 ,2, 3 ...
280 killagreg 92
        WPCount = 0;    // no waypoints
601 holgerb 93
        FsPointCnt = 0;
516 holgerb 94
    POICount = 0;
280 killagreg 95
        PointCount = 0; // no contents
296 killagreg 96
        WPActive = FALSE;
280 killagreg 97
        NaviData.WaypointNumber = WPCount;
98
        NaviData.WaypointIndex = 0;
99
        for(i = 0; i < MAX_LIST_LEN; i++)
41 ingob 100
        {
280 killagreg 101
                PointList[i].Position.Status = INVALID;
102
                PointList[i].Position.Latitude = 0;
103
                PointList[i].Position.Longitude = 0;
104
                PointList[i].Position.Altitude = 0;
105
                PointList[i].Heading = 361;             // invalid value
106
                PointList[i].ToleranceRadius = 0;       // in meters, if the MK is within that range around the target, then the next target is triggered
107
                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 108
                PointList[i].Event_Flag = 0;            // future implementation
109
                PointList[i].Index = 0;
286 killagreg 110
                PointList[i].Type = POINT_TYPE_INVALID;
490 killagreg 111
                PointList[i].WP_EventChannelValue = 0;
286 killagreg 112
                PointList[i].AltitudeRate = 0;          // no change of setpoint
490 killagreg 113
                PointList[i].Speed = 0;
114
                PointList[i].CamAngle = 0;
115
                PointList[i].Name[0] = 0;
41 ingob 116
        }
504 holgerb 117
        ClearWLP_Name();
516 holgerb 118
        return TRUE;           
41 ingob 119
}
120
 
280 killagreg 121
u8 PointList_GetCount(void)
41 ingob 122
{
280 killagreg 123
        return PointCount; // number of points in the list
41 ingob 124
}
125
 
295 killagreg 126
Point_t* PointList_GetAt(u8 index)
41 ingob 127
{
295 killagreg 128
        if((index > 0) && (index <= PointCount)) return(&(PointList[index-1])); // return pointer to this waypoint
129
        else return(NULL);
130
}
131
 
132
u8 PointList_SetAt(Point_t* pPoint)
133
{
134
        // if index is in range
296 killagreg 135
        if((pPoint->Index > 0) && (pPoint->Index <= MAX_LIST_LEN))
41 ingob 136
        {
295 killagreg 137
                // check list entry before update
138
                switch(PointList[pPoint->Index-1].Type)
277 killagreg 139
                {
295 killagreg 140
                        case POINT_TYPE_INVALID: // was invalid
141
                                switch(pPoint->Type)
142
                                {
143
                                        default:
144
                                        case POINT_TYPE_INVALID:
145
                                                // nothing to do
146
                                                break;
147
 
148
                                        case POINT_TYPE_WP:
149
                                                WPCount++;
150
                                                PointCount++;
151
                                                break;
516 holgerb 152
 
295 killagreg 153
                                        case POINT_TYPE_POI:
154
                                                POICount++;
155
                                                PointCount++;
156
                                                break;
601 holgerb 157
                                        case POINT_TYPE_FS:
158
                                                FsPointCnt++;
159
                                                PointCount++;
160
                                                break;
295 killagreg 161
                                }
516 holgerb 162
                                break;
163
 
490 killagreg 164
                        case POINT_TYPE_WP: // was a waypoint
295 killagreg 165
                                switch(pPoint->Type)
166
                                {
167
                                        case POINT_TYPE_INVALID:
168
                                                WPCount--;
169
                                                PointCount--;
170
                                                break;
277 killagreg 171
 
295 killagreg 172
                                        default:
173
                                        case POINT_TYPE_WP:
174
                                                //nothing to do
175
                                                break;
601 holgerb 176
                                        case POINT_TYPE_FS:
177
                                                FsPointCnt++;
178
                                                WPCount--;
179
                                                break;
295 killagreg 180
                                        case POINT_TYPE_POI:
181
                                                POICount++;
182
                                                WPCount--;
183
                                                break;
184
                                }
185
                                break;
516 holgerb 186
 
295 killagreg 187
                        case POINT_TYPE_POI: // was a poi
188
                                switch(pPoint->Type)
189
                                {
190
                                        case POINT_TYPE_INVALID:
191
                                                POICount--;
192
                                                PointCount--;
193
                                                break;
194
 
195
                                        case POINT_TYPE_WP:
196
                                                WPCount++;
197
                                                POICount--;
198
                                                break;
601 holgerb 199
 
200
                                        case POINT_TYPE_FS:
201
                                                FsPointCnt++;
202
                                                POICount--;
203
                                                break;
516 holgerb 204
 
295 killagreg 205
                                        case POINT_TYPE_POI:
206
                                        default:
207
                                                // nothing to do
208
                                                break;
209
                                }
601 holgerb 210
                        case POINT_TYPE_FS: // was a Failsafe
211
                                switch(pPoint->Type)
212
                                {
213
                                        case POINT_TYPE_INVALID:
214
                                                FsPointCnt--;
215
                                                PointCount--;
216
                                                break;
217
 
218
                                        case POINT_TYPE_WP:
219
                                                WPCount++;
220
                                                FsPointCnt--;
221
                                                break;
222
 
223
                                        case POINT_TYPE_POI:
224
                                                POICount++;
225
                                                FsPointCnt--;
226
                                                break;
227
 
228
                                        case POINT_TYPE_FS:
229
                                                break;
230
                                        default:
231
                                                // nothing to do
232
                                                break;
233
                                }
516 holgerb 234
                                break;         
277 killagreg 235
                }
516 holgerb 236
                memcpy(&PointList[pPoint->Index-1], pPoint, sizeof(Point_t)); // copy data to list entry                                                                                
295 killagreg 237
                NaviData.WaypointNumber = WPCount;
238
                return pPoint->Index;
41 ingob 239
        }
296 killagreg 240
        else return(0);
41 ingob 241
}
242
 
227 killagreg 243
// returns the pointer to the first waypoint within the list
280 killagreg 244
Point_t* PointList_WPBegin(void)
41 ingob 245
{
277 killagreg 246
        u8 i;
227 killagreg 247
        WPIndex = 0; // set list position invalid
295 killagreg 248
 
283 holgerb 249
        if(WPActive == FALSE) return(NULL);
295 killagreg 250
 
277 killagreg 251
        POIIndex = 0; // set invalid POI
516 holgerb 252
        if(PointCount > 0)
152 killagreg 253
        {
225 killagreg 254
                // search for first wp in list
295 killagreg 255
                for(i = 0; i <MAX_LIST_LEN; i++)
225 killagreg 256
                {
601 holgerb 257
                        if((PointList[i].Type == POINT_TYPE_WP) && (PointList[i].Position.Status != INVALID))  // jump over POIs and FS-Positions
225 killagreg 258
                        {
227 killagreg 259
                                WPIndex = i + 1;
225 killagreg 260
                                break;
261
                        }
601 holgerb 262
/*
263
                        else
264
                        if((PointList[i].Type == POINT_TYPE_FS) && (PointList[i].Position.Status != INVALID))
265
                        {
266
                                GPSPos_Copy(&(PointList[i].Position), &GPS_FailsafePosition);
267
                        }
268
*/
225 killagreg 269
                }
277 killagreg 270
                if(WPIndex) // found a WP in the list
271
                {
280 killagreg 272
                        NaviData.WaypointIndex = 1;
549 holgerb 273
                        NewWaypointsReceived = 1; // activates the Waypoint list as soon as CH is started
277 killagreg 274
                        // update index to POI
280 killagreg 275
                        if(PointList[WPIndex-1].Heading < 0) POIIndex = (u8)(-PointList[WPIndex-1].Heading);
516 holgerb 276
                        else POIIndex = 0;                     
277 killagreg 277
                }
278
                else // some points in the list but no WP found
279
                {
280 killagreg 280
                        NaviData.WaypointIndex = 0;
277 killagreg 281
                        //Check for an existing POI
295 killagreg 282
                        for(i = 0; i < MAX_LIST_LEN; i++)
277 killagreg 283
                        {
280 killagreg 284
                                if((PointList[i].Type == POINT_TYPE_POI) && (PointList[i].Position.Status != INVALID))
277 killagreg 285
                                {
286
                                        POIIndex = i + 1;
287
                                        break;
288
                                }
289
                        }
290
                }
152 killagreg 291
        }
277 killagreg 292
        else // no point in the list
152 killagreg 293
        {
516 holgerb 294
                POIIndex = 0;
295
                NaviData.WaypointIndex = 0;    
227 killagreg 296
        }
295 killagreg 297
 
298
        if(WPIndex) return(&(PointList[WPIndex-1]));
299
        else return(NULL);
41 ingob 300
}
301
 
225 killagreg 302
// returns the last waypoint
280 killagreg 303
Point_t* PointList_WPEnd(void)
151 killagreg 304
{
516 holgerb 305
 
277 killagreg 306
        u8 i;
227 killagreg 307
        WPIndex = 0; // set list position invalid
277 killagreg 308
        POIIndex = 0; // set invalid
283 holgerb 309
 
280 killagreg 310
        if(WPActive == FALSE) return(NULL);
277 killagreg 311
 
280 killagreg 312
        if(PointCount > 0)
151 killagreg 313
        {
227 killagreg 314
                // search backward!
295 killagreg 315
                for(i = 1; i <= MAX_LIST_LEN; i++)
225 killagreg 316
                {
295 killagreg 317
                        if((PointList[MAX_LIST_LEN - i].Type == POINT_TYPE_WP) && (PointList[MAX_LIST_LEN - i].Position.Status != INVALID))
516 holgerb 318
                        {      
295 killagreg 319
                                WPIndex = MAX_LIST_LEN - i + 1;
225 killagreg 320
                                break;
321
                        }
322
                }
277 killagreg 323
                if(WPIndex) // found a WP within the list
324
                {
280 killagreg 325
                        NaviData.WaypointIndex = WPCount;
326
                        if(PointList[WPIndex-1].Heading < 0) POIIndex = (u8)(-PointList[WPIndex-1].Heading);
516 holgerb 327
                        else POIIndex = 0;     
277 killagreg 328
                }
278 killagreg 329
                else // list contains some points but no WP in the list
277 killagreg 330
                {
331
                        // search backward for a POI!
295 killagreg 332
                        for(i = 1; i <= MAX_LIST_LEN; i++)
277 killagreg 333
                        {
295 killagreg 334
                                if((PointList[MAX_LIST_LEN - i].Type == POINT_TYPE_POI) && (PointList[MAX_LIST_LEN - i].Position.Status != INVALID))
516 holgerb 335
                                {      
295 killagreg 336
                                        POIIndex = MAX_LIST_LEN - i + 1;
277 killagreg 337
                                        break;
338
                                }
339
                        }
516 holgerb 340
                        NaviData.WaypointIndex = 0;    
277 killagreg 341
                }
151 killagreg 342
        }
277 killagreg 343
        else // no point in the list
227 killagreg 344
        {
280 killagreg 345
                POIIndex = 0;
346
                NaviData.WaypointIndex = 0;
227 killagreg 347
        }
295 killagreg 348
        if(WPIndex) return(&(PointList[WPIndex-1]));
349
        else return(NULL);
151 killagreg 350
}
351
 
41 ingob 352
// returns a pointer to the next waypoint or NULL if the end of the list has been reached
280 killagreg 353
Point_t* PointList_WPNext(void)
41 ingob 354
{
227 killagreg 355
        u8 wp_found = 0;
280 killagreg 356
        if(WPActive == FALSE) return(NULL);
516 holgerb 357
 
295 killagreg 358
        if(WPIndex < MAX_LIST_LEN) // if there is a next entry in the list
41 ingob 359
        {
227 killagreg 360
                u8 i;
295 killagreg 361
                for(i = WPIndex; i < MAX_LIST_LEN; i++) // start search for next at next list entry
225 killagreg 362
                {
601 holgerb 363
                        if((PointList[i].Type == POINT_TYPE_WP) && (PointList[i].Position.Status != INVALID)) // jump over POIs and FS-Positions
225 killagreg 364
                        {
227 killagreg 365
                                wp_found = i+1;
225 killagreg 366
                                break;
367
                        }
601 holgerb 368
/*
369
                        else
370
                        if((PointList[i].Type == POINT_TYPE_FS) && (PointList[i].Position.Status != INVALID)) // jump over POIs
371
                        {
372
                        GPSPos_Copy(&(PointList[i].Position), &GPS_FailsafePosition);
373
                        }
374
*/
225 killagreg 375
                }
41 ingob 376
        }
227 killagreg 377
        if(wp_found)
378
        {
379
                WPIndex = wp_found; // update list position
280 killagreg 380
                NaviData.WaypointIndex++;
381
                if(PointList[WPIndex-1].Heading < 0) POIIndex = (u8)(-PointList[WPIndex-1].Heading);
227 killagreg 382
                else POIIndex = 0;
280 killagreg 383
                return(&(PointList[WPIndex-1]));        // return pointer to this waypoint
227 killagreg 384
        }
516 holgerb 385
        else
280 killagreg 386
        {  // no next wp found
516 holgerb 387
                NaviData.WaypointIndex = 0;    
277 killagreg 388
                POIIndex = 0;
389
                return(NULL);
390
        }
516 holgerb 391
}      
280 killagreg 392
 
393
void PointList_WPActive(u8 set)
394
{
295 killagreg 395
        if(set)
516 holgerb 396
        {      
295 killagreg 397
                WPActive = TRUE;
491 killagreg 398
                PointList_WPBegin(); // updates POI index
295 killagreg 399
        }
400
        else
401
        {
402
                WPActive = FALSE;
403
                POIIndex = 0;  // disable POI also
404
        }
280 killagreg 405
}
516 holgerb 406
 
280 killagreg 407
Point_t* PointList_GetPOI(void)
224 killagreg 408
{
516 holgerb 409
        return PointList_GetAt(POIIndex);      
224 killagreg 410
}
411
 
513 killagreg 412
 
490 killagreg 413
#define LINE_MAX 70
414
#define WP_FILE_VERSION_COMPATIBLE 3
513 killagreg 415
 
416
u8 PointList_Save(u8 * filename, u8* listname, u8 overwride)
490 killagreg 417
{
418
        File_t *fp;
495 killagreg 419
        s8 wpline[LINE_MAX];
420
        u8 retval = WPL_ERROR;
513 killagreg 421
 
490 killagreg 422
        UART1_PutString("\n\r Save WPL...");
423
 
424
        if(Fat16_IsValid())
425
        {       // check if wpl file is existing
513 killagreg 426
                if(fexist_(filename))
495 killagreg 427
                {       //file is existent
516 holgerb 428
                        if(!(overwride))
495 killagreg 429
                        {
430
                                UART1_PutString("Error: file exist!\r\n");
431
                                return(WPL_FILEEXIST);
516 holgerb 432
                        }      
495 killagreg 433
                }
513 killagreg 434
                fp = fopen_(filename, 'w');             // try to open the file
490 killagreg 435
                if(fp == NULL)
436
                {
437
                        UART1_PutString("ERROR: Creating waypoint file!\r\n");
438
                        return(retval);
439
                }
440
                // Create general section and key entries
441
                fputs_("[General]\r\n", fp);
516 holgerb 442
                sprintf(wpline, "Name=%s\r\n",  listname);
494 killagreg 443
                fputs_(wpline, fp);
516 holgerb 444
                sprintf(wpline, "FileVersion=%d\r\n", WP_FILE_VERSION_COMPATIBLE);
490 killagreg 445
                fputs_(wpline, fp);
446
                sprintf(wpline, "NumberOfWaypoints=%d\r\n", PointCount);
447
                fputs_(wpline, fp);
448
                // dump all points if existent
449
                if(PointCount)
450
                {
451
                        u8 i, u8_1;
452
                        s32 i32_1, i32_2;
504 holgerb 453
                        NewWPL_Name = 1;
490 killagreg 454
                        for (i = 0; i < PointCount; i++)
455
                        {
456
                                sprintf(wpline, "[Point%d]\r\n",PointList[i].Index);
457
                                fputs_(wpline, fp);
458
                                // write latitude in deg
459
                                if(PointList[i].Position.Latitude < 0) u8_1 = '-';
460
                                else u8_1 = '+';
461
                                i32_1 = abs(PointList[i].Position.Latitude)/10000000L;
462
                                i32_2 = abs(PointList[i].Position.Latitude)%10000000L;
463
                                sprintf(wpline, "Latitude=%c%ld.%07ld\r\n", u8_1, i32_1, i32_2);
464
                                fputs_(wpline, fp);
465
                                // write longitude in deg
466
                                if(PointList[i].Position.Longitude < 0) u8_1 = '-';
467
                                else u8_1 = '+';
468
                                i32_1 = abs(PointList[i].Position.Longitude)/10000000L;
469
                                i32_2 = abs(PointList[i].Position.Longitude)%10000000L;
470
                                sprintf(wpline, "Longitude=%c%ld.%07ld\r\n", u8_1, i32_1, i32_2);
471
                                fputs_(wpline, fp);
472
                                // write tolerace radius in m
473
                                sprintf(wpline, "Radius=%d\r\n", PointList[i].ToleranceRadius);
474
                                fputs_(wpline, fp);
475
                                // write altitude in m
476
                                if(PointList[i].Position.Altitude < 0) u8_1 = '-';
477
                                else u8_1 = '+';
478
                                if(PointList[i].Type == POINT_TYPE_POI)
479
                                {
480
                                        i32_1 = abs(PointList[i].Position.Altitude)/100L;  // cm --> m
481
                                        i32_2 = abs(PointList[i].Position.Altitude)%100L;
482
                                }
483
                                else
484
                                {
485
                                        i32_1 = abs(PointList[i].Position.Altitude)/10L; // dm --> m
486
                                        i32_2 = abs(PointList[i].Position.Altitude)%10L;
487
                                }
488
                                sprintf(wpline, "Altitude=%c%ld.%01ld\r\n",  u8_1, i32_1, i32_2);
489
                                fputs_(wpline, fp);
490
                                // write climb rate in 0.1 m/s
491
                                sprintf(wpline, "ClimbRate=%d\r\n", PointList[i].AltitudeRate);
492
                                fputs_(wpline, fp);
493
                                // write hold time in s
494
                                sprintf(wpline, "DelayTime=%d\r\n", PointList[i].HoldTime);
495
                                fputs_(wpline, fp);
496
                                // write event channel value
497
                                sprintf(wpline, "WP_Event_Channel_Value=%d\r\n", PointList[i].WP_EventChannelValue);
498
                                fputs_(wpline, fp);
499
                                // write heading in deg (0= nothing, neg. values index to poi)
500
                                sprintf(wpline, "Heading=%d\r\n", PointList[i].Heading);
501
                                fputs_(wpline, fp);
502
                                // write speed in 0.1 m/s
503
                                sprintf(wpline, "Speed=%d\r\n", PointList[i].Speed);
504
                                fputs_(wpline, fp);
505
                                // write cam angle in degree (255 -> POI-Automatic)
506
                                sprintf(wpline, "CAM-Nick=%d\r\n", PointList[i].CamAngle);
507
                                fputs_(wpline, fp);
508
                                // write point type
509
                                sprintf(wpline, "Type=%d\r\n", PointList[i].Type + 1);
516 holgerb 510
                                fputs_(wpline, fp);    
511
                                // write prefix 
490 killagreg 512
                                sprintf(wpline, "Prefix=%s\r\n", PointList[i].Name);
513
                                fputs_(wpline, fp);
514
                        } // EOF loop over all points
515
                } // EOF if(PointCount)
516
                if(EOF == fclose_(fp))
517
                {
518
                        UART1_PutString("failed!\r\n");
519
                }
520
                else
521
                {
522
                        UART1_PutString("ok\r\n");
495 killagreg 523
                        retval = WPL_OK;
516 holgerb 524
                }                                
490 killagreg 525
        } // EOF if(Fat16_IsValid())
516 holgerb 526
        else
497 killagreg 527
        {
528
                UART1_PutString("no file system found!\r\n");
529
                retval = WPL_NO_SDCARD_FOUND;
530
        }
490 killagreg 531
        return(retval);
532
}
533
 
519 holgerb 534
u8 PointList_Load(u8 * filename, u8* listname, u8 listnamelen, u8 use_preset_speed)
490 killagreg 535
{
536
        File_t *fp;
496 killagreg 537
        s8 wpline[LINE_MAX];
538
        u8 retval = WPL_ERROR;
516 holgerb 539
 
490 killagreg 540
        s8 *name, *value;
541
        u8 i;
542
 
543
        u8 IsGeneralSection = 0;
544
        u8 IsPointSection  = 0;
545
        u8 WPNumber = 0;
498 killagreg 546
 
547
        // clear point list first
548
        PointList_Clear();
517 holgerb 549
        HeadingOld = -1; // updates the direction if the new direction is the same like last time
490 killagreg 550
        UART1_PutString("\n\r Read ");
516 holgerb 551
        UART1_PutString(filename);
490 killagreg 552
        UART1_PutString("...");
553
 
554
        if(Fat16_IsValid())
555
        {       // check if wpl file is existing
513 killagreg 556
                fp = fopen_(filename, 'r');             // try to open the file
490 killagreg 557
                if(fp == NULL)
558
                {
559
                        UART1_PutString("ERROR: Reading waypoint file!\r\n");
496 killagreg 560
                        return(retval);
490 killagreg 561
                }
562
                // read all lines from file
563
                while(fgets_(wpline, LINE_MAX, fp) != 0)
564
                {
565
                        if ( // ignorelines starting with \r,\n,' ',';','#'
566
                                (wpline[0] != '\n') &&
567
                                (wpline[0] != '\r') &&
568
                                (wpline[0] != ' ' ) &&
569
                                (wpline[0] != ';' ) &&
570
                                (wpline[0] != '#' )
571
                                )
572
                        {
573
                                // check for section line found
574
                                if(wpline[0] == '[')
575
                                {
576
                                        // next section found
577
                                        IsGeneralSection = 0;
578
                                        IsPointSection = 0;
579
 
580
                                        name  = strtok(&wpline[1], "]");
581
                                        if(name != NULL)   // if section name
582
                                        {
583
                                                // check section type
584
                                                for(i=0; name[i]; i++) name[i] = toupper(name[i]); // convert to upper case
585
 
586
                                                if(strncmp(name, "POINT", 5) == 0)
587
                                                {
588
                                                        IsPointSection = (u8)atoi(&name[5]);
589
                                                        PointCount++;
590
                                                }
591
                                                else if(strcmp(name, "GENERAL") == 0)
592
                                                {
593
                                                        IsGeneralSection = 1;
594
                                                }
595
                                                else
596
                                                {
597
                                                        UART1_PutString("Unknown section: ");
598
                                                        UART1_PutString(name);
599
                                                        UART1_PutString("\r\n");
600
                                                }
601
                                        }
602
                                } // EOF section line
603
                                else
604
                                {       // look for key entrys of each sections
605
                                        name  = strtok(wpline, "="); // get name
606
                                        value = strtok(NULL, "="); // get value
607
                                        if ((name != NULL) && (value != NULL))
608
                                        {
609
                                                for(i=0; name[i]; i++) name[i] = toupper(name[i]); // convert to upper case
610
                                                if(IsPointSection &&  (IsPointSection <= WPNumber))
611
                                                {
612
                                                        if(strcmp(name, "LATITUDE") == 0)
613
                                                        {
614
                                                                PointList[IsPointSection-1].Position.Latitude = (s32)(atof(value) * 1E7);
615
                                                        }
616
                                                        else if(strcmp(name, "LONGITUDE") == 0)
617
                                                        {
618
                                                                PointList[IsPointSection-1].Position.Longitude = (s32)(atof(value) * 1E7);
619
                                                        }
620
                                                        else if(strcmp(name, "RADIUS") == 0)
621
                                                        {
622
                                                                PointList[IsPointSection-1].ToleranceRadius = (u8)atoi(value);
623
                                                        }
624
                                                        else if(strcmp(name, "ALTITUDE") == 0)
625
                                                        {
626
                                                                PointList[IsPointSection-1].Position.Altitude = (s32)(atof(value) * 100.0);      // in cm
627
                                                                PointList[IsPointSection-1].Position.Status = NEWDATA;
628
                                                        }
629
                                                        else if(strcmp(name, "CLIMBRATE") == 0)
630
                                                        {
631
                                                                PointList[IsPointSection-1].AltitudeRate = (u8)atoi(value);
632
                                                        }
633
                                                        else if(strcmp(name, "DELAYTIME") == 0)
634
                                                        {
635
                                                                PointList[IsPointSection-1].HoldTime = (u8)atoi(value);
636
                                                        }
637
                                                        else if(strcmp(name, "WP_EVENT_CHANNEL_VALUE") == 0)
638
                                                        {
639
                                                                PointList[IsPointSection-1].WP_EventChannelValue = (u8)atoi(value);
640
                                                        }
641
                                                        else if(strcmp(name, "HEADING") == 0)
642
                                                        {
643
                                                                PointList[IsPointSection-1].Heading = (s16)atoi(value);
644
                                                        }
645
                                                        else if(strcmp(name, "SPEED") == 0)
646
                                                        {
519 holgerb 647
                                                                if(use_preset_speed) PointList[IsPointSection-1].Speed = use_preset_speed;
648
                                                                else PointList[IsPointSection-1].Speed = (u8)atoi(value);
490 killagreg 649
                                                        }
650
                                                        else if(strcmp(name, "CAM-NICK") == 0)
651
                                                        {
652
                                                                PointList[IsPointSection-1].CamAngle = (u8)atoi(value);
653
                                                        }
654
                                                        else if(strcmp(name, "TYPE") == 0)
655
                                                        {
656
                                                                PointList[IsPointSection-1].Type = (u8)atoi(value);
657
                                                                if(PointList[IsPointSection-1].Type > 0) PointList[IsPointSection-1].Type--;  // index shift
658
                                                                else PointList[IsPointSection-1].Type = POINT_TYPE_INVALID;
516 holgerb 659
 
490 killagreg 660
                                                                switch(PointList[IsPointSection-1].Type)
661
                                                                {
662
                                                                        case POINT_TYPE_WP:
663
                                                                                // this works only if altitude key is set before point type key in WPL file     !!
516 holgerb 664
                                                                                PointList[IsPointSection-1].Position.Altitude /= 10; // dm only for WPs 
490 killagreg 665
                                                                                WPCount++;
666
                                                                                break;
601 holgerb 667
                                                                        case POINT_TYPE_FS:
668
                                                                                FsPointCnt++;
669
                                                                                break;
490 killagreg 670
                                                                        case POINT_TYPE_POI:
671
                                                                                POICount++;
672
                                                                                break;
673
                                                                }
674
                                                        }
675
                                                        else if(strcmp(name, "PREFIX") == 0)
676
                                                        {
677
                                                                strncpy(PointList[IsPointSection-1].Name, value, 4);
678
                                                                PointList[IsPointSection-1].Name[3] = 0; // Terminate string
679
                                                        }
680
                                                        else
681
                                                        {
682
                                                                UART1_PutString("Unknown key: ");
683
                                                                UART1_PutString(name);
684
                                                                UART1_PutString("\r\n");
516 holgerb 685
                                                        }      
490 killagreg 686
                                                } // EOF point section
687
                                                else if(IsGeneralSection)
688
                                                {
689
                                                        if(strcmp(name, "NUMBEROFWAYPOINTS") == 0)
516 holgerb 690
                                                        {      
490 killagreg 691
                                                                WPNumber = (u8)atoi(value);
692
                                                                if(!WPNumber) // no waypoints in file
693
                                                                {
516 holgerb 694
                                                                        return(WPL_NO_WAYPOINTS); // we are done here   
490 killagreg 695
                                                                }
696
                                                                else if(WPNumber > MAX_LIST_LEN) // number o points larger than ram list
697
                                                                {
514 holgerb 698
                                                                        UART1_PutString("To many points!");
497 killagreg 699
                                                                        return(WPL_ERROR);
516 holgerb 700
                                                                }
490 killagreg 701
                                                        }
702
                                                        else if (strcmp(name, "FILEVERSION") == 0)
703
                                                        {
704
                                                                if((u8)atoi(value) != WP_FILE_VERSION_COMPATIBLE)
705
                                                                {
496 killagreg 706
                                                                        PointList_Clear();
490 killagreg 707
                                                                        UART1_PutString("Bad file version!\r\n");
516 holgerb 708
                                                                        return(WPL_ERROR);     
490 killagreg 709
                                                                }
710
                                                        }
495 killagreg 711
                                                        else if (strcmp(name, "NAME") == 0)
712
                                                        {
513 killagreg 713
                                                                if(listname)
498 killagreg 714
                                                                {
513 killagreg 715
                                                                        u8 len = strlen(value);
716
                                                                        if(len)
717
                                                                        {
718
                                                                                if(value[len-1] == '\r')
719
                                                                                {
720
                                                                                        value[len-1] = 0;
540 holgerb 721
//                                                                                      len--;
513 killagreg 722
                                                                                }
723
                                                                        }
724
                                                                        if(len > listnamelen) len = listnamelen;
725
                                                                        if(len)
726
                                                                        {
727
                                                                                value[len-1] = 0; // terminate string
728
                                                                                if(listname) memcpy(listname, value, len);
729
                                                                        }
730
                                                                        NewWPL_Name = 1;
498 killagreg 731
                                                                }
495 killagreg 732
                                                        }
490 killagreg 733
                                                        else
734
                                                        {
735
                                                                UART1_PutString("Unknown key: ");
736
                                                                UART1_PutString(name);
737
                                                                UART1_PutString("\r\n");
738
                                                        }
739
                                                } // EOF general section
740
                                        } // EOF valid key entry
741
                                } // EOF key entry line
742
                        } // valid line
743
                } // EOF loop over all lines
744
                fclose_(fp);
745
                NaviData.WaypointNumber = WPCount;
516 holgerb 746
                retval = WPL_OK;        
747
                UART1_PutString("ok\r\n");                               
490 killagreg 748
        } // EOF if(Fat16_IsValid())
516 holgerb 749
        else
497 killagreg 750
        {
751
                UART1_PutString("no file system found!\r\n");
752
                retval = WPL_NO_SDCARD_FOUND;
516 holgerb 753
        }      
490 killagreg 754
        return(retval);
491 killagreg 755
}
490 killagreg 756
 
513 killagreg 757
// load actual point list from SD card
758
u8 PointList_ReadFromFile(WPL_Store_t * pWPL_Store)
759
{
516 holgerb 760
        u8 filename[30];       
513 killagreg 761
 
762
        pWPL_Store->Name[0] = 0; // clear current list name
763
 
764
        // user absolute path, i.e. leading /
765
        if(pWPL_Store->Index == 0) // index 0 looks for a default WPL file in the root
766
        {
516 holgerb 767
                sprintf(filename, "/default.wpl");     
513 killagreg 768
        }
769
        else
770
        {
771
                sprintf(filename, "/WPL/list_%03d.wpl", pWPL_Store->Index);
772
        }
519 holgerb 773
        return PointList_Load(filename, pWPL_Store->Name, sizeof(pWPL_Store->Name),0);
513 killagreg 774
}
775
 
776
// save actual point list to SD card
777
u8 PointList_WriteToFile(WPL_Store_t * pWPL_Store)
778
{
779
        u8 filename[30];
516 holgerb 780
 
781
 
513 killagreg 782
        if(PointCount == 0) return(WPL_NO_WAYPOINTS);
516 holgerb 783
        // user absolute path, i.e. leading /   
513 killagreg 784
        if(pWPL_Store->Index == 0)
785
        {
786
                sprintf(filename, "/default.wpl");
787
        }
788
        else
789
        {
790
                sprintf(filename, "/WPL/list_%03d.wpl", pWPL_Store->Index);
791
        }
792
        return PointList_Save(filename, pWPL_Store->Name, pWPL_Store->OverwriteFile);
793
}
794
 
795
 
516 holgerb 796
// save actual gps positiin and heading to file
513 killagreg 797
u8 PointList_SaveSinglePoint(WPL_Store_t * pWPL_Store)
798
{
799
        u8 retval = WPL_ERROR;
800
        u8 filename[30];
801
        Point_t WP;
802
 
514 holgerb 803
        UART1_PutString("\n\r write single point\n\r");
516 holgerb 804
        if(GPSData.Position.Status == INVALID)
805
         {
514 holgerb 806
                UART1_PutString("ERROR: No GPS - Fix\n\r");
807
                return(retval);
516 holgerb 808
         }
513 killagreg 809
 
810
        // clear current point list
811
        PointList_Clear();
812
        // prepare WP at current position
519 holgerb 813
 
814
        if(NCFlags & NC_FLAG_FREE || NaviData.TargetPositionDeviation.Distance > 7*10)
815
        {  // take actual position
816
                GPSPos_Copy(&GPSData.Position, &(WP.Position));
817
        }
818
        else
819
        {  // take last target position
820
                GPSPos_Copy(&NaviData.TargetPosition, &(WP.Position));
821
        }
822
 
513 killagreg 823
        GPSPos_Copy(&GPSData.Position, &(WP.Position));
824
        // set heading
545 holgerb 825
        WP.Heading = (CompassSetpointCorrected/10 + Parameter.CamOrientation * 15) % 360;
513 killagreg 826
        if(WP.Heading == 0) WP.Heading = 360;
519 holgerb 827
        WP.ToleranceRadius = 120; // 12m 
828
        WP.HoldTime  = 2;
513 killagreg 829
        WP.Index  = 1;
830
        WP.Type = POINT_TYPE_WP;
831
        WP.WP_EventChannelValue = 0;
576 holgerb 832
        if(FC.StatusFlags & FC_STATUS_FLY && ((FromFC_VarioCharacter != ' ') || (SimulationFlags & SIMULATION_ACTIVE))) // only in flight and if the Altitude control is enabled
516 holgerb 833
         {
519 holgerb 834
          WP.AltitudeRate = 255;  // Auto
835
          WP.Position.Altitude = NaviData.SetpointAltitude / 2;
516 holgerb 836
         }
837
        else
514 holgerb 838
        {
516 holgerb 839
         WP.AltitudeRate = 0;
840
     WP.Position.Altitude = 0;
514 holgerb 841
        }
519 holgerb 842
        WP.Speed = 50; // beim Laden wird der Wert nochmal neu gesetzt
513 killagreg 843
        WP.CamAngle = 0;
521 holgerb 844
        WP.Name[0] = 'S';
845
        WP.Name[1] = 'P';
846
        WP.Name[2] = 'T';
847
        WP.Name[3] = 0;
513 killagreg 848
        // add this point to wp list
849
        PointList_SetAt(&WP);
514 holgerb 850
 
851
        sprintf(filename, "/POINT/POINT%03d.wpl", pWPL_Store->Index);
516 holgerb 852
        UART1_PutString(filename);
514 holgerb 853
        sprintf(pWPL_Store->Name, "POINT%03d ", pWPL_Store->Index);
513 killagreg 854
        retval = PointList_Save(filename, pWPL_Store->Name, 1);
514 holgerb 855
 
856
        // clear current point list
519 holgerb 857
        if((NC_GPS_ModeCharacter != 'w') && (NC_GPS_ModeCharacter != 'W')) PointList_Clear();
549 holgerb 858
        else // es ist gearde ein Wegpunktflug aktiv -> updaten
519 holgerb 859
        {
860
                if(FC.StatusFlags & FC_STATUS_FLY) PointList_WPActive(TRUE);
861
                GPS_pWaypoint = PointList_WPBegin(); // updates POI index
862
        }
863
 
513 killagreg 864
        return(retval);
865
}
866
// load target gps posititon and heading from file
867
u8 PointList_LoadSinglePoint(WPL_Store_t * pWPL_Store)
868
{
519 holgerb 869
        u8 filename[30], result = 0;
516 holgerb 870
 
514 holgerb 871
        sprintf(filename, "/POINT/POINT%03d.wpl", pWPL_Store->Index);
513 killagreg 872
        pWPL_Store->Name[0] = 0; // clear current list name
519 holgerb 873
        result = PointList_Load(filename, pWPL_Store->Name, sizeof(pWPL_Store->Name),Parameter.SingleWpSpeed);
874
        if(result) UART1_Request_ReadPoint = 1; // Sends Point 1 to the PC
875
        return(result);
513 killagreg 876
}
877
 
878
 
516 holgerb 879
 
504 holgerb 880
void ClearWLP_Name(void)
881
{
513 killagreg 882
        u8 i;
883
        for(i=0; i<sizeof(WPL_Store.Name);i++) WPL_Store.Name[i] = 0;
884
        NewWPL_Name = 1;
504 holgerb 885
}
513 killagreg 886
 
491 killagreg 887
// move actual point list to ref pos., the point in the list marked by index gets the RefPos afterwards
503 holgerb 888
u8 PointList_Move(u8 RefIndex, GPS_Pos_t* pRefPos, u16 RotationAngle)
491 killagreg 889
{
890
        u8 retval = 0;
513 killagreg 891
        s32 altitude;
892
        GPS_Pos_t OldRefPos;
491 killagreg 893
        GPS_Pos_Deviation_t RefDeviation;
516 holgerb 894
 
491 killagreg 895
        // check inputs for plausibility;
516 holgerb 896
        if((RefIndex == 0) || (RefIndex > PointCount)) return(retval); 
491 killagreg 897
        if(pRefPos == NULL) return(retval);
898
        if(pRefPos->Status == INVALID) return(retval);
899
 
513 killagreg 900
        if(GPSPos_Copy(&(PointList[RefIndex-1].Position), &OldRefPos)) // backup old reference position
491 killagreg 901
        {
902
                u8 i;
513 killagreg 903
                // iterate the position list
491 killagreg 904
                for(i = 0; i < PointCount; i++)
905
                {
906
                        retval = 0;
513 killagreg 907
                        // backup altitude of this point
908
                        altitude = PointList[i].Position.Altitude;
909
                        // calculate deviation form old ref, i.e the north and east shift of each point in the list from the reference position
910
                        if(!GPSPos_Deviation(&(PointList[i].Position), &OldRefPos, &RefDeviation)) break;
491 killagreg 911
                        // copy of the new reference position into this list place
912
                        if(!GPSPos_Copy(pRefPos, &(PointList[i].Position))) break;
516 holgerb 913
                        // restore former altitude 
513 killagreg 914
                        PointList[i].Position.Altitude = altitude;
491 killagreg 915
                        // move new reference according to the deviation of the old reference
513 killagreg 916
                        if(RotationAngle > 0)
917
                        {
918
                                retval = GPSPos_ShiftGeodetic(&(PointList[i].Position), (RefDeviation.Bearing + 180 + RotationAngle)%360, RefDeviation.Distance );
919
                                // Now rotate the heading positions if they are fixed angles
920
                                if(PointList[i].Heading >= 0 && PointList[i].Heading <= 360) PointList[i].Heading = (PointList[i].Heading + RotationAngle) % 360;
921
                        }
922
                        else // no rotation
923
                        {
924
                                // move new reference according to the deviation of the old reference
925
                                retval = GPSPos_ShiftCartesian(&(PointList[i].Position), RefDeviation.North, RefDeviation.East);
926
                        }
516 holgerb 927
                        if(!retval) break;             
491 killagreg 928
                }
929
        } // else ref pos old not copied!
930
        if(!retval) PointList_Clear();
931
        return(retval);
932
}
933
 
516 holgerb 934
 
491 killagreg 935