Subversion Repositories NaviCtrl

Rev

Rev 62 | Rev 74 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 62 Rev 73
Line 93... Line 93...
93
        float P;
93
        float P;
94
        float I;
94
        float I;
95
        float D;
95
        float D;
96
        float A;
96
        float A;
97
        float ACC;
97
        float ACC;
-
 
98
        s32 P_Limit;
-
 
99
        s32 I_Limit;
-
 
100
        s32 D_Limit;
-
 
101
        s32 PID_Limit;
-
 
102
        u32 BrakingDuration;
98
        u8 MinSat;
103
        u8 MinSat;
99
        s8 StickThreshold;
104
        s8 StickThreshold;
100
        float WindCorrection;
105
        float WindCorrection;
101
        s32 OperatingRadius;
106
        s32 OperatingRadius;
102
        u32 NaviAngleLimitation;
-
 
103
        GPS_FlightMode_t  FlightMode;
107
        GPS_FlightMode_t  FlightMode;
104
} __attribute__((packed)) GPS_Parameter_t;
108
} __attribute__((packed)) GPS_Parameter_t;
Line 105... Line 109...
105
 
109
 
106
typedef struct
110
typedef struct
Line 109... Line 113...
109
        s32 North;              // in cm
113
        s32 North;              // in cm
110
        s32 East;               // in cm
114
        s32 East;               // in cm
111
        s32 Bearing;    // in deg
115
        s32 Bearing;    // in deg
112
        s32 Distance;   // in cm
116
        s32 Distance;   // in cm
113
} __attribute__((packed)) GPS_Deviation_t;
117
} __attribute__((packed)) GPS_Deviation_t;
114
GPS_Deviation_t TargetDeviation;
118
GPS_Deviation_t CurrentTargetDeviation;         // Deviation from Target
-
 
119
GPS_Deviation_t CurrentHomeDeviation;           // Deviation from Home
-
 
120
GPS_Deviation_t TargetHomeDeviation;            // Deviation from Target to Home
Line 115... Line 121...
115
 
121
 
116
GPS_Stick_t             GPS_Stick;
122
GPS_Stick_t             GPS_Stick;
Line 117... Line 123...
117
GPS_Parameter_t GPS_Parameter;
123
GPS_Parameter_t GPS_Parameter;
Line 135... Line 141...
135
                GPS_Parameter.P         = (float) 90;
141
                GPS_Parameter.P         = (float) 90;
136
                GPS_Parameter.I         = (float) 90;
142
                GPS_Parameter.I         = (float) 90;
137
                GPS_Parameter.D         = (float) 90;
143
                GPS_Parameter.D         = (float) 90;
138
                GPS_Parameter.A         = (float) 90;
144
                GPS_Parameter.A         = (float) 90;
139
                GPS_Parameter.ACC       = (float) 0;
145
                GPS_Parameter.ACC       = (float) 0;
-
 
146
                GPS_Parameter.P_Limit = 90;
-
 
147
                GPS_Parameter.I_Limit = 90;
-
 
148
                GPS_Parameter.D_Limit = 90;
-
 
149
                GPS_Parameter.PID_Limit = 200;
-
 
150
                GPS_Parameter.BrakingDuration = 0;
140
                GPS_Parameter.MinSat = 6;
151
                GPS_Parameter.MinSat = 6;
141
                GPS_Parameter.StickThreshold = 8;
152
                GPS_Parameter.StickThreshold = 8;
142
                GPS_Parameter.WindCorrection = 0.0;
153
                GPS_Parameter.WindCorrection = 0.0;
143
                GPS_Parameter.OperatingRadius = 0; // forces the aircraft to fly to home positon
154
                GPS_Parameter.OperatingRadius = 0; // forces the aircraft to fly to home positon
144
                GPS_Parameter.NaviAngleLimitation = 125;
-
 
-
 
155
 
145
        }
156
        }
146
        else
157
        else
