Subversion Repositories NaviCtrl

Rev

Rev 737 | Rev 785 | 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
 
767 holgerb 422
        UART1_PutString("\r\n Save WPL...");
490 killagreg 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);
642 holgerb 514
                                sprintf(wpline, "AutoTrigger=%d\r\n", PointList[i].AutoPhotoDistance);
515
                                fputs_(wpline, fp);    
490 killagreg 516
                        } // EOF loop over all points
517
                } // EOF if(PointCount)
518
                if(EOF == fclose_(fp))
519
                {
520
                        UART1_PutString("failed!\r\n");
521
                }
522
                else
523
                {
524
                        UART1_PutString("ok\r\n");
495 killagreg 525
                        retval = WPL_OK;
516 holgerb 526
                }                                
490 killagreg 527
        } // EOF if(Fat16_IsValid())
516 holgerb 528
        else
497 killagreg 529
        {
530
                UART1_PutString("no file system found!\r\n");
531
                retval = WPL_NO_SDCARD_FOUND;
532
        }
490 killagreg 533
        return(retval);
534
}
535
 
519 holgerb 536
u8 PointList_Load(u8 * filename, u8* listname, u8 listnamelen, u8 use_preset_speed)
490 killagreg 537
{
538
        File_t *fp;
496 killagreg 539
        s8 wpline[LINE_MAX];
540
        u8 retval = WPL_ERROR;
516 holgerb 541
 
490 killagreg 542
        s8 *name, *value;
543
        u8 i;
544
 
545
        u8 IsGeneralSection = 0;
546
        u8 IsPointSection  = 0;
547
        u8 WPNumber = 0;
498 killagreg 548
 
549
        // clear point list first
550
        PointList_Clear();
517 holgerb 551
        HeadingOld = -1; // updates the direction if the new direction is the same like last time
767 holgerb 552
        UART1_PutString("\r\n Read ");
516 holgerb 553
        UART1_PutString(filename);
490 killagreg 554
        UART1_PutString("...");
555
 
556
        if(Fat16_IsValid())
557
        {       // check if wpl file is existing
513 killagreg 558
                fp = fopen_(filename, 'r');             // try to open the file
490 killagreg 559
                if(fp == NULL)
560
                {
561
                        UART1_PutString("ERROR: Reading waypoint file!\r\n");
496 killagreg 562
                        return(retval);
490 killagreg 563
                }
564
                // read all lines from file
565
                while(fgets_(wpline, LINE_MAX, fp) != 0)
566
                {
567
                        if ( // ignorelines starting with \r,\n,' ',';','#'
568
                                (wpline[0] != '\n') &&
569
                                (wpline[0] != '\r') &&
570
                                (wpline[0] != ' ' ) &&
571
                                (wpline[0] != ';' ) &&
572
                                (wpline[0] != '#' )
573
                                )
574
                        {
575
                                // check for section line found
576
                                if(wpline[0] == '[')
577
                                {
578
                                        // next section found
579
                                        IsGeneralSection = 0;
580
                                        IsPointSection = 0;
581
 
582
                                        name  = strtok(&wpline[1], "]");
583
                                        if(name != NULL)   // if section name
584
                                        {
585
                                                // check section type
586
                                                for(i=0; name[i]; i++) name[i] = toupper(name[i]); // convert to upper case
587
 
588
                                                if(strncmp(name, "POINT", 5) == 0)
589
                                                {
590
                                                        IsPointSection = (u8)atoi(&name[5]);
591
                                                        PointCount++;
592
                                                }
593
                                                else if(strcmp(name, "GENERAL") == 0)
594
                                                {
595
                                                        IsGeneralSection = 1;
596
                                                }
597
                                                else
598
                                                {
599
                                                        UART1_PutString("Unknown section: ");
600
                                                        UART1_PutString(name);
601
                                                        UART1_PutString("\r\n");
602
                                                }
603
                                        }
604
                                } // EOF section line
605
                                else
606
                                {       // look for key entrys of each sections
607
                                        name  = strtok(wpline, "="); // get name
608
                                        value = strtok(NULL, "="); // get value
609
                                        if ((name != NULL) && (value != NULL))
610
                                        {
611
                                                for(i=0; name[i]; i++) name[i] = toupper(name[i]); // convert to upper case
612
                                                if(IsPointSection &&  (IsPointSection <= WPNumber))
613
                                                {
614
                                                        if(strcmp(name, "LATITUDE") == 0)
615
                                                        {
616
                                                                PointList[IsPointSection-1].Position.Latitude = (s32)(atof(value) * 1E7);
617
                                                        }
618
                                                        else if(strcmp(name, "LONGITUDE") == 0)
619
                                                        {
620
                                                                PointList[IsPointSection-1].Position.Longitude = (s32)(atof(value) * 1E7);
621
                                                        }
622
                                                        else if(strcmp(name, "RADIUS") == 0)
623
                                                        {
624
                                                                PointList[IsPointSection-1].ToleranceRadius = (u8)atoi(value);
625
                                                        }
626
                                                        else if(strcmp(name, "ALTITUDE") == 0)
627
                                                        {
628
                                                                PointList[IsPointSection-1].Position.Altitude = (s32)(atof(value) * 100.0);      // in cm
629
                                                                PointList[IsPointSection-1].Position.Status = NEWDATA;
630
                                                        }
631
                                                        else if(strcmp(name, "CLIMBRATE") == 0)
632
                                                        {
633
                                                                PointList[IsPointSection-1].AltitudeRate = (u8)atoi(value);
634
                                                        }
635
                                                        else if(strcmp(name, "DELAYTIME") == 0)
636
                                                        {
637
                                                                PointList[IsPointSection-1].HoldTime = (u8)atoi(value);
638
                                                        }
639
                                                        else if(strcmp(name, "WP_EVENT_CHANNEL_VALUE") == 0)
640
                                                        {
641
                                                                PointList[IsPointSection-1].WP_EventChannelValue = (u8)atoi(value);
642
                                                        }
643
                                                        else if(strcmp(name, "HEADING") == 0)
644
                                                        {
645
                                                                PointList[IsPointSection-1].Heading = (s16)atoi(value);
646
                                                        }
647
                                                        else if(strcmp(name, "SPEED") == 0)
648
                                                        {
519 holgerb 649
                                                                if(use_preset_speed) PointList[IsPointSection-1].Speed = use_preset_speed;
650
                                                                else PointList[IsPointSection-1].Speed = (u8)atoi(value);
490 killagreg 651
                                                        }
652
                                                        else if(strcmp(name, "CAM-NICK") == 0)
653
                                                        {
654
                                                                PointList[IsPointSection-1].CamAngle = (u8)atoi(value);
655
                                                        }
656
                                                        else if(strcmp(name, "TYPE") == 0)
657
                                                        {
658
                                                                PointList[IsPointSection-1].Type = (u8)atoi(value);
659
                                                                if(PointList[IsPointSection-1].Type > 0) PointList[IsPointSection-1].Type--;  // index shift
660
                                                                else PointList[IsPointSection-1].Type = POINT_TYPE_INVALID;
516 holgerb 661
 
490 killagreg 662
                                                                switch(PointList[IsPointSection-1].Type)
663
                                                                {
664
                                                                        case POINT_TYPE_WP:
665
                                                                                // this works only if altitude key is set before point type key in WPL file     !!
516 holgerb 666
                                                                                PointList[IsPointSection-1].Position.Altitude /= 10; // dm only for WPs 
490 killagreg 667
                                                                                WPCount++;
668
                                                                                break;
601 holgerb 669
                                                                        case POINT_TYPE_FS:
670
                                                                                FsPointCnt++;
671
                                                                                break;
490 killagreg 672
                                                                        case POINT_TYPE_POI:
673
                                                                                POICount++;
674
                                                                                break;
675
                                                                }
676
                                                        }
677
                                                        else if(strcmp(name, "PREFIX") == 0)
678
                                                        {
679
                                                                strncpy(PointList[IsPointSection-1].Name, value, 4);
680
                                                                PointList[IsPointSection-1].Name[3] = 0; // Terminate string
681
                                                        }
642 holgerb 682
                                                        else if(strcmp(name, "AUTOTRIGGER") == 0)
683
                                                        {
684
                                                                PointList[IsPointSection-1].AutoPhotoDistance = (u8)atoi(value);
685
                                                        }
490 killagreg 686
                                                        else
687
                                                        {
688
                                                                UART1_PutString("Unknown key: ");
689
                                                                UART1_PutString(name);
690
                                                                UART1_PutString("\r\n");
516 holgerb 691
                                                        }      
490 killagreg 692
                                                } // EOF point section
693
                                                else if(IsGeneralSection)
694
                                                {
695
                                                        if(strcmp(name, "NUMBEROFWAYPOINTS") == 0)
516 holgerb 696
                                                        {      
490 killagreg 697
                                                                WPNumber = (u8)atoi(value);
698
                                                                if(!WPNumber) // no waypoints in file
699
                                                                {
516 holgerb 700
                                                                        return(WPL_NO_WAYPOINTS); // we are done here   
490 killagreg 701
                                                                }
702
                                                                else if(WPNumber > MAX_LIST_LEN) // number o points larger than ram list
703
                                                                {
514 holgerb 704
                                                                        UART1_PutString("To many points!");
497 killagreg 705
                                                                        return(WPL_ERROR);
516 holgerb 706
                                                                }
490 killagreg 707
                                                        }
708
                                                        else if (strcmp(name, "FILEVERSION") == 0)
709
                                                        {
710
                                                                if((u8)atoi(value) != WP_FILE_VERSION_COMPATIBLE)
711
                                                                {
496 killagreg 712
                                                                        PointList_Clear();
490 killagreg 713
                                                                        UART1_PutString("Bad file version!\r\n");
516 holgerb 714
                                                                        return(WPL_ERROR);     
490 killagreg 715
                                                                }
716
                                                        }
495 killagreg 717
                                                        else if (strcmp(name, "NAME") == 0)
718
                                                        {
513 killagreg 719
                                                                if(listname)
498 killagreg 720
                                                                {
513 killagreg 721
                                                                        u8 len = strlen(value);
722
                                                                        if(len)
723
                                                                        {
724
                                                                                if(value[len-1] == '\r')
725
                                                                                {
726
                                                                                        value[len-1] = 0;
540 holgerb 727
//                                                                                      len--;
513 killagreg 728
                                                                                }
729
                                                                        }
730
                                                                        if(len > listnamelen) len = listnamelen;
731
                                                                        if(len)
732
                                                                        {
733
                                                                                value[len-1] = 0; // terminate string
734
                                                                                if(listname) memcpy(listname, value, len);
735
                                                                        }
736
                                                                        NewWPL_Name = 1;
498 killagreg 737
                                                                }
495 killagreg 738
                                                        }
490 killagreg 739
                                                        else
740
                                                        {
741
                                                                UART1_PutString("Unknown key: ");
742
                                                                UART1_PutString(name);
743
                                                                UART1_PutString("\r\n");
744
                                                        }
745
                                                } // EOF general section
746
                                        } // EOF valid key entry
747
                                } // EOF key entry line
748
                        } // valid line
749
                } // EOF loop over all lines
750
                fclose_(fp);
751
                NaviData.WaypointNumber = WPCount;
516 holgerb 752
                retval = WPL_OK;        
753
                UART1_PutString("ok\r\n");                               
490 killagreg 754
        } // EOF if(Fat16_IsValid())
516 holgerb 755
        else
497 killagreg 756
        {
757
                UART1_PutString("no file system found!\r\n");
758
                retval = WPL_NO_SDCARD_FOUND;
516 holgerb 759
        }      
490 killagreg 760
        return(retval);
491 killagreg 761
}
490 killagreg 762
 
