Rev 1967 | Rev 1971 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1967 | - | 1 | #include "sensors.h" |
1612 | dongfang | 2 | #include "printf_P.h" |
3 | #include "analog.h" |
||
4 | #include "twimaster.h" |
||
5 | #include "configuration.h" |
||
1969 | - | 6 | #include "eeprom.h" |
1612 | dongfang | 7 | #include "timer0.h" |
8 | |||
9 | #define PITCHROLL_MINLIMIT GYRO_SUMMATION_FACTOR_PITCHROLL * 510 |
||
10 | #define PITCHROLL_MAXLIMIT GYRO_SUMMATION_FACTOR_PITCHROLL * 515 |
||
11 | |||
1821 | - | 12 | const uint8_t GYRO_REVERSED[3] = { 0, 0, 1 }; |
13 | const uint8_t ACC_REVERSED[3] = { 0, 1, 0 }; |
||
1612 | dongfang | 14 | |
1967 | - | 15 | void gyro_calibrate(void) { |
16 | printf("gyro_calibrate"); |
||
1821 | - | 17 | uint8_t i, axis, factor, numberOfAxesInRange = 0; |
18 | // GyroDefectNick = 0; GyroDefectRoll = 0; GyroDefectYaw = 0; |
||
1967 | - | 19 | |
1821 | - | 20 | for (i = 140; i != 0; i--) { |
21 | // If all 3 axis are in range, shorten the remaining number of iterations. |
||
22 | if (numberOfAxesInRange == 3 && i > 10) |
||
1964 | - | 23 | i = 10; |
1967 | - | 24 | |
1821 | - | 25 | numberOfAxesInRange = 0; |
26 | |||
27 | for (axis = PITCH; axis <= YAW; axis++) { |
||
28 | if (axis == YAW) |
||
29 | factor = GYRO_SUMMATION_FACTOR_YAW; |
||
30 | else |
||
31 | factor = GYRO_SUMMATION_FACTOR_PITCHROLL; |
||
32 | |||
33 | if (rawGyroSum[axis] < 510 * factor) |
||
1967 | - | 34 | gyroAmplifierOffset.offsets[axis]--; |
1821 | - | 35 | else if (rawGyroSum[axis] > 515 * factor) |
1967 | - | 36 | gyroAmplifierOffset.offsets[axis]++; |
1821 | - | 37 | else |
38 | numberOfAxesInRange++; |
||
39 | |||
40 | /* Gyro is defective. But do keep DAC within bounds (it's an op amp not a differential amp). */ |
||
1967 | - | 41 | if (gyroAmplifierOffset.offsets[axis] < 10) { |
42 | gyroAmplifierOffset.offsets[axis] = 10; |
||
43 | } else if (gyroAmplifierOffset.offsets[axis] > 245) { |
||
44 | gyroAmplifierOffset.offsets[axis] = 245; |
||
1821 | - | 45 | } |
46 | } |
||
1967 | - | 47 | |
48 | gyro_loadOffsets(0); |
||
1821 | - | 49 | |
1967 | - | 50 | delay_ms_with_adc_measurement(i <= 10 ? 10 : 2); |
51 | } |
||
52 | gyroAmplifierOffset_writeToEEProm(); |
||
53 | delay_ms_with_adc_measurement(70); |
||
54 | } |
||
1821 | - | 55 | |
1967 | - | 56 | void gyro_loadOffsets(uint8_t overwriteWithDefaults) { |
57 | uint16_t timeout = setDelay(2000); |
||
1821 | - | 58 | |
1967 | - | 59 | if (overwriteWithDefaults) { |
60 | gyroAmplifierOffset.offsets[PITCH] = |
||
61 | gyroAmplifierOffset.offsets[ROLL] = |
||
62 | gyroAmplifierOffset.offsets[YAW] = (uint8_t)(255 * 1.2089 / 3.0); |
||
1775 | - | 63 | } |
1967 | - | 64 | |
65 | I2C_Start(TWI_STATE_GYRO_OFFSET_TX); // initiate data transmission |
||
66 | // Wait for I2C to finish transmission. |
||
67 | while (twi_state) { |
||
68 | // Did it take too long? |
||
69 | if (checkDelay(timeout)) { |
||
70 | printf("\r\n DAC or I2C Error! check I2C, 3Vref, DAC, and BL-Ctrl"); |
||
71 | break; |
||
72 | } |
||
73 | } |
||
1612 | dongfang | 74 | } |
75 | |||
76 | void gyro_setDefaults(void) { |
||
1960 | - | 77 | staticParams.gyroD = 3; |
78 | staticParams.driftCompDivider = 1; |
||
79 | staticParams.driftCompLimit = 200; |
||
80 | staticParams.zerothOrderCorrection = 25; |
||
1612 | dongfang | 81 | } |