147
        {
158
        {
148
                // update parameter from FC
159
                // update parameter from FC
149
                if(StopNavigation) GPS_Parameter.FlightMode = GPS_FLIGHT_MODE_FREE;
160
                if(StopNavigation) GPS_Parameter.FlightMode = GPS_FLIGHT_MODE_FREE;
Line 157... Line 168...
157
                GPS_Parameter.P         = (float)Parameter.NaviGpsP;
168
                GPS_Parameter.P         = (float)Parameter.NaviGpsP;
158
                GPS_Parameter.I         = (float)Parameter.NaviGpsI;
169
                GPS_Parameter.I         = (float)Parameter.NaviGpsI;
159
                GPS_Parameter.D         = (float)Parameter.NaviGpsD;
170
                GPS_Parameter.D         = (float)Parameter.NaviGpsD;
160
                GPS_Parameter.A         = (float)Parameter.NaviGpsD;
171
                GPS_Parameter.A         = (float)Parameter.NaviGpsD;
161
                GPS_Parameter.ACC       = (float)Parameter.NaviGpsACC;
172
                GPS_Parameter.ACC       = (float)Parameter.NaviGpsACC;
-
 
173
                GPS_Parameter.P_Limit = (s32)Parameter.NaviGpsPLimit;
-
 
174
                GPS_Parameter.I_Limit = (s32)Parameter.NaviGpsILimit;
-
 
175
                GPS_Parameter.D_Limit = (s32)Parameter.NaviGpsDLimit;
-
 
176
                GPS_Parameter.PID_Limit = 2* (s32)Parameter.NaviAngleLimitation;
-
 
177
                GPS_Parameter.BrakingDuration = (u32)Parameter.NaviPH_LoginTime;
162
                GPS_Parameter.MinSat = (u8)Parameter.NaviGpsMinSat;
178
                GPS_Parameter.MinSat = (u8)Parameter.NaviGpsMinSat;
163
                GPS_Parameter.StickThreshold = (s8)Parameter.NaviStickThreshold;
179
                GPS_Parameter.StickThreshold = (s8)Parameter.NaviStickThreshold;
164
                GPS_Parameter.WindCorrection = (float)Parameter.NaviWindCorrection;
180
                GPS_Parameter.WindCorrection = (float)Parameter.NaviWindCorrection;
165
                GPS_Parameter.OperatingRadius = (s32)Parameter.NaviOperatingRadius * 100; // conversion of m to cm
181
                GPS_Parameter.OperatingRadius = (s32)Parameter.NaviOperatingRadius * 100; // conversion of m to cm
166
                GPS_Parameter.NaviAngleLimitation = (u32) Parameter.NaviAngleLimitation * 2;
-
 
167
        }
182
        }
168
        // FlightMode changed?
183
        // FlightMode changed?
169
        if(GPS_Parameter.FlightMode != FlightMode_Old) BeepTime = 100; // beep to indicate that mode has switched
184
        if(GPS_Parameter.FlightMode != FlightMode_Old) BeepTime = 100; // beep to indicate that mode has switched
170
        FlightMode_Old = GPS_Parameter.FlightMode;
185
        FlightMode_Old = GPS_Parameter.FlightMode;
171
}
186
}
Line 261... Line 276...
261
// Rescale xy-vector length if length limit is violated
276
// Rescale xy-vector length if length limit is violated
262
void GPS_LimitXY(s32 *x, s32 *y, s32 limit)
277
void GPS_LimitXY(s32 *x, s32 *y, s32 limit)
263
{
278
{
264
        s32 dist;
279
        s32 dist;
265
        dist = (s32)hypot(*x,*y);       // the length of the vector
280
        dist = (s32)hypot(*x,*y);       // the length of the vector
-
 
281
        if (dist == 0)
-
 
282
        {
-
 
283
                *x = 0;
-
 
284
                *y = 0;
-
 
285
        }
266
        if ((dist != 0L) && (dist > limit))
286
        else if (dist > limit)
267
        // if vector length is larger than the given limit
287
        // if vector length is larger than the given limit
268
        {       // scale vector compontents so that the length is cut off to limit
288
        {       // scale vector compontents so that the length is cut off to limit
269
                *x = (*x * limit) / dist;
289
                *x = (*x * limit) / dist;
270
                *y = (*y * limit) / dist;
290
                *y = (*y * limit) / dist;
271
        }
291
        }
272
}
292
}
Line 273... Line 293...
273
 
293
 
Line 285... Line 305...
285
  return ((double)deg  * 1e-7f); // 1E-7 because deg is the value in ° * 1E7
305
  return ((double)deg  * 1e-7f); // 1E-7 because deg is the value in ° * 1E7
286
}
306
}
Line 287... Line 307...
287
 
307
 