513 killagreg 763
// load actual point list from SD card
764
u8 PointList_ReadFromFile(WPL_Store_t * pWPL_Store)
765
{
516 holgerb 766
        u8 filename[30];       
513 killagreg 767
 
768
        pWPL_Store->Name[0] = 0; // clear current list name
769
 
770
        // user absolute path, i.e. leading /
771
        if(pWPL_Store->Index == 0) // index 0 looks for a default WPL file in the root
772
        {
516 holgerb 773
                sprintf(filename, "/default.wpl");     
513 killagreg 774
        }
775
        else
776
        {
777
                sprintf(filename, "/WPL/list_%03d.wpl", pWPL_Store->Index);
778
        }
519 holgerb 779
        return PointList_Load(filename, pWPL_Store->Name, sizeof(pWPL_Store->Name),0);
513 killagreg 780
}
781
 
782
// save actual point list to SD card
783
u8 PointList_WriteToFile(WPL_Store_t * pWPL_Store)
784
{
785
        u8 filename[30];
516 holgerb 786
 
787
 
513 killagreg 788
        if(PointCount == 0) return(WPL_NO_WAYPOINTS);
516 holgerb 789
        // user absolute path, i.e. leading /   
513 killagreg 790
        if(pWPL_Store->Index == 0)
791
        {
792
                sprintf(filename, "/default.wpl");
793
        }
794
        else
795
        {
796
                sprintf(filename, "/WPL/list_%03d.wpl", pWPL_Store->Index);
797
        }
798
        return PointList_Save(filename, pWPL_Store->Name, pWPL_Store->OverwriteFile);
799
}
800
 
