Rev 2112 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2112 | Rev 2189 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | #include "sensors.h" |
1 | #include "sensors.h" |
2 | #include "printf_P.h" |
- | |
3 | #include "analog.h" |
2 | #include "analog.h" |
4 | #include "twimaster.h" |
3 | #include "twimaster.h" |
5 | #include "configuration.h" |
4 | #include "configuration.h" |
6 | #include "eeprom.h" |
5 | #include "eeprom.h" |
7 | #include "timer0.h" |
6 | #include "timer0.h" |
- | 7 | #include "output.h" |
|
- | 8 | ||
- | 9 | #include <stdio.h> |
|
Line 8... | Line 10... | ||
8 | 10 | ||
9 | void I2C_OutputAmplifierOffsets(void) { |
11 | void I2C_OutputAmplifierOffsets(void) { |
10 | uint16_t timeout = setDelay(2000); |
12 | uint16_t timeout = setDelay(2000); |
11 | I2C_Start(TWI_STATE_GYRO_OFFSET_TX); // initiate data transmission |
13 | I2C_Start(TWI_STATE_GYRO_OFFSET_TX); // initiate data transmission |
Line 17... | Line 19... | ||
17 | break; |
19 | break; |
18 | } |
20 | } |
19 | } |
21 | } |
20 | } |
22 | } |
Line -... | Line 23... | ||
- | 23 | ||
- | 24 | uint8_t DACChannelFor(uint8_t axis) { |
|
- | 25 | switch (axis) { |
|
- | 26 | case X: |
|
- | 27 | return 1; |
|
- | 28 | case Y: |
|
- | 29 | return 0; |
|
- | 30 | case Z: |
|
- | 31 | return 2; |
|
- | 32 | default: |
|
- | 33 | return -1; // should never happen. |
|
- | 34 | } |
|
- | 35 | } |
|
21 | 36 | ||
22 | void gyro_calibrate(void) { |
37 | void gyro_calibrate(void) { |
23 | printf("gyro_calibrate"); |
38 | printf("gyro_calibrate"); |
24 | uint8_t i, axis, factor, numberOfAxesInRange = 0; |
39 | uint8_t i, axis, factor, numberOfAxesInRange = 0; |
25 | // GyroDefectNick = 0; GyroDefectRoll = 0; GyroDefectYaw = 0; |
40 | // GyroDefectNick = 0; GyroDefectRoll = 0; GyroDefectYaw = 0; |
26 | 41 | ||
27 | for (i = 140; i != 0; i--) { |
42 | for (i = 200; i != 0; i--) { |
28 | delay_ms_with_adc_measurement(i <= 10 ? 10 : 2, 1); |
- | |
29 | 43 | waitADCCycle(i <= 10 ? 10 : 2); |
|
30 | // If all 3 axis are in range, shorten the remaining number of iterations. |
44 | // If all 3 axis are in range, shorten the remaining number of iterations. |
31 | if (numberOfAxesInRange == 3 && i > 10) i = 10; |
- | |
32 | 45 | if (numberOfAxesInRange == 3 && i > 10) i = 10; |
|
33 | numberOfAxesInRange = 0; |
- | |
34 | 46 | numberOfAxesInRange = 0; |
|
- | 47 | for (axis=X; axis<=Z; axis++) { |
|
35 | for (axis = PITCH; axis <= YAW; axis++) { |
48 | uint8_t dacChannel = DACChannelFor(axis); |
36 | if (axis == YAW) |
49 | if (axis==Z) |
37 | factor = GYRO_OVERSAMPLING_YAW; |
50 | factor=GYRO_OVERSAMPLING_Z; |
38 | else |
51 | else |
Line 39... | Line 52... | ||
39 | factor = GYRO_OVERSAMPLING_PITCHROLL; |
52 | factor=GYRO_OVERSAMPLING_XY; |
40 | 53 | ||
41 | if (rawGyroValue(axis) < 510 * factor) |
54 | if (gyroValueForFC13DACCalibration(axis) < (uint16_t)(510*factor)) |
42 | gyroAmplifierOffset.offsets[axis]--; |
55 | gyroAmplifierOffset.offsets[dacChannel]--; |
43 | else if (rawGyroValue(axis) > 515 * factor) |
56 | else if (gyroValueForFC13DACCalibration(axis) > (uint16_t)(515 * factor)) |
44 | gyroAmplifierOffset.offsets[axis]++; |
57 | gyroAmplifierOffset.offsets[dacChannel]++; |
Line 45... | Line 58... | ||
45 | else |
58 | else |
46 | numberOfAxesInRange++; |
59 | numberOfAxesInRange++; |
47 | 60 | ||
48 | /* Gyro is defective. But do keep DAC within bounds (it's an op amp not a differential amp). */ |
61 | /* Gyro is defective. But do keep DAC within bounds (it's an op amp not a differential amp). */ |
49 | if (gyroAmplifierOffset.offsets[axis] < 10) { |
62 | if (gyroAmplifierOffset.offsets[dacChannel] < 10) { |
50 | gyroAmplifierOffset.offsets[axis] = 10; |
63 | gyroAmplifierOffset.offsets[dacChannel] = 10; |
51 | versionInfo.hardwareErrors[0] |= (FC_ERROR0_GYRO_PITCH << axis); |
64 | versionInfo.hardwareErrors[0] |= (FC_ERROR0_GYRO_X << axis); |
52 | } else if (gyroAmplifierOffset.offsets[axis] > 245) { |
65 | } else if (gyroAmplifierOffset.offsets[dacChannel] > 245) { |
53 | gyroAmplifierOffset.offsets[axis] = 245; |
66 | gyroAmplifierOffset.offsets[dacChannel] = 245; |
Line 54... | Line 67... | ||
54 | versionInfo.hardwareErrors[0] |= (FC_ERROR0_GYRO_PITCH << axis); |
67 | versionInfo.hardwareErrors[0] |= (FC_ERROR0_GYRO_X << axis); |
55 | } |
68 | } |
56 | } |
69 | } |
57 | 70 | ||
58 | I2C_OutputAmplifierOffsets(); |
71 | I2C_OutputAmplifierOffsets(); |
Line 59... | Line 72... | ||
59 | } |
72 | } |
60 | gyroAmplifierOffset_writeToEEProm(); |
73 | gyroAmplifierOffset_writeToEEProm(); |
61 | delay_ms_with_adc_measurement(70, 0); |
74 | waitADCCycle(70); |
62 | } |
75 | } |
63 | 76 | ||
64 | void gyro_init(void) { |
77 | void gyro_init(void) { |
65 | if (gyroAmplifierOffset_readFromEEProm()) { |
78 | if (gyroAmplifierOffset_readFromEEProm()) { |
66 | printf("gyro amp invalid, recalibrate."); |
79 | printf("gyro amp invalid, recalibrate."); |
67 | gyroAmplifierOffset.offsets[PITCH] = |
80 | gyroAmplifierOffset.offsets[X] = |
68 | gyroAmplifierOffset.offsets[ROLL] = |
81 | gyroAmplifierOffset.offsets[Y] = |
Line 69... | Line 82... | ||
69 | gyroAmplifierOffset.offsets[YAW] = (uint8_t)(255 * 1.2089 / 3.0); |
82 | gyroAmplifierOffset.offsets[Z] = (uint8_t)(255 * 1.2089 / 3.0); |
70 | } else { |
83 | } else { |
71 | I2C_OutputAmplifierOffsets(); |
84 | I2C_OutputAmplifierOffsets(); |
72 | } |
85 | } |
73 | } |
86 | } |
74 | - | ||
75 | void gyro_setDefaultParameters(void) { |
- | |
76 | IMUConfig.gyroQuadrant = 0; |
- | |
77 | IMUConfig.accQuadrant = 4; |
87 |