288
//------------------------------------------------------------
308
//------------------------------------------------------------
289
// calculate the deviation from the current position to the target position
309
// calculate the deviation from the current position to the target position
290
u8 GPS_CalculateDeviation(GPS_Pos_t * pCurrentPos, GPS_Pos_t * pTargetPos)
310
u8 GPS_CalculateDeviation(GPS_Pos_t * pCurrentPos, GPS_Pos_t * pTargetPos, GPS_Deviation_t* pDeviationFromTarget)
291
{
311
{
292
        double temp1, temp2;
312
        double temp1, temp2;
293
        // if given pointer is NULL
313
        // if given pointer is NULL
294
        if((pCurrentPos == NULL) || (pTargetPos == NULL)) goto baddata;
314
        if((pCurrentPos == NULL) || (pTargetPos == NULL)) goto baddata;
295
        // if positions are invalid
315
        // if positions are invalid
Line 307... Line 327...
307
        temp1 = DegFromGPS(pCurrentPos->Longitude) - DegFromGPS(pTargetPos->Longitude);
327
        temp1 = DegFromGPS(pCurrentPos->Longitude) - DegFromGPS(pTargetPos->Longitude);
308
        // outside an angular difference of -180 deg ... +180 deg its shorter to go the other way around
328
        // outside an angular difference of -180 deg ... +180 deg its shorter to go the other way around
309
        // In our application we wont fly more than 20.000 km but along the date line this is important.
329
        // In our application we wont fly more than 20.000 km but along the date line this is important.
310
        if(temp1 > 180.0f) temp1 -= 360.0f;
330
        if(temp1 > 180.0f) temp1 -= 360.0f;
311
        else if (temp1 < -180.0f) temp1 += 360.0f;
331
        else if (temp1 < -180.0f) temp1 += 360.0f;
312
        temp1 *= cos(RadiansFromGPS(pTargetPos->Latitude));
332
        temp1 *= cos((RadiansFromGPS(pTargetPos->Latitude) + RadiansFromGPS(pCurrentPos->Latitude))/2);
313
        // calculate latitude deviation from target
333
        // calculate latitude deviation from target
314
        // this is allways within -180 deg ... 180 deg
334
        // this is allways within -180 deg ... 180 deg
315
        temp2 = DegFromGPS(pCurrentPos->Latitude) - DegFromGPS(pTargetPos->Latitude);
335
        temp2 = DegFromGPS(pCurrentPos->Latitude) - DegFromGPS(pTargetPos->Latitude);
316
        // deviation from target position in cm
336
        // deviation from target position in cm
317
        // i.e. the distance to walk from the target in northern and eastern direction to reach the current position
337
        // i.e. the distance to walk from the target in northern and eastern direction to reach the current position
Line 318... Line 338...
318
 
338
 
319
        TargetDeviation.Status = INVALID;
339
        pDeviationFromTarget->Status = INVALID;
320
        TargetDeviation.North = (s32)(11119492.7f * temp2);
340
        pDeviationFromTarget->North = (s32)(11119492.7f * temp2);
321
        TargetDeviation.East  = (s32)(11119492.7f * temp1);
341
        pDeviationFromTarget->East  = (s32)(11119492.7f * temp1);
322
        // If the position deviation is small enough to neglect the earth curvature
342
        // If the position deviation is small enough to neglect the earth curvature
323
        // (this is for our application always fulfilled) the distance to target
343
        // (this is for our application always fulfilled) the distance to target
324
        // can be calculated by the pythagoras of north and east deviation.
344
        // can be calculated by the pythagoras of north and east deviation.
325
        TargetDeviation.Distance = (s32)(11119492.7f * hypot(temp1, temp2));
345
        pDeviationFromTarget->Distance = (s32)(11119492.7f * hypot(temp1, temp2));
326
        if (TargetDeviation.Distance == 0L) TargetDeviation.Bearing = 0L;
346
        if (pDeviationFromTarget->Distance == 0L) pDeviationFromTarget->Bearing = 0L;
327
        else TargetDeviation.Bearing = DirectionToTarget_N_E(temp2, temp1);
347
        else pDeviationFromTarget->Bearing = DirectionToTarget_N_E(temp2, temp1);
328
        TargetDeviation.Status = NEWDATA;
348
        pDeviationFromTarget->Status = NEWDATA;
Line 329... Line 349...
329
        return TRUE;
349
        return TRUE;
330
 
350
 
331
        baddata:
351
        baddata:
332
        TargetDeviation.North           = 0L;
352
        pDeviationFromTarget->North     = 0L;
333
        TargetDeviation.East            = 0L;
353
        pDeviationFromTarget->East              = 0L;
334
        TargetDeviation.Distance        = 0L;
354
        pDeviationFromTarget->Distance  = 0L;
335
        TargetDeviation.Bearing         = 0L;
355
        pDeviationFromTarget->Bearing   = 0L;
336
        TargetDeviation.Status = INVALID;
356
        pDeviationFromTarget->Status = INVALID;
Line 337... Line 357...
337
        return FALSE;
357
        return FALSE;
338
}
358
}
Line 357... Line 377...
357
        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
