Subversion Repositories NaviCtrl

Rev

Rev 183 | Rev 187 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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