Rev 36 | Rev 41 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 36 | Rev 37 | ||
---|---|---|---|
Line 92... | Line 92... | ||
92 | 92 | ||
93 | Calibration_t eeCalibration EEMEM; // calibration data in EEProm |
93 | Calibration_t eeCalibration EEMEM; // calibration data in EEProm |
Line 94... | Line 94... | ||
94 | Calibration_t Calibration; // calibration data in RAM |
94 | Calibration_t Calibration; // calibration data in RAM |
95 | 95 | ||
96 | // magnet sensor variable |
96 | // magnet sensor variable |
97 | int16_t RawMagnet1a, RawMagnet1b; // raw magnet sensor data |
97 | int16_t RawMagnet1a, RawMagnet1b; // raw magnet sensor data |
98 | int16_t RawMagnet2a, RawMagnet2b; |
98 | int16_t RawMagnet2a, RawMagnet2b; |
99 | int16_t RawMagnet3a, RawMagnet3b; |
99 | int16_t RawMagnet3a, RawMagnet3b; |
Line 100... | Line 100... | ||
100 | int16_t UncalMagX, UncalMagY, UncalMagZ; // sensor signal difference without Scaling |
100 | int16_t UncalMagX, UncalMagY, UncalMagZ; // sensor signal difference without Scaling |
101 | int16_t MagX = 0, MagY = 0, MagZ = 0; // rescaled magnetic field readings |
101 | int16_t MagX = 0, MagY = 0, MagZ = 0; // rescaled magnetic field readings |
102 | 102 | ||
Line 163... | Line 163... | ||
163 | int16_t heading = -1; |
163 | int16_t heading = -1; |
Line 164... | Line 164... | ||
164 | 164 | ||
165 | // blink code for normal operation |
165 | // blink code for normal operation |
166 | if(CheckDelay(Led_Timer)) |
166 | if(CheckDelay(Led_Timer)) |
167 | { |
167 | { |
168 | if(Calibration.Version != CALIBRATION_VERSION) LED_GRN_TOGGLE; |
168 | if(Calibration.Version != CALIBRATION_VERSION) LED_GRN_TOGGLE; |
169 | else LED_GRN_ON; |
169 | else LED_GRN_ON; |
170 | Led_Timer = SetDelay(150); |
170 | Led_Timer = SetDelay(150); |
171 | } |
171 | } |
172 | switch(Orientation) |
172 | switch(Orientation) |
Line 187... | Line 187... | ||
187 | 187 | ||
188 | // calculate nick and roll angle in rad |
188 | // calculate nick and roll angle in rad |
189 | switch(AttitudeSource) |
189 | switch(AttitudeSource) |
190 | { |
190 | { |
191 | case ATTITUDE_SOURCE_I2C: |
191 | case ATTITUDE_SOURCE_I2C: |
192 | cli(); // stop interrupts |
192 | cli(); // stop interrupts |
193 | nick = I2C_WriteAttitude.Nick; |
193 | nick = I2C_WriteAttitude.Nick; |
194 | roll = I2C_WriteAttitude.Roll; |
194 | roll = I2C_WriteAttitude.Roll; |
195 | sei(); // start interrupts |
195 | sei(); // start interrupts |
196 | break; |
196 | break; |
197 | case ATTITUDE_SOURCE_UART: |
197 | case ATTITUDE_SOURCE_UART: |
198 | cli(); // stop interrupts |
198 | cli(); // stop interrupts |
199 | nick = ExternData.Attitude[NICK]; |
199 | nick = ExternData.Attitude[NICK]; |
200 | roll = ExternData.Attitude[ROLL]; |
200 | roll = ExternData.Attitude[ROLL]; |
201 | sei(); // start interrupts |
201 | sei(); // start interrupts |
202 | break; |
202 | break; |
203 | case ATTITUDE_SOURCE_ACC: |
203 | case ATTITUDE_SOURCE_ACC: |
204 | nick = AccAttitudeNick; |
204 | nick = AccAttitudeNick; |
205 | roll = AccAttitudeRoll; |
205 | roll = AccAttitudeRoll; |
206 | break; |
206 | break; |
207 | default: |
207 | default: |
208 | nick = 0; |
208 | nick = 0; |
209 | roll = 0; |
209 | roll = 0; |
210 | break; |
210 | break; |
Line 211... | Line 211... | ||
211 | } |
211 | } |
212 | 212 | ||
Line 213... | Line 213... | ||
213 | nick_rad = ((double)nick) * M_PI / (double)(1800.0); |
213 | nick_rad = ((double)nick) * M_PI / (double)(1800.0); |
214 | roll_rad = ((double)roll) * M_PI / (double)(1800.0); |
214 | roll_rad = ((double)roll) * M_PI / (double)(1800.0); |
215 | 215 | ||
Line 244... | Line 244... | ||
244 | static int16_t Xmin = 0, Xmax = 0, Ymin = 0, Ymax = 0, Zmin = 0, Zmax = 0; |
244 | static int16_t Xmin = 0, Xmax = 0, Ymin = 0, Ymax = 0, Zmin = 0, Zmax = 0; |
245 | static uint8_t blinkcount = 0; |
245 | static uint8_t blinkcount = 0; |
246 | static uint8_t invert_blinking = 0; |
246 | static uint8_t invert_blinking = 0; |
Line 247... | Line 247... | ||
247 | 247 | ||
248 | // check both sources of communication for calibration request |
248 | // check both sources of communication for calibration request |
249 | if(I2C_WriteCal.CalByte) cal = I2C_WriteCal.CalByte; |
249 | if(I2C_WriteCal.CalByte) cal = I2C_WriteCal.CalByte; |
Line 250... | Line 250... | ||
250 | else cal = ExternData.CalState; |
250 | else cal = ExternData.CalState; |
251 | 251 | ||
252 | 252 | ||
Line 288... | Line 288... | ||
288 | Ymax = -10000; |
288 | Ymax = -10000; |
289 | Zmin = 10000; |
289 | Zmin = 10000; |
290 | Zmax = -10000; |
290 | Zmax = -10000; |
291 | Calibration.AccX.Offset = RawAccX; |
291 | Calibration.AccX.Offset = RawAccX; |
292 | Calibration.AccY.Offset = RawAccY; |
292 | Calibration.AccY.Offset = RawAccY; |
293 | Calibration.AccZ.Offset = RawAccZ; |
293 | Calibration.AccZ.Offset = RawAccZ; |
294 | invert_blinking = 0; |
294 | invert_blinking = 0; |
295 | break; |
295 | break; |
Line 296... | Line 296... | ||
296 | 296 | ||
297 | case 2: // 2nd step of calibration |
297 | case 2: // 2nd step of calibration |
298 | // find Min and Max of the X- and Y-Sensors during rotation in the horizontal plane |
298 | // find Min and Max of the X- and Y-Sensors during rotation in the horizontal plane |
299 | if(UncalMagX < Xmin) Xmin = UncalMagX; |
299 | if(UncalMagX < Xmin) Xmin = UncalMagX; |
300 | if(UncalMagX > Xmax) Xmax = UncalMagX; |
300 | if(UncalMagX > Xmax) Xmax = UncalMagX; |
301 | if(UncalMagY < Ymin) Ymin = UncalMagY; |
301 | if(UncalMagY < Ymin) Ymin = UncalMagY; |
302 | if(UncalMagY > Ymax) Ymax = UncalMagY; |
302 | if(UncalMagY > Ymax) Ymax = UncalMagY; |
303 | invert_blinking = 1; |
303 | invert_blinking = 1; |
Line 304... | Line 304... | ||
304 | break; |
304 | break; |
305 | 305 | ||
306 | case 3: // 3rd step of calibration |
306 | case 3: // 3rd step of calibration |
307 | // used to change the orientation of the MK3MAG vertical to the horizontal plane |
307 | // used to change the orientation of the MK3MAG vertical to the horizontal plane |
Line 308... | Line 308... | ||
308 | invert_blinking = 0; |
308 | invert_blinking = 0; |
309 | break; |
309 | break; |
310 | 310 | ||
311 | case 4: |
311 | case 4: |
312 | // find Min and Max of the Z-Sensor |
312 | // find Min and Max of the Z-Sensor |
313 | if(UncalMagZ < Zmin) Zmin = UncalMagZ; |
313 | if(UncalMagZ < Zmin) Zmin = UncalMagZ; |
Line 314... | Line 314... | ||
314 | if(UncalMagZ > Zmax) Zmax = UncalMagZ; |
314 | if(UncalMagZ > Zmax) Zmax = UncalMagZ; |
315 | invert_blinking = 1; |
315 | invert_blinking = 1; |
316 | break; |
316 | break; |
Line 334... | Line 334... | ||
334 | Led_Timer = SetDelay(2000); |
334 | Led_Timer = SetDelay(2000); |
335 | // reset blinkcode |
335 | // reset blinkcode |
336 | blinkcount = 0; |
336 | blinkcount = 0; |
337 | } |
337 | } |
338 | } |
338 | } |
339 | invert_blinking = 0; |
339 | invert_blinking = 0; |
340 | break; |
340 | break; |
Line 341... | Line 341... | ||
341 | 341 | ||
342 | default: |
342 | default: |
343 | break; |
343 | break; |
Line 389... | Line 389... | ||
389 | DebugOut.Analog[21] = RawAccX; |
389 | DebugOut.Analog[21] = RawAccX; |
390 | DebugOut.Analog[22] = RawAccY; |
390 | DebugOut.Analog[22] = RawAccY; |
391 | DebugOut.Analog[23] = RawAccZ; |
391 | DebugOut.Analog[23] = RawAccZ; |
392 | DebugOut.Analog[24] = Calibration.AccX.Offset; |
392 | DebugOut.Analog[24] = Calibration.AccX.Offset; |
393 | DebugOut.Analog[25] = Calibration.AccY.Offset; |
393 | DebugOut.Analog[25] = Calibration.AccY.Offset; |
394 | DebugOut.Analog[26] = Calibration.AccZ.Offset; |
394 | DebugOut.Analog[26] = Calibration.AccZ.Offset; |
395 | DebugOut.Analog[29] = AttitudeSource; |
395 | DebugOut.Analog[29] = AttitudeSource; |
396 | } |
396 | } |
Line 397... | Line 397... | ||
397 | 397 | ||
398 | void AccMeasurement(void) |
398 | void AccMeasurement(void) |
399 | { |
399 | { |
Line 415... | Line 415... | ||
415 | { |
415 | { |
416 | // reset input pullup |
416 | // reset input pullup |
417 | DDRC &=~((1<<DDC6)); |
417 | DDRC &=~((1<<DDC6)); |
418 | PORTC |= (1<<PORTC6); |
418 | PORTC |= (1<<PORTC6); |
Line 419... | Line 419... | ||
419 | 419 | ||
420 | LED_Init(); |
420 | LED_Init(); |
421 | TIMER0_Init(); |
421 | TIMER0_Init(); |
422 | USART0_Init(); |
422 | USART0_Init(); |
423 | ADC_Init(); |
423 | ADC_Init(); |
Line 424... | Line 424... | ||
424 | I2C_Init(); |
424 | I2C_Init(); |
Line 425... | Line 425... | ||
425 | 425 | ||
426 | sei(); // enable globale interrupts |
426 | sei(); // enable globale interrupts |
427 | 427 | ||
428 | if(AccPresent) |
428 | if(AccPresent) |
Line 429... | Line 429... | ||
429 | { |
429 | { |
Line 430... | Line 430... | ||
430 | USART0_Print("ACC present\n"); |
430 | USART0_Print("ACC present\n"); |
431 | } |
431 | } |
Line 432... | Line 432... | ||
432 | 432 | ||
433 | LED_GRN_ON; |
433 | LED_GRN_ON; |
Line 434... | Line 434... | ||
434 | 434 | ||
435 | Debug_Timer = SetDelay(200); |
435 | Debug_Timer = SetDelay(200); |
Line 436... | Line 436... | ||
436 | Led_Timer = SetDelay(200); |
436 | Led_Timer = SetDelay(200); |
437 | 437 | ||
438 | // read calibration info from eeprom |
438 | // read calibration info from eeprom |
439 | eeprom_read_block(&Calibration, &eeCalibration, sizeof(Calibration)); |
439 | eeprom_read_block(&Calibration, &eeCalibration, sizeof(Calibration)); |
440 | 440 | ||
441 | ExternData.CalState = 0; |
441 | ExternData.CalState = 0; |
442 | I2C_WriteCal.CalByte = 0; |
442 | I2C_WriteCal.CalByte = 0; |
443 | 443 | ||
Line 465... | Line 465... | ||
465 | 465 | ||
466 | if(ExternData.CalState || I2C_WriteCal.CalByte) Calibrate(); |
466 | if(ExternData.CalState || I2C_WriteCal.CalByte) Calibrate(); |
Line 467... | Line 467... | ||
467 | else CalcHeading(); |
467 | else CalcHeading(); |
468 | 468 | ||
Line 469... | Line 469... | ||
469 | // check data from USART |
469 | // check data from USART |
470 | USART0_ProcessRxData(); |
470 | USART0_ProcessRxData(); |
471 | 471 | ||
472 | if(NC_Connected) NC_Connected--; |
472 | if(NC_Connected) NC_Connected--; |
473 | if(FC_Connected) FC_Connected--; |
473 | if(FC_Connected) FC_Connected--; |
474 | // fall back to attitude estimation from acc sensor if NC or FC does'nt send attittude data |
474 | // fall back to attitude estimation from acc sensor if NC or FC does'nt send attittude data |
475 | if(!FC_Connected && ! NC_Connected) |
475 | if(!FC_Connected && ! NC_Connected) |
476 | { |
476 | { |
Line 477... | Line 477... | ||
477 | AttitudeSource = ATTITUDE_SOURCE_ACC; |
477 | AttitudeSource = ATTITUDE_SOURCE_ACC; |
478 | Orientation = ORIENTATION_FC; |
478 | Orientation = ORIENTATION_FC; |
479 | } |
479 | } |
480 | 480 | ||
481 | if(PC_Connected) |
481 | if(PC_Connected) |
482 | { |
482 | { |
483 | USART0_EnableTXD(); |
483 | USART0_EnableTXD(); |
484 | USART0_TransmitTxData(); |
484 | USART0_TransmitTxData(); |
485 | PC_Connected--; |
485 | PC_Connected--; |
486 | } |
486 | } |