Subversion Repositories FlightCtrl

Rev

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

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