Subversion Repositories NaviCtrl

Rev

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

Rev Author Line No. Line
1 ingob 1
/*#######################################################################################*/
41 ingob 2
/*#######################################################################################*/
3
 
4
// IMPORTANT NOTE:
5
 
6
// This is only a dummy implementation for errorfree compiling of the NaviCtrl sources.
7
 
8
// The GPS navigation routines are NOT included !
9
 
10
/*#######################################################################################*/
11
/*#######################################################################################*/
1 ingob 12
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
13
/*#######################################################################################*/
14
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
15
// + Copyright (c) 2008 Ingo Busker, Holger Buss
16
// + Nur für den privaten Gebrauch
17
// + FOR NON COMMERCIAL USE ONLY
18
// + www.MikroKopter.com
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
41 ingob 20
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
21
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
22
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
23
// + bzgl. der Nutzungsbedingungen aufzunehmen.
1 ingob 24
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
25
// + Verkauf von Luftbildaufnahmen, usw.
26
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
41 ingob 27
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
1 ingob 28
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
29
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
30
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
31
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
32
// + eindeutig als Ursprung verlinkt werden
33
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
34
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
35
// + Benutzung auf eigene Gefahr
36
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
37
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
41 ingob 38
// + Die PORTIERUNG der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
1 ingob 39
// + mit unserer Zustimmung zulässig
40
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
41
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
42
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
41 ingob 43
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
1 ingob 44
// + this list of conditions and the following disclaimer.
45
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
46
// +     from this software without specific prior written permission.
41 ingob 47
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permitted
1 ingob 48
// +     for non-commercial use (directly or indirectly)
41 ingob 49
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
1 ingob 50
// +     with our written permission
41 ingob 51
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
52
// +     clearly linked as origin
1 ingob 53
// +   * PORTING this software (or part of it) to systems (other than hardware from www.mikrokopter.de) is NOT allowed
54
//
55
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
56
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
59
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
60
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
61
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
62
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
63
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
64
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 ingob 65
// +  POSSIBILITY OF SUCH DAMAGE.
1 ingob 66
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
41 ingob 67
#include <stdio.h>
68
#include <stdlib.h>
69
#include <math.h>
70
#include "91x_lib.h"
1 ingob 71
#include "main.h"
41 ingob 72
#include "uart1.h"
73
#include "GPS.h"
74
#include "timer.h"
75
#include "spi_slave.h"
76
#include "waypoints.h"
77
#include "i2c.h"
1 ingob 78
 
79
 
41 ingob 80
#define M_PI_180        (M_PI / 180.0f)
81
#define GPS_UPDATETIME_MS 200           // 200ms is 5 Hz
82
typedef enum
83
{
84
        GPS_FLIGHT_MODE_UNDEF,
85
        GPS_FLIGHT_MODE_FREE,
86
        GPS_FLIGHT_MODE_AID,
87
        GPS_FLIGHT_MODE_WAYPOINT
88
} GPS_FlightMode_t;
1 ingob 89
 
41 ingob 90
typedef struct
91
{
92
        float Gain;
93
        float P;
94
        float I;
95
        float D;
96
        float A;
97
        float ACC;
92 killagreg 98
        u32 P_Limit;
99
        u32 I_Limit;
100
        u32 D_Limit;
101
        u32 PID_Limit;
73 killagreg 102
        u32 BrakingDuration;
41 ingob 103
        u8 MinSat;
104
        s8 StickThreshold;
105
        float WindCorrection;
89 killagreg 106
        float SpeedCompensation;
92 killagreg 107
        u32 OperatingRadius;
41 ingob 108
        GPS_FlightMode_t  FlightMode;
109
} __attribute__((packed)) GPS_Parameter_t;
1 ingob 110
 
41 ingob 111
typedef struct
112
{
52 killagreg 113
        u8  Status; // invalid, newdata, processed
41 ingob 114
        s32 North;              // in cm
115
        s32 East;               // in cm
116
        s32 Bearing;    // in deg
92 killagreg 117
        u32 Distance;   // in cm
41 ingob 118
} __attribute__((packed)) GPS_Deviation_t;
73 killagreg 119
GPS_Deviation_t CurrentTargetDeviation;         // Deviation from Target
120
GPS_Deviation_t CurrentHomeDeviation;           // Deviation from Home
121
GPS_Deviation_t TargetHomeDeviation;            // Deviation from Target to Home
41 ingob 122
 
123
GPS_Stick_t             GPS_Stick;
124
GPS_Parameter_t GPS_Parameter;
125
 
