Subversion Repositories FlightCtrl

Rev

Rev 701 | 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>
1 ingob 2
 
726 killagreg 3
#include "fc.h"
4
#include "ubx.h"
5
#include "mymath.h"
701 killagreg 6
 
726 killagreg 7
int16_t GPS_Pitch = 0;
8
int16_t GPS_Roll = 0;
9
 
10
uint8_t GPS_P_Factor = 0;
11
uint8_t GPS_D_Factor = 0;
12
 
13
typedef struct
1 ingob 14
{
726 killagreg 15
        int32_t Northing;
16
        int32_t Easting;
17
        uint8_t Status;
1 ingob 18
 
726 killagreg 19
} GPS_Pos_t;
20
 
21
// GPS coordinates for hold position
22
GPS_Pos_t GPSHoldPoint  = {0,0, INVALID};
23
// GPS coordinates for flying home
24
GPS_Pos_t GPSHomePoint = {0,0, INVALID};;
25
 
26
 
27
 
28
// ---------------------------------------------------------------------------------
29
 
30
 
31
void GPS_Main(void)
1 ingob 32
{
726 killagreg 33
        int32_t coscompass, sincompass;
34
        int32_t P_North = 0, D_North = 0, P_East = 0, D_East = 0;
35
        int32_t PD_North = 0, PD_East = 0;
36
        int32_t GPSPosDev_North = 0, GPSPosDev_East = 0;
1 ingob 37
 
726 killagreg 38
        // poti2 enables the gps feature
39
        if(Poti2 > 70) // run GPS function only if Poti 2 is larger than 70 (switch on)
40
        {
41
                switch(GPSInfo.status)
42
                {
43
                case INVALID:  // invalid gps data
44
                        GPS_Pitch = 0;
45
                        GPS_Roll = 0;
46
                        break;
47
                case PROCESSED: // if gps data are already processed
48
                        // downcount timeout
49
                        if(GPSTimeout) GPSTimeout--;
50
                        // if no new data arrived within timeout set current data invalid
51
                        // and therefore disable GPS
52
                        else
53
                        {
54
                                GPS_Pitch = 0;
55
                                GPS_Roll = 0;
56
                                GPSInfo.status = INVALID;
57
                        }
58
                        break;
59
                case VALID: // new valid data from gps device
60
                        // if the gps data quality is sufficient
61
                        if (GPSInfo.satfix == SATFIX_3D)
62
                        {
63
                                // if sticks are centered  and hold position is valid enable position hold
64
                                if ((MaxStickPitch < 20) && (MaxStickRoll < 20) && (GPSHoldPoint.Status == VALID))
65
                                {
66
                                        coscompass = (int32_t)c_cos_8192(CompassHeading);
67
                                        sincompass = (int32_t)c_sin_8192(CompassHeading);
1 ingob 68
 
726 killagreg 69
                                        // Calculate deviation from hold position
70
                                        GPSPosDev_North = GPSInfo.utmnorth - GPSHoldPoint.Northing;
71
                                        GPSPosDev_East  = GPSInfo.utmeast  - GPSHoldPoint.Easting;
1 ingob 72
 
726 killagreg 73
                                        //Calculate PD-components of the controller
74
                                        P_North = (GPS_P_Factor * GPSPosDev_North)/2048;
75
                                        D_North = (GPS_D_Factor * GPSInfo.velnorth)/255;
76
                                        P_East =  (GPS_P_Factor * GPSPosDev_East)/2048;
77
                                        D_East =  (GPS_D_Factor * GPSInfo.veleast)/255;
1 ingob 78
 
726 killagreg 79
                                        // PD-controller
80
                                        PD_North = (-P_North + D_North);
81
                                        PD_East = (P_East - D_East);
82
 
83
                                        // GPS to pitch and roll
84
                                        GPS_Pitch = (int16_t)((coscompass * PD_North - sincompass * PD_East) / 8192);
85
                                        GPS_Roll  = (int16_t)((sincompass * PD_North + coscompass * PD_East) / 8192);
86
 
87
                                        // limit GPS controls
88
                                        if (GPS_Pitch >  35) GPS_Pitch =  35;
89
                                        if (GPS_Pitch < -35) GPS_Pitch = -35;
90
                                        if (GPS_Roll >  35) GPS_Roll =  35;
91
                                        if (GPS_Roll < -35) GPS_Roll = -35;
92
                                }
93
                                else // MK controlled by user
94
                                {
95
                                        // update hold point to current gps position
96
                                        GPSHoldPoint.Northing = GPSInfo.utmnorth;
97
                                        GPSHoldPoint.Easting  = GPSInfo.utmeast;
98
                                        GPSHoldPoint.Status = VALID;
99
                                        // disable gps control
100
                                        GPS_Pitch = 0;
101
                                        GPS_Roll = 0;
102
                                }
103
                        } // eof 3D-FIX
104
                        else // no 3D-SATFIX
105
                        {       // diable gps control
106
                                GPS_Pitch = 0;
107
                                GPS_Roll = 0;
108
                        }
109
                        // set current data as processed to avoid further calculations on the same gps data
110
                        GPSInfo.status = PROCESSED;
111
                        break;
112
                } // eof GPSInfo.status
113
        }
114
        return;
115
}
116