Subversion Repositories FlightCtrl

Rev

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