Subversion Repositories FlightCtrl

Rev

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

Rev Author Line No. Line
726 killagreg 1
#include <inttypes.h>
764 killagreg 2
#include <stdlib.h>
726 killagreg 3
#include "fc.h"
4
#include "ubx.h"
5
#include "mymath.h"
727 killagreg 6
#include "timer0.h"
741 killagreg 7
#include "uart.h"
794 killagreg 8
#include "rc.h"
9
#include "eeprom.h"
701 killagreg 10
 
741 killagreg 11
#define TSK_IDLE                0
12
#define TSK_HOLD                1
13
#define TSK_HOME                2
14
 
767 killagreg 15
#define GPS_STICK_SENSE         20              // must be at least in a range where 90% of the trimming does not switch of the GPS function
16
#define GPS_STICK_LIMIT         45              // limit of gps stick control to avoid critical flight attitudes
812 killagreg 17
#define GPS_POSDEV_INTEGRAL_LIMIT  32000  // limit for the position error integral
741 killagreg 18
 
726 killagreg 19
 
812 killagreg 20
int16_t GPS_Pitch = 0, GPS_Roll = 0;
21
int32_t GPS_P_Factor = 0, GPS_I_Factor = 0, GPS_D_Factor = 0;
726 killagreg 22
 
746 killagreg 23
 
781 killagreg 24
 
726 killagreg 25
typedef struct
1 ingob 26
{
781 killagreg 27
        int32_t Longitude;
28
        int32_t Latitude;
794 killagreg 29
        int32_t Altitude;
726 killagreg 30
        uint8_t Status;
31
} GPS_Pos_t;
32
 
33
// GPS coordinates for hold position
794 killagreg 34
GPS_Pos_t HoldPosition  = {0,0,0,INVALID};
741 killagreg 35
// GPS coordinates for home position
794 killagreg 36
GPS_Pos_t HomePosition  = {0,0,0,INVALID};
726 killagreg 37
 
38
 
39
// ---------------------------------------------------------------------------------
40
 
794 killagreg 41
// checks pitch and roll sticks for manual control
42
uint8_t IsManualControlled(void)
43
{
44
        if ( (abs(PPM_in[ParamSet.ChannelAssignment[CH_PITCH]]) < GPS_STICK_SENSE) && (abs(PPM_in[ParamSet.ChannelAssignment[CH_ROLL]]) < GPS_STICK_SENSE)) return 0;
45
        else return 1;
46
}
47
 
781 killagreg 48
// set home position to current positon
746 killagreg 49
void GPS_SetHomePosition(void)
50
{
781 killagreg 51
        if( ((GPSInfo.status == VALID) || (GPSInfo.status == PROCESSED)) && GPSInfo.satfix == SATFIX_3D)
52
        {
53
                HomePosition.Longitude = GPSInfo.longitude;
54
                HomePosition.Latitude = GPSInfo.latitude;
794 killagreg 55
                HomePosition.Altitude = GPSInfo.altitude;
781 killagreg 56
                HomePosition.Status = VALID;
57
                BeepTime = 1000; // signal if new home position was set
58
        }
59
        else
60
        {
61
                HomePosition.Status = INVALID;
62
        }
746 killagreg 63
}
726 killagreg 64
 
781 killagreg 65
// set hold position to current positon
66
void GPS_SetHoldPosition(void)
67
{
68
        if( ((GPSInfo.status == VALID) || (GPSInfo.status == PROCESSED)) && GPSInfo.satfix == SATFIX_3D)
69
        {
70
                HoldPosition.Longitude = GPSInfo.longitude;
71
                HoldPosition.Latitude = GPSInfo.latitude;
794 killagreg 72
                HoldPosition.Altitude = GPSInfo.altitude;
781 killagreg 73
                HoldPosition.Status = VALID;
74
        }
75
        else
76
        {
77
                HoldPosition.Status = INVALID;
78
        }
79
}
80
 