801
 
516 holgerb 802
// save actual gps positiin and heading to file
513 killagreg 803
u8 PointList_SaveSinglePoint(WPL_Store_t * pWPL_Store)
804
{
805
        u8 retval = WPL_ERROR;
806
        u8 filename[30];
807
        Point_t WP;
808
 
767 holgerb 809
        UART1_PutString("\r\n write single point\r\n");
516 holgerb 810
        if(GPSData.Position.Status == INVALID)
811
         {
767 holgerb 812
                UART1_PutString("ERROR: No GPS - Fix\r\n");
514 holgerb 813
                return(retval);
516 holgerb 814
         }
513 killagreg 815
 
816
        // clear current point list
817
        PointList_Clear();
818
        // prepare WP at current position
519 holgerb 819
 
737 holgerb 820
        if(NCFlags & NC_FLAG_FREE || NaviData.TargetPositionDeviation.Distance_dm > 7*10)
519 holgerb 821
        {  // take actual position
822
                GPSPos_Copy(&GPSData.Position, &(WP.Position));
823
        }
824
        else
825
        {  // take last target position
826
                GPSPos_Copy(&NaviData.TargetPosition, &(WP.Position));
827
        }
828
 
513 killagreg 829
        GPSPos_Copy(&GPSData.Position, &(WP.Position));
830
        // set heading
545 holgerb 831
        WP.Heading = (CompassSetpointCorrected/10 + Parameter.CamOrientation * 15) % 360;
513 killagreg 832
        if(WP.Heading == 0) WP.Heading = 360;
519 holgerb 833
        WP.ToleranceRadius = 120; // 12m 
834
        WP.HoldTime  = 2;
513 killagreg 835
        WP.Index  = 1;
836
        WP.Type = POINT_TYPE_WP;
695 holgerb 837
        WP.WP_EventChannelValue = 100;
576 holgerb 838
        if(FC.StatusFlags & FC_STATUS_FLY && ((FromFC_VarioCharacter != ' ') || (SimulationFlags & SIMULATION_ACTIVE))) // only in flight and if the Altitude control is enabled
516 holgerb 839
         {
519 holgerb 840
          WP.AltitudeRate = 255;  // Auto
841
          WP.Position.Altitude = NaviData.SetpointAltitude / 2;
516 holgerb 842
         }
843
        else
514 holgerb 844
        {
516 holgerb 845
         WP.AltitudeRate = 0;
846
     WP.Position.Altitude = 0;
514 holgerb 847
        }
519 holgerb 848
        WP.Speed = 50; // beim Laden wird der Wert nochmal neu gesetzt
513 killagreg 849
        WP.CamAngle = 0;
521 holgerb 850
        WP.Name[0] = 'S';
851
        WP.Name[1] = 'P';
852
        WP.Name[2] = 'T';
853
        WP.Name[3] = 0;
513 killagreg 854
        // add this point to wp list
855
        PointList_SetAt(&WP);
514 holgerb 856
 
857
        sprintf(filename, "/POINT/POINT%03d.wpl", pWPL_Store->Index);
516 holgerb 858
        UART1_PutString(filename);
514 holgerb 859
        sprintf(pWPL_Store->Name, "POINT%03d ", pWPL_Store->Index);
513 killagreg 860
        retval = PointList_Save(filename, pWPL_Store->Name, 1);
514 holgerb 861
 
862
        // clear current point list
519 holgerb 863
        if((NC_GPS_ModeCharacter != 'w') && (NC_GPS_ModeCharacter != 'W')) PointList_Clear();
549 holgerb 864
        else // es ist gearde ein Wegpunktflug aktiv -> updaten
519 holgerb 865
        {
866
                if(FC.StatusFlags & FC_STATUS_FLY) PointList_WPActive(TRUE);
867
                GPS_pWaypoint = PointList_WPBegin(); // updates POI index
868
        }
869
 
513 killagreg 870
        return(retval);
871
}
872
// load target gps posititon and heading from file
873
u8 PointList_LoadSinglePoint(WPL_Store_t * pWPL_Store)
874
{
519 holgerb 875
        u8 filename[30], result = 0;
516 holgerb 876
 
514 holgerb 877
        sprintf(filename, "/POINT/POINT%03d.wpl", pWPL_Store->Index);
513 killagreg 878
        pWPL_Store->Name[0] = 0; // clear current list name
519 holgerb 879
        result = PointList_Load(filename, pWPL_Store->Name, sizeof(pWPL_Store->Name),Parameter.SingleWpSpeed);
880
        if(result) UART1_Request_ReadPoint = 1; // Sends Point 1 to the PC
881
        return(result);
513 killagreg 882
}
883
 
