Subversion Repositories NaviCtrl

Rev

Rev 81 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 81 Rev 171
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
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 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 this software (or part of it) to systems (other than 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 "timer.h"
74
#include "timer.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
        s32 OperatingRadius;
106
        s32 OperatingRadius;
107
        GPS_FlightMode_t  FlightMode;
107
        GPS_FlightMode_t  FlightMode;
108
} __attribute__((packed)) GPS_Parameter_t;
108
} __attribute__((packed)) GPS_Parameter_t;
109
 
109
 
110
typedef struct
110
typedef struct
111
{
111
{
112
        u8  Status; // invalid, newdata, processed
112
        u8  Status; // invalid, newdata, processed
113
        s32 North;              // in cm
113
        s32 North;              // in cm
114
        s32 East;               // in cm
114
        s32 East;               // in cm
115
        s32 Bearing;    // in deg
115
        s32 Bearing;    // in deg
116
        s32 Distance;   // in cm
116
        s32 Distance;   // in cm
117
} __attribute__((packed)) GPS_Deviation_t;
117
} __attribute__((packed)) GPS_Deviation_t;
118
GPS_Deviation_t CurrentTargetDeviation;         // Deviation from Target
118
GPS_Deviation_t CurrentTargetDeviation;         // Deviation from Target
119
GPS_Deviation_t CurrentHomeDeviation;           // Deviation from Home
119
GPS_Deviation_t CurrentHomeDeviation;           // Deviation from Home
120
GPS_Deviation_t TargetHomeDeviation;            // Deviation from Target to Home
120
GPS_Deviation_t TargetHomeDeviation;            // Deviation from Target to Home
121
 
121
 
122
GPS_Stick_t             GPS_Stick;
122
GPS_Stick_t             GPS_Stick;
123
GPS_Parameter_t GPS_Parameter;
123
GPS_Parameter_t GPS_Parameter;
124
 
124
 
125
// the gps reference positions
125
// the gps reference positions
126
GPS_Pos_t GPS_HoldPosition      = {0,0,0, INVALID};                     // the hold position
126
GPS_Pos_t GPS_HoldPosition      = {0,0,0, INVALID};                     // the hold position
127
GPS_Pos_t GPS_HomePosition      = {0,0,0, INVALID};                     // the home position
127
GPS_Pos_t GPS_HomePosition      = {0,0,0, INVALID};                     // the home position
128
GPS_Pos_t * GPS_pTargetPosition = NULL;                             // pointer to the actual target position
128
GPS_Pos_t * GPS_pTargetPosition = NULL;                             // pointer to the actual target position
129
Waypoint_t* GPS_pWaypoint = NULL;                                               // pointer to the actual waypoint
129
Waypoint_t* GPS_pWaypoint = NULL;                                               // pointer to the actual waypoint
130
 
130
 
131
//-------------------------------------------------------------
131
//-------------------------------------------------------------
132
// Update GPSParamter
132
// Update GPSParamter
133
void GPS_UpdateParameter(void)
133
void GPS_UpdateParameter(void)
134
{
134
{
135
        static GPS_FlightMode_t FlightMode_Old = GPS_FLIGHT_MODE_UNDEF;
135
        static GPS_FlightMode_t FlightMode_Old = GPS_FLIGHT_MODE_UNDEF;
136
        // in case of bad receiving conditions
136
        // in case of bad receiving conditions
137
        if(FC.RC_Quality < 100)
137
        if(FC.RC_Quality < 100)
138
        {       // set fixed parameter
138
        {       // set fixed parameter
139
                GPS_Parameter.FlightMode = GPS_FLIGHT_MODE_FREE;
139
                GPS_Parameter.FlightMode = GPS_FLIGHT_MODE_FREE;
140
                GPS_Parameter.Gain      = (float) 100;
140
                GPS_Parameter.Gain      = (float) 100;
141
                GPS_Parameter.P         = (float) 90;
141
                GPS_Parameter.P         = (float) 90;
142
                GPS_Parameter.I         = (float) 90;
142
                GPS_Parameter.I         = (float) 90;
143
                GPS_Parameter.D         = (float) 90;
143
                GPS_Parameter.D         = (float) 90;
144
                GPS_Parameter.A         = (float) 90;
144
                GPS_Parameter.A         = (float) 90;
145
                GPS_Parameter.ACC       = (float) 0;
145
                GPS_Parameter.ACC       = (float) 0;
146
                GPS_Parameter.P_Limit = 90;
146
                GPS_Parameter.P_Limit = 90;
147
                GPS_Parameter.I_Limit = 90;
147
                GPS_Parameter.I_Limit = 90;
148
                GPS_Parameter.D_Limit = 90;
148
                GPS_Parameter.D_Limit = 90;
149
                GPS_Parameter.PID_Limit = 200;
149
                GPS_Parameter.PID_Limit = 200;
150
                GPS_Parameter.BrakingDuration = 0;
150
                GPS_Parameter.BrakingDuration = 0;
151
                GPS_Parameter.MinSat = 6;
151
                GPS_Parameter.MinSat = 6;
152
                GPS_Parameter.StickThreshold = 8;
152
                GPS_Parameter.StickThreshold = 8;
153
                GPS_Parameter.WindCorrection = 0.0;
153
                GPS_Parameter.WindCorrection = 0.0;
154
                GPS_Parameter.OperatingRadius = 0; // forces the aircraft to fly to home positon
154
                GPS_Parameter.OperatingRadius = 0; // forces the aircraft to fly to home positon
155
 
155
 
156
        }
156
        }
157
        else
157
        else
158
        {
158
        {
159
                // update parameter from FC
159
                // update parameter from FC
160
                if(StopNavigation) GPS_Parameter.FlightMode = GPS_FLIGHT_MODE_FREE;
160
                if(StopNavigation) GPS_Parameter.FlightMode = GPS_FLIGHT_MODE_FREE;
161
                else
161
                else
162
                {
162
                {
163
                        if     (Parameter.NaviGpsModeControl <  50) GPS_Parameter.FlightMode = GPS_FLIGHT_MODE_AID;
163
                        if     (Parameter.NaviGpsModeControl <  50) GPS_Parameter.FlightMode = GPS_FLIGHT_MODE_AID;
164
                        else if(Parameter.NaviGpsModeControl < 180) GPS_Parameter.FlightMode = GPS_FLIGHT_MODE_FREE;
164
                        else if(Parameter.NaviGpsModeControl < 180) GPS_Parameter.FlightMode = GPS_FLIGHT_MODE_FREE;
165
                        else                                                    GPS_Parameter.FlightMode = GPS_FLIGHT_MODE_WAYPOINT;
165
                        else                                                    GPS_Parameter.FlightMode = GPS_FLIGHT_MODE_WAYPOINT;
166
                }
166
                }
167
                GPS_Parameter.Gain      = (float)Parameter.NaviGpsGain;
167
                GPS_Parameter.Gain      = (float)Parameter.NaviGpsGain;
168
                GPS_Parameter.P         = (float)Parameter.NaviGpsP;
168
                GPS_Parameter.P         = (float)Parameter.NaviGpsP;
169
                GPS_Parameter.I         = (float)Parameter.NaviGpsI;
169
                GPS_Parameter.I         = (float)Parameter.NaviGpsI;
170
                GPS_Parameter.D         = (float)Parameter.NaviGpsD;
170
                GPS_Parameter.D         = (float)Parameter.NaviGpsD;
171
                GPS_Parameter.A         = (float)Parameter.NaviGpsD;
171
                GPS_Parameter.A         = (float)Parameter.NaviGpsD;
172
                GPS_Parameter.ACC       = (float)Parameter.NaviGpsACC;
172
                GPS_Parameter.ACC       = (float)Parameter.NaviGpsACC;
173
                GPS_Parameter.P_Limit = (s32)Parameter.NaviGpsPLimit;
173
                GPS_Parameter.P_Limit = (s32)Parameter.NaviGpsPLimit;
174
                GPS_Parameter.I_Limit = (s32)Parameter.NaviGpsILimit;
174
                GPS_Parameter.I_Limit = (s32)Parameter.NaviGpsILimit;
175
                GPS_Parameter.D_Limit = (s32)Parameter.NaviGpsDLimit;
175
                GPS_Parameter.D_Limit = (s32)Parameter.NaviGpsDLimit;
176
                GPS_Parameter.PID_Limit = 2* (s32)Parameter.NaviAngleLimitation;
176
                GPS_Parameter.PID_Limit = 2* (s32)Parameter.NaviAngleLimitation;
177
                GPS_Parameter.BrakingDuration = (u32)Parameter.NaviPH_LoginTime;
177
                GPS_Parameter.BrakingDuration = (u32)Parameter.NaviPH_LoginTime;
178
                GPS_Parameter.MinSat = (u8)Parameter.NaviGpsMinSat;
178
                GPS_Parameter.MinSat = (u8)Parameter.NaviGpsMinSat;
179
                GPS_Parameter.StickThreshold = (s8)Parameter.NaviStickThreshold;
179
                GPS_Parameter.StickThreshold = (s8)Parameter.NaviStickThreshold;
180
                GPS_Parameter.WindCorrection = (float)Parameter.NaviWindCorrection;
180
                GPS_Parameter.WindCorrection = (float)Parameter.NaviWindCorrection;
181
                GPS_Parameter.OperatingRadius = (s32)Parameter.NaviOperatingRadius * 100; // conversion of m to cm
181
                GPS_Parameter.OperatingRadius = (s32)Parameter.NaviOperatingRadius * 100; // conversion of m to cm
182
        }
182
        }
183
        // FlightMode changed?
183
        // FlightMode changed?
184
        if(GPS_Parameter.FlightMode != FlightMode_Old) BeepTime = 100; // beep to indicate that mode has switched
184
        if(GPS_Parameter.FlightMode != FlightMode_Old) BeepTime = 100; // beep to indicate that mode has switched
185
        FlightMode_Old = GPS_Parameter.FlightMode;
185
        FlightMode_Old = GPS_Parameter.FlightMode;
186
}
186
}
187
 
