Subversion Repositories FlightCtrl

Rev

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