Subversion Repositories FlightCtrl

Rev

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

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