187
 
188
//-------------------------------------------------------------
188
//-------------------------------------------------------------
189
// This function defines a good GPS signal condition
189
// This function defines a good GPS signal condition
190
u8 GPS_IsSignalOK(void)
190
u8 GPS_IsSignalOK(void)
191
{
191
{
192
        if( (GPSData.Status != INVALID) && (GPSData.SatFix == SATFIX_3D) &&  (GPSData.NumOfSats >= GPS_Parameter.MinSat)) return(1);
192
        if( (GPSData.Status != INVALID) && (GPSData.SatFix == SATFIX_3D) &&  (GPSData.NumOfSats >= GPS_Parameter.MinSat)) return(1);
193
        else return(0);
193
        else return(0);
194
}
194
}
195
 
195
 
196
//------------------------------------------------------------
196
//------------------------------------------------------------
197
// Checks for manual control action
197
// Checks for manual control action
198
u8 GPS_IsManuallyControlled(void)
198
u8 GPS_IsManuallyControlled(void)
199
{
199
{
200
        if( ( (abs(FC.StickNick) > GPS_Parameter.StickThreshold) || (abs(FC.StickRoll) > GPS_Parameter.StickThreshold)) && (GPS_Parameter.StickThreshold > 0)) return 1;
200
        if( ( (abs(FC.StickNick) > GPS_Parameter.StickThreshold) || (abs(FC.StickRoll) > GPS_Parameter.StickThreshold)) && (GPS_Parameter.StickThreshold > 0)) return 1;
201
        else return 0;
201
        else return 0;
202
}
202
}
203
 
203
 
204
//------------------------------------------------------------
204
//------------------------------------------------------------
205
// copy GPS position from source position to target position
205
// copy GPS position from source position to target position
206
u8 GPS_CopyPosition(GPS_Pos_t * pGPSPosSrc, GPS_Pos_t* pGPSPosTgt)
206
u8 GPS_CopyPosition(GPS_Pos_t * pGPSPosSrc, GPS_Pos_t* pGPSPosTgt)
207
{
207
{
208
        u8 retval = 0;
208
        u8 retval = 0;
209
        if((pGPSPosSrc == NULL) || (pGPSPosTgt == NULL)) return(retval);        // bad pointer
209
        if((pGPSPosSrc == NULL) || (pGPSPosTgt == NULL)) return(retval);        // bad pointer
210
        // copy only valid positions
210
        // copy only valid positions
211
        if(pGPSPosSrc->Status != INVALID)
211
        if(pGPSPosSrc->Status != INVALID)
212
        {
212
        {
213
                // if the source GPS position is not invalid
213
                // if the source GPS position is not invalid
214
                pGPSPosTgt->Longitude   = pGPSPosSrc->Longitude;
214
                pGPSPosTgt->Longitude   = pGPSPosSrc->Longitude;
215
                pGPSPosTgt->Latitude    = pGPSPosSrc->Latitude;
215
                pGPSPosTgt->Latitude    = pGPSPosSrc->Latitude;
216
                pGPSPosTgt->Altitude    = pGPSPosSrc->Altitude;
216
                pGPSPosTgt->Altitude    = pGPSPosSrc->Altitude;
217
                pGPSPosTgt->Status              = NEWDATA; // mark data in target position as new
217
                pGPSPosTgt->Status              = NEWDATA; // mark data in target position as new
218
                retval = 1;
218
                retval = 1;
219
        }
219
        }
220
        return(retval);
220
        return(retval);
221
}
221
}
222
 
222
 
223
//------------------------------------------------------------
223
//------------------------------------------------------------
224
// clear position data
224
// clear position data
225
u8 GPS_ClearPosition(GPS_Pos_t * pGPSPos)
225
u8 GPS_ClearPosition(GPS_Pos_t * pGPSPos)
226
{
226
{
227
        u8 retval = FALSE;
227
        u8 retval = FALSE;
228
        if(pGPSPos == NULL) return(retval);     // bad pointer
228
        if(pGPSPos == NULL) return(retval);     // bad pointer
229
        else
229
        else
230
        {
230
        {
231
                pGPSPos->Longitude      = 0;
231
                pGPSPos->Longitude      = 0;
232
                pGPSPos->Latitude       = 0;
232
                pGPSPos->Latitude       = 0;
233
                pGPSPos->Altitude       = 0;
233
                pGPSPos->Altitude       = 0;
234
                pGPSPos->Status         = INVALID;
234
                pGPSPos->Status         = INVALID;
235
                retval = TRUE;
235
                retval = TRUE;
236
        }
236
        }
237
        return (retval);
237
        return (retval);
238
}
238
}
239
 