126
// the gps reference positions
127
GPS_Pos_t GPS_HoldPosition      = {0,0,0, INVALID};                     // the hold position
128
GPS_Pos_t GPS_HomePosition      = {0,0,0, INVALID};                     // the home position
129
GPS_Pos_t * GPS_pTargetPosition = NULL;                             // pointer to the actual target position
92 killagreg 130
u32 GPS_TargetRadius = 0;                                                               // catch radius for target area
41 ingob 131
Waypoint_t* GPS_pWaypoint = NULL;                                               // pointer to the actual waypoint
132
 
133
//-------------------------------------------------------------
134
// Update GPSParamter
135
void GPS_UpdateParameter(void)
136
{
137
        static GPS_FlightMode_t FlightMode_Old = GPS_FLIGHT_MODE_UNDEF;
138
        // in case of bad receiving conditions
139
        if(FC.RC_Quality < 100)
140
        {       // set fixed parameter
97 killagreg 141
                GPS_Parameter.FlightMode = GPS_FLIGHT_MODE_WAYPOINT;
41 ingob 142
                GPS_Parameter.Gain      = (float) 100;
143
                GPS_Parameter.P         = (float) 90;
144
                GPS_Parameter.I         = (float) 90;
145
                GPS_Parameter.D         = (float) 90;
146
                GPS_Parameter.A         = (float) 90;
147
                GPS_Parameter.ACC       = (float) 0;
73 killagreg 148
                GPS_Parameter.P_Limit = 90;
149
                GPS_Parameter.I_Limit = 90;
150
                GPS_Parameter.D_Limit = 90;
151
                GPS_Parameter.PID_Limit = 200;
152
                GPS_Parameter.BrakingDuration = 0;
89 killagreg 153
                GPS_Parameter.SpeedCompensation = (float) 30;
41 ingob 154
                GPS_Parameter.MinSat = 6;
155
                GPS_Parameter.StickThreshold = 8;
156
                GPS_Parameter.WindCorrection = 0.0;
157
                GPS_Parameter.OperatingRadius = 0; // forces the aircraft to fly to home positon
73 killagreg 158
 
41 ingob 159
        }
160
        else
161
        {
162
                // update parameter from FC
163
                if(StopNavigation) GPS_Parameter.FlightMode = GPS_FLIGHT_MODE_FREE;
164
                else
165
                {
96 killagreg 166
                        if(Parameter.NaviGpsModeControl <  50)
167
                        {
168
                                GPS_Parameter.FlightMode = GPS_FLIGHT_MODE_FREE;
169
                                NCFlags &= ~(NC_FLAG_PH | NC_FLAG_CH);
170
                                NCFlags |= NC_FLAG_FREE;
171
                        }
172
                        else if(Parameter.NaviGpsModeControl < 180)
173
                        {
174
                                GPS_Parameter.FlightMode = GPS_FLIGHT_MODE_AID;
175
                                NCFlags &= ~(NC_FLAG_FREE | NC_FLAG_CH);
176
                                NCFlags |= NC_FLAG_PH;
177
                        }
178
                        else
179
                        {
180
                                GPS_Parameter.FlightMode = GPS_FLIGHT_MODE_WAYPOINT;
181
                                NCFlags &= ~(NC_FLAG_FREE | NC_FLAG_PH);
182
                                NCFlags |= NC_FLAG_CH;
183
                        }
41 ingob 184
                }
185
                GPS_Parameter.Gain      = (float)Parameter.NaviGpsGain;
186
                GPS_Parameter.P         = (float)Parameter.NaviGpsP;
187
                GPS_Parameter.I         = (float)Parameter.NaviGpsI;
188
                GPS_Parameter.D         = (float)Parameter.NaviGpsD;
189
                GPS_Parameter.A         = (float)Parameter.NaviGpsD;
190
                GPS_Parameter.ACC       = (float)Parameter.NaviGpsACC;
92 killagreg 191
                GPS_Parameter.P_Limit = (u32)Parameter.NaviGpsPLimit;
192
                GPS_Parameter.I_Limit = (u32)Parameter.NaviGpsILimit;
193
                GPS_Parameter.D_Limit = (u32)Parameter.NaviGpsDLimit;
194
                GPS_Parameter.PID_Limit = 2* (u32)Parameter.NaviAngleLimitation;
73 killagreg 195
                GPS_Parameter.BrakingDuration = (u32)Parameter.NaviPH_LoginTime;
89 killagreg 196
                GPS_Parameter.SpeedCompensation = (float)Parameter.NaviSpeedCompensation;
41 ingob 197
                GPS_Parameter.MinSat = (u8)Parameter.NaviGpsMinSat;
198
                GPS_Parameter.StickThreshold = (s8)Parameter.NaviStickThreshold;
199
                GPS_Parameter.WindCorrection = (float)Parameter.NaviWindCorrection;
92 killagreg 200
                GPS_Parameter.OperatingRadius = (u32)Parameter.NaviOperatingRadius * 100; // conversion of m to cm
41 ingob 201
        }
202
        // FlightMode changed?
203
        if(GPS_Parameter.FlightMode != FlightMode_Old) BeepTime = 100; // beep to indicate that mode has switched
204
        FlightMode_Old = GPS_Parameter.FlightMode;
205
}
206
 
