Subversion Repositories FlightCtrl

Rev

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

Rev 1796 Rev 1805
1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2
// + Copyright (c) 04.2007 Holger Buss
2
// + Copyright (c) 04.2007 Holger Buss
3
// + Nur für den privaten Gebrauch
3
// + Nur für den privaten Gebrauch
4
// + www.MikroKopter.com
4
// + www.MikroKopter.com
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
6
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
7
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
7
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
8
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
8
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
9
// + bzgl. der Nutzungsbedingungen aufzunehmen.
9
// + bzgl. der Nutzungsbedingungen aufzunehmen.
10
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
10
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
11
// + Verkauf von Luftbildaufnahmen, usw.
11
// + Verkauf von Luftbildaufnahmen, usw.
12
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
12
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
13
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
14
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
14
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
15
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
15
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
16
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
16
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
17
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
17
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
18
// + eindeutig als Ursprung verlinkt werden
18
// + eindeutig als Ursprung verlinkt werden
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
20
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
20
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
21
// + Benutzung auf eigene Gefahr
21
// + Benutzung auf eigene Gefahr
22
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
22
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
24
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
25
// + mit unserer Zustimmung zulässig
25
// + mit unserer Zustimmung zulässig
26
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
26
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
27
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
27
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
28
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
28
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
29
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
30
// + this list of conditions and the following disclaimer.
30
// + this list of conditions and the following disclaimer.
31
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
31
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
32
// +     from this software without specific prior written permission.
32
// +     from this software without specific prior written permission.
33
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
33
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
34
// +     for non-commercial use (directly or indirectly)
34
// +     for non-commercial use (directly or indirectly)
35
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
35
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
36
// +     with our written permission
36
// +     with our written permission
37
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
37
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
38
// +     clearly linked as origin
38
// +     clearly linked as origin
39
// +   * porting to systems other than hardware from www.mikrokopter.de is not allowed
39
// +   * porting to systems other than hardware from www.mikrokopter.de is not allowed
40
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
40
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
41
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
42
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
43
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
44
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
44
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
45
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
45
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
46
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
46
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
47
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
47
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
48
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49
// +  POSSIBILITY OF SUCH DAMAGE.
49
// +  POSSIBILITY OF SUCH DAMAGE.
50
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
50
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
51
#include <inttypes.h>
51
#include <inttypes.h>
52
#include <stdlib.h>
52
#include <stdlib.h>
53
//#include "mymath.h"
53
//#include "mymath.h"
54
#include "timer0.h"
54
#include "timer0.h"
55
#include "uart0.h"
55
#include "uart0.h"
56
#include "rc.h"
56
#include "rc.h"
57
#include "eeprom.h"
57
#include "eeprom.h"
58
 
58
 
59
typedef enum
-
 
60
  {
59
typedef enum {
61
    GPS_FLIGHT_MODE_UNDEF,
60
        GPS_FLIGHT_MODE_UNDEF,
62
    GPS_FLIGHT_MODE_FREE,
61
        GPS_FLIGHT_MODE_FREE,
63
    GPS_FLIGHT_MODE_AID,
62
        GPS_FLIGHT_MODE_AID,
64
    GPS_FLIGHT_MODE_HOME,
63
        GPS_FLIGHT_MODE_HOME,
65
  } FlightMode_t;
64
} FlightMode_t;
66
 
65
 
67
#define GPS_POSINTEGRAL_LIMIT 32000
66
#define GPS_POSINTEGRAL_LIMIT 32000
68
#define GPS_STICK_LIMIT         45              // limit of gps stick control to avoid critical flight attitudes
67
#define GPS_STICK_LIMIT         45              // limit of gps stick control to avoid critical flight attitudes
69
#define GPS_P_LIMIT                     25
68
#define GPS_P_LIMIT                     25
70
 
-
 
71
 
69
 
72
typedef struct
-
 
73
{
70
typedef struct {
74
  int32_t Longitude;
71
        int32_t Longitude;
75
  int32_t Latitude;
72
        int32_t Latitude;
76
  int32_t Altitude;
73
        int32_t Altitude;
77
  Status_t Status;
74
        Status_t Status;
78
} GPS_Pos_t;
75
} GPS_Pos_t;
79
 
76
 
