Rev 752 | Rev 764 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 752 | Rev 762 | ||
---|---|---|---|
1 | #include <inttypes.h> |
1 | #include <inttypes.h> |
2 | - | ||
3 | #include "fc.h" |
2 | #include "fc.h" |
4 | #include "ubx.h" |
3 | #include "ubx.h" |
5 | #include "mymath.h" |
4 | #include "mymath.h" |
6 | #include "timer0.h" |
5 | #include "timer0.h" |
7 | #include "uart.h" |
6 | #include "uart.h" |
8 | 7 | ||
9 | #define TSK_IDLE 0 |
8 | #define TSK_IDLE 0 |
10 | #define TSK_HOLD 1 |
9 | #define TSK_HOLD 1 |
11 | #define TSK_HOME 2 |
10 | #define TSK_HOME 2 |
- | 11 | ||
- | 12 | #define GPS_STICK_SENSE 12 |
|
12 | 13 | #define GPS_STICK_LIMIT 45 |
|
13 | 14 | ||
14 | int16_t GPS_Pitch = 0; |
15 | int16_t GPS_Pitch = 0; |
15 | int16_t GPS_Roll = 0; |
16 | int16_t GPS_Roll = 0; |
16 | 17 | ||
17 | int32_t GPS_P_Factor = 0, GPS_D_Factor = 0; |
18 | int32_t GPS_P_Factor = 0, GPS_D_Factor = 0; |
18 | int32_t GPSPosDev_North = 0, GPSPosDev_East = 0; |
19 | int32_t GPSPosDev_North = 0, GPSPosDev_East = 0; |
19 | 20 | ||
20 | 21 | ||
21 | typedef struct |
22 | typedef struct |
22 | { |
23 | { |
23 | int32_t Northing; |
24 | int32_t Northing; |
24 | int32_t Easting; |
25 | int32_t Easting; |
25 | uint8_t Status; |
26 | uint8_t Status; |
26 | 27 | ||
27 | } GPS_Pos_t; |
28 | } GPS_Pos_t; |
28 | 29 | ||
29 | // GPS coordinates for hold position |
30 | // GPS coordinates for hold position |
30 | GPS_Pos_t HoldPosition = {0,0, INVALID}; |
31 | GPS_Pos_t HoldPosition = {0,0, INVALID}; |
31 | // GPS coordinates for home position |
32 | // GPS coordinates for home position |
32 | GPS_Pos_t HomePosition = {0,0, INVALID}; |
33 | GPS_Pos_t HomePosition = {0,0, INVALID}; |
33 | 34 | ||
34 | 35 | ||
35 | // --------------------------------------------------------------------------------- |
36 | // --------------------------------------------------------------------------------- |
36 | 37 | ||
37 | // set home position to current hold positon |
38 | // set home position to current hold positon |
38 | void GPS_SetHomePosition(void) |
39 | void GPS_SetHomePosition(void) |
39 | { |
40 | { |
40 | HomePosition.Northing = HoldPosition.Northing; |
41 | HomePosition.Northing = HoldPosition.Northing; |
41 | HomePosition.Easting = HoldPosition.Easting; |
42 | HomePosition.Easting = HoldPosition.Easting; |
42 | HomePosition.Status = HoldPosition.Status; |
43 | HomePosition.Status = HoldPosition.Status; |
43 | if(HomePosition.Status == VALID) BeepTime = 1000; // signal if new home position was set |
44 | if(HomePosition.Status == VALID) BeepTime = 1000; // signal if new home position was set |
44 | } |
45 | } |
- | 46 | ||
- | 47 | // clear home position |
|
- | 48 | void GPS_ClearHomePosition(void) |
|
- | 49 | { |
|
- | 50 | HomePosition.Status = INVALID; |
|
45 | 51 | } |
|
46 | 52 | ||
47 | // disable GPS control sticks |
53 | // disable GPS control sticks |
48 | void GPS_Neutral(void) |
54 | void GPS_Neutral(void) |
49 | { |
55 | { |
50 | GPS_Pitch = 0; |
56 | GPS_Pitch = 0; |
51 | GPS_Roll = 0; |
57 | GPS_Roll = 0; |
52 | } |
58 | } |
53 | 59 | ||
54 | // calculates the GPS control sticks values from the position deviation |
60 | // calculates the GPS control sticks values from the deviation to target position |
55 | void GPS_PDController(void) |
61 | void GPS_PDController(GPS_Pos_t *TargetPos) |
56 | { |
62 | { |
57 | int32_t coscompass, sincompass; |
63 | int32_t coscompass, sincompass; |
58 | int32_t P_North = 0, D_North = 0, P_East = 0, D_East = 0; |
64 | int32_t P_North = 0, D_North = 0, P_East = 0, D_East = 0; |
59 | int32_t PD_North = 0, PD_East = 0; |
65 | int32_t PD_North = 0, PD_East = 0; |
- | 66 | ||
- | 67 | if( (TargetPos->Status == VALID) && (GPSInfo.status == VALID) && (GPSInfo.satfix == SATFIX_3D)) |
|
- | 68 | { |
|
- | 69 | GPSPosDev_North = GPSInfo.utmnorth - TargetPos->Northing; |
|
- | 70 | GPSPosDev_East = GPSInfo.utmeast - TargetPos->Easting; |
|
- | 71 | ||
- | 72 | DebugOut.Analog[12] = GPSPosDev_North; |
|
- | 73 | DebugOut.Analog[13] = GPSPosDev_East; |
|
- | 74 | ||
60 | //Calculate PD-components of the controller (negative sign for compensation) |
75 | //Calculate PD-components of the controller (negative sign for compensation) |
61 | P_North = -(GPS_P_Factor * GPSPosDev_North)/2048; |
76 | P_North = -(GPS_P_Factor * GPSPosDev_North)/2048; |
62 | D_North = -(GPS_D_Factor * GPSInfo.velnorth)/512; |
77 | D_North = -(GPS_D_Factor * GPSInfo.velnorth)/512; |
63 | P_East = -(GPS_P_Factor * GPSPosDev_East)/2048; |
78 | P_East = -(GPS_P_Factor * GPSPosDev_East)/2048; |
64 | D_East = -(GPS_D_Factor * GPSInfo.veleast)/512; |
79 | D_East = -(GPS_D_Factor * GPSInfo.veleast)/512; |
65 | 80 | ||
66 | // PD-controller |
81 | // PD-controller |
67 | PD_North = P_North + D_North; |
82 | PD_North = P_North + D_North; |
68 | PD_East = P_East + D_East; |
83 | PD_East = P_East + D_East; |
69 | 84 | ||
70 | // GPS to pitch and roll settings |
85 | // GPS to pitch and roll settings |
71 | 86 | ||
72 | //A positive pitch angle moves head downwards (flying forward). |
87 | //A positive pitch angle moves head downwards (flying forward). |
73 | //A positive roll angle tilts left side downwards (flying left). |
88 | //A positive roll angle tilts left side downwards (flying left). |
74 | 89 | ||
75 | // If compass heading is 0 the head of the copter is in north direction. |
90 | // If compass heading is 0 the head of the copter is in north direction. |
76 | // A positive pitch angle will fly to north and a positive roll angle will fly to west. |
91 | // A positive pitch angle will fly to north and a positive roll angle will fly to west. |
77 | // In case of a positive north deviation/velocity the |
92 | // In case of a positive north deviation/velocity the |
78 | // copter should fly to south (negative pitch). |
93 | // copter should fly to south (negative pitch). |
79 | // In case of a positive east position deviation and a positive east velocity the |
94 | // In case of a positive east position deviation and a positive east velocity the |
80 | // copter should fly to west (positive roll). |
95 | // copter should fly to west (positive roll). |
81 | 96 | ||
82 | // The influence of the GPS_Pitch and GPS_Roll variable is contrarily to the stick values |
97 | // The influence of the GPS_Pitch and GPS_Roll variable is contrarily to the stick values |
83 | // in the fc.c. Therefore a positive north deviation/velocity should result in a positive |
98 | // in the fc.c. Therefore a positive north deviation/velocity should result in a positive |
84 | // GPS_Pitch and a positive east deviation/velocity should result in a negative GPS_Roll. |
99 | // GPS_Pitch and a positive east deviation/velocity should result in a negative GPS_Roll. |
85 | 100 | ||
86 | // rotation transformation to compass heading to match any copter orientation |
101 | // rotation transformation to compass heading to match any copter orientation |
87 | if (CompassHeading < 0) // no valid compass data |
102 | if (CompassHeading < 0) // no valid compass data |
88 | { // disable GPS |
103 | { // disable GPS |
89 | GPS_Neutral(); |
104 | GPS_Neutral(); |
- | 105 | } |
|
- | 106 | else |
|
- | 107 | { |
|
- | 108 | coscompass = (int32_t)c_cos_8192(CompassHeading); |
|
- | 109 | sincompass = (int32_t)c_sin_8192(CompassHeading); |
|
- | 110 | GPS_Roll = (int16_t)((coscompass * PD_East - sincompass * PD_North) / 8192); |
|
- | 111 | GPS_Pitch = -1*(int16_t)((sincompass * PD_East + coscompass * PD_North) / 8192); |
|
- | 112 | } |
|
- | 113 | // limit GPS controls |
|
- | 114 | if (GPS_Pitch > GPS_STICK_LIMIT) GPS_Pitch = GPS_STICK_LIMIT; |
|
- | 115 | if (GPS_Pitch < -GPS_STICK_LIMIT) GPS_Pitch = -GPS_STICK_LIMIT; |
|
- | 116 | if (GPS_Roll > GPS_STICK_LIMIT) GPS_Roll = GPS_STICK_LIMIT; |
|
- | 117 | if (GPS_Roll < -GPS_STICK_LIMIT) GPS_Roll = -GPS_STICK_LIMIT; |
|
90 | } |
118 | } |
91 | else |
119 | else // invalid input data |
92 | { |
120 | { |
93 | coscompass = (int32_t)c_cos_8192(CompassHeading); |
121 | BeepTime = 50; |
94 | sincompass = (int32_t)c_sin_8192(CompassHeading); |
122 | GPS_Neutral(); |
95 | GPS_Roll = (int16_t)((coscompass * PD_East - sincompass * PD_North) / 8192); |
- | |
96 | GPS_Pitch = -1*(int16_t)((sincompass * PD_East + coscompass * PD_North) / 8192); |
- | |
97 | } |
123 | } |
98 | // limit GPS controls |
- | |
99 | #define GPS_CTRL_LIMIT 35 |
- | |
100 | if (GPS_Pitch > GPS_CTRL_LIMIT) GPS_Pitch = GPS_CTRL_LIMIT; |
- | |
101 | if (GPS_Pitch < -GPS_CTRL_LIMIT) GPS_Pitch = -GPS_CTRL_LIMIT; |
- | |
102 | if (GPS_Roll > GPS_CTRL_LIMIT) GPS_Roll = GPS_CTRL_LIMIT; |
- | |
103 | if (GPS_Roll < -GPS_CTRL_LIMIT) GPS_Roll = -GPS_CTRL_LIMIT; |
- | |
104 | } |
124 | } |
105 | 125 | ||
106 | 126 | ||
107 | void GPS_Main(void) |
127 | void GPS_Main(uint8_t ctrl) |
108 | { |
128 | { |
109 | static uint8_t GPS_Task = TSK_IDLE; |
129 | static uint8_t GPS_Task = TSK_IDLE; |
110 | int16_t satbeep; |
130 | int16_t satbeep; |
111 | 131 | ||
112 | // poti2 enables the gps feature |
132 | // ctrl enables the gps feature |
113 | if(Poti2 < 70) GPS_Task = TSK_IDLE; |
133 | if(ctrl < 70) GPS_Task = TSK_IDLE; |
114 | else if (Poti2 < 160) GPS_Task = TSK_HOLD; |
134 | else if (ctrl < 160) GPS_Task = TSK_HOLD; |
115 | else GPS_Task = TSK_HOME; // Poti2 >= 160 |
135 | else GPS_Task = TSK_HOME; // ctrl >= 160 |
116 | 136 | ||
117 | 137 | ||
118 | switch(GPSInfo.status) |
138 | switch(GPSInfo.status) |
119 | { |
139 | { |
120 | case INVALID: // invalid gps data |
140 | case INVALID: // invalid gps data |
121 | GPS_Neutral(); |
141 | GPS_Neutral(); |
122 | if(GPS_Task != TSK_IDLE) |
142 | if(GPS_Task != TSK_IDLE) |
123 | { |
143 | { |
124 | BeepTime = 100; // beep if signal is neccesary |
144 | BeepTime = 100; // beep if signal is neccesary |
125 | } |
145 | } |
126 | break; |
146 | break; |
127 | case PROCESSED: // if gps data are already processed |
147 | case PROCESSED: // if gps data are already processed |
128 | // downcount timeout |
148 | // downcount timeout |
129 | if(GPSTimeout) GPSTimeout--; |
149 | if(GPSTimeout) GPSTimeout--; |
130 | // if no new data arrived within timeout set current data invalid |
150 | // if no new data arrived within timeout set current data invalid |
131 | // and therefore disable GPS |
151 | // and therefore disable GPS |
132 | else |
152 | else |
133 | { |
153 | { |
134 | GPS_Neutral(); |
154 | GPS_Neutral(); |
135 | GPSInfo.status = INVALID; |
155 | GPSInfo.status = INVALID; |
136 | } |
156 | } |
137 | break; |
157 | break; |
138 | case VALID: // new valid data from gps device |
158 | case VALID: // new valid data from gps device |
139 | // if the gps data quality is sufficient |
159 | // if the gps data quality is sufficient |
140 | if (GPSInfo.satfix == SATFIX_3D) |
160 | if (GPSInfo.satfix == SATFIX_3D) |
141 | { |
161 | { |
142 | switch(GPS_Task) // check what's to do |
162 | switch(GPS_Task) // check what's to do |
143 | { |
163 | { |
144 | case TSK_IDLE: |
164 | case TSK_IDLE: |
145 | // update hold point to current gps position |
165 | // update hold point to current gps position |
146 | HoldPosition.Northing = GPSInfo.utmnorth; |
166 | HoldPosition.Northing = GPSInfo.utmnorth; |
147 | HoldPosition.Easting = GPSInfo.utmeast; |
167 | HoldPosition.Easting = GPSInfo.utmeast; |
148 | HoldPosition.Status = VALID; |
168 | HoldPosition.Status = VALID; |
149 | // disable gps control |
169 | // disable gps control |
150 | GPS_Neutral(); |
170 | GPS_Neutral(); |
151 | break; // eof TSK_IDLE |
171 | break; // eof TSK_IDLE |
152 | case TSK_HOLD: |
172 | case TSK_HOLD: |
153 | // if sticks are centered and hold position is valid enable position hold control |
- | |
154 | if ((MaxStickPitch < 20) && (MaxStickRoll < 20) && (HoldPosition.Status == VALID)) |
173 | if(HoldPosition.Status == VALID) |
155 | { |
174 | { |
156 | // Calculate deviation from hold position |
175 | // if sticks are centered (no manual control) |
157 | GPSPosDev_North = GPSInfo.utmnorth - HoldPosition.Northing; |
176 | if ((MaxStickPitch < GPS_STICK_SENSE) && (MaxStickRoll < GPS_STICK_SENSE)) |
158 | GPSPosDev_East = GPSInfo.utmeast - HoldPosition.Easting; |
- | |
159 | DebugOut.Analog[12] = GPSPosDev_North; |
- | |
160 | DebugOut.Analog[13] = GPSPosDev_East; |
- | |
161 | 177 | { |
|
162 | GPS_PDController(); |
178 | GPS_PDController(&HoldPosition); |
163 | } |
179 | } |
164 | else // MK controlled by user |
180 | else // MK controlled by user |
165 | { |
181 | { |
166 | // update hold point to current gps position |
182 | // update hold point to current gps position |
167 | HoldPosition.Northing = GPSInfo.utmnorth; |
183 | HoldPosition.Northing = GPSInfo.utmnorth; |
168 | HoldPosition.Easting = GPSInfo.utmeast; |
184 | HoldPosition.Easting = GPSInfo.utmeast; |
169 | HoldPosition.Status = VALID; |
185 | HoldPosition.Status = GPSInfo.status; |
170 | // disable gps control |
186 | // disable gps control |
171 | GPS_Neutral(); |
187 | GPS_Neutral(); |
- | 188 | } |
|
172 | } |
189 | } |
173 | break; // eof TSK_HOLD |
190 | break; // eof TSK_HOLD |
174 | case TSK_HOME: |
191 | case TSK_HOME: |
175 | if(HomePosition.Status == VALID) |
192 | if(HomePosition.Status == VALID) |
176 | { |
193 | { |
177 | // update hold point to current gps position |
194 | // update hold point to current gps position |
- | 195 | // to avoid a flight back if home comming is deactivated |
|
178 | HoldPosition.Northing = GPSInfo.utmnorth; |
196 | HoldPosition.Northing = GPSInfo.utmnorth; |
179 | HoldPosition.Easting = GPSInfo.utmeast; |
197 | HoldPosition.Easting = GPSInfo.utmeast; |
180 | HoldPosition.Status = VALID; |
198 | HoldPosition.Status = GPSInfo.status; |
181 | - | ||
182 | // Calculate deviation from home position |
199 | // if sticks are centered (no manual control) |
183 | GPSPosDev_North = GPSInfo.utmnorth - HomePosition.Northing; |
200 | if((MaxStickPitch < GPS_STICK_SENSE) && (MaxStickRoll < GPS_STICK_SENSE)) |
184 | GPSPosDev_East = GPSInfo.utmeast - HomePosition.Easting; |
- | |
185 | DebugOut.Analog[12] = GPSPosDev_North; |
201 | { |
186 | DebugOut.Analog[13] = GPSPosDev_East; |
202 | GPS_PDController(&HomePosition); |
187 | - | ||
188 | GPS_PDController(); |
203 | } |
189 | } |
204 | } |
190 | else // bad home position |
205 | else // bad home position |
191 | { |
206 | { |
192 | GPS_Neutral(); |
- | |
193 | BeepTime = 50; // signal invalid home position |
207 | BeepTime = 50; // signal invalid home position |
- | 208 | // try to hold at least the position as a fallback option |
|
- | 209 | // if sticks are centered (no manual control) |
|
- | 210 | if((MaxStickPitch < GPS_STICK_SENSE) && (MaxStickRoll < GPS_STICK_SENSE) && (HoldPosition.Status == VALID)) |
|
- | 211 | { |
|
- | 212 | GPS_PDController(&HoldPosition); |
|
- | 213 | } |
|
194 | } |
214 | } |
195 | break; // eof TSK_HOME |
215 | break; // eof TSK_HOME |
196 | default: // unhandled task |
216 | default: // unhandled task |
197 | GPS_Neutral(); |
217 | GPS_Neutral(); |
198 | break; // eof default |
218 | break; // eof default |
199 | } // eof switch GPS_Task |
219 | } // eof switch GPS_Task |
200 | } // eof 3D-FIX |
220 | } // eof 3D-FIX |
201 | 221 | ||
202 | else // no 3D-SATFIX |
222 | else // no 3D-SATFIX |
203 | { // disable gps control |
223 | { // disable gps control |
204 | GPS_Neutral(); |
224 | GPS_Neutral(); |
205 | if(GPS_Task != TSK_IDLE) |
225 | if(GPS_Task != TSK_IDLE) |
206 | { |
226 | { |
207 | satbeep = 2048 - (int16_t)GPSInfo.satnum * 256; // is zero at 8 sats |
227 | satbeep = 2048 - (int16_t)GPSInfo.satnum * 256; // is zero at 8 sats |
208 | if (satbeep < 0) satbeep = 0; |
228 | if (satbeep < 0) satbeep = 0; |
209 | BeepTime = 50 + (uint16_t)satbeep; |
229 | BeepTime = 50 + (uint16_t)satbeep; |
210 | } |
230 | } |
211 | } |
231 | } |
212 | // set current data as processed to avoid further calculations on the same gps data |
232 | // set current data as processed to avoid further calculations on the same gps data |
213 | GPSInfo.status = PROCESSED; |
233 | GPSInfo.status = PROCESSED; |
214 | break; |
234 | break; |
215 | } // eof GPSInfo.status |
235 | } // eof GPSInfo.status |
216 | DebugOut.Analog[14] = GPS_Pitch; |
236 | DebugOut.Analog[14] = GPS_Pitch; |
217 | DebugOut.Analog[15] = GPS_Roll; |
237 | DebugOut.Analog[15] = GPS_Roll; |
218 | } |
238 | } |
219 | 239 | ||
220 | 240 |