Rev 27 | Rev 29 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 27 | Rev 28 | ||
---|---|---|---|
Line 65... | Line 65... | ||
65 | #include "led.h" |
65 | #include "led.h" |
66 | #include "analog.h" |
66 | #include "analog.h" |
67 | #include "uart.h" |
67 | #include "uart.h" |
Line 68... | Line 68... | ||
68 | 68 | ||
69 | - | ||
70 | int16_t RawMagnet1a, RawMagnet1b; // raw AD-Data |
- | |
Line 71... | Line 69... | ||
71 | int16_t RawMagnet2a, RawMagnet2b; |
69 | |
Line 72... | Line 70... | ||
72 | int16_t RawMagnet3a, RawMagnet3b; |
70 | AttitudeSource_t AttitudeSource = ATTITUDE_SOURCE_ACC; |
73 | 71 | ||
Line 79... | Line 77... | ||
79 | int16_t Offset; |
77 | int16_t Offset; |
80 | } ; |
78 | } ; |
Line 81... | Line 79... | ||
81 | 79 | ||
82 | struct Calibration_t |
80 | struct Calibration_t |
83 | { |
81 | { |
- | 82 | struct Scaling_t MagX; |
|
- | 83 | struct Scaling_t MagY; |
|
- | 84 | struct Scaling_t MagZ; |
|
84 | struct Scaling_t X; |
85 | struct Scaling_t AccX; |
85 | struct Scaling_t Y; |
86 | struct Scaling_t AccY; |
86 | struct Scaling_t Z; |
87 | struct Scaling_t AccZ; |
Line 87... | Line 88... | ||
87 | } ; |
88 | } ; |
88 | 89 | ||
89 | struct Calibration_t eeCalibration EEMEM; // calibration data in EEProm |
- | |
90 | struct Calibration_t Calibration; // calibration data in RAM |
- | |
91 | - | ||
92 | - | ||
Line -... | Line 90... | ||
- | 90 | struct Calibration_t eeCalibration EEMEM; // calibration data in EEProm |
|
- | 91 | struct Calibration_t Calibration; // calibration data in RAM |
|
- | 92 | ||
- | 93 | // magnet sensor variable |
|
- | 94 | int16_t RawMagnet1a, RawMagnet1b; // raw magnet sensor data |
|
- | 95 | int16_t RawMagnet2a, RawMagnet2b; |
|
- | 96 | int16_t RawMagnet3a, RawMagnet3b; |
|
- | 97 | int16_t UncalMagX, UncalMagY, UncalMagZ; // sensor signal difference without Scaling |
|
- | 98 | int16_t MagX, MagY, MagZ; // rescaled magnetic field readings |
|
- | 99 | // acc sensor variables |
|
93 | int16_t UncalMagnetX, UncalMagnetY, UncalMagnetZ; // sensor signal difference without Scaling |
100 | int16_t RawAccX, RawAccY, RawAccZ; // raw acceleration readings |
Line 94... | Line 101... | ||
94 | int16_t MagnetX, MagnetY, MagnetZ; // rescaled magnetic field readings |
101 | int16_t AccX, AccY, AccZ; // rescaled acceleration readings |
Line 95... | Line 102... | ||
95 | 102 | uint8_t AccPresent = 0; |
|
96 | uint8_t PC_Connected = 0; |
103 | uint8_t PC_Connected = 0; |
97 | 104 | ||
98 | int16_t Heading = -1; |
105 | int16_t Heading = -1; |
99 | 106 | ||
100 | 107 | ||
101 | void CalcFields(void) |
108 | void CalcFields(void) |
102 | { |
109 | { |
103 | UncalMagnetX = (RawMagnet1a - RawMagnet1b) / 2; |
110 | UncalMagX = (RawMagnet1a - RawMagnet1b); |
104 | UncalMagnetY = (RawMagnet3a - RawMagnet3b) / 2; |
111 | UncalMagY = (RawMagnet3a - RawMagnet3b); |
105 | UncalMagnetZ = (RawMagnet2a - RawMagnet2b) / 2; |
112 | UncalMagZ = (RawMagnet2a - RawMagnet2b); |
106 | 113 | ||
107 | if(Calibration.X.Range != 0) MagnetX = (1024L * (int32_t)(UncalMagnetX - Calibration.X.Offset)) / (Calibration.X.Range); |
114 | if(Calibration.MagX.Range != 0) MagX = (1024L * (int32_t)(UncalMagX - Calibration.MagX.Offset)) / (Calibration.MagX.Range); |
Line 108... | Line 115... | ||
108 | else MagnetX = 0; |
115 | else MagX = 0; |
109 | if(Calibration.Y.Range != 0) MagnetY = (1024L * (int32_t)(UncalMagnetY - Calibration.Y.Offset)) / (Calibration.Y.Range); |
116 | if(Calibration.MagY.Range != 0) MagY = (1024L * (int32_t)(UncalMagY - Calibration.MagY.Offset)) / (Calibration.MagY.Range); |
Line 123... | Line 130... | ||
123 | { |
130 | { |
124 | LED_GRN_TOGGLE; |
131 | LED_GRN_TOGGLE; |
125 | Led_Timer = SetDelay(500); |
132 | Led_Timer = SetDelay(500); |
126 | } |
133 | } |
Line 127... | Line 134... | ||
127 | 134 | ||
128 | Cx = MagnetX; |
135 | Cx = MagX; |
129 | Cy = MagnetY; |
136 | Cy = MagY; |
Line 130... | Line 137... | ||
130 | Cz = MagnetZ; |
137 | Cz = MagZ; |
131 | 138 | ||
132 | if(ExternData.Orientation == 1) |
139 | if(ExternData.Orientation == 1) |
133 | { |
140 | { |
134 | Cx = MagnetX; |
141 | Cx = MagX; |
135 | Cy = -MagnetY; |
142 | Cy = -MagY; |
Line 136... | Line 143... | ||
136 | Cz = MagnetZ; |
143 | Cz = MagZ; |
- | 144 | } |
|
- | 145 | ||
- | 146 | // calculate nick and roll angle in rad |
|
137 | } |
147 | switch(AttitudeSource) |
138 | 148 | { |
|
- | 149 | case ATTITUDE_SOURCE_I2C: |
|
- | 150 | nick_rad = ((double)I2C_WriteAttitude.Nick) * M_PI / (double)(1800.0); |
|
- | 151 | roll_rad = ((double)I2C_WriteAttitude.Roll) * M_PI / (double)(1800.0); |
|
- | 152 | break; |
|
- | 153 | ||
- | 154 | case ATTITUDE_SOURCE_UART: |
|
- | 155 | nick_rad = ((double)ExternData.Attitude[NICK]) * M_PI / (double)(1800.0); |
|
- | 156 | roll_rad = ((double)ExternData.Attitude[ROLL]) * M_PI / (double)(1800.0); |
|
- | 157 | break; |
|
- | 158 | ||
- | 159 | case ATTITUDE_SOURCE_ACC: |
|
- | 160 | if(AccX > 125) nick_rad = M_PI / 2; |
|
- | 161 | else |
|
- | 162 | if(AccX < -125) nick_rad = -M_PI / 2; |
|
- | 163 | else |
|
- | 164 | { |
|
- | 165 | nick_rad = asin((double) AccX / 125.0); |
|
- | 166 | } |
|
- | 167 | ||
- | 168 | if(AccY > 125) roll_rad = M_PI / 2; |
|
- | 169 | else |
|
- | 170 | if(AccY < -125) roll_rad = -M_PI / 2; |
|
- | 171 | else |
|
- | 172 | { |
|
- | 173 | roll_rad = asin((double) AccY / 125.0); |
|
- | 174 | } |
|
139 | // calculate nick and roll angle in rad |
175 | break; |
140 | nick_rad = ((double)I2C_WriteAttitude.Nick) * M_PI / (double)(1800.0); |
176 | } |
141 | roll_rad = ((double)I2C_WriteAttitude.Roll) * M_PI / (double)(1800.0); |
177 | |
Line 142... | Line 178... | ||
142 | // calculate attitude correction |
178 | // calculate attitude correction |
Line 204... | Line 240... | ||
204 | Xmax = -10000; |
240 | Xmax = -10000; |
205 | Ymin = 10000; |
241 | Ymin = 10000; |
206 | Ymax = -10000; |
242 | Ymax = -10000; |
207 | Zmin = 10000; |
243 | Zmin = 10000; |
208 | Zmax = -10000; |
244 | Zmax = -10000; |
- | 245 | Calibration.AccX.Offset = RawAccX; |
|
- | 246 | Calibration.AccY.Offset = RawAccY; |
|
- | 247 | Calibration.AccZ.Offset = RawAccZ; |
|
209 | break; |
248 | break; |
Line 210... | Line 249... | ||
210 | 249 | ||
211 | case 2: // 2nd step of calibration |
250 | case 2: // 2nd step of calibration |
212 | // find Min and Max of the X- and Y-Sensors during rotation in the horizontal plane |
251 | // find Min and Max of the X- and Y-Sensors during rotation in the horizontal plane |
213 | if(UncalMagnetX < Xmin) Xmin = UncalMagnetX; |
252 | if(UncalMagX < Xmin) Xmin = UncalMagX; |
214 | if(UncalMagnetX > Xmax) Xmax = UncalMagnetX; |
253 | if(UncalMagX > Xmax) Xmax = UncalMagX; |
215 | if(UncalMagnetY < Ymin) Ymin = UncalMagnetY; |
254 | if(UncalMagY < Ymin) Ymin = UncalMagY; |
216 | if(UncalMagnetY > Ymax) Ymax = UncalMagnetY; |
255 | if(UncalMagY > Ymax) Ymax = UncalMagY; |
Line 217... | Line 256... | ||
217 | break; |
256 | break; |
218 | 257 | ||
219 | case 3: // 3rd step of calibration |
258 | case 3: // 3rd step of calibration |
Line 220... | Line 259... | ||
220 | // used to change the orientation of the MK3MAG vertical to the horizontal plane |
259 | // used to change the orientation of the MK3MAG vertical to the horizontal plane |
221 | break; |
260 | break; |
222 | 261 | ||
223 | case 4: |
262 | case 4: |
224 | // find Min and Max of the Z-Sensor |
263 | // find Min and Max of the Z-Sensor |
Line 225... | Line 264... | ||
225 | if(UncalMagnetZ < Zmin) Zmin = UncalMagnetZ; |
264 | if(UncalMagZ < Zmin) Zmin = UncalMagZ; |
226 | if(UncalMagnetZ > Zmax) Zmax = UncalMagnetZ; |
265 | if(UncalMagZ > Zmax) Zmax = UncalMagZ; |
227 | break; |
266 | break; |
228 | 267 | ||
229 | case 5: |
268 | case 5: |
230 | // Save values |
269 | // Save values |
231 | if(cal != calold) // avoid continously writing of eeprom! |
270 | if(cal != calold) // avoid continously writing of eeprom! |
232 | { |
271 | { |
233 | Calibration.X.Range = Xmax - Xmin; |
272 | Calibration.MagY.Range = Xmax - Xmin; |
234 | Calibration.X.Offset = (Xmin + Xmax) / 2; |
273 | Calibration.MagX.Offset = (Xmin + Xmax) / 2; |
235 | Calibration.Y.Range = Ymax - Ymin; |
274 | Calibration.MagY.Range = Ymax - Ymin; |
236 | Calibration.Y.Offset = (Ymin + Ymax) / 2; |
275 | Calibration.MagY.Offset = (Ymin + Ymax) / 2; |
237 | Calibration.Z.Range = Zmax - Zmin; |
276 | Calibration.MagZ.Range = Zmax - Zmin; |
238 | Calibration.Z.Offset = (Zmin + Zmax) / 2; |
277 | Calibration.MagZ.Offset = (Zmin + Zmax) / 2; |
239 | if((Calibration.X.Range > 150) && (Calibration.Y.Range > 150) && (Calibration.Z.Range > 150)) |
278 | if((Calibration.MagX.Range > 150) && (Calibration.MagY.Range > 150) && (Calibration.MagZ.Range > 150)) |
240 | { |
279 | { |
Line 258... | Line 297... | ||
258 | } |
297 | } |
Line 259... | Line 298... | ||
259 | 298 | ||
260 | 299 | ||
261 | void SetDebugValues(void) |
300 | void SetDebugValues(void) |
262 | { |
301 | { |
263 | DebugOut.Analog[0] = MagnetX; |
302 | DebugOut.Analog[0] = MagX; |
264 | DebugOut.Analog[1] = MagnetY; |
303 | DebugOut.Analog[1] = MagY; |
265 | DebugOut.Analog[2] = MagnetZ; |
304 | DebugOut.Analog[2] = MagZ; |
266 | DebugOut.Analog[3] = UncalMagnetX; |
305 | DebugOut.Analog[3] = UncalMagX; |
- | 306 | DebugOut.Analog[4] = UncalMagY; |
|
- | 307 | DebugOut.Analog[5] = UncalMagZ; |
|
- | 308 | switch(AttitudeSource) |
|
- | 309 | { |
|
- | 310 | case ATTITUDE_SOURCE_ACC: |
|
- | 311 | ||
- | 312 | break; |
|
- | 313 | ||
- | 314 | case ATTITUDE_SOURCE_UART: |
|
- | 315 | DebugOut.Analog[6] = ExternData.Attitude[NICK]; |
|
- | 316 | DebugOut.Analog[7] = ExternData.Attitude[ROLL]; |
|
- | 317 | break; |
|
- | 318 | ||
267 | DebugOut.Analog[4] = UncalMagnetY; |
319 | |
268 | DebugOut.Analog[5] = UncalMagnetZ; |
320 | case ATTITUDE_SOURCE_I2C: |
- | 321 | DebugOut.Analog[6] = I2C_WriteAttitude.Nick; |
|
- | 322 | DebugOut.Analog[7] = I2C_WriteAttitude.Roll; |
|
269 | DebugOut.Analog[6] = I2C_WriteAttitude.Nick; |
323 | break; |
270 | DebugOut.Analog[7] = I2C_WriteAttitude.Roll; |
324 | } |
271 | DebugOut.Analog[8] = Calibration.X.Offset; |
325 | DebugOut.Analog[8] = Calibration.MagX.Offset; |
272 | DebugOut.Analog[9] = Calibration.X.Range; |
326 | DebugOut.Analog[9] = Calibration.MagX.Range; |
273 | DebugOut.Analog[10] = Calibration.Y.Offset; |
327 | DebugOut.Analog[10] = Calibration.MagY.Offset; |
274 | DebugOut.Analog[11] = Calibration.Y.Range; |
328 | DebugOut.Analog[11] = Calibration.MagY.Range; |
275 | DebugOut.Analog[12] = Calibration.Z.Offset; |
329 | DebugOut.Analog[12] = Calibration.MagZ.Offset; |
276 | DebugOut.Analog[13] = Calibration.Z.Range; |
330 | DebugOut.Analog[13] = Calibration.MagZ.Range; |
277 | DebugOut.Analog[14] = ExternData.CalState; |
331 | DebugOut.Analog[14] = ExternData.CalState; |
278 | DebugOut.Analog[15] = Heading; |
332 | DebugOut.Analog[15] = Heading; |
- | 333 | DebugOut.Analog[16] = ExternData.UserParam[0]; |
|
- | 334 | DebugOut.Analog[17] = ExternData.UserParam[1]; |
|
- | 335 | DebugOut.Analog[18] = AccX; |
|
- | 336 | DebugOut.Analog[19] = AccY; |
|
- | 337 | DebugOut.Analog[20] = AccZ; |
|
- | 338 | DebugOut.Analog[21] = RawAccX; |
|
- | 339 | DebugOut.Analog[22] = RawAccY; |
|
- | 340 | DebugOut.Analog[23] = RawAccZ; |
|
- | 341 | DebugOut.Analog[24] = Calibration.AccX.Offset; |
|
- | 342 | DebugOut.Analog[25] = Calibration.AccY.Offset; |
|
- | 343 | DebugOut.Analog[26] = Calibration.AccZ.Offset; |
|
- | 344 | } |
|
- | 345 | ||
- | 346 | void AccMeasurement(void) |
|
- | 347 | { |
|
- | 348 | if(AccPresent) |
|
- | 349 | { |
|
- | 350 | RawAccX = (RawAccX + (int16_t)ADC_GetValue(ADC2))/2; |
|
- | 351 | RawAccY = (RawAccY + (int16_t)ADC_GetValue(ADC3))/2; |
|
- | 352 | RawAccZ = (RawAccZ + (int16_t)ADC_GetValue(ADC6))/2; |
|
- | 353 | } |
|
- | 354 | else |
|
- | 355 | { |
|
- | 356 | RawAccX = 0; |
|
- | 357 | RawAccY = 0; |
|
- | 358 | RawAccZ = 0; |
|
- | 359 | } |
|
- | 360 | AccX = ((RawAccX - Calibration.AccX.Offset) + AccX * 7) / 8; |
|
279 | DebugOut.Analog[16] = ExternData.UserParam[0]; |
361 | AccY = ((RawAccY - Calibration.AccY.Offset) + AccY * 7) / 8; |
Line -... | Line 362... | ||
- | 362 | AccZ = ((Calibration.AccZ.Offset - RawAccZ) + AccZ * 7) / 8; |
|
280 | DebugOut.Analog[17] = ExternData.UserParam[1]; |
363 | } |
281 | } |
364 | |
282 | 365 | ||
283 | 366 | ||
284 | int main (void) |
367 | int main (void) |
Line 314... | Line 397... | ||
314 | FLIP_LOW; |
397 | FLIP_LOW; |
315 | Delay_ms(2); |
398 | Delay_ms(2); |
316 | RawMagnet1a = ADC_GetValue(ADC0); |
399 | RawMagnet1a = ADC_GetValue(ADC0); |
317 | RawMagnet2a = -ADC_GetValue(ADC1); |
400 | RawMagnet2a = -ADC_GetValue(ADC1); |
318 | RawMagnet3a = ADC_GetValue(ADC7); |
401 | RawMagnet3a = ADC_GetValue(ADC7); |
- | 402 | AccMeasurement(); |
|
319 | Delay_ms(1); |
403 | Delay_ms(1); |
Line 320... | Line -... | ||
320 | - | ||
321 | 404 | ||
322 | FLIP_HIGH; |
405 | FLIP_HIGH; |
323 | Delay_ms(2); |
406 | Delay_ms(2); |
324 | RawMagnet1b = ADC_GetValue(ADC0); |
407 | RawMagnet1b = ADC_GetValue(ADC0); |
325 | RawMagnet2b = -ADC_GetValue(ADC1); |
408 | RawMagnet2b = -ADC_GetValue(ADC1); |
- | 409 | RawMagnet3b = ADC_GetValue(ADC7); |
|
326 | RawMagnet3b = ADC_GetValue(ADC7); |
410 | AccMeasurement(); |
Line 327... | Line 411... | ||
327 | Delay_ms(1); |
411 | Delay_ms(1); |
Line 328... | Line 412... | ||
328 | 412 |