377
        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
358
        switch(GPSData.Status)
378
        switch(GPSData.Status)
359
        {
379
        {
360
                case INVALID: // no gps data available
380
                case INVALID: // no gps data available
361
                        // do nothing
381
                        // do nothing
-
 
382
                        GPS_Parameter.PID_Limit = 0; // disables PID output
362
                        break;
383
                        break;
Line 363... Line 384...
363
 
384
 
364
                case PROCESSED: // the current data have been allready processed
385
                case PROCESSED: // the current data have been allready processed
365
                        // if no new data are available within the timeout switch to invalid state.
386
                        // if no new data are available within the timeout switch to invalid state.
Line 382... Line 403...
382
                        DebugOut.Analog[31] = (u16)GPSData.NumOfSats;
403
                        DebugOut.Analog[31] = (u16)GPSData.NumOfSats;
Line 383... Line 404...
383
 
404
 
384
                        // If GPS signal condition is sufficient for a reliable position measurement
405
                        // If GPS signal condition is sufficient for a reliable position measurement
385
                        if(GPS_IsSignalOK())
406
                        if(GPS_IsSignalOK())
-
 
407
                        {
-
 
408
                                // update home deviation info
-
 
409
                                GPS_CalculateDeviation(&(GPSData.Position), &GPS_HomePosition, &CurrentHomeDeviation);
386
                        {
410
                               
387
                                // if the MK is starting or the home position is invalid then store the home position
411
                                // if the MK is starting or the home position is invalid then store the home position
388
                                if((FC.MKFlags & MKFLAG_START) || (GPS_HomePosition.Status == INVALID))
412
                                if((FC.MKFlags & MKFLAG_START) || (GPS_HomePosition.Status == INVALID))
389
                                {       // try to update the home position from the current position
413
                                {       // try to update the home position from the current position
390
                                        if(GPS_CopyPosition(&(GPSData.Position), &GPS_HomePosition))
414
                                        if(GPS_CopyPosition(&(GPSData.Position), &GPS_HomePosition))
Line 402... Line 426...
402
                                {
426
                                {
403
                                        // the GPS control is deactived
427
                                        // the GPS control is deactived
404
                                        case GPS_FLIGHT_MODE_FREE:
428
                                        case GPS_FLIGHT_MODE_FREE:
405
                                                NaviData.NCFlags &= ~(NC_FLAG_PH | NC_FLAG_CH);
429
                                                NaviData.NCFlags &= ~(NC_FLAG_PH | NC_FLAG_CH);
406
                                                NaviData.NCFlags |= NC_FLAG_FREE;
430
                                                NaviData.NCFlags |= NC_FLAG_FREE;
407
 
-
 
-
 
431
                                                GPS_Parameter.PID_Limit = 0; // disables PID output
408
                                                // update hold position
432
                                                // update hold position
409
                                                GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition);
433
                                                GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition);
410
                                                // no target position
434
                                                // no target position
411
                                                GPS_pTargetPosition = NULL;
435
                                                GPS_pTargetPosition = NULL;
412
                                                break;
436
                                                break;
Line 418... Line 442...
418
                                                // reset WPList to begin
442
                                                // reset WPList to begin
419
                                                GPS_pWaypoint = WPList_Begin();
443
                                                GPS_pWaypoint = WPList_Begin();
Line 420... Line 444...
420
 
444
 
421
                                                if(GPS_IsManuallyControlled())
445
                                                if(GPS_IsManuallyControlled())
-
 
446
                                                {
422
                                                {
447
                                                        GPS_Parameter.PID_Limit = 0; // disables PID output, as long as the manual conrol is active
423
                                                    GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition);
448
                                                    GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition);
424
                                                        GPS_pTargetPosition = NULL;
449
                                                        GPS_pTargetPosition = NULL;
425
                                                }
450
                                                }
426
                                                else
451
                                                else
Line 432... Line 457...
432
                                        // the GPS control is directed to a target position
457
                                        // the GPS control is directed to a target position
433
                                        // given by a waypoint or by the home position
458
                                        // given by a waypoint or by the home position
434
                                        case GPS_FLIGHT_MODE_WAYPOINT:
459
                                        case GPS_FLIGHT_MODE_WAYPOINT:
435
                                                NaviData.NCFlags &= ~(NC_FLAG_FREE | NC_FLAG_PH);
460
                                                NaviData.NCFlags &= ~(NC_FLAG_FREE | NC_FLAG_PH);
436
                                                NaviData.NCFlags |= NC_FLAG_CH;
461
                                                NaviData.NCFlags |= NC_FLAG_CH;
437
 
462
                                               
438
                                                // waypoint trigger logic
-
 
439
                                                if(GPS_pWaypoint != NULL) // waypoint exist
463
                                                if(GPS_IsManuallyControlled()) // the human pilot takes the action
440
                                                {
464
                                                {
-
 
465
                                                        GPS_Parameter.PID_Limit = 0; // disables PID output, as long as the manual conrol is active
-
 
466
                                                    GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition);  // update hold position
441
                                                        if(GPS_pWaypoint->Position.Status == INVALID) // should never happen
467
                                                        GPS_pTargetPosition = NULL;     // set target position invalid
442
                                                        {
468
                                                }
443
                                                                GPS_pWaypoint = WPList_Next(); // goto to next WP
469
                                                else // no manual control  -> gps position hold active
444
                                                                WPArrived = FALSE;
-
 
445
                                                                BeepTime = 255;
-
 
446
                                                        }
470
                                                {
447
                                                        else // waypoint position is valid
471
                                                        // waypoint trigger logic
-
 
472
                                                        if(GPS_pWaypoint != NULL) // waypoint exist
448
                                                        {
473
                                                        {
449
                                                                // check if the pointer to the waypoint has been changed or the data have been updated
-
 
450
                                                                if((GPS_pWaypoint != GPS_pWaypointOld) || (GPS_pWaypoint->Position.Status == NEWDATA))
474
                                                                if(GPS_pWaypoint->Position.Status == INVALID) // should never happen
451
                                                                {
475
                                                                {
452
                                                                        GPS_pWaypointOld = GPS_pWaypoint;
476
                                                                        GPS_pWaypoint = WPList_Next(); // goto to next WP
453
                                                                        // reset the arrived bit to break a pending HoldTime of the old WP
-
 
454
                                                                        WPArrived = FALSE;
477
                                                                        WPArrived = FALSE;
-
 
478
                                                                        BeepTime = 255;
455
                                                                }
479
                                                                }
456
 
-
 
457
                                                                if(TargetDeviation.Status != INVALID)
480
                                                                else // waypoint position is valid
458
                                                                {       // if the waypoint was not catched and the target area has been reached
-
 
459
                                                                        if(!WPArrived && (TargetDeviation.Distance < (GPS_pWaypoint->ToleranceRadius * 100)))
-
 
460
                                                                        {
-
 
461
                                                                                WPArrived = TRUE;
-
 
462
                                                                                WPTime = SetDelay(GPS_pWaypoint->HoldTime * 1000); // set hold time stamp
-
 
463
                                                                        }
-
 
464
                                                                }
-
 
465
                                                                // if WP has been reached once, wait hold time before trigger to next one
-
 
466
                                                                if(WPArrived)
-
 
467
                                                                {
481
                                                                {
468
                                                                        /* ToDo: Adjust GPS_pWaypoint->Heading, GPS_pWaypoint->Event handling */
482
                                                                        // check if the pointer to the waypoint has been changed or the data have been updated
469
                                                                        if(CheckDelay(WPTime))
483
                                                                        if((GPS_pWaypoint != GPS_pWaypointOld) || (GPS_pWaypoint->Position.Status == NEWDATA))
470
                                                                        {
484
                                                                        {
-
 
485
                                                                                GPS_pWaypointOld = GPS_pWaypoint;
471
                                                                                GPS_pWaypoint = WPList_Next(); // goto to next waypoint, return NULL if end of list has been reached
486
                                                                                // reset the arrived bit to break a pending HoldTime of the old WP
472
                                                                                WPArrived = FALSE; // which is not arrived
487
                                                                                WPArrived = FALSE;
473
                                                                        }
488
                                                                        }
474
                                                                } // EOF if(WPArrived)
-
 
475
                                                        }
-
 
476
                                                } // EOF waypoint trigger logic
-
 
477
 
489
       
478
                                                if(GPS_pWaypoint != NULL) // Waypoint exist
490
                                                                        if(CurrentTargetDeviation.Status != INVALID)
-
 
491
                                                                        {       // if the waypoint was not catched and the target area has been reached
-
 
492
                                                                                if(!WPArrived && (CurrentTargetDeviation.Distance < (GPS_pWaypoint->ToleranceRadius * 100)))
479
                                                {
493
                                                                                {
480
                                                        // update the hold position
494
                                                                                        WPArrived = TRUE;
481
                                                        GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition);
495
                                                                                        WPTime = SetDelay(GPS_pWaypoint->HoldTime * 1000); // set hold time stamp
482
                                                        GPS_pTargetPosition = &(GPS_pWaypoint->Position);
496
                                                                                }
483
                                                }
497
                                                                        }
484
                                                else // no waypoint info available, i.e. the WPList is empty or the end of the list has been reached
498
                                                                        // if WP has been reached once, wait hold time before trigger to next one
-
 
499
                                                                        if(WPArrived)
485
                                                {
500
                                                                        {
486
                                                        // fly back to home postion
501
                                                                                /* ToDo: Adjust GPS_pWaypoint->Heading, GPS_pWaypoint->Event handling */
487
                                                        if(GPS_HomePosition.Status == INVALID)
502
                                                                                if(CheckDelay(WPTime))
488
                                                        {
503
                                                                                {
489
                                                                GPS_pTargetPosition = &GPS_HoldPosition; // fall back to hold mode if home position is not available
504
                                                                                        GPS_pWaypoint = WPList_Next(); // goto to next waypoint, return NULL if end of list has been reached
490
                                                                BeepTime = 255; // beep to indicate missin home position
505
                                                                                        WPArrived = FALSE; // which is not arrived
-
 
506
                                                                                }
-
 
507
                                                                        } // EOF if(WPArrived)
491
                                                        }
508
                                                                }
492
                                                        else // the home position is valid
509
                                                        } // EOF waypoint trigger logic
-
 
510
       
-
 
511
                                                        if(GPS_pWaypoint != NULL) // Waypoint exist
493
                                                        {
512
                                                        {
494
                                                                // update the hold position
513
                                                                // update the hold position
495
                                                                GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition);
514
                                                                GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition);
496
                                                                // set target to home position
-
 
497
                                                                GPS_pTargetPosition = &GPS_HomePosition;
515
                                                                GPS_pTargetPosition = &(GPS_pWaypoint->Position);
498
                                                        }
516
                                                        }
-
 
517
                                                        else // no waypoint info available, i.e. the WPList is empty or the end of the list has been reached
-
 
518
                                                        {
-
 
519
                                                                // fly back to home postion
-
 
520
                                                                if(GPS_HomePosition.Status == INVALID)
-
 
521
                                                                {
-
 
522
                                                                        GPS_pTargetPosition = &GPS_HoldPosition; // fall back to hold mode if home position is not available
-
 
523
                                                                        BeepTime = 255; // beep to indicate missin home position
-
 
524
                                                                }
-
 
525
                                                                else // the home position is valid
-
 
526
                                                                {
-
 
527
                                                                        // update the hold position
-
 
528
                                                                        GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition);
-
 
529
                                                                        // set target to home position
-
 
530
                                                                        GPS_pTargetPosition = &GPS_HomePosition;
-
 
531
                                                                }
499
                                                }
532
                                                        }