239
 
240
 
240
 
241
//------------------------------------------------------------
241
//------------------------------------------------------------
242
void GPS_Neutral()
242
void GPS_Neutral()
243
{
243
{
244
        GPS_Stick.Nick  = 0;
244
        GPS_Stick.Nick  = 0;
245
        GPS_Stick.Roll  = 0;
245
        GPS_Stick.Roll  = 0;
246
        GPS_Stick.Yaw   = 0;
246
        GPS_Stick.Yaw   = 0;
247
}
247
}
248
 
248
 
249
//------------------------------------------------------------
249
//------------------------------------------------------------
250
void GPS_Init(void)
250
void GPS_Init(void)
251
{
251
{
252
        SerialPutString("\r\n GPS init...");
252
        SerialPutString("\r\n GPS init...");
253
        UBX_Init();
253
        UBX_Init();
254
        GPS_Neutral();
254
        GPS_Neutral();
255
        GPS_ClearPosition(&GPS_HoldPosition);
255
        GPS_ClearPosition(&GPS_HoldPosition);
256
        GPS_ClearPosition(&GPS_HomePosition);
256
        GPS_ClearPosition(&GPS_HomePosition);
257
        GPS_pTargetPosition = NULL;
257
        GPS_pTargetPosition = NULL;
258
        WPList_Init();
258
        WPList_Init();
259
        GPS_pWaypoint = WPList_Begin();
259
        GPS_pWaypoint = WPList_Begin();
260
        GPS_UpdateParameter();
260
        GPS_UpdateParameter();
261
        SerialPutString("ok");
261
        SerialPutString("ok");
262
}
262
}
263
 
263
 
264
//------------------------------------------------------------
264
//------------------------------------------------------------
265
// calculate the bearing to target position from its deviation
265
// calculate the bearing to target position from its deviation
266
s32 DirectionToTarget_N_E(float northdev, float eastdev)
266
s32 DirectionToTarget_N_E(float northdev, float eastdev)
267
{
267
{
268
        s32 bearing;
268
        s32 bearing;
269
        bearing = (s32)(atan2(northdev, eastdev) / M_PI_180);
269
        bearing = (s32)(atan2(northdev, eastdev) / M_PI_180);
270
        bearing = (270L - bearing)%360L;
270
        bearing = (270L - bearing)%360L;
271
        return(bearing);
271
        return(bearing);
272
}
272
}
273
 
273
 
274
 
274
 
275
//------------------------------------------------------------
275
//------------------------------------------------------------
276
// Rescale xy-vector length if length limit is violated
276
// Rescale xy-vector length if length limit is violated
277
void GPS_LimitXY(s32 *x, s32 *y, s32 limit)
277
void GPS_LimitXY(s32 *x, s32 *y, s32 limit)
278
{
278
{
279
        s32 dist;
279
        s32 dist;
280
        dist = (s32)hypot(*x,*y);       // the length of the vector
280
        dist = (s32)hypot(*x,*y);       // the length of the vector
281
        if (dist == 0)
281
        if (dist == 0)
282
        {
282
        {
283
                *x = 0;
283
                *x = 0;
284
                *y = 0;
284
                *y = 0;
285
        }
285
        }
286
        else if (dist > limit)
286
        else if (dist > limit)
287
        // if vector length is larger than the given limit
287
        // if vector length is larger than the given limit
288
        {       // scale vector compontents so that the length is cut off to limit
288
        {       // scale vector compontents so that the length is cut off to limit
289
                *x = (*x * limit) / dist;
289
                *x = (*x * limit) / dist;
290
                *y = (*y * limit) / dist;
290
                *y = (*y * limit) / dist;
291
        }
291
        }
292
}
292
}
293
 
293
 
294
//------------------------------------------------------------
294
//------------------------------------------------------------
295
// transform the integer deg into float radians
295
// transform the integer deg into float radians
296
inline double RadiansFromGPS(s32 deg)
296
inline double RadiansFromGPS(s32 deg)
297
{
297
{
298
  return ((double)deg * 1e-7f * M_PI_180); // 1E-7 because deg is the value in ° * 1E7
298
  return ((double)deg * 1e-7f * M_PI_180); // 1E-7 because deg is the value in ° * 1E7
299
}
299
}
300
 
300
 
301
//------------------------------------------------------------
301
//------------------------------------------------------------
302
// transform the integer deg into float deg
302
// transform the integer deg into float deg
303
inline double DegFromGPS(s32 deg)
303
inline double DegFromGPS(s32 deg)
304
{
304
{
305
  return ((double)deg  * 1e-7f); // 1E-7 because deg is the value in ° * 1E7
305
  return ((double)deg  * 1e-7f); // 1E-7 because deg is the value in ° * 1E7
306
}
306
}
307
 
307
 