762 killagreg 81
// clear home position
82
void GPS_ClearHomePosition(void)
83
{
84
                HomePosition.Status = INVALID;
85
}
746 killagreg 86
 
747 killagreg 87
// disable GPS control sticks
746 killagreg 88
void GPS_Neutral(void)
1 ingob 89
{
746 killagreg 90
        GPS_Pitch = 0;
91
        GPS_Roll = 0;
92
}
93
 
792 killagreg 94
// calculates the GPS control stick values from the deviation to target position
95
// if the pointer to the target positin is NULL or is the target position invalid
96
// then the P part of the controller is deactivated.
812 killagreg 97
void GPS_PIDController(GPS_Pos_t *pTargetPos)
746 killagreg 98
{
828 killagreg 99
        int32_t PID_Stick, PID_Pitch, PID_Roll;
726 killagreg 100
        int32_t coscompass, sincompass;
781 killagreg 101
        int32_t GPSPosDev_North, GPSPosDev_East; // Position deviation in cm
812 killagreg 102
        int32_t P_North = 0, D_North = 0, P_East = 0, D_East = 0, I_North = 0, I_East = 0;
103
        int32_t PID_North = 0, PID_East = 0;
781 killagreg 104
        static int32_t cos_target_latitude = 1;
812 killagreg 105
        static int32_t GPSPosDevIntegral_North = 0, GPSPosDevIntegral_East = 0;
106
        static GPS_Pos_t *pLastTargetPos = 0;
746 killagreg 107
 
792 killagreg 108
        // if GPS data and Compass are ok
109
        if((GPSInfo.status == VALID) && (GPSInfo.satfix == SATFIX_3D) && (CompassHeading >= 0) )
110
        {
781 killagreg 111
 
792 killagreg 112
                if(pTargetPos != NULL) // if there is a target position
781 killagreg 113
                {
792 killagreg 114
                        if(pTargetPos->Status != INVALID) // and the position data are valid
812 killagreg 115
                        {
116
                                // if the target data are updated or the target pointer has changed
117
                                if ((pTargetPos->Status != PROCESSED) || (pTargetPos != pLastTargetPos) )
792 killagreg 118
                                {
812 killagreg 119
                                        // reset error integral
120
                                        GPSPosDevIntegral_North = 0;
121
                                        GPSPosDevIntegral_East = 0;
122
                                        // recalculate latitude projection
792 killagreg 123
                                        cos_target_latitude = (int32_t)c_cos_8192((int16_t)(pTargetPos->Latitude/10000000L));
812 killagreg 124
                                        // remember last target pointer
125
                                        pLastTargetPos = pTargetPos;
126
                                        // mark data as processed
792 killagreg 127
                                        pTargetPos->Status = PROCESSED;
128
                                }
812 killagreg 129
                                // calculate position deviation from latitude and longitude differences
130
                                GPSPosDev_North = (GPSInfo.latitude - pTargetPos->Latitude); // to calculate real cm we would need *111/100 additionally
131
                                GPSPosDev_East  = (GPSInfo.longitude - pTargetPos->Longitude); // to calculate real cm we would need *111/100 additionally
792 killagreg 132
                                // calculate latitude projection
133
                                GPSPosDev_East  *= cos_target_latitude;
134
                                GPSPosDev_East /= 8192;
746 killagreg 135
 
812 killagreg 136
                        DebugOut.Analog[12] = GPSPosDev_North;
792 killagreg 137
                                DebugOut.Analog[13] = GPSPosDev_East;
138
                                //DebugOut.Analog[12] = GPSInfo.velnorth;
139
                                //DebugOut.Analog[13] = GPSInfo.veleast;
140
                        }
812 killagreg 141
                        else // no valid target position available
792 killagreg 142
                        {
812 killagreg 143
                                // reset error
792 killagreg 144
                                GPSPosDev_North = 0;
145
                                GPSPosDev_East = 0;
812 killagreg 146
                                // reset error integral
147
                                GPSPosDevIntegral_North = 0;
148
                                GPSPosDevIntegral_East = 0;
792 killagreg 149
                        }
150
                }
812 killagreg 151
                else // no target position available
792 killagreg 152
                {
812 killagreg 153
                        // reset error
792 killagreg 154
                        GPSPosDev_North = 0;
155
                        GPSPosDev_East = 0;
812 killagreg 156
                        // reset error integral
157
                        GPSPosDevIntegral_North = 0;
158
                        GPSPosDevIntegral_East = 0;
792 killagreg 159
                }
767 killagreg 160
 
812 killagreg 161
                //Calculate PID-components of the controller (negative sign for compensation)
762 killagreg 162
                P_North = -(GPS_P_Factor * GPSPosDev_North)/2048;
792 killagreg 163
                P_East =  -(GPS_P_Factor * GPSPosDev_East)/2048;
818 killagreg 164
                I_North = -(GPS_I_Factor * GPSPosDevIntegral_North)/8192;
165
                I_East =  -(GPS_I_Factor * GPSPosDevIntegral_East)/8192;
762 killagreg 166
                D_North = -(GPS_D_Factor * GPSInfo.velnorth)/512;
167
                D_East =  -(GPS_D_Factor * GPSInfo.veleast)/512;
168
                // PD-controller
812 killagreg 169
                PID_North = P_North + I_North + D_North;
170
                PID_East  = P_East  + I_East  + D_East;
746 killagreg 171
 
762 killagreg 172
                // GPS to pitch and roll settings
746 killagreg 173
 
792 killagreg 174
                // A positive pitch angle moves head downwards (flying forward).
175
                // A positive roll angle tilts left side downwards (flying left).
762 killagreg 176
                // If compass heading is 0 the head of the copter is in north direction.
177
                // A positive pitch angle will fly to north and a positive roll angle will fly to west.
178
                // In case of a positive north deviation/velocity the
179
                // copter should fly to south (negative pitch).
180
                // In case of a positive east position deviation and a positive east velocity the
181
                // copter should fly to west (positive roll).
182
                // The influence of the GPS_Pitch and GPS_Roll variable is contrarily to the stick values
183
                // in the fc.c. Therefore a positive north deviation/velocity should result in a positive
184
                // GPS_Pitch and a positive east deviation/velocity should result in a negative GPS_Roll.
185
 
792 killagreg 186
                coscompass = (int32_t)c_cos_8192(CompassHeading);
187
                sincompass = (int32_t)c_sin_8192(CompassHeading);
828 killagreg 188
                PID_Roll  =  (coscompass * PID_East - sincompass * PID_North) / 8192;
189
                PID_Pitch =   -1*((sincompass * PID_East + coscompass * PID_North) / 8192);
792 killagreg 190
 
828 killagreg 191
                // limit GPS control vector
192
                PID_Stick = (int32_t)c_sqrt(PID_Roll*PID_Roll + PID_Pitch*PID_Pitch);
193
                if (PID_Stick > GPS_STICK_LIMIT)
812 killagreg 194
                {
828 killagreg 195
                        // normalize control vector components to the limit
196
                        PID_Roll  = (PID_Roll  * GPS_STICK_LIMIT)/PID_Stick;
197
                        PID_Pitch = (PID_Pitch * GPS_STICK_LIMIT)/PID_Stick;
812 killagreg 198
                }
828 killagreg 199
                else // prevent further growing of error integrals if control limit is reached
812 killagreg 200
                {
201
                        // calculate position error integrals
818 killagreg 202
                        GPSPosDevIntegral_North += GPSPosDev_North/16;
812 killagreg 203
                        if( GPSPosDevIntegral_North > GPS_POSDEV_INTEGRAL_LIMIT) GPSPosDevIntegral_North = GPS_POSDEV_INTEGRAL_LIMIT;
204
                        else if (GPSPosDevIntegral_North < -GPS_POSDEV_INTEGRAL_LIMIT) GPSPosDevIntegral_North = -GPS_POSDEV_INTEGRAL_LIMIT;
818 killagreg 205
                        GPSPosDevIntegral_East += GPSPosDev_East/16;
812 killagreg 206
                        if( GPSPosDevIntegral_East > GPS_POSDEV_INTEGRAL_LIMIT) GPSPosDevIntegral_East = GPS_POSDEV_INTEGRAL_LIMIT;
207
                        else if (GPSPosDevIntegral_East < -GPS_POSDEV_INTEGRAL_LIMIT) GPSPosDevIntegral_East = -GPS_POSDEV_INTEGRAL_LIMIT;
208
                }
209
 
828 killagreg 210
                GPS_Roll  = (int16_t)PID_Roll;
211
                GPS_Pitch = (int16_t)PID_Pitch;
212
 
746 killagreg 213
        }
818 killagreg 214
        else // invalid GPS data or bad compass reading
746 killagreg 215
        {
792 killagreg 216
                GPS_Neutral(); // do nothing
812 killagreg 217
                // reset error integral
218
                GPSPosDevIntegral_North = 0;
219
                GPSPosDevIntegral_East = 0;
746 killagreg 220
        }
221
}
222
 