207
//-------------------------------------------------------------
208
// This function defines a good GPS signal condition
209
u8 GPS_IsSignalOK(void)
210
{
211
        if( (GPSData.Status != INVALID) && (GPSData.SatFix == SATFIX_3D) &&  (GPSData.NumOfSats >= GPS_Parameter.MinSat)) return(1);
212
        else return(0);
213
}
214
 
1 ingob 215
//------------------------------------------------------------
41 ingob 216
// Checks for manual control action
217
u8 GPS_IsManuallyControlled(void)
218
{
219
        if( ( (abs(FC.StickNick) > GPS_Parameter.StickThreshold) || (abs(FC.StickRoll) > GPS_Parameter.StickThreshold)) && (GPS_Parameter.StickThreshold > 0)) return 1;
220
        else return 0;
221
}
222
 
223
//------------------------------------------------------------
224
// copy GPS position from source position to target position
225
u8 GPS_CopyPosition(GPS_Pos_t * pGPSPosSrc, GPS_Pos_t* pGPSPosTgt)
226
{
227
        u8 retval = 0;
228
        if((pGPSPosSrc == NULL) || (pGPSPosTgt == NULL)) return(retval);        // bad pointer
229
        // copy only valid positions
230
        if(pGPSPosSrc->Status != INVALID)
231
        {
232
                // if the source GPS position is not invalid
233
                pGPSPosTgt->Longitude   = pGPSPosSrc->Longitude;
234
                pGPSPosTgt->Latitude    = pGPSPosSrc->Latitude;
235
                pGPSPosTgt->Altitude    = pGPSPosSrc->Altitude;
236
                pGPSPosTgt->Status              = NEWDATA; // mark data in target position as new
237
                retval = 1;
238
        }
239
        return(retval);
240
}
241
 
242
//------------------------------------------------------------
243
// clear position data
244
u8 GPS_ClearPosition(GPS_Pos_t * pGPSPos)
245
{
246
        u8 retval = FALSE;
247
        if(pGPSPos == NULL) return(retval);     // bad pointer
248
        else
249
        {
250
                pGPSPos->Longitude      = 0;
251
                pGPSPos->Latitude       = 0;
252
                pGPSPos->Altitude       = 0;
253
                pGPSPos->Status         = INVALID;
254
                retval = TRUE;
255
        }
256
        return (retval);
257
}
258
 
259
 
260
//------------------------------------------------------------
97 killagreg 261
void GPS_Neutral(void)
41 ingob 262
{
263
        GPS_Stick.Nick  = 0;
264
        GPS_Stick.Roll  = 0;
265
        GPS_Stick.Yaw   = 0;
266
}
267
 
268
//------------------------------------------------------------
1 ingob 269
void GPS_Init(void)
270
{
41 ingob 271
        SerialPutString("\r\n GPS init...");
272
        UBX_Init();
273
        GPS_Neutral();
274
        GPS_ClearPosition(&GPS_HoldPosition);
275
        GPS_ClearPosition(&GPS_HomePosition);
276
        GPS_pTargetPosition = NULL;
277
        WPList_Init();
278
        GPS_pWaypoint = WPList_Begin();
279
        GPS_UpdateParameter();
280
        SerialPutString("ok");
281
}
1 ingob 282
 
41 ingob 283
//------------------------------------------------------------
284
// calculate the bearing to target position from its deviation
285
s32 DirectionToTarget_N_E(float northdev, float eastdev)
286
{
287
        s32 bearing;
288
        bearing = (s32)(atan2(northdev, eastdev) / M_PI_180);
289
        bearing = (270L - bearing)%360L;
290
        return(bearing);
1 ingob 291
}
41 ingob 292
 
293
 