308
//------------------------------------------------------------
308
//------------------------------------------------------------
309
// calculate the deviation from the current position to the target position
309
// calculate the deviation from the current position to the target position
310
u8 GPS_CalculateDeviation(GPS_Pos_t * pCurrentPos, GPS_Pos_t * pTargetPos, GPS_Deviation_t* pDeviationFromTarget)
310
u8 GPS_CalculateDeviation(GPS_Pos_t * pCurrentPos, GPS_Pos_t * pTargetPos, GPS_Deviation_t* pDeviationFromTarget)
311
{
311
{
312
        double temp1, temp2;
312
        double temp1, temp2;
313
        // if given pointer is NULL
313
        // if given pointer is NULL
314
        if((pCurrentPos == NULL) || (pTargetPos == NULL)) goto baddata;
314
        if((pCurrentPos == NULL) || (pTargetPos == NULL)) goto baddata;
315
        // if positions are invalid
315
        // if positions are invalid
316
        if((pCurrentPos->Status == INVALID) || (pTargetPos->Status == INVALID)) goto baddata;
316
        if((pCurrentPos->Status == INVALID) || (pTargetPos->Status == INVALID)) goto baddata;
317
 
317
 
318
        // The deviation from the current to the target position along north and east direction is
318
        // The deviation from the current to the target position along north and east direction is
319
        // simple the lat/lon difference. To convert that angular deviation into an
319
        // simple the lat/lon difference. To convert that angular deviation into an
320
        // arc length the spherical projection has to be considered.
320
        // arc length the spherical projection has to be considered.
321
        // The mean earth radius is 6371km. Therfore the arc length per latitude degree
321
        // The mean earth radius is 6371km. Therfore the arc length per latitude degree
322
        // is always 6371km * 2 * Pi / 360deg =  111.2 km/deg.
322
        // is always 6371km * 2 * Pi / 360deg =  111.2 km/deg.
323
        // The arc length per longitude degree depends on the correspondig latitude and
323
        // The arc length per longitude degree depends on the correspondig latitude and
324
        // is 111.2km * cos(latitude).
324
        // is 111.2km * cos(latitude).
325
 
325
 
326
        // calculate the shortest longitude deviation from target
326
        // calculate the shortest longitude deviation from target
327
        temp1 = DegFromGPS(pCurrentPos->Longitude) - DegFromGPS(pTargetPos->Longitude);
327
        temp1 = DegFromGPS(pCurrentPos->Longitude) - DegFromGPS(pTargetPos->Longitude);
328
        // outside an angular difference of -180 deg ... +180 deg its shorter to go the other way around
328
        // outside an angular difference of -180 deg ... +180 deg its shorter to go the other way around
329
        // In our application we wont fly more than 20.000 km but along the date line this is important.
329
        // In our application we wont fly more than 20.000 km but along the date line this is important.
330
        if(temp1 > 180.0f) temp1 -= 360.0f;
330
        if(temp1 > 180.0f) temp1 -= 360.0f;
331
        else if (temp1 < -180.0f) temp1 += 360.0f;
331
        else if (temp1 < -180.0f) temp1 += 360.0f;
332
        temp1 *= cos((RadiansFromGPS(pTargetPos->Latitude) + RadiansFromGPS(pCurrentPos->Latitude))/2);
332
        temp1 *= cos((RadiansFromGPS(pTargetPos->Latitude) + RadiansFromGPS(pCurrentPos->Latitude))/2);
333
        // calculate latitude deviation from target
333
        // calculate latitude deviation from target
334
        // this is allways within -180 deg ... 180 deg
334
        // this is allways within -180 deg ... 180 deg
335
        temp2 = DegFromGPS(pCurrentPos->Latitude) - DegFromGPS(pTargetPos->Latitude);
335
        temp2 = DegFromGPS(pCurrentPos->Latitude) - DegFromGPS(pTargetPos->Latitude);
336
        // deviation from target position in cm
336
        // deviation from target position in cm
337
        // i.e. the distance to walk from the target in northern and eastern direction to reach the current position
337
        // i.e. the distance to walk from the target in northern and eastern direction to reach the current position
338
 
338
 
339
        pDeviationFromTarget->Status = INVALID;
339
        pDeviationFromTarget->Status = INVALID;
340
        pDeviationFromTarget->North = (s32)(11119492.7f * temp2);
340
        pDeviationFromTarget->North = (s32)(11119492.7f * temp2);
341
        pDeviationFromTarget->East  = (s32)(11119492.7f * temp1);
341
        pDeviationFromTarget->East  = (s32)(11119492.7f * temp1);
342
        // If the position deviation is small enough to neglect the earth curvature
342
        // If the position deviation is small enough to neglect the earth curvature
343
        // (this is for our application always fulfilled) the distance to target
343
        // (this is for our application always fulfilled) the distance to target
344
        // can be calculated by the pythagoras of north and east deviation.
344
        // can be calculated by the pythagoras of north and east deviation.
345
        pDeviationFromTarget->Distance = (s32)(11119492.7f * hypot(temp1, temp2));
345
        pDeviationFromTarget->Distance = (s32)(11119492.7f * hypot(temp1, temp2));
346
        if (pDeviationFromTarget->Distance == 0L) pDeviationFromTarget->Bearing = 0L;
346
        if (pDeviationFromTarget->Distance == 0L) pDeviationFromTarget->Bearing = 0L;
347
        else pDeviationFromTarget->Bearing = DirectionToTarget_N_E(temp2, temp1);
347
        else pDeviationFromTarget->Bearing = DirectionToTarget_N_E(temp2, temp1);
348
        pDeviationFromTarget->Status = NEWDATA;
348
        pDeviationFromTarget->Status = NEWDATA;
349
        return TRUE;
349
        return TRUE;
350
 
350
 
351
        baddata:
351
        baddata:
352
        pDeviationFromTarget->North     = 0L;
352
        pDeviationFromTarget->North     = 0L;
353
        pDeviationFromTarget->East              = 0L;
353
        pDeviationFromTarget->East              = 0L;
354
        pDeviationFromTarget->Distance  = 0L;
354
        pDeviationFromTarget->Distance  = 0L;
355
        pDeviationFromTarget->Bearing   = 0L;
355
        pDeviationFromTarget->Bearing   = 0L;
356
        pDeviationFromTarget->Status = INVALID;
356
        pDeviationFromTarget->Status = INVALID;
357
        return FALSE;
357
        return FALSE;
358
}
358
}
359
 
359
 
