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