1 ingob 294
//------------------------------------------------------------
41 ingob 295
// Rescale xy-vector length if length limit is violated
99 killagreg 296
// return vector length after scaling
297
u32 GPS_LimitXY(s32 *x, s32 *y, u32 limit)
41 ingob 298
{
99 killagreg 299
        u32 dist;
98 killagreg 300
 
99 killagreg 301
        dist = (u32)hypot(*x,*y);       // the length of the vector
302
        if (dist > limit)
41 ingob 303
        // if vector length is larger than the given limit
304
        {       // scale vector compontents so that the length is cut off to limit
99 killagreg 305
                *x = (s32)(((double)(*x) * (double)limit) / dist);
306
                *y = (s32)(((double)(*y) * (double)limit) / dist);
307
                dist = limit;
41 ingob 308
        }
99 killagreg 309
        return(dist);
41 ingob 310
}
1 ingob 311
 
41 ingob 312
//------------------------------------------------------------
313
// transform the integer deg into float radians
314
inline double RadiansFromGPS(s32 deg)
1 ingob 315
{
41 ingob 316
  return ((double)deg * 1e-7f * M_PI_180); // 1E-7 because deg is the value in ° * 1E7
317
}
1 ingob 318
 
41 ingob 319
//------------------------------------------------------------
320
// transform the integer deg into float deg
321
inline double DegFromGPS(s32 deg)
322
{
323
  return ((double)deg  * 1e-7f); // 1E-7 because deg is the value in ° * 1E7
324
}
1 ingob 325
 
41 ingob 326
//------------------------------------------------------------
327
// calculate the deviation from the current position to the target position
73 killagreg 328
u8 GPS_CalculateDeviation(GPS_Pos_t * pCurrentPos, GPS_Pos_t * pTargetPos, GPS_Deviation_t* pDeviationFromTarget)
41 ingob 329
{
73 killagreg 330
        double temp1, temp2;
41 ingob 331
        // if given pointer is NULL
332
        if((pCurrentPos == NULL) || (pTargetPos == NULL)) goto baddata;
333
        // if positions are invalid
334
        if((pCurrentPos->Status == INVALID) || (pTargetPos->Status == INVALID)) goto baddata;
1 ingob 335
 
41 ingob 336
        // The deviation from the current to the target position along north and east direction is
337
        // simple the lat/lon difference. To convert that angular deviation into an
338
        // arc length the spherical projection has to be considered.
339
        // The mean earth radius is 6371km. Therfore the arc length per latitude degree
340
        // is always 6371km * 2 * Pi / 360deg =  111.2 km/deg.
341
        // The arc length per longitude degree depends on the correspondig latitude and
342
        // is 111.2km * cos(latitude).
1 ingob 343
 
41 ingob 344
        // calculate the shortest longitude deviation from target
345
        temp1 = DegFromGPS(pCurrentPos->Longitude) - DegFromGPS(pTargetPos->Longitude);
346
        // outside an angular difference of -180 deg ... +180 deg its shorter to go the other way around
347
        // In our application we wont fly more than 20.000 km but along the date line this is important.
348
        if(temp1 > 180.0f) temp1 -= 360.0f;
349
        else if (temp1 < -180.0f) temp1 += 360.0f;
73 killagreg 350
        temp1 *= cos((RadiansFromGPS(pTargetPos->Latitude) + RadiansFromGPS(pCurrentPos->Latitude))/2);
41 ingob 351
        // calculate latitude deviation from target
352
        // this is allways within -180 deg ... 180 deg
353
        temp2 = DegFromGPS(pCurrentPos->Latitude) - DegFromGPS(pTargetPos->Latitude);
354
        // deviation from target position in cm
355
        // i.e. the distance to walk from the target in northern and eastern direction to reach the current position
356
 
73 killagreg 357
        pDeviationFromTarget->Status = INVALID;
358
        pDeviationFromTarget->North = (s32)(11119492.7f * temp2);
359
        pDeviationFromTarget->East  = (s32)(11119492.7f * temp1);
41 ingob 360
        // If the position deviation is small enough to neglect the earth curvature
361
        // (this is for our application always fulfilled) the distance to target
362
        // can be calculated by the pythagoras of north and east deviation.
92 killagreg 363
        pDeviationFromTarget->Distance = (u32)(11119492.7f * hypot(temp1, temp2));
73 killagreg 364
        if (pDeviationFromTarget->Distance == 0L) pDeviationFromTarget->Bearing = 0L;
365
        else pDeviationFromTarget->Bearing = DirectionToTarget_N_E(temp2, temp1);
366
        pDeviationFromTarget->Status = NEWDATA;
41 ingob 367
        return TRUE;
368
 
369
        baddata:
73 killagreg 370
        pDeviationFromTarget->North     = 0L;
371
        pDeviationFromTarget->East              = 0L;
372
        pDeviationFromTarget->Distance  = 0L;
373
        pDeviationFromTarget->Bearing   = 0L;
374
        pDeviationFromTarget->Status = INVALID;
41 ingob 375
        return FALSE;
376
}
377
 