360
//------------------------------------------------------------
360
//------------------------------------------------------------
361
void GPS_Navigation(void)
361
void GPS_Navigation(void)
362
{
362
{
363
        static u32 beep_rythm;
363
        static u32 beep_rythm;
364
        static u32 GPSDataTimeout = 0;
364
        static u32 GPSDataTimeout = 0;
365
 
365
 
366
        // pointer to current target position
366
        // pointer to current target position
367
        static GPS_Pos_t * pTargetPositionOld = NULL;
367
        static GPS_Pos_t * pTargetPositionOld = NULL;
368
        static Waypoint_t* GPS_pWaypointOld = NULL;
368
        static Waypoint_t* GPS_pWaypointOld = NULL;
369
 
369
 
370
        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
370
        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
371
        static s32 OperatingRadiusOld = -1;
371
        static s32 OperatingRadiusOld = -1;
372
        static u8 WPArrived = FALSE;
372
        static u8 WPArrived = FALSE;
373
        static u32 WPTime = 0;
373
        static u32 WPTime = 0;
374
 
374
 
375
        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
375
        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
376
        //+ Check for new data from GPS-receiver
376
        //+ Check for new data from GPS-receiver
377
        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
377
        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
378
        switch(GPSData.Status)
378
        switch(GPSData.Status)
379
        {
379
        {
380
                case INVALID: // no gps data available
380
                case INVALID: // no gps data available
381
                        // do nothing
381
                        // do nothing
382
                        GPS_Parameter.PID_Limit = 0; // disables PID output
382
                        GPS_Parameter.PID_Limit = 0; // disables PID output
383
                        break;
383
                        break;
384
 
384
 
385
                case PROCESSED: // the current data have been allready processed
385
                case PROCESSED: // the current data have been allready processed
386
                        // if no new data are available within the timeout switch to invalid state.
386
                        // if no new data are available within the timeout switch to invalid state.
387
                        if(CheckDelay(GPSDataTimeout)) GPSData.Status = INVALID;
387
                        if(CheckDelay(GPSDataTimeout)) GPSData.Status = INVALID;
388
                        // wait for new gps data
388
                        // wait for new gps data
389
                        break;
389
                        break;
390
 
390
 
391
                case NEWDATA: // handle new gps data
391
                case NEWDATA: // handle new gps data
392
 
392
 
393
                // update GPS Parameter from FC-Data via SPI interface
393
                // update GPS Parameter from FC-Data via SPI interface
394
                GPS_UpdateParameter();
394
                GPS_UpdateParameter();
395
 
395
 
396
                        // wait maximum of 3 times the normal data update time before data timemout
396
                        // wait maximum of 3 times the normal data update time before data timemout
397
                        GPSDataTimeout = SetDelay(3 * GPS_UPDATETIME_MS);
397
                        GPSDataTimeout = SetDelay(3 * GPS_UPDATETIME_MS);
398
                        beep_rythm++;
398
                        beep_rythm++;
399
 
399
 
400
                        // debug
400
                        // debug
401
                        DebugOut.Analog[21] = (u16)GPSData.Speed_North;
401
                        DebugOut.Analog[21] = (u16)GPSData.Speed_North;
402
                        DebugOut.Analog[22] = (u16)GPSData.Speed_East;
402
                        DebugOut.Analog[22] = (u16)GPSData.Speed_East;
403
                        DebugOut.Analog[31] = (u16)GPSData.NumOfSats;
403
                        DebugOut.Analog[31] = (u16)GPSData.NumOfSats;
404
 
404
 
405
                        // If GPS signal condition is sufficient for a reliable position measurement
405
                        // If GPS signal condition is sufficient for a reliable position measurement
406
                        if(GPS_IsSignalOK())
406
                        if(GPS_IsSignalOK())
407
                        {
407
                        {
408
                                // update home deviation info
408
                                // update home deviation info
409
                                GPS_CalculateDeviation(&(GPSData.Position), &GPS_HomePosition, &CurrentHomeDeviation);
409
                                GPS_CalculateDeviation(&(GPSData.Position), &GPS_HomePosition, &CurrentHomeDeviation);
410
 
410
 
411
                                // if the MK is starting or the home position is invalid then store the home position
411
                                // if the MK is starting or the home position is invalid then store the home position
412
                                if((FC.MKFlags & MKFLAG_START) || (GPS_HomePosition.Status == INVALID))
412
                                if((FC.MKFlags & MKFLAG_START) || (GPS_HomePosition.Status == INVALID))
413
                                {       // try to update the home position from the current position
413
                                {       // try to update the home position from the current position
414
                                        if(GPS_CopyPosition(&(GPSData.Position), &GPS_HomePosition))
414
                                        if(GPS_CopyPosition(&(GPSData.Position), &GPS_HomePosition))
415
                                        {
415
                                        {
416
                                                BeepTime = 700; // beep on success
416
                                                BeepTime = 700; // beep on success
417
                                                GPS_CopyPosition(&GPS_HomePosition, &(NaviData.HomePosition));
417
                                                GPS_CopyPosition(&GPS_HomePosition, &(NaviData.HomePosition));
418
                                        }
418
                                        }
419
                                        GPS_pWaypoint = WPList_Begin(); // go to start of waypoint list, return NULL of the list is empty
419
                                        GPS_pWaypoint = WPList_Begin(); // go to start of waypoint list, return NULL of the list is empty
420
                                }
420
                                }
421
 
421
 
422
                                /* The selected flight mode influences the target position pointer and therefore the behavior */
422
                                /* The selected flight mode influences the target position pointer and therefore the behavior */
423
 
423
 
424
                                // check for current flight mode and set the target pointer GPS_pTargetPosition respectively
424
                                // check for current flight mode and set the target pointer GPS_pTargetPosition respectively
425
                                switch(GPS_Parameter.FlightMode)
425
                                switch(GPS_Parameter.FlightMode)
426
                                {
426
                                {
427
                                        // the GPS control is deactived
427
                                        // the GPS control is deactived
428
                                        case GPS_FLIGHT_MODE_FREE:
428
                                        case GPS_FLIGHT_MODE_FREE:
429
                                                NaviData.NCFlags &= ~(NC_FLAG_PH | NC_FLAG_CH);
429
                                                NaviData.NCFlags &= ~(NC_FLAG_PH | NC_FLAG_CH);
430
                                                NaviData.NCFlags |= NC_FLAG_FREE;
430
                                                NaviData.NCFlags |= NC_FLAG_FREE;
431
                                                GPS_Parameter.PID_Limit = 0; // disables PID output
431
                                                GPS_Parameter.PID_Limit = 0; // disables PID output
432
                                                // update hold position
432
                                                // update hold position
433
                                                GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition);
433
                                                GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition);
434
                                                // no target position
434
                                                // no target position
435
                                                GPS_pTargetPosition = NULL;
435
                                                GPS_pTargetPosition = NULL;
436
                                                break;
436
                                                break;
437
 
437
 
438
                                        // the GPS supports the position hold, if the pilot takes no action
438
                                        // the GPS supports the position hold, if the pilot takes no action
439
                                        case GPS_FLIGHT_MODE_AID:
439
                                        case GPS_FLIGHT_MODE_AID:
440
                                                NaviData.NCFlags &= ~(NC_FLAG_FREE | NC_FLAG_CH);
440
                                                NaviData.NCFlags &= ~(NC_FLAG_FREE | NC_FLAG_CH);
441
                                                NaviData.NCFlags |= NC_FLAG_PH;
441
                                                NaviData.NCFlags |= NC_FLAG_PH;
442
                                                // reset WPList to begin
442
                                                // reset WPList to begin
443
                                                GPS_pWaypoint = WPList_Begin();
443
                                                GPS_pWaypoint = WPList_Begin();
444
 
444
 
445
                                                if(GPS_IsManuallyControlled())
445
                                                if(GPS_IsManuallyControlled())
446
                                                {
446
                                                {
447
                                                        GPS_Parameter.PID_Limit = 0; // disables PID output, as long as the manual conrol is active
447
                                                        GPS_Parameter.PID_Limit = 0; // disables PID output, as long as the manual conrol is active
448
                                                    GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition);
448
                                                    GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition);
449
                                                        GPS_pTargetPosition = NULL;
449
                                                        GPS_pTargetPosition = NULL;
450
                                                }
450
                                                }
451
                                                else
451
                                                else
452
                                                {
452
                                                {
453
                                                        GPS_pTargetPosition = &GPS_HoldPosition;
453
                                                        GPS_pTargetPosition = &GPS_HoldPosition;
454
                                                }
454
                                                }
455
                                                break;
455
                                                break;
456
 
456
 
457
                                        // the GPS control is directed to a target position
457
                                        // the GPS control is directed to a target position
458
                                        // given by a waypoint or by the home position
458
                                        // given by a waypoint or by the home position
459
                                        case GPS_FLIGHT_MODE_WAYPOINT:
459
                                        case GPS_FLIGHT_MODE_WAYPOINT:
460
                                                NaviData.NCFlags &= ~(NC_FLAG_FREE | NC_FLAG_PH);
460
                                                NaviData.NCFlags &= ~(NC_FLAG_FREE | NC_FLAG_PH);
461
                                                NaviData.NCFlags |= NC_FLAG_CH;
461
                                                NaviData.NCFlags |= NC_FLAG_CH;
462
 
462
 
463
                                                if(GPS_IsManuallyControlled()) // the human pilot takes the action
463
                                                if(GPS_IsManuallyControlled()) // the human pilot takes the action
464
                                                {
464
                                                {
465
                                                        GPS_Parameter.PID_Limit = 0; // disables PID output, as long as the manual conrol is active
465
                                                        GPS_Parameter.PID_Limit = 0; // disables PID output, as long as the manual conrol is active
466
                                                    GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition);  // update hold position
466
                                                    GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition);  // update hold position
467
                                                        GPS_pTargetPosition = NULL;     // set target position invalid
467
                                                        GPS_pTargetPosition = NULL;     // set target position invalid
468
                                                }
468
                                                }
469
                                                else // no manual control  -> gps position hold active
469
                                                else // no manual control  -> gps position hold active
470
                                                {
470
                                                {
471
                                                        // waypoint trigger logic
471
                                                        // waypoint trigger logic
472
                                                        if(GPS_pWaypoint != NULL) // waypoint exist
472
                                                        if(GPS_pWaypoint != NULL) // waypoint exist
473
                                                        {
473
                                                        {
474
                                                                if(GPS_pWaypoint->Position.Status == INVALID) // should never happen
474
                                                                if(GPS_pWaypoint->Position.Status == INVALID) // should never happen
475
                                                                {
475
                                                                {
476
                                                                        GPS_pWaypoint = WPList_Next(); // goto to next WP
476
                                                                        GPS_pWaypoint = WPList_Next(); // goto to next WP
477
                                                                        WPArrived = FALSE;
477
                                                                        WPArrived = FALSE;
478
                                                                        BeepTime = 255;
478
                                                                        BeepTime = 255;
479
                                                                }
479
                                                                }
480
                                                                else // waypoint position is valid
480
                                                                else // waypoint position is valid
481
                                                                {
481
                                                                {
482
                                                                        // check if the pointer to the waypoint has been changed or the data have been updated
482
                                                                        // check if the pointer to the waypoint has been changed or the data have been updated
483
                                                                        if((GPS_pWaypoint != GPS_pWaypointOld) || (GPS_pWaypoint->Position.Status == NEWDATA))
483
                                                                        if((GPS_pWaypoint != GPS_pWaypointOld) || (GPS_pWaypoint->Position.Status == NEWDATA))
484
                                                                        {
484
                                                                        {
485
                                                                                GPS_pWaypointOld = GPS_pWaypoint;
485
                                                                                GPS_pWaypointOld = GPS_pWaypoint;
486
                                                                                // reset the arrived bit to break a pending HoldTime of the old WP
486
                                                                                // reset the arrived bit to break a pending HoldTime of the old WP
487
                                                                                WPArrived = FALSE;
487
                                                                                WPArrived = FALSE;
488
                                                                        }
488
                                                                        }
489
 
489
 
490
                                                                        if(CurrentTargetDeviation.Status != INVALID)
490
                                                                        if(CurrentTargetDeviation.Status != INVALID)
491
                                                                        {       // if the waypoint was not catched and the target area has been reached
491
                                                                        {       // if the waypoint was not catched and the target area has been reached
492
                                                                                if(!WPArrived && (CurrentTargetDeviation.Distance < (GPS_pWaypoint->ToleranceRadius * 100)))
492
                                                                                if(!WPArrived && (CurrentTargetDeviation.Distance < (GPS_pWaypoint->ToleranceRadius * 100)))
493
                                                                                {
493
                                                                                {
494
                                                                                        WPArrived = TRUE;
494
                                                                                        WPArrived = TRUE;
495
                                                                                        WPTime = SetDelay(GPS_pWaypoint->HoldTime * 1000); // set hold time stamp
495
                                                                                        WPTime = SetDelay(GPS_pWaypoint->HoldTime * 1000); // set hold time stamp
496
                                                                                }
496
                                                                                }
497
                                                                        }
497
                                                                        }
498
                                                                        // if WP has been reached once, wait hold time before trigger to next one
498
                                                                        // if WP has been reached once, wait hold time before trigger to next one
499
                                                                        if(WPArrived)
499
                                                                        if(WPArrived)
500
                                                                        {
500
                                                                        {
501
                                                                                /* ToDo: Adjust GPS_pWaypoint->Heading, GPS_pWaypoint->Event handling */
501
                                                                                /* ToDo: Adjust GPS_pWaypoint->Heading, GPS_pWaypoint->Event handling */
502
                                                                                if(CheckDelay(WPTime))
502
                                                                                if(CheckDelay(WPTime))
503
                                                                                {
503
                                                                                {
504
                                                                                        GPS_pWaypoint = WPList_Next(); // goto to next waypoint, return NULL if end of list has been reached
504
                                                                                        GPS_pWaypoint = WPList_Next(); // goto to next waypoint, return NULL if end of list has been reached
505
                                                                                        WPArrived = FALSE; // which is not arrived
505
                                                                                        WPArrived = FALSE; // which is not arrived
506
                                                                                }
506
                                                                                }
507
                                                                        } // EOF if(WPArrived)
507
                                                                        } // EOF if(WPArrived)
508
                                                                }
508
                                                                }
509
                                                        } // EOF waypoint trigger logic
509
                                                        } // EOF waypoint trigger logic
510
 
510
 
511
                                                        if(GPS_pWaypoint != NULL) // Waypoint exist
511
                                                        if(GPS_pWaypoint != NULL) // Waypoint exist
512
                                                        {
512
                                                        {
513
                                                                // update the hold position
513
                                                                // update the hold position
514
                                                                GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition);
514
                                                                GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition);
515
                                                                GPS_pTargetPosition = &(GPS_pWaypoint->Position);
515
                                                                GPS_pTargetPosition = &(GPS_pWaypoint->Position);
516
                                                        }
516
                                                        }
517
                                                        else // no waypoint info available, i.e. the WPList is empty or the end of the list has been reached
517
                                                        else // no waypoint info available, i.e. the WPList is empty or the end of the list has been reached
518
                                                        {
518
                                                        {
519
                                                                // fly back to home postion
519
                                                                // fly back to home postion
520
                                                                if(GPS_HomePosition.Status == INVALID)
520
                                                                if(GPS_HomePosition.Status == INVALID)
521
                                                                {
521
                                                                {
522
                                                                        GPS_pTargetPosition = &GPS_HoldPosition; // fall back to hold mode if home position is not available
522
                                                                        GPS_pTargetPosition = &GPS_HoldPosition; // fall back to hold mode if home position is not available
523
                                                                        BeepTime = 255; // beep to indicate missin home position
523
                                                                        BeepTime = 255; // beep to indicate missin home position
524
                                                                }
524
                                                                }
525
                                                                else // the home position is valid
525
                                                                else // the home position is valid
526
                                                                {
526
                                                                {
527
                                                                        // update the hold position
527
                                                                        // update the hold position
528
                                                                        GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition);
528
                                                                        GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition);
529
                                                                        // set target to home position
529
                                                                        // set target to home position
530
                                                                        GPS_pTargetPosition = &GPS_HomePosition;
530
                                                                        GPS_pTargetPosition = &GPS_HomePosition;
531
                                                                }
531
                                                                }
532
                                                        }
532
                                                        }
