Subversion Repositories FlightCtrl

Rev

Rev 1864 | Rev 1874 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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