80
// GPS coordinates for hold position
77
// GPS coordinates for hold position
81
GPS_Pos_t HoldPosition  = {0,0,0,INVALID};
78
GPS_Pos_t HoldPosition = { 0, 0, 0, INVALID };
82
// GPS coordinates for home position
79
// GPS coordinates for home position
83
GPS_Pos_t HomePosition  = {0,0,0,INVALID};
80
GPS_Pos_t HomePosition = { 0, 0, 0, INVALID };
84
// the current flight mode
81
// the current flight mode
85
FlightMode_t FlightMode = GPS_FLIGHT_MODE_UNDEF;
82
FlightMode_t FlightMode = GPS_FLIGHT_MODE_UNDEF;
86
 
-
 
87
 
83
 
88
// ---------------------------------------------------------------------------------
84
// ---------------------------------------------------------------------------------
89
void GPS_UpdateParameter(void) {
85
void GPS_UpdateParameter(void) {
90
  static FlightMode_t FlightModeOld = GPS_FLIGHT_MODE_UNDEF;
86
        static FlightMode_t FlightModeOld = GPS_FLIGHT_MODE_UNDEF;
91
 
87
 
92
  if((RC_Quality < 100) || (MKFlags & MKFLAG_EMERGENCY_LANDING)) {
88
        if ((RC_Quality < 100) || (MKFlags & MKFLAG_EMERGENCY_LANDING)) {
93
    FlightMode = GPS_FLIGHT_MODE_FREE;
89
                FlightMode = GPS_FLIGHT_MODE_FREE;
94
  } else {
90
        } else {
95
    if     (dynamicParams.NaviGpsModeControl <  50) FlightMode = GPS_FLIGHT_MODE_AID;
91
                if (dynamicParams.NaviGpsModeControl < 50)
-
 
92
                        FlightMode = GPS_FLIGHT_MODE_AID;
96
    else if(dynamicParams.NaviGpsModeControl < 180) FlightMode = GPS_FLIGHT_MODE_FREE;
93
                else if (dynamicParams.NaviGpsModeControl < 180)
-
 
94
                        FlightMode = GPS_FLIGHT_MODE_FREE;
-
 
95
                else
97
    else                                          FlightMode = GPS_FLIGHT_MODE_HOME;
96
                        FlightMode = GPS_FLIGHT_MODE_HOME;
98
  }
97
        }
99
 
98
 
100
  if (FlightMode != FlightModeOld) {
99
        if (FlightMode != FlightModeOld) {
101
    BeepTime = 100;
100
                BeepTime = 100;
102
  }
101
        }
103
  FlightModeOld = FlightMode;
102
        FlightModeOld = FlightMode;
104
}
103
}
105
 
104
 
106
// ---------------------------------------------------------------------------------
105
// ---------------------------------------------------------------------------------
107
// This function defines a good GPS signal condition
106
// This function defines a good GPS signal condition
108
uint8_t GPS_IsSignalOK(void) {
107
uint8_t GPS_IsSignalOK(void) {
109
  static uint8_t GPSFix = 0;
108
        static uint8_t GPSFix = 0;
110
  if( (GPSInfo.status != INVALID)  && (GPSInfo.satfix == SATFIX_3D) && (GPSInfo.flags & FLAG_GPSFIXOK) && ((GPSInfo.satnum >= staticParams.NaviGpsMinSat) || GPSFix)) {
109
        if ((GPSInfo.status != INVALID) && (GPSInfo.satfix == SATFIX_3D)
-
 
110
                        && (GPSInfo.flags & FLAG_GPSFIXOK) && ((GPSInfo.satnum
-
 
111
                        >= staticParams.NaviGpsMinSat) || GPSFix)) {
111
    GPSFix = 1;
112
                GPSFix = 1;
112
    return 1;
113
                return 1;
113
  }
114
        } else
114
  else return (0);
115
                return (0);
115
}
116
}
116
 
117
 
117
// ---------------------------------------------------------------------------------
118
// ---------------------------------------------------------------------------------
118
// rescale xy-vector length to  limit
119
// rescale xy-vector length to  limit
119
uint8_t GPS_LimitXY(int32_t *x, int32_t *y, int32_t limit) {
120
uint8_t GPS_LimitXY(int32_t *x, int32_t *y, int32_t limit) {
120
  uint8_t retval = 0;
121
        uint8_t retval = 0;
121
  int32_t len;
122
        int32_t len;
122
  len = (int32_t)c_sqrt(*x * *x + *y * *y);
123
        len = (int32_t) c_sqrt(*x * *x + *y * *y);
123
  if (len > limit) {
124
        if (len > limit) {
124
    // normalize control vector components to the limit
125
                // normalize control vector components to the limit
125
    *x  = (*x  * limit) / len;
126
                *x = (*x * limit) / len;
126
    *y  = (*y  * limit) / len;
127
                *y = (*y * limit) / len;
127
    retval = 1;
128
                retval = 1;
128
  }
129
        }
129
  return(retval);
130
        return (retval);
130
}
131
}
131
 