378
//------------------------------------------------------------
379
void GPS_Navigation(void)
380
{
381
        static u32 beep_rythm;
382
        static u32 GPSDataTimeout = 0;
383
 
384
        // pointer to current target position
385
        static GPS_Pos_t * pTargetPositionOld = NULL;
386
        static Waypoint_t* GPS_pWaypointOld = NULL;
387
 
388
        static GPS_Pos_t RangedTargetPosition = {0,0,0, INVALID};               // the limited target position, this is derived from the target position with repect to the operating radius
92 killagreg 389
        static u32 OperatingRadiusOld = -1;
41 ingob 390
        static u32 WPTime = 0;
391
 
392
        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
393
        //+ Check for new data from GPS-receiver
394
        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
395
        switch(GPSData.Status)
396
        {
397
                case INVALID: // no gps data available
398
                        // do nothing
73 killagreg 399
                        GPS_Parameter.PID_Limit = 0; // disables PID output
41 ingob 400
                        break;
401
 
402
                case PROCESSED: // the current data have been allready processed
403
                        // if no new data are available within the timeout switch to invalid state.
404
                        if(CheckDelay(GPSDataTimeout)) GPSData.Status = INVALID;
405
                        // wait for new gps data
406
                        break;
407
 
408
                case NEWDATA: // handle new gps data
409
 
410
                // update GPS Parameter from FC-Data via SPI interface
411
                GPS_UpdateParameter();
412
 
413
                        // wait maximum of 3 times the normal data update time before data timemout
414
                        GPSDataTimeout = SetDelay(3 * GPS_UPDATETIME_MS);
415
                        beep_rythm++;
416
 
417
                        // debug
92 killagreg 418
                        DebugOut.Analog[21] = (s16)GPSData.Speed_North;
419
                        DebugOut.Analog[22] = (s16)GPSData.Speed_East;
420
                        DebugOut.Analog[31] = (s16)GPSData.NumOfSats;
41 ingob 421
 
422
                        // If GPS signal condition is sufficient for a reliable position measurement
423
                        if(GPS_IsSignalOK())
424
                        {
73 killagreg 425
                                // update home deviation info
426
                                GPS_CalculateDeviation(&(GPSData.Position), &GPS_HomePosition, &CurrentHomeDeviation);
74 killagreg 427
 
41 ingob 428
                                // if the MK is starting or the home position is invalid then store the home position
429
                                if((FC.MKFlags & MKFLAG_START) || (GPS_HomePosition.Status == INVALID))
430
                                {       // try to update the home position from the current position
431
                                        if(GPS_CopyPosition(&(GPSData.Position), &GPS_HomePosition))
432
                                        {
433
                                                BeepTime = 700; // beep on success
434
                                                GPS_CopyPosition(&GPS_HomePosition, &(NaviData.HomePosition));
435
                                        }
436
                                        GPS_pWaypoint = WPList_Begin(); // go to start of waypoint list, return NULL of the list is empty
437
                                }
438
 
439
                                /* The selected flight mode influences the target position pointer and therefore the behavior */
440
 
441
                                // check for current flight mode and set the target pointer GPS_pTargetPosition respectively
442
                                switch(GPS_Parameter.FlightMode)
443
                                {
444
                                        // the GPS control is deactived
445
                                        case GPS_FLIGHT_MODE_FREE:
96 killagreg 446
 
73 killagreg 447
                                                GPS_Parameter.PID_Limit = 0; // disables PID output
41 ingob 448
                                                // update hold position
449
                                                GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition);
450
                                                // no target position
451
                                                GPS_pTargetPosition = NULL;
92 killagreg 452
                                                GPS_TargetRadius = 0;
41 ingob 453
                                                break;
454
 
455
                                        // the GPS supports the position hold, if the pilot takes no action
456
                                        case GPS_FLIGHT_MODE_AID:
96 killagreg 457
 
41 ingob 458
                                                // reset WPList to begin
459
                                                GPS_pWaypoint = WPList_Begin();
460
 
461
                                                if(GPS_IsManuallyControlled())
462
                                                {
73 killagreg 463
                                                        GPS_Parameter.PID_Limit = 0; // disables PID output, as long as the manual conrol is active
41 ingob 464
                                                    GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition);
465
                                                        GPS_pTargetPosition = NULL;
92 killagreg 466
                                                        GPS_TargetRadius = 0;
41 ingob 467
                                                }
468
                                                else
469
                                                {
470
                                                        GPS_pTargetPosition = &GPS_HoldPosition;
92 killagreg 471
                                                        GPS_TargetRadius = 100; // 1 meter
41 ingob 472
                                                }
473
                                                break;
474
 
475
                                        // the GPS control is directed to a target position
476
                                        // given by a waypoint or by the home position
477
                                        case GPS_FLIGHT_MODE_WAYPOINT:
74 killagreg 478
 
73 killagreg 479
                                                if(GPS_IsManuallyControlled()) // the human pilot takes the action
41 ingob 480
                                                {
73 killagreg 481
                                                        GPS_Parameter.PID_Limit = 0; // disables PID output, as long as the manual conrol is active
482
                                                    GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition);  // update hold position
483
                                                        GPS_pTargetPosition = NULL;     // set target position invalid
92 killagreg 484
                                                        GPS_TargetRadius = 0;
73 killagreg 485
                                                }
486
                                                else // no manual control  -> gps position hold active
487
                                                {
488
                                                        // waypoint trigger logic
489
                                                        if(GPS_pWaypoint != NULL) // waypoint exist
41 ingob 490
                                                        {
73 killagreg 491
                                                                if(GPS_pWaypoint->Position.Status == INVALID) // should never happen
41 ingob 492
                                                                {
73 killagreg 493
                                                                        GPS_pWaypoint = WPList_Next(); // goto to next WP
494
                                                                        BeepTime = 255;
41 ingob 495
                                                                }
73 killagreg 496
                                                                else // waypoint position is valid
497
                                                                {
498
                                                                        // check if the pointer to the waypoint has been changed or the data have been updated
499
                                                                        if((GPS_pWaypoint != GPS_pWaypointOld) || (GPS_pWaypoint->Position.Status == NEWDATA))
41 ingob 500
                                                                        {
73 killagreg 501
                                                                                GPS_pWaypointOld = GPS_pWaypoint;
93 killagreg 502
                                                                        }
73 killagreg 503
                                                                        // if WP has been reached once, wait hold time before trigger to next one
92 killagreg 504
                                                                        if(NCFlags & NC_FLAG_TARGET_REACHED)
73 killagreg 505
                                                                        {
506
                                                                                /* ToDo: Adjust GPS_pWaypoint->Heading, GPS_pWaypoint->Event handling */
507
                                                                                if(CheckDelay(WPTime))
508
                                                                                {
509
                                                                                        GPS_pWaypoint = WPList_Next(); // goto to next waypoint, return NULL if end of list has been reached
510
                                                                                }
511
                                                                        } // EOF if(WPArrived)
92 killagreg 512
                                                                        else
513
                                                                        {
514
                                                                                WPTime = SetDelay(GPS_pWaypoint->HoldTime * 1000); // set hold time stamp
515
                                                                        }
41 ingob 516
                                                                }
73 killagreg 517
                                                        } // EOF waypoint trigger logic
74 killagreg 518
 
73 killagreg 519
                                                        if(GPS_pWaypoint != NULL) // Waypoint exist
41 ingob 520
                                                        {
521
                                                                // update the hold position
522
                                                                GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition);
73 killagreg 523
                                                                GPS_pTargetPosition = &(GPS_pWaypoint->Position);
93 killagreg 524
                                                                GPS_TargetRadius = (u32)(GPS_pWaypoint->ToleranceRadius) * 100L;
92 killagreg 525
 
41 ingob 526
                                                        }
73 killagreg 527
                                                        else // no waypoint info available, i.e. the WPList is empty or the end of the list has been reached
528
                                                        {
529
                                                                // fly back to home postion
530
                                                                if(GPS_HomePosition.Status == INVALID)
531
                                                                {
532
                                                                        GPS_pTargetPosition = &GPS_HoldPosition; // fall back to hold mode if home position is not available
92 killagreg 533
                                                                        GPS_TargetRadius = 100;
73 killagreg 534
                                                                        BeepTime = 255; // beep to indicate missin home position
535
                                                                }
536
                                                                else // the home position is valid
537
                                                                {
538
                                                                        // update the hold position
539
                                                                        GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition);
540
                                                                        // set target to home position
541
                                                                        GPS_pTargetPosition = &GPS_HomePosition;
92 killagreg 542
                                                                        GPS_TargetRadius = 100;
73 killagreg 543
                                                                }
544
                                                        }