533
                                                } // EOF no manual control
533
                                                } // EOF no manual control
534
                                                break;
534
                                                break;
535
 
535
 
536
                                        case GPS_FLIGHT_MODE_UNDEF:
536
                                        case GPS_FLIGHT_MODE_UNDEF:
537
                                        default:
537
                                        default:
538
                                                GPS_Parameter.PID_Limit = 0; // disables PID output
538
                                                GPS_Parameter.PID_Limit = 0; // disables PID output
539
                                                // update hold position
539
                                                // update hold position
540
                                                GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition);
540
                                                GPS_CopyPosition(&(GPSData.Position), &GPS_HoldPosition);
541
                                                // no target position
541
                                                // no target position
542
                                                GPS_pTargetPosition = NULL;
542
                                                GPS_pTargetPosition = NULL;
543
                                                break;
543
                                                break;
544
 
544
 
545
                                }// EOF GPS Mode Handling
545
                                }// EOF GPS Mode Handling
546
 
546
 
547
 
547
 
548
                                /* Calculation of range target based on the real target */
548
                                /* Calculation of range target based on the real target */
549
 
549
 
550
                                // if no target position exist clear the ranged target position
550
                                // if no target position exist clear the ranged target position
551
                                if(GPS_pTargetPosition == NULL) GPS_ClearPosition(&RangedTargetPosition);