132
 
132
// checks nick and roll sticks for manual control
133
// checks nick and roll sticks for manual control
133
uint8_t GPS_IsManualControlled(void) {
134
uint8_t GPS_IsManualControlled(void) {
134
  if ((abs(PPM_in[staticParams.ChannelAssignment[CH_NICK]]) < staticParams.NaviStickThreshold) && (abs(PPM_in[staticParams.ChannelAssignment[CH_ROLL]]) < staticParams.NaviStickThreshold)) return 0;
135
        if ((abs(PPM_in[staticParams.ChannelAssignment[CH_NICK]])
-
 
136
                        < staticParams.NaviStickThreshold)
-
 
137
                        && (abs(PPM_in[staticParams.ChannelAssignment[CH_ROLL]])
-
 
138
                                        < staticParams.NaviStickThreshold))
-
 
139
                return 0;
-
 
140
        else
135
  else return 1;
141
                return 1;
136
}
142
}
137
 
143
 
138
// set given position to current gps position
144
// set given position to current gps position
139
uint8_t GPS_SetCurrPosition(GPS_Pos_t * pGPSPos) {
145
uint8_t GPS_SetCurrPosition(GPS_Pos_t * pGPSPos) {
140
  uint8_t retval = 0;
146
        uint8_t retval = 0;
-
 
147
        if (pGPSPos == NULL)
141
  if(pGPSPos == NULL) return(retval);   // bad pointer
148
                return (retval); // bad pointer
142
 
149
 
143
  if(GPS_IsSignalOK()) {        // is GPS signal condition is fine
150
        if (GPS_IsSignalOK()) { // is GPS signal condition is fine
144
    pGPSPos->Longitude  = GPSInfo.longitude;
151
                pGPSPos->Longitude = GPSInfo.longitude;
145
    pGPSPos->Latitude   = GPSInfo.latitude;
152
                pGPSPos->Latitude = GPSInfo.latitude;
146
    pGPSPos->Altitude   = GPSInfo.altitude;
153
                pGPSPos->Altitude = GPSInfo.altitude;
147
    pGPSPos->Status     = NEWDATA;
154
                pGPSPos->Status = NEWDATA;
148
    retval = 1;
155
                retval = 1;
149
  } else {      // bad GPS signal condition
156
        } else { // bad GPS signal condition
150
    pGPSPos->Status = INVALID;
157
                pGPSPos->Status = INVALID;
151
    retval = 0;
158
                retval = 0;
152
  }
159
        }
153
  return(retval);
160
        return (retval);
154
}
161
}
155
 
162
 
156
// clear position
163
// clear position
157
uint8_t GPS_ClearPosition(GPS_Pos_t * pGPSPos) {
164
uint8_t GPS_ClearPosition(GPS_Pos_t * pGPSPos) {
158
  uint8_t retval = 0;
165
        uint8_t retval = 0;
159
  if(pGPSPos == NULL)
166
        if (pGPSPos == NULL)
160
    return retval;      // bad pointer
167
                return retval; // bad pointer
161
  else {
168
        else {
162
    pGPSPos->Longitude  = 0;
169
                pGPSPos->Longitude = 0;
163
    pGPSPos->Latitude   = 0;
170
                pGPSPos->Latitude = 0;
164
    pGPSPos->Altitude   = 0;
171
                pGPSPos->Altitude = 0;
165
    pGPSPos->Status     = INVALID;
172
                pGPSPos->Status = INVALID;
166
    retval = 1;
173
                retval = 1;
167
  }
174
        }
168
  return (retval);
175
        return (retval);
169
}
176
}
170
 
177
 
171
// disable GPS control sticks
178
// disable GPS control sticks
172
void GPS_Neutral(void) {
179
void GPS_Neutral(void) {
173
  GPSStickNick = 0;
180
        GPSStickNick = 0;
174
  GPSStickRoll = 0;
181
        GPSStickRoll = 0;
175
}
182
}
176
 
183
 