545
                                                } // EOF no manual control
41 ingob 546
                                                break;
547
 
548
                                        case GPS_FLIGHT_MODE_UNDEF:
549
                                        default:
73 killagreg 550
                                                GPS_Parameter.PID_Limit = 0; // disables PID output
41 ingob 551
                                                // update hold position
552
                                                GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition);
553
                                                // no target position
554
                                                GPS_pTargetPosition = NULL;
92 killagreg 555
                                                GPS_TargetRadius = 0;
41 ingob 556
                                                break;
557
 
558
                                }// EOF GPS Mode Handling
559
 
560
 
561
                                /* Calculation of range target based on the real target */
562
 
563
                                // if no target position exist clear the ranged target position
564
                                if(GPS_pTargetPosition == NULL) GPS_ClearPosition(&RangedTargetPosition);
565
                                else
566
                                {       // if the target position has been changed or the value has been updated or the OperatingRadius has changed
567
                                        if((GPS_pTargetPosition != pTargetPositionOld)  || (GPS_pTargetPosition->Status == NEWDATA) || (GPS_Parameter.OperatingRadius != OperatingRadiusOld) )
568
                                        {
73 killagreg 569
                                                BeepTime = 255; // beep to indicate setting of a new target position
92 killagreg 570
                                                NCFlags &= ~NC_FLAG_TARGET_REACHED;     // clear target reached flag
41 ingob 571
                                                // calculate deviation of new target position from home position
73 killagreg 572
                                                if(GPS_CalculateDeviation(GPS_pTargetPosition, &GPS_HomePosition, &TargetHomeDeviation))
41 ingob 573
                                                {
574
                                                        // check distance from home position
73 killagreg 575
                                                        if(TargetHomeDeviation.Distance > GPS_Parameter.OperatingRadius)
41 ingob 576
                                                        {
577
                                                                //calculate ranged target position to be within the operation radius area
92 killagreg 578
                                                                NCFlags |= NC_FLAG_RANGE_LIMIT;
41 ingob 579
 
73 killagreg 580
                                                                TargetHomeDeviation.North *= GPS_Parameter.OperatingRadius;
581
                                                                TargetHomeDeviation.North /= TargetHomeDeviation.Distance;
582
                                                                TargetHomeDeviation.East *= GPS_Parameter.OperatingRadius;
583
                                                                TargetHomeDeviation.East /= TargetHomeDeviation.Distance;
584
                                                            TargetHomeDeviation.Distance = GPS_Parameter.OperatingRadius;
585
 
41 ingob 586
                                                                RangedTargetPosition.Status = INVALID;
587
                                                                RangedTargetPosition.Latitude = GPS_HomePosition.Latitude;
73 killagreg 588
                                                                RangedTargetPosition.Latitude += (s32)((float)TargetHomeDeviation.North / 1.11194927f);
41 ingob 589
                                                                RangedTargetPosition.Longitude = GPS_HomePosition.Longitude;
73 killagreg 590
                                                                RangedTargetPosition.Longitude += (s32)((float)TargetHomeDeviation.East / (1.11194927f * cos(RadiansFromGPS(GPS_HomePosition.Latitude))) );
41 ingob 591
                                                                RangedTargetPosition.Altitude = GPS_pTargetPosition->Altitude;
592
                                                                RangedTargetPosition.Status = NEWDATA;
593
                                                        }
594
                                                        else
595
                                                        {       // the target is located within the operation radius area
596
                                                                // simple copy the loaction to the ranged target position
597
                                                                GPS_CopyPosition(GPS_pTargetPosition, &RangedTargetPosition);
92 killagreg 598
                                                                NCFlags &= ~NC_FLAG_RANGE_LIMIT;
41 ingob 599
                                                        }
600
                                                }
601
                                                else
602
                                                {       // deviation could not be determined
603
                                                        GPS_ClearPosition(&RangedTargetPosition);
604
                                                }
605
                                                GPS_pTargetPosition->Status = PROCESSED;        // mark current target as processed!
606
                                        }
607
                                }