551
                                if(GPS_pTargetPosition == NULL) GPS_ClearPosition(&RangedTargetPosition);
552
                                else
552
                                else
553
                                {       // if the target position has been changed or the value has been updated or the OperatingRadius has changed
553
                                {       // if the target position has been changed or the value has been updated or the OperatingRadius has changed
554
                                        if((GPS_pTargetPosition != pTargetPositionOld)  || (GPS_pTargetPosition->Status == NEWDATA) || (GPS_Parameter.OperatingRadius != OperatingRadiusOld) )
554
                                        if((GPS_pTargetPosition != pTargetPositionOld)  || (GPS_pTargetPosition->Status == NEWDATA) || (GPS_Parameter.OperatingRadius != OperatingRadiusOld) )
555
                                        {
555
                                        {
556
                                                BeepTime = 255; // beep to indicate setting of a new target position
556
                                                BeepTime = 255; // beep to indicate setting of a new target position
557
                                                // calculate deviation of new target position from home position
557
                                                // calculate deviation of new target position from home position
558
                                                if(GPS_CalculateDeviation(GPS_pTargetPosition, &GPS_HomePosition, &TargetHomeDeviation))
558
                                                if(GPS_CalculateDeviation(GPS_pTargetPosition, &GPS_HomePosition, &TargetHomeDeviation))
559
                                                {
559
                                                {
560
                                                        // check distance from home position
560
                                                        // check distance from home position
561
                                                        if(TargetHomeDeviation.Distance > GPS_Parameter.OperatingRadius)
561
                                                        if(TargetHomeDeviation.Distance > GPS_Parameter.OperatingRadius)
562
                                                        {
562
                                                        {
563
                                                                //calculate ranged target position to be within the operation radius area
563
                                                                //calculate ranged target position to be within the operation radius area
564
                                                                NaviData.NCFlags |= NC_FLAG_RANGE_LIMIT;
564
                                                                NaviData.NCFlags |= NC_FLAG_RANGE_LIMIT;
565
 
565
 
566
                                                                TargetHomeDeviation.North *= GPS_Parameter.OperatingRadius;
566
                                                                TargetHomeDeviation.North *= GPS_Parameter.OperatingRadius;
567
                                                                TargetHomeDeviation.North /= TargetHomeDeviation.Distance;
567
                                                                TargetHomeDeviation.North /= TargetHomeDeviation.Distance;
568
                                                                TargetHomeDeviation.East *= GPS_Parameter.OperatingRadius;
568
                                                                TargetHomeDeviation.East *= GPS_Parameter.OperatingRadius;
569
                                                                TargetHomeDeviation.East /= TargetHomeDeviation.Distance;
569
                                                                TargetHomeDeviation.East /= TargetHomeDeviation.Distance;
570
                                                            TargetHomeDeviation.Distance = GPS_Parameter.OperatingRadius;
570
                                                            TargetHomeDeviation.Distance = GPS_Parameter.OperatingRadius;
571
 
571
 
572
                                                                RangedTargetPosition.Status = INVALID;
572
                                                                RangedTargetPosition.Status = INVALID;
573
                                                                RangedTargetPosition.Latitude = GPS_HomePosition.Latitude;
573
                                                                RangedTargetPosition.Latitude = GPS_HomePosition.Latitude;
574
                                                                RangedTargetPosition.Latitude += (s32)((float)TargetHomeDeviation.North / 1.11194927f);
574
                                                                RangedTargetPosition.Latitude += (s32)((float)TargetHomeDeviation.North / 1.11194927f);
575
                                                                RangedTargetPosition.Longitude = GPS_HomePosition.Longitude;
575
                                                                RangedTargetPosition.Longitude = GPS_HomePosition.Longitude;
576
                                                                RangedTargetPosition.Longitude += (s32)((float)TargetHomeDeviation.East / (1.11194927f * cos(RadiansFromGPS(GPS_HomePosition.Latitude))) );
576
                                                                RangedTargetPosition.Longitude += (s32)((float)TargetHomeDeviation.East / (1.11194927f * cos(RadiansFromGPS(GPS_HomePosition.Latitude))) );
577
                                                                RangedTargetPosition.Altitude = GPS_pTargetPosition->Altitude;
577
                                                                RangedTargetPosition.Altitude = GPS_pTargetPosition->Altitude;
578
                                                                RangedTargetPosition.Status = NEWDATA;
578
                                                                RangedTargetPosition.Status = NEWDATA;
579
                                                        }
579
                                                        }
580
                                                        else
580
                                                        else
581
                                                        {       // the target is located within the operation radius area
581
                                                        {       // the target is located within the operation radius area
582
                                                                // simple copy the loaction to the ranged target position
582
                                                                // simple copy the loaction to the ranged target position
583
                                                                GPS_CopyPosition(GPS_pTargetPosition, &RangedTargetPosition);
583
                                                                GPS_CopyPosition(GPS_pTargetPosition, &RangedTargetPosition);
584
                                                                NaviData.NCFlags &= ~NC_FLAG_RANGE_LIMIT;
584
                                                                NaviData.NCFlags &= ~NC_FLAG_RANGE_LIMIT;
585
                                                        }
585
                                                        }
586
                                                }
586
                                                }
587
                                                else
587
                                                else
588
                                                {       // deviation could not be determined
588
                                                {       // deviation could not be determined
589
                                                        GPS_ClearPosition(&RangedTargetPosition);
589
                                                        GPS_ClearPosition(&RangedTargetPosition);
590
                                                }
590
                                                }
591
                                                GPS_pTargetPosition->Status = PROCESSED;        // mark current target as processed!
591
                                                GPS_pTargetPosition->Status = PROCESSED;        // mark current target as processed!
592
                                        }
592
                                        }
593
                                }
593
                                }
594
                                OperatingRadiusOld = GPS_Parameter.OperatingRadius;
594
                                OperatingRadiusOld = GPS_Parameter.OperatingRadius;
595
                                // remember last target position pointer
595
                                // remember last target position pointer
