Subversion Repositories FlightCtrl

Rev

Rev 806 | Rev 813 | 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
{
726 killagreg 99
        int32_t coscompass, sincompass;
781 killagreg 100
        int32_t GPSPosDev_North, GPSPosDev_East; // Position deviation in cm
812 killagreg 101
        int32_t P_North = 0, D_North = 0, P_East = 0, D_East = 0, I_North = 0, I_East = 0;
102
        int32_t PID_North = 0, PID_East = 0;
103
        uint8_t GPS_Stick_Limited = 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;
812 killagreg 164
                I_North = -(GPS_I_Factor * GPSPosDevIntegral_North)/2048;
165
                I_East =  -(GPS_I_Factor * GPSPosDevIntegral_East)/2048;
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);
812 killagreg 188
                GPS_Roll  =    (int16_t)((coscompass * PID_East - sincompass * PID_North) / 8192);
189
                GPS_Pitch = -1*(int16_t)((sincompass * PID_East + coscompass * PID_North) / 8192);
792 killagreg 190
 
762 killagreg 191
                // limit GPS controls
812 killagreg 192
                if (GPS_Pitch > GPS_STICK_LIMIT)
193
                {
194
                        GPS_Pitch =  GPS_STICK_LIMIT;
195
                        GPS_Stick_Limited = 1;
196
                }
197
                else if (GPS_Pitch < -GPS_STICK_LIMIT)
198
                {
199
                        GPS_Pitch = -GPS_STICK_LIMIT;
200
                        GPS_Stick_Limited = 1;
201
                }
202
                if (GPS_Roll > GPS_STICK_LIMIT)
203
                {
204
                        GPS_Roll = GPS_STICK_LIMIT;
205
                        GPS_Stick_Limited = 1;
206
                }
207
                else if (GPS_Roll < -GPS_STICK_LIMIT)
208
                {
209
                        GPS_Roll = -GPS_STICK_LIMIT;
210
                        GPS_Stick_Limited = 1;
211
                }
212
 
213
                if(!GPS_Stick_Limited) // prevent further growing if error integrals if control limit is reached
214
                {
215
                        // calculate position error integrals
216
                        GPSPosDevIntegral_North += GPSPosDev_North/4;
217
                        if( GPSPosDevIntegral_North > GPS_POSDEV_INTEGRAL_LIMIT) GPSPosDevIntegral_North = GPS_POSDEV_INTEGRAL_LIMIT;
218
                        else if (GPSPosDevIntegral_North < -GPS_POSDEV_INTEGRAL_LIMIT) GPSPosDevIntegral_North = -GPS_POSDEV_INTEGRAL_LIMIT;
219
                        GPSPosDevIntegral_East += GPSPosDev_East/4;
220
                        if( GPSPosDevIntegral_East > GPS_POSDEV_INTEGRAL_LIMIT) GPSPosDevIntegral_East = GPS_POSDEV_INTEGRAL_LIMIT;
221
                        else if (GPSPosDevIntegral_East < -GPS_POSDEV_INTEGRAL_LIMIT) GPSPosDevIntegral_East = -GPS_POSDEV_INTEGRAL_LIMIT;
222
                }
223
 
746 killagreg 224
        }
792 killagreg 225
        else // invalid GPS data
746 killagreg 226
        {
792 killagreg 227
                GPS_Neutral(); // do nothing
812 killagreg 228
                // reset error integral
229
                GPSPosDevIntegral_North = 0;
230
                GPSPosDevIntegral_East = 0;
746 killagreg 231
        }
232
}
233
 
234
 
812 killagreg 235
 
236
 
