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