596
                                pTargetPositionOld = GPS_pTargetPosition;
596
                                pTargetPositionOld = GPS_pTargetPosition;
597
 
597
 
598
                                /* Calculate position deviation from ranged target */
598
                                /* Calculate position deviation from ranged target */
599
 
599
 
600
                                // calculate deviation of current position to ranged target position in cm
600
                                // calculate deviation of current position to ranged target position in cm
601
                                if(GPS_CalculateDeviation(&(GPSData.Position), &RangedTargetPosition, &CurrentTargetDeviation))
601
                                if(GPS_CalculateDeviation(&(GPSData.Position), &RangedTargetPosition, &CurrentTargetDeviation))
602
                                {
602
                                {
603
                                        // implement your control code here based
603
                                        // implement your control code here based
604
                                        // in the info available in the CurrentTargetDeviation, GPSData and FromFlightCtrl.GyroHeading
604
                                        // in the info available in the CurrentTargetDeviation, GPSData and FromFlightCtrl.GyroHeading
605
                                        GPS_Stick.Nick = 0;
605
                                        GPS_Stick.Nick = 0;
606
                                        GPS_Stick.Roll = 0;
606
                                        GPS_Stick.Roll = 0;
607
                                        GPS_Stick.Yaw  = 0;
607
                                        GPS_Stick.Yaw  = 0;
608
                                }
608
                                }
609
                                else // deviation could not be calculated
609
                                else // deviation could not be calculated
610
                                {   // do nothing on gps sticks!
610
                                {   // do nothing on gps sticks!
611
                                        GPS_Neutral();
611
                                        GPS_Neutral();
612
                                }
612
                                }
613
 
613
 
614
                        }// eof if GPSSignal is OK
614
                        }// eof if GPSSignal is OK
615
                        else // GPSSignal not OK
615
                        else // GPSSignal not OK
616
                        {
616
                        {
617
                                GPS_Neutral();
617
                                GPS_Neutral();
618
                                // beep if signal is not sufficient
618
                                // beep if signal is not sufficient
619
                                if(GPS_Parameter.FlightMode != GPS_FLIGHT_MODE_FREE)
619
                                if(GPS_Parameter.FlightMode != GPS_FLIGHT_MODE_FREE)
620
                                {
620
                                {
621
                                        if(!(GPSData.Flags & FLAG_GPSFIXOK) && !(beep_rythm % 5)) BeepTime = 100;
621
                                        if(!(GPSData.Flags & FLAG_GPSFIXOK) && !(beep_rythm % 5)) BeepTime = 100;
622
                                        else if (GPSData.NumOfSats < GPS_Parameter.MinSat && !(beep_rythm % 5)) BeepTime = 10;
622
                                        else if (GPSData.NumOfSats < GPS_Parameter.MinSat && !(beep_rythm % 5)) BeepTime = 10;
623
                                }
623
                                }
624
                        }
624
                        }
625
                    GPSData.Status = PROCESSED; // mark as processed
625
                    GPSData.Status = PROCESSED; // mark as processed
626
                        break;
626
                        break;
627
        }
627
        }
628
 
628
 
629
        DebugOut.Analog[27] = (s16)CurrentTargetDeviation.North;
629
        DebugOut.Analog[27] = (s16)CurrentTargetDeviation.North;
630
        DebugOut.Analog[28] = (s16)CurrentTargetDeviation.East;
630
        DebugOut.Analog[28] = (s16)CurrentTargetDeviation.East;
631
        DebugOut.Analog[29] = GPS_Stick.Nick;
631
        DebugOut.Analog[29] = GPS_Stick.Nick;
632
        DebugOut.Analog[30] = GPS_Stick.Roll;
632
        DebugOut.Analog[30] = GPS_Stick.Roll;
633
 
633
 
634
        // update navi data, send back to ground station
634
        // update navi data, send back to ground station
635
        GPS_CopyPosition(&(GPSData.Position),   &(NaviData.CurrentPosition));
635
        GPS_CopyPosition(&(GPSData.Position),   &(NaviData.CurrentPosition));
636
        GPS_CopyPosition(&RangedTargetPosition, &(NaviData.TargetPosition));
636
        GPS_CopyPosition(&RangedTargetPosition, &(NaviData.TargetPosition));
637
        GPS_CopyPosition(&GPS_HomePosition,     &(NaviData.HomePosition));
637
        GPS_CopyPosition(&GPS_HomePosition,     &(NaviData.HomePosition));
638
        NaviData.SatsInUse = GPSData.NumOfSats;
638
        NaviData.SatsInUse = GPSData.NumOfSats;
639
        NaviData.TargetPositionDeviation.Distance = CurrentTargetDeviation.Distance;
639
        NaviData.TargetPositionDeviation.Distance = CurrentTargetDeviation.Distance;
640
        NaviData.TargetPositionDeviation.Bearing  = CurrentTargetDeviation.Bearing;
640
        NaviData.TargetPositionDeviation.Bearing  = CurrentTargetDeviation.Bearing;
641
        NaviData.HomePositionDeviation.Distance   = CurrentHomeDeviation.Distance;
641
        NaviData.HomePositionDeviation.Distance   = CurrentHomeDeviation.Distance;
642
        NaviData.HomePositionDeviation.Bearing    = CurrentHomeDeviation.Bearing;
642
        NaviData.HomePositionDeviation.Bearing    = CurrentHomeDeviation.Bearing;
643
        NaviData.UBat = FC.UBat;
643
        NaviData.UBat = FC.UBat;
644
        NaviData.GroundSpeed = (u16)GPSData.Speed_Ground;
644
        NaviData.GroundSpeed = (u16)GPSData.Speed_Ground;
645
        NaviData.Heading = (s16)(GPSData.Heading/100000L);
645
        NaviData.Heading = (s16)(GPSData.Heading/100000L);
646
        NaviData.CompassHeading = (s16)FromFlightCtrl.GyroHeading/10; // in deg
646
        NaviData.CompassHeading = (s16)FromFlightCtrl.GyroHeading/10; // in deg
647
        NaviData.AngleNick = FromFlightCtrl.AngleNick / 10;        // in deg
647
        NaviData.AngleNick = FromFlightCtrl.AngleNick / 10;        // in deg
648
        NaviData.AngleRoll = FromFlightCtrl.AngleRoll / 10;        // in deg
648
        NaviData.AngleRoll = FromFlightCtrl.AngleRoll / 10;        // in deg
649
        NaviData.RC_Quality = (u8) FC.RC_Quality;
649
        NaviData.RC_Quality = (u8) FC.RC_Quality;
650
        NaviData.MKFlags = (u8)FC.MKFlags;
650
        NaviData.MKFlags = (u8)FC.MKFlags;
651
        NaviData.OperatingRadius = Parameter.NaviOperatingRadius;
651
        NaviData.OperatingRadius = Parameter.NaviOperatingRadius;
652
 
652
 
653
        //+++++++++++++++++++++++++++++++++++++++++++++++++++
653
        //+++++++++++++++++++++++++++++++++++++++++++++++++++
654
  return;
654
  return;
655
}
655
}
656
 
656
 
657
 
657