Quadrocopter - Freescale K64F - IMU Pololu

Dependencies:   mbed

Fork of frdm_serial by Freescale

Tasks

  • reading from an accelerometer and a gyro placed on Pololu IMUv9 board
  • sending in serial (SCA, SCL) to show time histories on LabVIEW charts (configured with the use of VISA)

Used libraries

  • IMU
  • Kalman

Description

This simple program uses available libraries to read data from an accelerometer and a gyro placed on Pololu board.

Boards

All with serial data transmission, i.e. Freescale FRDM-K64F.

Committer:
Pawel_13
Date:
Mon Dec 08 13:21:33 2014 +0000
Revision:
8:7a22b8294c5d
Quadrocopter - Freescale k64F IMU and serial to NI LabView

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Pawel_13 8:7a22b8294c5d 1 /**
Pawel_13 8:7a22b8294c5d 2 * @author Eric Van den Bulck
Pawel_13 8:7a22b8294c5d 3 *
Pawel_13 8:7a22b8294c5d 4 * @section LICENSE
Pawel_13 8:7a22b8294c5d 5 *
Pawel_13 8:7a22b8294c5d 6 * Copyright (c) 2010 ARM Limited
Pawel_13 8:7a22b8294c5d 7 *
Pawel_13 8:7a22b8294c5d 8 * @section DESCRIPTION
Pawel_13 8:7a22b8294c5d 9 *
Pawel_13 8:7a22b8294c5d 10 * Pololu MinIMU-9 v2 sensor:
Pawel_13 8:7a22b8294c5d 11 * L3GD20 three-axis digital output gyroscope.
Pawel_13 8:7a22b8294c5d 12 * LSM303 three-axis digital accelerometer and magnetometer
Pawel_13 8:7a22b8294c5d 13 *
Pawel_13 8:7a22b8294c5d 14 * Information to build this library:
Pawel_13 8:7a22b8294c5d 15 * 1. The Arduino libraries for this sensor from the Pololu and Adafruit websites, available at gitbub.
Pawel_13 8:7a22b8294c5d 16 * https://github.com/adafruit/Adafruit_L3GD20
Pawel_13 8:7a22b8294c5d 17 * https://github.com/pololu/L3G/tree/master/L3G
Pawel_13 8:7a22b8294c5d 18 * 2. The Rasberry Pi code at https://github.com/idavidstory/goPiCopter/tree/master/io/sensors
Pawel_13 8:7a22b8294c5d 19 * https://github.com/idavidstory/goPiCopter/tree/master/io/sensors
Pawel_13 8:7a22b8294c5d 20 * 3. Information on how to write libraries: http://mbed.org/cookbook/Writing-a-Library
Pawel_13 8:7a22b8294c5d 21 * 4. Information on I2C control: http://mbed.org/users/mbed_official/code/mbed/
Pawel_13 8:7a22b8294c5d 22 * 5. The Youtube videos from Brian Douglas (3 x 15') at http://www.youtube.com/playlist?list=PLUMWjy5jgHK30fkGrufluENJqZmLZkmqI
Pawel_13 8:7a22b8294c5d 23 * http://www.x-io.co.uk/open-source-imu-and-ahrs-algorithms/
Pawel_13 8:7a22b8294c5d 24 * Reading an IMU Without Kalman: The Complementary Filter: http://www.pieter-jan.com/node/11
Pawel_13 8:7a22b8294c5d 25 * setup info on the minIMU-9 v2 on http://forum.pololu.com/viewtopic.php?f=3&t=4801&start=30
Pawel_13 8:7a22b8294c5d 26 */
Pawel_13 8:7a22b8294c5d 27
Pawel_13 8:7a22b8294c5d 28 #include "mbed.h"
Pawel_13 8:7a22b8294c5d 29 #include "IMU.h"
Pawel_13 8:7a22b8294c5d 30
Pawel_13 8:7a22b8294c5d 31 IMU::IMU(PinName sda, PinName scl) : _i2c(sda, scl) {
Pawel_13 8:7a22b8294c5d 32 _i2c.frequency(400000); /* 400kHz, fast mode. */
Pawel_13 8:7a22b8294c5d 33 }
Pawel_13 8:7a22b8294c5d 34
Pawel_13 8:7a22b8294c5d 35 char IMU::init(void) /* returns error upon a non-zero return */
Pawel_13 8:7a22b8294c5d 36 {
Pawel_13 8:7a22b8294c5d 37 char ack, rx, tx[2];
Pawel_13 8:7a22b8294c5d 38 double pi, a, A;
Pawel_13 8:7a22b8294c5d 39
Pawel_13 8:7a22b8294c5d 40 // 1. Initialize selected registers: 2c.read and i2c.write return 0 on success (ack)
Pawel_13 8:7a22b8294c5d 41 // --------------------------------
Pawel_13 8:7a22b8294c5d 42 //
Pawel_13 8:7a22b8294c5d 43 // 1.a Enable L3DG20 gyrosensor and set operational mode:
Pawel_13 8:7a22b8294c5d 44 // CTRL_REG1: set to 0x1F = 0001-1111 --> enable sensor, DR= 95Hz, LPF-Cut-off-freq=25Hz.
Pawel_13 8:7a22b8294c5d 45 // CTRL_REG1: set to 0x5F = 0101-1111 --> enable sensor, DR=190Hz, LPF-Cut-off-freq=25Hz.
Pawel_13 8:7a22b8294c5d 46 // CTRL_REG4: left at default = 0x00 --> Full Scale = 250 degrees/second --> Sensitivity = 0.00875 dps/digit.
Pawel_13 8:7a22b8294c5d 47 address = L3GD20_ADDR;
Pawel_13 8:7a22b8294c5d 48 tx[0] = L3GD20_CTRL_REG1; // address contrl_register 1
Pawel_13 8:7a22b8294c5d 49 tx[1] = 0x1F; // 00-01-1-111 enable sensor and set operational mode.
Pawel_13 8:7a22b8294c5d 50 ack = _i2c.write(address, tx, 2);
Pawel_13 8:7a22b8294c5d 51 ack |= _i2c.write(address, tx, 1);
Pawel_13 8:7a22b8294c5d 52 ack |= _i2c.read(address+1, &rx, 1); if (rx != 0x1F) ack |= 1;
Pawel_13 8:7a22b8294c5d 53 //
Pawel_13 8:7a22b8294c5d 54 // 1.b Enable LSM303 accelerometer and set operational mode:
Pawel_13 8:7a22b8294c5d 55 // CTRL_REG1: set to 0x37 = 0011 1111 --> DR = 25Hz & enable sensor in low power mode (normal mode zmienic ostatnie 4 na 0111)
Pawel_13 8:7a22b8294c5d 56 // CTRL_REG1: set to 0x47 = 0100 1111 --> DR = 50Hz & enable sensor -//
Pawel_13 8:7a22b8294c5d 57 // CTRL_REG1: set to 0x57 = 0101 1111 --> DR = 100Hz & enable sensor
Pawel_13 8:7a22b8294c5d 58 // CTRL_REG1: set to 0x67 = 0110 1111 --> DR = 200Hz & enable sensor
Pawel_13 8:7a22b8294c5d 59 // CTRL_REG1: set to 0x77 = 0111 0111 --> DR = 400Hz & enable sensor (0x7F low power mode - b duze oscylacje)
Pawel_13 8:7a22b8294c5d 60 // CTRL_REG4: set to 0x08 = 0000 1000 --> Full Scale = +/- 2G & high resolution --> Sensitivity = 0.001G/digit.
Pawel_13 8:7a22b8294c5d 61 address = LSM303_A_ADDR;
Pawel_13 8:7a22b8294c5d 62 tx[0] = LSM303_A_CTRL_REG1;
Pawel_13 8:7a22b8294c5d 63 tx[1] = 0x77; // --> 400 Hz Data rate speed - p.24/42 of datasheet
Pawel_13 8:7a22b8294c5d 64 ack |= _i2c.write(address, tx, 2);
Pawel_13 8:7a22b8294c5d 65 ack |= _i2c.write(address, tx, 1);
Pawel_13 8:7a22b8294c5d 66 ack |= _i2c.read(address+1, &rx, 1); if (rx != 0x77) ack |= 1;
Pawel_13 8:7a22b8294c5d 67 tx[0] = LSM303_A_CTRL_REG4;
Pawel_13 8:7a22b8294c5d 68 //tx[1] = 0x08; // 0000 1000 enable high resolution mode + selects default 2G scale. p.26/42
Pawel_13 8:7a22b8294c5d 69 tx[1] = 0x18; // 0001 1000 enable high resolution mode + selects 4G scale.
Pawel_13 8:7a22b8294c5d 70 ack |= _i2c.write(address, tx ,2);
Pawel_13 8:7a22b8294c5d 71 ack |= _i2c.write(address, tx, 1);
Pawel_13 8:7a22b8294c5d 72 ack |= _i2c.read(address+1, &rx, 1); if (rx != 0x18) ack |= 1;
Pawel_13 8:7a22b8294c5d 73 //
Pawel_13 8:7a22b8294c5d 74 // 1.c enable LSM303 magnetometer and set operational mode:
Pawel_13 8:7a22b8294c5d 75 // CRA_REG is reset from 0x10 to 0x14 = 00010100 --> 30 Hz data output rate.
Pawel_13 8:7a22b8294c5d 76 // CRA_REG is reset from 0x10 to 0x18 = 00011000 --> 75 Hz data output rate.
Pawel_13 8:7a22b8294c5d 77 // CRA_REG is reset from 0x10 to 0x1C = 00011100 --> 220 Hz data output rate.
Pawel_13 8:7a22b8294c5d 78 // CRB_REG is kept at default = 00100000 = 0x20 --> range +/- 1.3 Gauss, Gain = 1100/980(Z) LSB/Gauss.
Pawel_13 8:7a22b8294c5d 79 // MR_REG is reset from 0x03 to 0x00 -> continuos conversion mode in stead of sleep mode.
Pawel_13 8:7a22b8294c5d 80 address = LSM303_M_ADDR;
Pawel_13 8:7a22b8294c5d 81 tx[0] = LSM303_M_CRA_REG;
Pawel_13 8:7a22b8294c5d 82 tx[1] = 0x18; // --> 75 Hz minimum output rate - p.36/42 of datasheet
Pawel_13 8:7a22b8294c5d 83 ack |= _i2c.write(address, tx, 2);
Pawel_13 8:7a22b8294c5d 84 ack |= _i2c.write(address, tx, 1);
Pawel_13 8:7a22b8294c5d 85 ack |= _i2c.read(address+1, &rx, 1); if (rx != 0x18) ack |= 1;
Pawel_13 8:7a22b8294c5d 86 tx[0] = LSM303_M_MR_REG;
Pawel_13 8:7a22b8294c5d 87 tx[1] = 0x00; // 0000 0000 --> continuous-conversion mode 25 Hz Data rate speed - p.24/42 of datasheet
Pawel_13 8:7a22b8294c5d 88 ack |= _i2c.write(address, tx, 2);
Pawel_13 8:7a22b8294c5d 89 ack |= _i2c.write(address, tx, 1);
Pawel_13 8:7a22b8294c5d 90 ack |= _i2c.read(address+1, &rx, 1); if (rx != 0x00) ack |= 1;
Pawel_13 8:7a22b8294c5d 91
Pawel_13 8:7a22b8294c5d 92 // 2. Initialize calibration constants with predetermined values.
Pawel_13 8:7a22b8294c5d 93 // acceleration:
Pawel_13 8:7a22b8294c5d 94 // My calibration values, vs. the website http://rwsarduino.blogspot.be/2013/01/inertial-orientation-sensing.html
Pawel_13 8:7a22b8294c5d 95
Pawel_13 8:7a22b8294c5d 96 /* my predetermined static bias counts */
Pawel_13 8:7a22b8294c5d 97 L3GD20_biasX = (int16_t) 97; // BTLO 90 /* digit counts */
Pawel_13 8:7a22b8294c5d 98 L3GD20_biasY = (int16_t) 5; //BYLO 180
Pawel_13 8:7a22b8294c5d 99 L3GD20_biasZ = (int16_t) 140; //BYLO -10
Pawel_13 8:7a22b8294c5d 100
Pawel_13 8:7a22b8294c5d 101 /* reference gravity acceleration */
Pawel_13 8:7a22b8294c5d 102 //g_0 = 9.815; //przy ustawieniu zakresu na +/- 2g
Pawel_13 8:7a22b8294c5d 103 g_0 = 19.63; //przy ustawienu zakresu na +/- 4g // CTR_REG 4 = 0001 1000 0x18
Pawel_13 8:7a22b8294c5d 104
Pawel_13 8:7a22b8294c5d 105 /* filter parameters: assume 400 Hz sampling rare and 2nd orcer Butterworth filter with fc = 5Hz. */
Pawel_13 8:7a22b8294c5d 106 pi = 3.1415926536;
Pawel_13 8:7a22b8294c5d 107 A = tan(pi*5/400); a = 1 + sqrt(2.0)*A + A*A;
Pawel_13 8:7a22b8294c5d 108 FF[1] = 2*(A*A-1)/a;
Pawel_13 8:7a22b8294c5d 109 FF[2] = (1-sqrt(2.0)*A+A*A)/a;
Pawel_13 8:7a22b8294c5d 110 FF[0] = (1+FF[1]+FF[2])/4;
Pawel_13 8:7a22b8294c5d 111
Pawel_13 8:7a22b8294c5d 112 return ack;
Pawel_13 8:7a22b8294c5d 113 }
Pawel_13 8:7a22b8294c5d 114
Pawel_13 8:7a22b8294c5d 115 char IMU::readData(float *d)
Pawel_13 8:7a22b8294c5d 116 {
Pawel_13 8:7a22b8294c5d 117 char ack, reg, D[6];
Pawel_13 8:7a22b8294c5d 118 int16_t W[3];
Pawel_13 8:7a22b8294c5d 119
Pawel_13 8:7a22b8294c5d 120 // report the data in rad/s
Pawel_13 8:7a22b8294c5d 121 // gyro data are 16 bit readings per axis, stored: X_l, X_h, Y_l, Y_h, Z_l, Z_h
Pawel_13 8:7a22b8294c5d 122 // #define L3GD20_SENSITIVITY_250DPS 0.00875 --- #define L3GD20_DPS_TO_RADS 0.017453293
Pawel_13 8:7a22b8294c5d 123 address = L3GD20_ADDR;
Pawel_13 8:7a22b8294c5d 124 reg = L3GD20_OUT_X_L | 0x80; // set address auto-increment bit
Pawel_13 8:7a22b8294c5d 125 ack = _i2c.write(address,&reg,1); ack |= _i2c.read(address+1,D,6);
Pawel_13 8:7a22b8294c5d 126 W[0] = (int16_t) (D[1] << 8 | D[0]);
Pawel_13 8:7a22b8294c5d 127 W[1] = (int16_t) (D[3] << 8 | D[2]);
Pawel_13 8:7a22b8294c5d 128 W[2] = (int16_t) (D[5] << 8 | D[4]);
Pawel_13 8:7a22b8294c5d 129 *(d+0) = (float) 0.971*(W[0]-L3GD20_biasX)*L3GD20_SENSITIVITY_250DPS*L3GD20_DPS_TO_RADS;
Pawel_13 8:7a22b8294c5d 130 *(d+1) = (float) 0.998*(W[1]-L3GD20_biasY)*L3GD20_SENSITIVITY_250DPS*L3GD20_DPS_TO_RADS;
Pawel_13 8:7a22b8294c5d 131 *(d+2) = (float) 1.002*(W[2]-L3GD20_biasZ)*L3GD20_SENSITIVITY_250DPS*L3GD20_DPS_TO_RADS;
Pawel_13 8:7a22b8294c5d 132
Pawel_13 8:7a22b8294c5d 133 // Accelerometer data are stored as 12 bit readings, left justified per axis.
Pawel_13 8:7a22b8294c5d 134 // The data needs to be shifted 4 digits to the right! This is not general, only for the A measurement.
Pawel_13 8:7a22b8294c5d 135 address = LSM303_A_ADDR;
Pawel_13 8:7a22b8294c5d 136 reg = LSM303_A_OUT_X_L | 0x80; // set address auto-increment bit
Pawel_13 8:7a22b8294c5d 137 ack |= _i2c.write(address,&reg,1); ack |= _i2c.read(address+1,D,6);
Pawel_13 8:7a22b8294c5d 138 W[0] = ((int16_t) (D[1] << 8 | D[0])) >> 4;
Pawel_13 8:7a22b8294c5d 139 W[1] = ((int16_t) (D[3] << 8 | D[2])) >> 4;
Pawel_13 8:7a22b8294c5d 140 W[2] = ((int16_t) (D[5] << 8 | D[4])) >> 4;
Pawel_13 8:7a22b8294c5d 141 *(d+3) = (float) g_0*0.986*(W[0]+18)/1000; // kalibracja - zmiana z 0.991 na 0.986 i z +34 na +18
Pawel_13 8:7a22b8294c5d 142 *(d+4) = (float) g_0*0.99*(W[1]-4)/1000; // kalibracja - zmiana z 0.970 na 0.99 i z +2 na -4
Pawel_13 8:7a22b8294c5d 143 *(d+5) = (float) g_0*0.983*(W[2]+9)/1000; // kalibracja - zmiana +28 na +9
Pawel_13 8:7a22b8294c5d 144 // GN = 001
Pawel_13 8:7a22b8294c5d 145 // Magnetometer; are stored as 12 bit readings, right justified per axis.
Pawel_13 8:7a22b8294c5d 146 address = LSM303_M_ADDR;
Pawel_13 8:7a22b8294c5d 147 reg = LSM303_M_OUT_X_H | 0x80; // set address auto-increment bit
Pawel_13 8:7a22b8294c5d 148 ack |= _i2c.write(address,&reg,1); ack |= _i2c.read(address+1,D,6);
Pawel_13 8:7a22b8294c5d 149 W[0] = ((int16_t) (D[0] << 8 | D[1]));
Pawel_13 8:7a22b8294c5d 150 W[1] = ((int16_t) (D[4] << 8 | D[5]));
Pawel_13 8:7a22b8294c5d 151 W[2] = ((int16_t) (D[2] << 8 | D[3]));
Pawel_13 8:7a22b8294c5d 152 *(d+6) = (float) 2.813*(W[0]-220)/1100; //Z bylo 264
Pawel_13 8:7a22b8294c5d 153 *(d+7) = (float) 2.822*(W[1]+ 230)/1100; //X bylo
Pawel_13 8:7a22b8294c5d 154 *(d+8) = (float) 2.880*(W[2]- 380)/980; //Y
Pawel_13 8:7a22b8294c5d 155
Pawel_13 8:7a22b8294c5d 156 return ack;
Pawel_13 8:7a22b8294c5d 157 }
Pawel_13 8:7a22b8294c5d 158
Pawel_13 8:7a22b8294c5d 159 void IMU::filterData(float *d, double *D)
Pawel_13 8:7a22b8294c5d 160 // 2nd order Butterworth filter. Filter coefficients FF computed in function init.
Pawel_13 8:7a22b8294c5d 161 {
Pawel_13 8:7a22b8294c5d 162 for (int i=0; i<9; ++i) {
Pawel_13 8:7a22b8294c5d 163 // *(FD+9*i+2) = *(FD+9*i+1); *(FD+9*i+1) = *(FD+9*i); *(FD+9*i) = (double) d[i];
Pawel_13 8:7a22b8294c5d 164 FD[2][i] = FD[1][i]; FD[1][i] = FD[0][i]; FD[0][i] = (double) d[i];
Pawel_13 8:7a22b8294c5d 165 FD[5][i] = FD[4][i]; FD[4][i] = FD[3][i];
Pawel_13 8:7a22b8294c5d 166 FD[3][i] = FF[0]*(FD[0][i] + 2*FD[1][i] + FD[2][i]) - FF[1]*FD[4][i] - FF[2]*FD[5][i];
Pawel_13 8:7a22b8294c5d 167 D[i] = FD[3][i];
Pawel_13 8:7a22b8294c5d 168 }
Pawel_13 8:7a22b8294c5d 169 // D[0] = FD[0][2]; D[1] = FD[1][2]; D[2] = FD[2][2];
Pawel_13 8:7a22b8294c5d 170 }