Rev 1646 | Rev 1821 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1612 | dongfang | 1 | //#include "ENC-03_FC1.3.h" |
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 | |||
1646 | - | 11 | const uint8_t GYRO_REVERSED[3] = {0,0,1}; |
12 | const uint8_t ACC_REVERSED[3] = {0,1,0}; |
||
1612 | dongfang | 13 | |
14 | // void gyro_init(void) {} |
||
15 | void gyro_calibrate(void) { |
||
1646 | - | 16 | uint8_t i, axis, factor, numberOfAxesInRange = 0; |
1612 | dongfang | 17 | uint16_t timeout; |
18 | // GyroDefectNick = 0; GyroDefectRoll = 0; GyroDefectYaw = 0; |
||
19 | timeout = SetDelay(2000); |
||
20 | |||
21 | for(i = 140; i != 0; i--) { |
||
22 | // If all 3 axis are in range, shorten the remaining number of iterations. |
||
23 | if(numberOfAxesInRange == 3 && i > 10) i = 9; |
||
24 | numberOfAxesInRange = 0; |
||
25 | |||
1646 | - | 26 | for (axis=PITCH; axis<=YAW; axis++) { |
27 | if (axis==YAW) factor = GYRO_SUMMATION_FACTOR_YAW; |
||
1775 | - | 28 | else factor = GYRO_SUMMATION_FACTOR_PITCHROLL; |
29 | |||
30 | if(rawGyroSum[axis] < 510 * factor) DACValues[axis]--; |
||
31 | else if(rawGyroSum[axis] > 515 * factor) DACValues[axis]++; |
||
32 | else numberOfAxesInRange++; |
||
1612 | dongfang | 33 | |
1775 | - | 34 | /* Gyro is defective. But do keep DAC within bounds (it's an op amp not a differential amp). */ |
35 | if(DACValues[axis] < 10) { |
||
36 | DACValues[axis] = 10; |
||
37 | } else if(DACValues[axis] > 245) { |
||
38 | DACValues[axis] = 245; |
||
39 | } |
||
1612 | dongfang | 40 | } |
1775 | - | 41 | |
1612 | dongfang | 42 | I2C_Start(TWI_STATE_GYRO_OFFSET_TX); // initiate data transmission |
43 | |||
44 | // Wait for I2C to finish transmission. |
||
45 | while(twi_state) { |
||
46 | // Did it take too long? |
||
47 | if(CheckDelay(timeout)) { |
||
1646 | - | 48 | printf("\r\n DAC or I2C Error! check I2C, 3Vref, DAC, and BL-Ctrl"); |
1775 | - | 49 | break; |
1612 | dongfang | 50 | } |
51 | } |
||
52 | |||
53 | analog_start(); |
||
1775 | - | 54 | |
1612 | dongfang | 55 | Delay_ms_Mess(i<10 ? 10 : 2); |
56 | } |
||
57 | Delay_ms_Mess(70); |
||
58 | } |
||
59 | |||
60 | void gyro_setDefaults(void) { |
||
1646 | - | 61 | staticParams.GyroD = 3; |
1775 | - | 62 | staticParams.DriftComp = 250; |
63 | staticParams.GyroAccFactor = 10; |
||
64 | staticParams.GyroAccTrim = 1; |
||
1612 | dongfang | 65 | |
66 | // Not used. |
||
67 | staticParams.AngleTurnOverPitch = 85; |
||
68 | staticParams.AngleTurnOverRoll = 85; |
||
69 | } |