Sensor library for quadcopter

Dependencies:   MotionDriverv512

Committer:
oprospero
Date:
Tue Jun 03 07:57:32 2014 +0000
Revision:
0:5c097bb1300f
Child:
1:fe68e0f86ecf
Initial Sensor Lib for quadcopter;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
oprospero 0:5c097bb1300f 1
oprospero 0:5c097bb1300f 2 #include "Sensor.h"
oprospero 0:5c097bb1300f 3 #ifdef DEBUG_S
oprospero 0:5c097bb1300f 4 #include "mbed.h"
oprospero 0:5c097bb1300f 5 Serial p(USBTX, USBRX);
oprospero 0:5c097bb1300f 6 Timer td;
oprospero 0:5c097bb1300f 7 int printCounter = 0;
oprospero 0:5c097bb1300f 8 int tIntStatus = 0;
oprospero 0:5c097bb1300f 9 int tgetFIFOCount = 0;
oprospero 0:5c097bb1300f 10 int tgetFIFOCheck = 0;
oprospero 0:5c097bb1300f 11 int tgetFIFOreset = 0;
oprospero 0:5c097bb1300f 12 int tgetFIFOBytes = 0;
oprospero 0:5c097bb1300f 13 int teulars = 0;
oprospero 0:5c097bb1300f 14 #define NL "\n\r"
oprospero 0:5c097bb1300f 15 #define PRINT(x) p.printf(x) //Serial.print(x)
oprospero 0:5c097bb1300f 16 #define PRINTF p.printf //Serial.print(x, y)
oprospero 0:5c097bb1300f 17 #define PRINTLN(x) PRINT(x);PRINT(NL)
oprospero 0:5c097bb1300f 18 #define START td.start();
oprospero 0:5c097bb1300f 19 #define STOP td.stop()
oprospero 0:5c097bb1300f 20 #define RESET td.reset()
oprospero 0:5c097bb1300f 21 #define READ td.read_us()
oprospero 0:5c097bb1300f 22 #define GET(x) x = READ
oprospero 0:5c097bb1300f 23 #else
oprospero 0:5c097bb1300f 24 #define PRINT(x)
oprospero 0:5c097bb1300f 25 #define PRINTF(x, y)
oprospero 0:5c097bb1300f 26 #define PRINTLN(x)
oprospero 0:5c097bb1300f 27 #define START
oprospero 0:5c097bb1300f 28 #define STOP
oprospero 0:5c097bb1300f 29 #define RESET
oprospero 0:5c097bb1300f 30 #define READ
oprospero 0:5c097bb1300f 31 #define GET(x)
oprospero 0:5c097bb1300f 32 #endif
oprospero 0:5c097bb1300f 33
oprospero 0:5c097bb1300f 34
oprospero 0:5c097bb1300f 35 Sensor::Sensor() : dmp()
oprospero 0:5c097bb1300f 36 {
oprospero 0:5c097bb1300f 37 gyro_orientation[0] = -1;
oprospero 0:5c097bb1300f 38 gyro_orientation[1] = 0;
oprospero 0:5c097bb1300f 39 gyro_orientation[2] = 0;
oprospero 0:5c097bb1300f 40 gyro_orientation[3] = 0;
oprospero 0:5c097bb1300f 41 gyro_orientation[4] = -1;
oprospero 0:5c097bb1300f 42 gyro_orientation[5] = 0;
oprospero 0:5c097bb1300f 43 gyro_orientation[6] = 0;
oprospero 0:5c097bb1300f 44 gyro_orientation[7] = 0;
oprospero 0:5c097bb1300f 45 gyro_orientation[8] = 1;
oprospero 0:5c097bb1300f 46 //InvMpu mpu;
oprospero 0:5c097bb1300f 47 }
oprospero 0:5c097bb1300f 48
oprospero 0:5c097bb1300f 49 int Sensor::initialize()
oprospero 0:5c097bb1300f 50 {
oprospero 0:5c097bb1300f 51 int result = 0;
oprospero 0:5c097bb1300f 52 result -= mpu_initialize();
oprospero 0:5c097bb1300f 53 result -= dmp_initialize();
oprospero 0:5c097bb1300f 54 run_self_test();
oprospero 0:5c097bb1300f 55 return result;
oprospero 0:5c097bb1300f 56 }
oprospero 0:5c097bb1300f 57
oprospero 0:5c097bb1300f 58 int Sensor::mpu_initialize()
oprospero 0:5c097bb1300f 59 {
oprospero 0:5c097bb1300f 60 int result = 0;
oprospero 0:5c097bb1300f 61 unsigned char accel_fsr;
oprospero 0:5c097bb1300f 62 unsigned short gyro_rate, gyro_fsr;
oprospero 0:5c097bb1300f 63 long accel_bias[3], gyro_bias[3];
oprospero 0:5c097bb1300f 64 result -= dmp.testConnection() ;
oprospero 0:5c097bb1300f 65 result -= dmp.mpu_init();
oprospero 0:5c097bb1300f 66 /* Get/set hardware configuration. Start gyro. */
oprospero 0:5c097bb1300f 67 /* Wake up all sensors. */
oprospero 0:5c097bb1300f 68 result -= dmp.mpu_set_sensors(INV_XYZ_GYRO | INV_XYZ_ACCEL);
oprospero 0:5c097bb1300f 69 /* Push both gyro and accel data into the FIFO. */
oprospero 0:5c097bb1300f 70 result -= dmp.mpu_configure_fifo(INV_XYZ_GYRO | INV_XYZ_ACCEL);
oprospero 0:5c097bb1300f 71 result -= dmp.mpu_set_sample_rate(SAMPLE_RATE);
oprospero 0:5c097bb1300f 72 /* Read back configuration in case it was set improperly. */
oprospero 0:5c097bb1300f 73 dmp.mpu_get_sample_rate(&gyro_rate);
oprospero 0:5c097bb1300f 74 dmp.mpu_get_gyro_fsr(&gyro_fsr);
oprospero 0:5c097bb1300f 75 dmp.mpu_get_accel_fsr(&accel_fsr);
oprospero 0:5c097bb1300f 76
oprospero 0:5c097bb1300f 77 if( !(gyro_rate == SAMPLE_RATE && gyro_fsr == 2000 && accel_fsr == 2) )
oprospero 0:5c097bb1300f 78 result -= 1;
oprospero 0:5c097bb1300f 79
oprospero 0:5c097bb1300f 80 // dmp.mpu_read_6500_gyro_bias(accel_bias);
oprospero 0:5c097bb1300f 81 if (dmp.mpu_run_self_test(gyro_bias,accel_bias) != 0x07);
oprospero 0:5c097bb1300f 82 result -= 1;
oprospero 0:5c097bb1300f 83 return result;
oprospero 0:5c097bb1300f 84 }
oprospero 0:5c097bb1300f 85
oprospero 0:5c097bb1300f 86 int Sensor::dmp_initialize()
oprospero 0:5c097bb1300f 87 {
oprospero 0:5c097bb1300f 88 int result = 0;
oprospero 0:5c097bb1300f 89 // Instruction via MotionDriver_Tutorial
oprospero 0:5c097bb1300f 90 result -= dmp.dmp_load_motion_driver_firmware();
oprospero 0:5c097bb1300f 91 unsigned short temp = dmp.inv_orientation_matrix_to_scalar(gyro_orientation);
oprospero 0:5c097bb1300f 92 result -= dmp.dmp_set_orientation(temp);
oprospero 0:5c097bb1300f 93 // unsigned short mask = DMP_FEATURE_6X_LP_QUAT | DMP_FEATURE_SEND_CAL_GYRO | DMP_FEATURE_GYRO_CAL | DMP_FEATURE_SEND_RAW_ACCEL ;
oprospero 0:5c097bb1300f 94 unsigned short mask = DMP_FEATURE_6X_LP_QUAT | DMP_FEATURE_SEND_CAL_GYRO | DMP_FEATURE_GYRO_CAL ;
oprospero 0:5c097bb1300f 95 result -= dmp.dmp_enable_feature(mask);
oprospero 0:5c097bb1300f 96 result -= dmp.dmp_set_fifo_rate(100);
oprospero 0:5c097bb1300f 97 result -= dmp.mpu_set_dmp_state(1);
oprospero 0:5c097bb1300f 98 return result;
oprospero 0:5c097bb1300f 99 }
oprospero 0:5c097bb1300f 100
oprospero 0:5c097bb1300f 101
oprospero 0:5c097bb1300f 102 void Sensor::run_self_test()
oprospero 0:5c097bb1300f 103 {
oprospero 0:5c097bb1300f 104 int result;
oprospero 0:5c097bb1300f 105 long gyro_cal[3], accel_cal[3];
oprospero 0:5c097bb1300f 106 unsigned char i = 0;
oprospero 0:5c097bb1300f 107
oprospero 0:5c097bb1300f 108 result = dmp.mpu_run_self_test(gyro_cal, accel_cal);
oprospero 0:5c097bb1300f 109 if (result == 0x7) {
oprospero 0:5c097bb1300f 110 /* Test passed. We can trust the gyro data here, so let's push it down
oprospero 0:5c097bb1300f 111 * to the DMP.
oprospero 0:5c097bb1300f 112 */
oprospero 0:5c097bb1300f 113 for(i = 0; i<3; i++) {
oprospero 0:5c097bb1300f 114 gyro_cal[i] = (long)(gyro_cal[i] * 32.8f); //convert to +-1000dps
oprospero 0:5c097bb1300f 115 accel_cal[i] *= 4096.f; //convert to +-8G
oprospero 0:5c097bb1300f 116 accel_cal[i] = accel[i] >> 16;
oprospero 0:5c097bb1300f 117 gyro_cal[i] = (long)(gyro_cal[i] >> 16);
oprospero 0:5c097bb1300f 118 }
oprospero 0:5c097bb1300f 119 dmp.mpu_set_gyro_bias_reg(gyro_cal);
oprospero 0:5c097bb1300f 120 dmp.mpu_set_accel_bias_6050_reg(accel_cal);
oprospero 0:5c097bb1300f 121 }
oprospero 0:5c097bb1300f 122 }
oprospero 0:5c097bb1300f 123
oprospero 0:5c097bb1300f 124 int Sensor::updateAngles()
oprospero 0:5c097bb1300f 125 {
oprospero 0:5c097bb1300f 126 bool result = false;
oprospero 0:5c097bb1300f 127 long quat[4];
oprospero 0:5c097bb1300f 128 unsigned char more;
oprospero 0:5c097bb1300f 129 short sensors;
oprospero 0:5c097bb1300f 130 result = dmp.dmp_read_fifo(gyro, accel, quat, NULL, &sensors, &more);
oprospero 0:5c097bb1300f 131 if (result == 0)
oprospero 0:5c097bb1300f 132 {
oprospero 0:5c097bb1300f 133 q.set(quat);
oprospero 0:5c097bb1300f 134 dmp.dmpGetGravity(&gravity,&q);
oprospero 0:5c097bb1300f 135 dmp.dmpGetYawPitchRoll(ypr,&q,&gravity);
oprospero 0:5c097bb1300f 136 }
oprospero 0:5c097bb1300f 137 // PRINTF("More: %d\n\r",more);
oprospero 0:5c097bb1300f 138 return more;
oprospero 0:5c097bb1300f 139 }
oprospero 0:5c097bb1300f 140
oprospero 0:5c097bb1300f 141 float Sensor::getPitch()
oprospero 0:5c097bb1300f 142 {
oprospero 0:5c097bb1300f 143 return ypr[1] * RAD2DEG;
oprospero 0:5c097bb1300f 144 }
oprospero 0:5c097bb1300f 145 float Sensor::getRoll()
oprospero 0:5c097bb1300f 146 {
oprospero 0:5c097bb1300f 147 return ypr[2] * RAD2DEG;
oprospero 0:5c097bb1300f 148 }
oprospero 0:5c097bb1300f 149 float Sensor::getYaw()
oprospero 0:5c097bb1300f 150 {
oprospero 0:5c097bb1300f 151 return ypr[0] * RAD2DEG;
oprospero 0:5c097bb1300f 152 }
oprospero 0:5c097bb1300f 153
oprospero 0:5c097bb1300f 154 float Sensor::getGyroPitch()
oprospero 0:5c097bb1300f 155 {
oprospero 0:5c097bb1300f 156 return gyro[1] * GYRO_GAIN;
oprospero 0:5c097bb1300f 157 }
oprospero 0:5c097bb1300f 158 float Sensor::getGyroRoll()
oprospero 0:5c097bb1300f 159 {
oprospero 0:5c097bb1300f 160 return gyro[2] * GYRO_GAIN;
oprospero 0:5c097bb1300f 161 }
oprospero 0:5c097bb1300f 162 float Sensor::getGyroYaw()
oprospero 0:5c097bb1300f 163 {
oprospero 0:5c097bb1300f 164 return gyro[0] * GYRO_GAIN;
oprospero 0:5c097bb1300f 165 }