608
                                OperatingRadiusOld = GPS_Parameter.OperatingRadius;
609
                                // remember last target position pointer
610
                                pTargetPositionOld = GPS_pTargetPosition;
611
 
612
                                /* Calculate position deviation from ranged target */
613
 
614
                                // calculate deviation of current position to ranged target position in cm
73 killagreg 615
                                if(GPS_CalculateDeviation(&(GPSData.Position), &RangedTargetPosition, &CurrentTargetDeviation))
92 killagreg 616
                                {       // set target reached flag of we once reached the target point
93 killagreg 617
                                        if(!(NCFlags & NC_FLAG_TARGET_REACHED) && (CurrentTargetDeviation.Distance < GPS_TargetRadius))
92 killagreg 618
                                        {
93 killagreg 619
                                                NCFlags |= NC_FLAG_TARGET_REACHED;      // set target reached flag
92 killagreg 620
                                        }
41 ingob 621
                                        // implement your control code here based
73 killagreg 622
                                        // in the info available in the CurrentTargetDeviation, GPSData and FromFlightCtrl.GyroHeading
41 ingob 623
                                        GPS_Stick.Nick = 0;
624
                                        GPS_Stick.Roll = 0;
625
                                        GPS_Stick.Yaw  = 0;
626
                                }
627
                                else // deviation could not be calculated