223
 
812 killagreg 224
 
225
 
762 killagreg 226
void GPS_Main(uint8_t ctrl)
746 killagreg 227
{
741 killagreg 228
        static uint8_t GPS_Task = TSK_IDLE;
792 killagreg 229
        static uint8_t GPS_P_Delay = 0;
752 killagreg 230
        int16_t satbeep;
1 ingob 231
 
762 killagreg 232
        // ctrl enables the gps feature
233
        if(ctrl < 70) GPS_Task = TSK_IDLE;
234
        else if (ctrl < 160) GPS_Task = TSK_HOLD;
235
        else GPS_Task = TSK_HOME; // ctrl >= 160
741 killagreg 236
 
237
 
238
        switch(GPSInfo.status)
726 killagreg 239
        {
741 killagreg 240
        case INVALID:  // invalid gps data
746 killagreg 241
                GPS_Neutral();
752 killagreg 242
                if(GPS_Task != TSK_IDLE)
243
                {
244
                        BeepTime = 100; // beep if signal is neccesary
245
                }
741 killagreg 246
                break;
781 killagreg 247
        case PROCESSED: // if gps data are already processed do nothing
741 killagreg 248
                // downcount timeout
249
                if(GPSTimeout) GPSTimeout--;
250
                // if no new data arrived within timeout set current data invalid
251
                // and therefore disable GPS
252
                else
726 killagreg 253
                {
746 killagreg 254
                        GPS_Neutral();
741 killagreg 255
                        GPSInfo.status = INVALID;
256
                }
257
                break;
258
        case VALID: // new valid data from gps device
781 killagreg 259
                // if the gps data quality is good
741 killagreg 260
                if (GPSInfo.satfix == SATFIX_3D)
261
                {
262
                        switch(GPS_Task) // check what's to do
726 killagreg 263
                        {
741 killagreg 264
                                case TSK_IDLE:
781 killagreg 265
                                        // update hold position to current gps position
266
                                        GPS_SetHoldPosition(); // can get invalid if gps signal is bad
741 killagreg 267
                                        // disable gps control
746 killagreg 268
                                        GPS_Neutral();
741 killagreg 269
                                        break; // eof TSK_IDLE
270
                                case TSK_HOLD:
781 killagreg 271
                                        if(HoldPosition.Status != INVALID)
741 killagreg 272
                                        {
794 killagreg 273
                                                if( IsManualControlled() ) // MK controlled by user
762 killagreg 274
                                                {
794 killagreg 275
                                                        // update hold point to current gps position
276
                                                        GPS_SetHoldPosition();
277
                                                        // disable gps control
278
                                                        GPS_Neutral();
279
                                                        GPS_P_Delay = 0;
280
                                                }
281
                                                else // GPS control active
282
                                                {
792 killagreg 283
                                                        if(GPS_P_Delay<7)
284
                                                        { // delayed activation of P-Part for 8 cycles (8*0.25s = 2s)
285
                                                                GPS_P_Delay++;
286
                                                                GPS_SetHoldPosition();  // update hold point to current gps position
812 killagreg 287
                                                                GPS_PIDController(NULL); // activates only the D-Part
792 killagreg 288
                                                        }
812 killagreg 289
                                                        else GPS_PIDController(&HoldPosition);// activates the P&D-Part
762 killagreg 290
                                                }
741 killagreg 291
                                        }
781 killagreg 292
                                        else // invalid Hold Position
293
                                        {  // try to catch a valid hold position from gps data input
294
                                                GPS_SetHoldPosition();
295
                                                GPS_Neutral();
296
                                        }
741 killagreg 297
                                        break; // eof TSK_HOLD
746 killagreg 298
                                case TSK_HOME:
781 killagreg 299
                                        if(HomePosition.Status != INVALID)
746 killagreg 300
                                        {
301
                                                // update hold point to current gps position
762 killagreg 302
                                                // to avoid a flight back if home comming is deactivated
781 killagreg 303
                                                GPS_SetHoldPosition();
794 killagreg 304
                                                if( IsManualControlled() ) // MK controlled by user
762 killagreg 305
                                                {
794 killagreg 306
                                                        GPS_Neutral();
762 killagreg 307
                                                }
794 killagreg 308
                                                else // GPS control active
767 killagreg 309
                                                {
812 killagreg 310
                                                        GPS_PIDController(&HomePosition);
767 killagreg 311
                                                }
746 killagreg 312
                                        }
313
                                        else // bad home position
314
                                        {
315
                                                BeepTime = 50; // signal invalid home position
762 killagreg 316
                                                // try to hold at least the position as a fallback option
781 killagreg 317
 
318
                                                if (HoldPosition.Status != INVALID)
762 killagreg 319
                                                {
794 killagreg 320
                                                        if( IsManualControlled() ) // MK controlled by user
781 killagreg 321
                                                        {
794 killagreg 322
                                                                GPS_Neutral();
781 killagreg 323
                                                        }
794 killagreg 324
                                                        else // GPS control active
781 killagreg 325
                                                        {
812 killagreg 326
                                                                GPS_PIDController(&HoldPosition);
781 killagreg 327
                                                        }
762 killagreg 328
                                                }
781 killagreg 329
                                                else
330
                                                { // try to catch a valid hold position
331
                                                        GPS_SetHoldPosition();
767 killagreg 332
                                                        GPS_Neutral();
333
                                                }
746 killagreg 334
                                        }
335
                                        break; // eof TSK_HOME
741 killagreg 336
                                default: // unhandled task
746 killagreg 337
                                        GPS_Neutral();
741 killagreg 338
                                        break; // eof default
339
                        } // eof switch GPS_Task
340
                } // eof 3D-FIX
341
                else // no 3D-SATFIX
342
                {       // disable gps control
746 killagreg 343
                        GPS_Neutral();
752 killagreg 344
                        if(GPS_Task != TSK_IDLE)
345
                        {
806 killagreg 346
                                satbeep = 1600 - (int16_t)GPSInfo.satnum * 200; // is zero at 8 sats
752 killagreg 347
                                if (satbeep < 0) satbeep = 0;
806 killagreg 348
                                BeepTime = 50 + (uint16_t)satbeep; // max 1650 * 0.1 ms =
752 killagreg 349
                        }
741 killagreg 350
                }
351
                // set current data as processed to avoid further calculations on the same gps data
352
                GPSInfo.status = PROCESSED;
353
                break;
354
        } // eof GPSInfo.status
355
        DebugOut.Analog[14] = GPS_Pitch;
356
        DebugOut.Analog[15] = GPS_Roll;
726 killagreg 357
}
358