-
 
533
                                                } // EOF no manual control
500
                                                break;
534
                                                break;
Line 501... Line 535...
501
 
535
 
502
                                        case GPS_FLIGHT_MODE_UNDEF:
536
                                        case GPS_FLIGHT_MODE_UNDEF:
-
 
537
                                        default:
503
                                        default:
538
                                                GPS_Parameter.PID_Limit = 0; // disables PID output
504
                                                // update hold position
539
                                                // update hold position
505
                                                GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition);
540
                                                GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition);
506
                                                // no target position
541
                                                // no target position
507
                                                GPS_pTargetPosition = NULL;
542
                                                GPS_pTargetPosition = NULL;
Line 516... Line 551...
516
                                if(GPS_pTargetPosition == NULL) GPS_ClearPosition(&RangedTargetPosition);
551
                                if(GPS_pTargetPosition == NULL) GPS_ClearPosition(&RangedTargetPosition);
517
                                else
552
                                else
518
                                {       // if the target position has been changed or the value has been updated or the OperatingRadius has changed
553
                                {       // if the target position has been changed or the value has been updated or the OperatingRadius has changed
519
                                        if((GPS_pTargetPosition != pTargetPositionOld)  || (GPS_pTargetPosition->Status == NEWDATA) || (GPS_Parameter.OperatingRadius != OperatingRadiusOld) )
554
                                        if((GPS_pTargetPosition != pTargetPositionOld)  || (GPS_pTargetPosition->Status == NEWDATA) || (GPS_Parameter.OperatingRadius != OperatingRadiusOld) )
520
                                        {
555
                                        {
-
 
556
                                                BeepTime = 255; // beep to indicate setting of a new target position
521
                                                // calculate deviation of new target position from home position
557
                                                // calculate deviation of new target position from home position
522
                                                if(GPS_CalculateDeviation(GPS_pTargetPosition, &GPS_HomePosition))
558
                                                if(GPS_CalculateDeviation(GPS_pTargetPosition, &GPS_HomePosition, &TargetHomeDeviation))
523
                                                {
559
                                                {
524
                                                        // check distance from home position
560
                                                        // check distance from home position
525
                                                        if(TargetDeviation.Distance > GPS_Parameter.OperatingRadius)
561
                                                        if(TargetHomeDeviation.Distance > GPS_Parameter.OperatingRadius)
526
                                                        {
562
                                                        {
527
                                                                //calculate ranged target position to be within the operation radius area
563
                                                                //calculate ranged target position to be within the operation radius area
528
                                                                NaviData.NCFlags |= NC_FLAG_RANGE_LIMIT;
564
                                                                NaviData.NCFlags |= NC_FLAG_RANGE_LIMIT;
-
 
565
 
529
                                                            TargetDeviation.Distance = GPS_Parameter.OperatingRadius;
566
                                                                TargetHomeDeviation.North *= GPS_Parameter.OperatingRadius;
530
                                                                GPS_LimitXY(&(TargetDeviation.East), &(TargetDeviation.North), TargetDeviation.Distance);
567
                                                                TargetHomeDeviation.North /= TargetHomeDeviation.Distance;
-
 
568
                                                                TargetHomeDeviation.East *= GPS_Parameter.OperatingRadius;
-
 
569
                                                                TargetHomeDeviation.East /= TargetHomeDeviation.Distance;
-
 
570
                                                            TargetHomeDeviation.Distance = GPS_Parameter.OperatingRadius;
Line 531... Line 571...
531
 
571
 
532
                                                                RangedTargetPosition.Status = INVALID;
572
                                                                RangedTargetPosition.Status = INVALID;
533
                                                                RangedTargetPosition.Latitude = GPS_HomePosition.Latitude;
573
                                                                RangedTargetPosition.Latitude = GPS_HomePosition.Latitude;
534
                                                                RangedTargetPosition.Latitude += (s32)((float)TargetDeviation.North / 1.11194927f);
574
                                                                RangedTargetPosition.Latitude += (s32)((float)TargetHomeDeviation.North / 1.11194927f);
535
                                                                RangedTargetPosition.Longitude = GPS_HomePosition.Longitude;
575
                                                                RangedTargetPosition.Longitude = GPS_HomePosition.Longitude;
536
                                                                RangedTargetPosition.Longitude += (s32)((float)TargetDeviation.East / (1.11194927f * cos(RadiansFromGPS(GPS_HomePosition.Latitude))) );
576
                                                                RangedTargetPosition.Longitude += (s32)((float)TargetHomeDeviation.East / (1.11194927f * cos(RadiansFromGPS(GPS_HomePosition.Latitude))) );
537
                                                                RangedTargetPosition.Altitude = GPS_pTargetPosition->Altitude;
577
                                                                RangedTargetPosition.Altitude = GPS_pTargetPosition->Altitude;
538
                                                                RangedTargetPosition.Status = NEWDATA;
578
                                                                RangedTargetPosition.Status = NEWDATA;
539
                                                        }
579
                                                        }
540
                                                        else
580
                                                        else
Line 556... Line 596...
556
                                pTargetPositionOld = GPS_pTargetPosition;
596
                                pTargetPositionOld = GPS_pTargetPosition;
Line 557... Line 597...
557
 
597
 
Line 558... Line 598...
558
                                /* Calculate position deviation from ranged target */
598
                                /* Calculate position deviation from ranged target */
559
 
599
 
560
                                // calculate deviation of current position to ranged target position in cm
600
                                // calculate deviation of current position to ranged target position in cm
561
                                if(GPS_CalculateDeviation(&(GPSData.Position), &RangedTargetPosition))
601
                                if(GPS_CalculateDeviation(&(GPSData.Position), &RangedTargetPosition, &CurrentTargetDeviation))
562
                                {
602
                                {
563
                                        // implement your control code here based
603
                                        // implement your control code here based
564
                                        // in the info available in the TargetDeviation, GPSData and FromFlightCtrl.GyroHeading
604
                                        // in the info available in the CurrentTargetDeviation, GPSData and FromFlightCtrl.GyroHeading
565
                                        GPS_Stick.Nick = 0;
605
                                        GPS_Stick.Nick = 0;
566
                                        GPS_Stick.Roll = 0;
606
                                        GPS_Stick.Roll = 0;
567
                                        GPS_Stick.Yaw  = 0;
607
                                        GPS_Stick.Yaw  = 0;
Line 584... Line 624...
584
                        }
624
                        }
585
                    GPSData.Status = PROCESSED; // mark as processed
625
                    GPSData.Status = PROCESSED; // mark as processed
586
                        break;
626
                        break;
587
        }
627
        }
Line 588... Line 628...
588
 
628
 
589
        DebugOut.Analog[27] = (s16)TargetDeviation.North;
629
        DebugOut.Analog[27] = (s16)CurrentTargetDeviation.North;
590
        DebugOut.Analog[28] = (s16)TargetDeviation.East;
630
        DebugOut.Analog[28] = (s16)CurrentTargetDeviation.East;
591
        DebugOut.Analog[29] = GPS_Stick.Nick;
631
        DebugOut.Analog[29] = GPS_Stick.Nick;
Line 592... Line 632...
592
        DebugOut.Analog[30] = GPS_Stick.Roll;
632
        DebugOut.Analog[30] = GPS_Stick.Roll;
593
 
633
 
594
        // update navi data, send back to ground station
634
        // update navi data, send back to ground station
-
 
635
        GPS_CopyPosition(&(GPSData.Position),   &(NaviData.CurrentPosition));
595
        GPS_CopyPosition(&(GPSData.Position), &(NaviData.CurrentPosition));
636
        GPS_CopyPosition(&RangedTargetPosition, &(NaviData.TargetPosition));
596
        GPS_CopyPosition(&RangedTargetPosition, &(NaviData.TargetPosition));
637
        GPS_CopyPosition(&GPS_HomePosition,     &(NaviData.HomePosition));
597
        NaviData.SatsInUse = GPSData.NumOfSats;
638
        NaviData.SatsInUse = GPSData.NumOfSats;
-
 
639
        NaviData.TargetPositionDeviation.Distance = CurrentTargetDeviation.Distance;
-
 
640
        NaviData.TargetPositionDeviation.Bearing  = CurrentTargetDeviation.Bearing;
598
        NaviData.TargetPositionDeviation.Distance = TargetDeviation.Distance;
641
        NaviData.HomePositionDeviation.Distance   = CurrentHomeDeviation.Distance;
599
        NaviData.TargetPositionDeviation.Bearing = TargetDeviation.Bearing;
642
        NaviData.HomePositionDeviation.Bearing    = CurrentHomeDeviation.Bearing;
600
        NaviData.UBat = FC.UBat;
643
        NaviData.UBat = FC.UBat;
601
        NaviData.GroundSpeed = (u16)GPSData.Speed_Ground;
644
        NaviData.GroundSpeed = (u16)GPSData.Speed_Ground;
602
        NaviData.Heading = (s16)GPSData.Heading;
645
        NaviData.Heading = (s16)GPSData.Heading;
603
        NaviData.CompassHeading = FromFlightCtrl.GyroHeading / 10; // in deg
646
        NaviData.CompassHeading = (s16)FromFlightCtrl.GyroHeading/10; // in deg
604
        NaviData.AngleNick = FromFlightCtrl.AngleNick / 10;     // in deg
647
        NaviData.AngleNick = FromFlightCtrl.AngleNick / 10;        // in deg
605
        NaviData.AngleRoll = FromFlightCtrl.AngleRoll / 10;     // in deg
648
        NaviData.AngleRoll = FromFlightCtrl.AngleRoll / 10;        // in deg
-
 
649
        NaviData.RC_Quality = (u8) FC.RC_Quality;
Line 606... Line 650...
606
        NaviData.RC_Quality = (u8) FC.RC_Quality;
650
        NaviData.MKFlags = (u8)FC.MKFlags;
607
        NaviData.MKFlags = (u8)FC.MKFlags;
651
        NaviData.OperatingRadius = Parameter.NaviOperatingRadius;
608
 
652