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