177
// calculates the GPS control stick values from the deviation to target position
184
// calculates the GPS control stick values from the deviation to target position
178
// if the pointer to the target positin is NULL or is the target position invalid
185
// if the pointer to the target positin is NULL or is the target position invalid
179
// then the P part of the controller is deactivated.
186
// then the P part of the controller is deactivated.
180
void GPS_PIDController(GPS_Pos_t *pTargetPos) {
187
void GPS_PIDController(GPS_Pos_t *pTargetPos) {
181
  static int32_t PID_Nick, PID_Roll;
188
        static int32_t PID_Nick, PID_Roll;
182
  int32_t coscompass, sincompass;
189
        int32_t coscompass, sincompass;
183
  int32_t GPSPosDev_North, GPSPosDev_East; // Position deviation in cm
190
        int32_t GPSPosDev_North, GPSPosDev_East; // Position deviation in cm
184
  int32_t P_North = 0, D_North = 0, P_East = 0, D_East = 0, I_North = 0, I_East = 0;
191
        int32_t P_North = 0, D_North = 0, P_East = 0, D_East = 0, I_North = 0,
-
 
192
                        I_East = 0;
185
  int32_t PID_North = 0, PID_East = 0;
193
        int32_t PID_North = 0, PID_East = 0;
186
  static int32_t cos_target_latitude = 1;
194
        static int32_t cos_target_latitude = 1;
187
  static int32_t GPSPosDevIntegral_North = 0, GPSPosDevIntegral_East = 0;
195
        static int32_t GPSPosDevIntegral_North = 0, GPSPosDevIntegral_East = 0;
188
  static GPS_Pos_t *pLastTargetPos = 0;
196
        static GPS_Pos_t *pLastTargetPos = 0;
189
 
197
 
190
  // if GPS data and Compass are ok
198
        // if GPS data and Compass are ok
191
  if( GPS_IsSignalOK() && (CompassHeading >= 0)) {
199
        if (GPS_IsSignalOK() && (CompassHeading >= 0)) {
192
    if(pTargetPos != NULL) // if there is a target position
200
                if (pTargetPos != NULL) // if there is a target position
193
      {
201
                {
194
        if(pTargetPos->Status != INVALID) // and the position data are valid
202
                        if (pTargetPos->Status != INVALID) // and the position data are valid
195
          {
203
                        {
196
            // if the target data are updated or the target pointer has changed
204
                                // if the target data are updated or the target pointer has changed
197
            if ((pTargetPos->Status != PROCESSED) || (pTargetPos != pLastTargetPos) )
205
                                if ((pTargetPos->Status != PROCESSED) || (pTargetPos
198
              {
206
                                                != pLastTargetPos)) {
199
                // reset error integral
207
                                        // reset error integral
200
                GPSPosDevIntegral_North = 0;
208
                                        GPSPosDevIntegral_North = 0;
201
                GPSPosDevIntegral_East = 0;
209
                                        GPSPosDevIntegral_East = 0;
202
                // recalculate latitude projection
210
                                        // recalculate latitude projection
-
 
211
                                        cos_target_latitude = (int32_t) c_cos_8192(
203
                cos_target_latitude = (int32_t)c_cos_8192((int16_t)(pTargetPos->Latitude/10000000L));
212
                                                        (int16_t) (pTargetPos->Latitude / 10000000L));
204
                // remember last target pointer
213
                                        // remember last target pointer
205
                pLastTargetPos = pTargetPos;
214
                                        pLastTargetPos = pTargetPos;
206
                // mark data as processed
215
                                        // mark data as processed
207
                pTargetPos->Status = PROCESSED;
216
                                        pTargetPos->Status = PROCESSED;
208
              }
217
                                }
209
            // calculate position deviation from latitude and longitude differences
218
                                // calculate position deviation from latitude and longitude differences
210
            GPSPosDev_North = (GPSInfo.latitude - pTargetPos->Latitude); // to calculate real cm we would need *111/100 additionally
219
                                GPSPosDev_North = (GPSInfo.latitude - pTargetPos->Latitude); // to calculate real cm we would need *111/100 additionally
211
            GPSPosDev_East  = (GPSInfo.longitude - pTargetPos->Longitude); // to calculate real cm we would need *111/100 additionally
220
                                GPSPosDev_East = (GPSInfo.longitude - pTargetPos->Longitude); // to calculate real cm we would need *111/100 additionally
212
            // calculate latitude projection
221
                                // calculate latitude projection
213
            GPSPosDev_East  *= cos_target_latitude;
222
                                GPSPosDev_East *= cos_target_latitude;
214
            GPSPosDev_East /= 8192;
223
                                GPSPosDev_East /= 8192;
215
          }
-
 
216
        else // no valid target position available
224
                        } else {// no valid target position available
217
          {
-
 
218
            // reset error
225
                                // reset error
219
            GPSPosDev_North = 0;
226
                                GPSPosDev_North = 0;
220
            GPSPosDev_East = 0;
227
                                GPSPosDev_East = 0;
221
            // reset error integral
228
                                // reset error integral
222
            GPSPosDevIntegral_North = 0;
229
                                GPSPosDevIntegral_North = 0;
223
            GPSPosDevIntegral_East = 0;
230
                                GPSPosDevIntegral_East = 0;
224
          }
231
                        }
225
      }
-
 
226
    else // no target position available
232
                } else { // no target position available
227
      {
-
 
228
        // reset error
233
                        // reset error
229
        GPSPosDev_North = 0;
234
                        GPSPosDev_North = 0;
230
        GPSPosDev_East = 0;
235
                        GPSPosDev_East = 0;
231
        // reset error integral
236
                        // reset error integral
232
        GPSPosDevIntegral_North = 0;
237
                        GPSPosDevIntegral_North = 0;
233
        GPSPosDevIntegral_East = 0;
238
                        GPSPosDevIntegral_East = 0;
234
      }
239
                }
235
   
240
 
236
    //Calculate PID-components of the controller
241
                //Calculate PID-components of the controller
237
   
242
 
238
    // D-Part
243
                // D-Part
239
    D_North = ((int32_t)dynamicParams.NaviGpsD * GPSInfo.velnorth)/512;
244
                D_North = ((int32_t) dynamicParams.NaviGpsD * GPSInfo.velnorth) / 512;
240
    D_East =  ((int32_t)dynamicParams.NaviGpsD * GPSInfo.veleast)/512;
245
                D_East = ((int32_t) dynamicParams.NaviGpsD * GPSInfo.veleast) / 512;
241
   
246
 
242
    // P-Part
247
                // P-Part
243
    P_North = ((int32_t)dynamicParams.NaviGpsP * GPSPosDev_North)/2048;
248
                P_North = ((int32_t) dynamicParams.NaviGpsP * GPSPosDev_North) / 2048;
244
    P_East =  ((int32_t)dynamicParams.NaviGpsP * GPSPosDev_East)/2048;
249
                P_East = ((int32_t) dynamicParams.NaviGpsP * GPSPosDev_East) / 2048;
245
   
250
 
246
    // I-Part
251
                // I-Part
247
    I_North = ((int32_t)dynamicParams.NaviGpsI * GPSPosDevIntegral_North)/8192;
252
                I_North = ((int32_t) dynamicParams.NaviGpsI * GPSPosDevIntegral_North)
-
 
253
                                / 8192;
248
    I_East =  ((int32_t)dynamicParams.NaviGpsI * GPSPosDevIntegral_East)/8192;
254
                I_East = ((int32_t) dynamicParams.NaviGpsI * GPSPosDevIntegral_East)
-
 
255
                                / 8192;
249
   
256
 
250
    // combine P & I
257
                // combine P & I
251
    PID_North = P_North + I_North;
258
                PID_North = P_North + I_North;
252
    PID_East  = P_East  + I_East;
259
                PID_East = P_East + I_East;
253
    if(!GPS_LimitXY(&PID_North, &PID_East, GPS_P_LIMIT))
260
                if (!GPS_LimitXY(&PID_North, &PID_East, GPS_P_LIMIT)) {
254
      {
-
 
255
        GPSPosDevIntegral_North += GPSPosDev_North/16;
261
                        GPSPosDevIntegral_North += GPSPosDev_North / 16;
256
        GPSPosDevIntegral_East  += GPSPosDev_East/16;
262
                        GPSPosDevIntegral_East += GPSPosDev_East / 16;
257
        GPS_LimitXY(&GPSPosDevIntegral_North, &GPSPosDevIntegral_East, GPS_POSINTEGRAL_LIMIT);
263
                        GPS_LimitXY(&GPSPosDevIntegral_North, &GPSPosDevIntegral_East,
-
 
264
                                        GPS_POSINTEGRAL_LIMIT);
258
      }
265
                }
259
   
266
 
260
    // combine PI- and D-Part
267
                // combine PI- and D-Part
261
    PID_North += D_North;
268
                PID_North += D_North;
262
    PID_East  += D_East;
269
                PID_East += D_East;
263
   
270
 
264
    // scale combination with gain.
271
                // scale combination with gain.
265
    PID_North = (PID_North * (int32_t)dynamicParams.NaviGpsGain) / 100;
272
                PID_North = (PID_North * (int32_t) dynamicParams.NaviGpsGain) / 100;
266
    PID_East  = (PID_East  * (int32_t)dynamicParams.NaviGpsGain) / 100;
273
                PID_East = (PID_East * (int32_t) dynamicParams.NaviGpsGain) / 100;
267
 
274
 
268
    // GPS to nick and roll settings
275
                // GPS to nick and roll settings
269
    // A positive nick angle moves head downwards (flying forward).
276
                // A positive nick angle moves head downwards (flying forward).
270
    // A positive roll angle tilts left side downwards (flying left).
277
                // A positive roll angle tilts left side downwards (flying left).
271
    // If compass heading is 0 the head of the copter is in north direction.
278
                // If compass heading is 0 the head of the copter is in north direction.
272
    // A positive nick angle will fly to north and a positive roll angle will fly to west.
279
                // A positive nick angle will fly to north and a positive roll angle will fly to west.
273
    // In case of a positive north deviation/velocity the
280
                // In case of a positive north deviation/velocity the
274
    // copter should fly to south (negative nick).
281
                // copter should fly to south (negative nick).
275
    // In case of a positive east position deviation and a positive east velocity the
282
                // In case of a positive east position deviation and a positive east velocity the
276
    // copter should fly to west (positive roll).
283
                // copter should fly to west (positive roll).
277
    // The influence of the GPSStickNick and GPSStickRoll variable is contrarily to the stick values
284
                // The influence of the GPSStickNick and GPSStickRoll variable is contrarily to the stick values
278
    // in the flight.c. Therefore a positive north deviation/velocity should result in a positive
285
                // in the flight.c. Therefore a positive north deviation/velocity should result in a positive
279
    // GPSStickNick and a positive east deviation/velocity should result in a negative GPSStickRoll.
286
                // GPSStickNick and a positive east deviation/velocity should result in a negative GPSStickRoll.
280
   
287
 
281
    coscompass = (int32_t)c_cos_8192(YawGyroHeading / GYRO_DEG_FACTOR);
288
                coscompass = (int32_t) c_cos_8192(YawGyroHeading / GYRO_DEG_FACTOR);
282
    sincompass = (int32_t)c_sin_8192(YawGyroHeading / GYRO_DEG_FACTOR);
289
                sincompass = (int32_t) c_sin_8192(YawGyroHeading / GYRO_DEG_FACTOR);
283
    PID_Nick =   (coscompass * PID_North + sincompass * PID_East) / 8192;
290
                PID_Nick = (coscompass * PID_North + sincompass * PID_East) / 8192;
284
    PID_Roll  =  (sincompass * PID_North - coscompass * PID_East) / 8192;
291
                PID_Roll = (sincompass * PID_North - coscompass * PID_East) / 8192;
285
   
292
 
286
    // limit resulting GPS control vector
293
                // limit resulting GPS control vector
287
    GPS_LimitXY(&PID_Nick, &PID_Roll, GPS_STICK_LIMIT);
294
                GPS_LimitXY(&PID_Nick, &PID_Roll, GPS_STICK_LIMIT);
288
   
295
 
289
    GPSStickNick = (int16_t)PID_Nick;
296
                GPSStickNick = (int16_t) PID_Nick;
290
    GPSStickRoll = (int16_t)PID_Roll;
297
                GPSStickRoll = (int16_t) PID_Roll;
291
  }
-
 
292
  else // invalid GPS data or bad compass reading
298
        } else { // invalid GPS data or bad compass reading
293
    {
-
 
294
      GPS_Neutral(); // do nothing
299
                GPS_Neutral(); // do nothing
295
      // reset error integral
300
                // reset error integral
296
      GPSPosDevIntegral_North = 0;
301
                GPSPosDevIntegral_North = 0;
297
      GPSPosDevIntegral_East = 0;
302
                GPSPosDevIntegral_East = 0;
298
    }
303
        }
299
}
304
}
300
 