628
                                {   // do nothing on gps sticks!
629
                                        GPS_Neutral();
630
                                }
631
 
632
                        }// eof if GPSSignal is OK
633
                        else // GPSSignal not OK
634
                        {
635
                                GPS_Neutral();
636
                                // beep if signal is not sufficient
637
                                if(GPS_Parameter.FlightMode != GPS_FLIGHT_MODE_FREE)
638
                                {
639
                                        if(!(GPSData.Flags & FLAG_GPSFIXOK) && !(beep_rythm % 5)) BeepTime = 100;
640
                                        else if (GPSData.NumOfSats < GPS_Parameter.MinSat && !(beep_rythm % 5)) BeepTime = 10;
641
                                }
642
                        }
643
                    GPSData.Status = PROCESSED; // mark as processed
644
                        break;
1 ingob 645
        }
41 ingob 646
 
95 killagreg 647
        DebugOut.Analog[6] = NCFlags;
73 killagreg 648
        DebugOut.Analog[27] = (s16)CurrentTargetDeviation.North;
649
        DebugOut.Analog[28] = (s16)CurrentTargetDeviation.East;
41 ingob 650
        DebugOut.Analog[29] = GPS_Stick.Nick;
651
        DebugOut.Analog[30] = GPS_Stick.Roll;
652
 
653
        // update navi data, send back to ground station
73 killagreg 654
        GPS_CopyPosition(&(GPSData.Position),   &(NaviData.CurrentPosition));
41 ingob 655
        GPS_CopyPosition(&RangedTargetPosition, &(NaviData.TargetPosition));
73 killagreg 656
        GPS_CopyPosition(&GPS_HomePosition,     &(NaviData.HomePosition));
41 ingob 657
        NaviData.SatsInUse = GPSData.NumOfSats;
89 killagreg 658
        NaviData.TargetPositionDeviation.Distance = (u16)CurrentTargetDeviation.Distance/10; // dm
659
        NaviData.TargetPositionDeviation.Bearing  = (s16)CurrentTargetDeviation.Bearing;
660
        NaviData.HomePositionDeviation.Distance   = (u16)CurrentHomeDeviation.Distance/10; // dm
661
        NaviData.HomePositionDeviation.Bearing    = (s16)CurrentHomeDeviation.Bearing;
41 ingob 662
        NaviData.UBat = FC.UBat;
663
        NaviData.GroundSpeed = (u16)GPSData.Speed_Ground;
74 killagreg 664
        NaviData.Heading = (s16)(GPSData.Heading/100000L);
73 killagreg 665
        NaviData.CompassHeading = (s16)FromFlightCtrl.GyroHeading/10; // in deg
666
        NaviData.AngleNick = FromFlightCtrl.AngleNick / 10;        // in deg
667
        NaviData.AngleRoll = FromFlightCtrl.AngleRoll / 10;        // in deg
92 killagreg 668
        NaviData.RC_Quality = (u8)FC.RC_Quality;
41 ingob 669
        NaviData.MKFlags = (u8)FC.MKFlags;
92 killagreg 670
        NaviData.NCFlags = NCFlags;
74 killagreg 671
        NaviData.OperatingRadius = Parameter.NaviOperatingRadius;
89 killagreg 672
        NaviData.TopSpeed = (s16)GPSData.Speed_Top;  // in cm/s
97 killagreg 673
        NaviData.TargetHoldTime = (u8)(GetDelay(WPTime)/1000); // in s
41 ingob 674
        //+++++++++++++++++++++++++++++++++++++++++++++++++++
89 killagreg 675
        return;
1 ingob 676
}
677