762 killagreg 237
void GPS_Main(uint8_t ctrl)
746 killagreg 238
{
741 killagreg 239
        static uint8_t GPS_Task = TSK_IDLE;
792 killagreg 240
        static uint8_t GPS_P_Delay = 0;
752 killagreg 241
        int16_t satbeep;
1 ingob 242
 
762 killagreg 243
        // ctrl enables the gps feature
244
        if(ctrl < 70) GPS_Task = TSK_IDLE;
245
        else if (ctrl < 160) GPS_Task = TSK_HOLD;
246
        else GPS_Task = TSK_HOME; // ctrl >= 160
741 killagreg 247
 
248
 
249
        switch(GPSInfo.status)
726 killagreg 250
        {
741 killagreg 251
        case INVALID:  // invalid gps data
746 killagreg 252
                GPS_Neutral();
752 killagreg 253
                if(GPS_Task != TSK_IDLE)
254
                {
255
                        BeepTime = 100; // beep if signal is neccesary
256
                }
741 killagreg 257
                break;
781 killagreg 258
        case PROCESSED: // if gps data are already processed do nothing
741 killagreg 259
                // downcount timeout
260
                if(GPSTimeout) GPSTimeout--;
261
                // if no new data arrived within timeout set current data invalid
262
                // and therefore disable GPS
263
                else
726 killagreg 264
                {
746 killagreg 265
                        GPS_Neutral();
741 killagreg 266
                        GPSInfo.status = INVALID;
267
                }
268
                break;
269
        case VALID: // new valid data from gps device
781 killagreg 270
                // if the gps data quality is good
741 killagreg 271
                if (GPSInfo.satfix == SATFIX_3D)
272
                {
273
                        switch(GPS_Task) // check what's to do
726 killagreg 274
                        {
741 killagreg 275
                                case TSK_IDLE:
781 killagreg 276
                                        // update hold position to current gps position
277
                                        GPS_SetHoldPosition(); // can get invalid if gps signal is bad
741 killagreg 278
                                        // disable gps control
746 killagreg 279
                                        GPS_Neutral();
741 killagreg 280
                                        break; // eof TSK_IDLE
281
                                case TSK_HOLD:
781 killagreg 282
                                        if(HoldPosition.Status != INVALID)
741 killagreg 283
                                        {
794 killagreg 284
                                                if( IsManualControlled() ) // MK controlled by user
762 killagreg 285
                                                {
794 killagreg 286
                                                        // update hold point to current gps position
287
                                                        GPS_SetHoldPosition();
288
                                                        // disable gps control
289
                                                        GPS_Neutral();
290
                                                        GPS_P_Delay = 0;
291
                                                }
292
                                                else // GPS control active
293
                                                {
792 killagreg 294
                                                        if(GPS_P_Delay<7)
295
                                                        { // delayed activation of P-Part for 8 cycles (8*0.25s = 2s)
296
                                                                GPS_P_Delay++;
297
                                                                GPS_SetHoldPosition();  // update hold point to current gps position
812 killagreg 298
                                                                GPS_PIDController(NULL); // activates only the D-Part
792 killagreg 299
                                                        }
812 killagreg 300
                                                        else GPS_PIDController(&HoldPosition);// activates the P&D-Part
762 killagreg 301
                                                }
741 killagreg 302
                                        }
781 killagreg 303
                                        else // invalid Hold Position
304
                                        {  // try to catch a valid hold position from gps data input
305
                                                GPS_SetHoldPosition();
306
                                                GPS_Neutral();
307
                                        }
741 killagreg 308
                                        break; // eof TSK_HOLD
746 killagreg 309
                                case TSK_HOME:
781 killagreg 310
                                        if(HomePosition.Status != INVALID)
746 killagreg 311
                                        {
312
                                                // update hold point to current gps position
762 killagreg 313
                                                // to avoid a flight back if home comming is deactivated
781 killagreg 314
                                                GPS_SetHoldPosition();
794 killagreg 315
                                                if( IsManualControlled() ) // MK controlled by user
762 killagreg 316
                                                {
794 killagreg 317
                                                        GPS_Neutral();
762 killagreg 318
                                                }
794 killagreg 319
                                                else // GPS control active
767 killagreg 320
                                                {
812 killagreg 321
                                                        GPS_PIDController(&HomePosition);
767 killagreg 322
                                                }
746 killagreg 323
                                        }
324
                                        else // bad home position
325
                                        {
326
                                                BeepTime = 50; // signal invalid home position
762 killagreg 327
                                                // try to hold at least the position as a fallback option
781 killagreg 328
 
329
                                                if (HoldPosition.Status != INVALID)
762 killagreg 330
                                                {
794 killagreg 331
                                                        if( IsManualControlled() ) // MK controlled by user
781 killagreg 332
                                                        {
794 killagreg 333
                                                                GPS_Neutral();
781 killagreg 334
                                                        }
794 killagreg 335
                                                        else // GPS control active
781 killagreg 336
                                                        {
812 killagreg 337
                                                                GPS_PIDController(&HoldPosition);
781 killagreg 338
                                                        }
762 killagreg 339
                                                }
781 killagreg 340
                                                else
341
                                                { // try to catch a valid hold position
342
                                                        GPS_SetHoldPosition();
767 killagreg 343
                                                        GPS_Neutral();
344
                                                }
746 killagreg 345
                                        }
346
                                        break; // eof TSK_HOME
741 killagreg 347
                                default: // unhandled task
746 killagreg 348
                                        GPS_Neutral();
741 killagreg 349
                                        break; // eof default
350
                        } // eof switch GPS_Task
351
                } // eof 3D-FIX
352
                else // no 3D-SATFIX
353
                {       // disable gps control
746 killagreg 354
                        GPS_Neutral();
752 killagreg 355
                        if(GPS_Task != TSK_IDLE)
356
                        {
806 killagreg 357
                                satbeep = 1600 - (int16_t)GPSInfo.satnum * 200; // is zero at 8 sats
752 killagreg 358
                                if (satbeep < 0) satbeep = 0;
806 killagreg 359
                                BeepTime = 50 + (uint16_t)satbeep; // max 1650 * 0.1 ms =
752 killagreg 360
                        }
741 killagreg 361
                }
362
                // set current data as processed to avoid further calculations on the same gps data
363
                GPSInfo.status = PROCESSED;
364
                break;
365
        } // eof GPSInfo.status
366
        DebugOut.Analog[14] = GPS_Pitch;
367
        DebugOut.Analog[15] = GPS_Roll;
726 killagreg 368
}
369