884
 
516 holgerb 885
 
504 holgerb 886
void ClearWLP_Name(void)
887
{
513 killagreg 888
        u8 i;
889
        for(i=0; i<sizeof(WPL_Store.Name);i++) WPL_Store.Name[i] = 0;
890
        NewWPL_Name = 1;
504 holgerb 891
}
513 killagreg 892
 
491 killagreg 893
// move actual point list to ref pos., the point in the list marked by index gets the RefPos afterwards
503 holgerb 894
u8 PointList_Move(u8 RefIndex, GPS_Pos_t* pRefPos, u16 RotationAngle)
491 killagreg 895
{
896
        u8 retval = 0;
513 killagreg 897
        s32 altitude;
898
        GPS_Pos_t OldRefPos;
491 killagreg 899
        GPS_Pos_Deviation_t RefDeviation;
516 holgerb 900
 
491 killagreg 901
        // check inputs for plausibility;
516 holgerb 902
        if((RefIndex == 0) || (RefIndex > PointCount)) return(retval); 
491 killagreg 903
        if(pRefPos == NULL) return(retval);
904
        if(pRefPos->Status == INVALID) return(retval);
905
 
513 killagreg 906
        if(GPSPos_Copy(&(PointList[RefIndex-1].Position), &OldRefPos)) // backup old reference position
491 killagreg 907
        {
908
                u8 i;
513 killagreg 909
                // iterate the position list
491 killagreg 910
                for(i = 0; i < PointCount; i++)
911
                {
912
                        retval = 0;
513 killagreg 913
                        // backup altitude of this point
914
                        altitude = PointList[i].Position.Altitude;
915
                        // calculate deviation form old ref, i.e the north and east shift of each point in the list from the reference position
916
                        if(!GPSPos_Deviation(&(PointList[i].Position), &OldRefPos, &RefDeviation)) break;
491 killagreg 917
                        // copy of the new reference position into this list place
918
                        if(!GPSPos_Copy(pRefPos, &(PointList[i].Position))) break;
516 holgerb 919
                        // restore former altitude 
513 killagreg 920
                        PointList[i].Position.Altitude = altitude;
491 killagreg 921
                        // move new reference according to the deviation of the old reference
513 killagreg 922
                        if(RotationAngle > 0)
923
                        {
737 holgerb 924
                                retval = GPSPos_ShiftGeodetic(&(PointList[i].Position), (RefDeviation.Bearing + 180 + RotationAngle)%360, RefDeviation.Distance_cm );
513 killagreg 925
                                // Now rotate the heading positions if they are fixed angles
926
                                if(PointList[i].Heading >= 0 && PointList[i].Heading <= 360) PointList[i].Heading = (PointList[i].Heading + RotationAngle) % 360;
927
                        }
928
                        else // no rotation
929
                        {
930
                                // move new reference according to the deviation of the old reference
931
                                retval = GPSPos_ShiftCartesian(&(PointList[i].Position), RefDeviation.North, RefDeviation.East);
932
                        }
516 holgerb 933
                        if(!retval) break;             
491 killagreg 934
                }
935
        } // else ref pos old not copied!
936
        if(!retval) PointList_Clear();
937
        return(retval);
938
}
939
 
516 holgerb 940
 
491 killagreg 941