305
 
301
void GPS_Main(void) {
306
void GPS_Main(void) {
302
  static uint8_t GPS_P_Delay = 0;
307
        static uint8_t GPS_P_Delay = 0;
303
  static uint16_t beep_rythm = 0;
308
        static uint16_t beep_rythm = 0;
304
 
309
 
305
  GPS_UpdateParameter();
310
        GPS_UpdateParameter();
306
 
311
 
307
  // store home position if start of flight flag is set
312
        // store home position if start of flight flag is set
308
  if(MKFlags & MKFLAG_CALIBRATE) {
313
        if (MKFlags & MKFLAG_CALIBRATE) {
309
    if(GPS_SetCurrPosition(&HomePosition)) BeepTime = 700;
314
                if (GPS_SetCurrPosition(&HomePosition))
-
 
315
                        BeepTime = 700;
310
  }
316
        }
311
 
317
 
312
  switch(GPSInfo.status) {
318
        switch (GPSInfo.status) {
313
  case INVALID:  // invalid gps data
319
        case INVALID: // invalid gps data
314
    GPS_Neutral();
320
                GPS_Neutral();
315
    if(FlightMode != GPS_FLIGHT_MODE_FREE) {
321
                if (FlightMode != GPS_FLIGHT_MODE_FREE) {
316
      BeepTime = 100; // beep if signal is neccesary
322
                        BeepTime = 100; // beep if signal is neccesary
317
    }
323
                }
318
    break;
324
                break;
319
  case PROCESSED: // if gps data are already processed do nothing
325
        case PROCESSED: // if gps data are already processed do nothing
320
    // downcount timeout
326
                // downcount timeout
321
    if(GPSTimeout) GPSTimeout--;
327
                if (GPSTimeout)
-
 
328
                        GPSTimeout--;
322
    // if no new data arrived within timeout set current data invalid
329
                // if no new data arrived within timeout set current data invalid
323
    // and therefore disable GPS
330
                // and therefore disable GPS
324
    else
331
                else {
325
      {
-
 
326
        GPS_Neutral();
332
                        GPS_Neutral();
327
          GPSInfo.status = INVALID;
333
                        GPSInfo.status = INVALID;
328
      }
334
                }
329
    break;
335
                break;
330
  case NEWDATA: // new valid data from gps device
336
        case NEWDATA: // new valid data from gps device
331
    // if the gps data quality is good
337
                // if the gps data quality is good
332
    beep_rythm++;
338
                beep_rythm++;
333
   
-
 
334
    if (GPS_IsSignalOK())
339
                if (GPS_IsSignalOK()) {
335
      {
-
 
336
        switch(FlightMode) // check what's to do
340
                        switch (FlightMode) { // check what's to do
337
          {
-
 
338
          case GPS_FLIGHT_MODE_FREE:
341
                        case GPS_FLIGHT_MODE_FREE:
339
            // update hold position to current gps position
342
                                // update hold position to current gps position
340
            GPS_SetCurrPosition(&HoldPosition); // can get invalid if gps signal is bad
343
                                GPS_SetCurrPosition(&HoldPosition); // can get invalid if gps signal is bad
341
            // disable gps control
344
                                // disable gps control
342
            GPS_Neutral();
345
                                GPS_Neutral();
343
            break;
346
                                break;
344
           
347
 
345
          case GPS_FLIGHT_MODE_AID:
348
                        case GPS_FLIGHT_MODE_AID:
346
            if(HoldPosition.Status != INVALID)
349
                                if (HoldPosition.Status != INVALID) {
347
              {
-
 
348
                if( GPS_IsManualControlled() ) // MK controlled by user
350
                                        if (GPS_IsManualControlled()) { // MK controlled by user
349
                  {
-
 
350
                    // update hold point to current gps position
351
                                                // update hold point to current gps position
351
                    GPS_SetCurrPosition(&HoldPosition);
352
                                                GPS_SetCurrPosition(&HoldPosition);
352
                    // disable gps control
353
                                                // disable gps control
353
                    GPS_Neutral();
354
                                                GPS_Neutral();
354
                    GPS_P_Delay = 0;
355
                                                GPS_P_Delay = 0;
355
                  }
-
 
356
                else // GPS control active
356
                                        } else { // GPS control active
357
                  {
-
 
358
                    if(GPS_P_Delay < 7)
357
                                                if (GPS_P_Delay < 7) {
359
                      { // delayed activation of P-Part for 8 cycles (8*0.25s = 2s)
358
                                                        // delayed activation of P-Part for 8 cycles (8*0.25s = 2s)
360
                        GPS_P_Delay++;
359
                                                        GPS_P_Delay++;
361
                        GPS_SetCurrPosition(&HoldPosition); // update hold point to current gps position
360
                                                        GPS_SetCurrPosition(&HoldPosition); // update hold point to current gps position
362
                        GPS_PIDController(NULL); // activates only the D-Part
361
                                                        GPS_PIDController(NULL); // activates only the D-Part
-
 
362
                                                } else
-
 
363
                                                        GPS_PIDController(&HoldPosition);// activates the P&D-Part
363
                      }
364
                                        }
364
                    else GPS_PIDController(&HoldPosition);// activates the P&D-Part
-
 
365
                  }
-
 
366
              }
-
 
367
            else // invalid Hold Position
365
                                } else // invalid Hold Position
368
              {  // try to catch a valid hold position from gps data input
366
                                { // try to catch a valid hold position from gps data input
369
                GPS_SetCurrPosition(&HoldPosition);
367
                                        GPS_SetCurrPosition(&HoldPosition);
370
                GPS_Neutral();
368
                                        GPS_Neutral();
371
              }
369
                                }
372
            break;
370
                                break;
373
           
371
 
374
          case GPS_FLIGHT_MODE_HOME:
372
                        case GPS_FLIGHT_MODE_HOME:
375
            if(HomePosition.Status != INVALID)
373
                                if (HomePosition.Status != INVALID) {
376
              {
-
 
377
                // update hold point to current gps position
374
                                        // update hold point to current gps position
378
                // to avoid a flight back if home comming is deactivated
375
                                        // to avoid a flight back if home comming is deactivated
379
                GPS_SetCurrPosition(&HoldPosition);
376
                                        GPS_SetCurrPosition(&HoldPosition);
380
                if( GPS_IsManualControlled() ) // MK controlled by user
377
                                        if (GPS_IsManualControlled()) // MK controlled by user
381
                  {
378
                                        {
382
                    GPS_Neutral();
379
                                                GPS_Neutral();
383
                  }
-
 
384
                else // GPS control active
380
                                        } else // GPS control active
385
                  {
381
                                        {
386
                    GPS_PIDController(&HomePosition);
382
                                                GPS_PIDController(&HomePosition);
387
                  }
383
                                        }
388
              }
-
 
389
            else // bad home position
384
                                } else // bad home position
390
              {
385
                                {
391
                BeepTime = 50; // signal invalid home position
386
                                        BeepTime = 50; // signal invalid home position
392
                // try to hold at least the position as a fallback option
387
                                        // try to hold at least the position as a fallback option
393
               
388
 
394
                if (HoldPosition.Status != INVALID)
-
 
395
                  {
389
                                        if (HoldPosition.Status != INVALID) {
396
                    if( GPS_IsManualControlled() ) // MK controlled by user
390
                                                if (GPS_IsManualControlled()) // MK controlled by user
397
                      {
391
                                                {
398
                        GPS_Neutral();
392
                                                        GPS_Neutral();
399
                      }
-
 
400
                    else // GPS control active
393
                                                } else // GPS control active
401
                      {
394
                                                {
402
                        GPS_PIDController(&HoldPosition);
395
                                                        GPS_PIDController(&HoldPosition);
403
                      }
396
                                                }
404
                  }
-
 
405
                else
-
 
406
                  { // try to catch a valid hold position
397
                                        } else { // try to catch a valid hold position
407
                    GPS_SetCurrPosition(&HoldPosition);
398
                                                GPS_SetCurrPosition(&HoldPosition);
408
                    GPS_Neutral();
399
                                                GPS_Neutral();
409
                  }
400
                                        }
410
              }
401
                                }
411
            break; // eof TSK_HOME
402
                                break; // eof TSK_HOME
412
          default: // unhandled task
403
                        default: // unhandled task
413
            GPS_Neutral();
404
                                GPS_Neutral();
414
            break; // eof default
405
                                break; // eof default
415
          } // eof switch GPS_Task
406
                        } // eof switch GPS_Task
416
      } // eof gps data quality is good
407
                } // eof gps data quality is good
417
    else // gps data quality is bad
408
                else // gps data quality is bad
418
      { // disable gps control
409
                { // disable gps control
419
        GPS_Neutral();
410
                        GPS_Neutral();
420
        if(FlightMode != GPS_FLIGHT_MODE_FREE)
411
                        if (FlightMode != GPS_FLIGHT_MODE_FREE) {
421
          {
-
 
422
            // beep if signal is not sufficient
412
                                // beep if signal is not sufficient
423
            if(!(GPSInfo.flags & FLAG_GPSFIXOK) && !(beep_rythm % 5)) BeepTime = 100;
413
                                if (!(GPSInfo.flags & FLAG_GPSFIXOK) && !(beep_rythm % 5))
-
 
414
                                        BeepTime = 100;
424
            else if (GPSInfo.satnum < staticParams.NaviGpsMinSat && !(beep_rythm % 5)) BeepTime = 10;
415
                                else if (GPSInfo.satnum < staticParams.NaviGpsMinSat
-
 
416
                                                && !(beep_rythm % 5))
-
 
417
                                        BeepTime = 10;
425
          }
418
                        }
426
      }
419
                }
427
      // set current data as processed to avoid further calculations on the same gps data
420
                // set current data as processed to avoid further calculations on the same gps data
428
    GPSInfo.status = PROCESSED;
421
                GPSInfo.status = PROCESSED;
429
    break;
422
                break;
430
  } // eof GPSInfo.status
423
        } // eof GPSInfo.status
431
}
424
}
432
 
425
 
433
 
426