Framework of classes and program to measure tilt angles using accelerometers
Fork of tilt_angles by
ENGO333_MPU9150.h@0:3bffc1862262, 2016-11-24 (annotated)
- Committer:
- mpetovello
- Date:
- Thu Nov 24 23:02:42 2016 +0000
- Revision:
- 0:3bffc1862262
Lab 10 draft, 2016
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mpetovello | 0:3bffc1862262 | 1 | /** |
mpetovello | 0:3bffc1862262 | 2 | * File : ENGO333_MPU9150.h |
mpetovello | 0:3bffc1862262 | 3 | * Created by : Chandra Tjhai |
mpetovello | 0:3bffc1862262 | 4 | * Created on : September 19, 2016 |
mpetovello | 0:3bffc1862262 | 5 | * |
mpetovello | 0:3bffc1862262 | 6 | * Description : |
mpetovello | 0:3bffc1862262 | 7 | * This library is created for ENGO 333 class. The inertial sensor needs to be |
mpetovello | 0:3bffc1862262 | 8 | * mounted on the mbed Application Board. The I2C connection is automatically |
mpetovello | 0:3bffc1862262 | 9 | * set to p28 (SDA) and p27 (SCL). |
mpetovello | 0:3bffc1862262 | 10 | */ |
mpetovello | 0:3bffc1862262 | 11 | |
mpetovello | 0:3bffc1862262 | 12 | #ifndef ENGO333_MPU9150_H |
mpetovello | 0:3bffc1862262 | 13 | #define ENGO333_MPU9150_H |
mpetovello | 0:3bffc1862262 | 14 | |
mpetovello | 0:3bffc1862262 | 15 | #include "ENGO333_I2C.h" |
mpetovello | 0:3bffc1862262 | 16 | #include "Tiltmeter.h" |
mpetovello | 0:3bffc1862262 | 17 | |
mpetovello | 0:3bffc1862262 | 18 | /** |
mpetovello | 0:3bffc1862262 | 19 | * Define Macros: Device I2C slave addresses (R/W) |
mpetovello | 0:3bffc1862262 | 20 | */ |
mpetovello | 0:3bffc1862262 | 21 | #define MPU9150_ADDRESS (0x68 << 1) |
mpetovello | 0:3bffc1862262 | 22 | |
mpetovello | 0:3bffc1862262 | 23 | /** |
mpetovello | 0:3bffc1862262 | 24 | * Define Macros: Configuration Registers |
mpetovello | 0:3bffc1862262 | 25 | */ |
mpetovello | 0:3bffc1862262 | 26 | #define MPU9150_CONFIG_REG 0x1A |
mpetovello | 0:3bffc1862262 | 27 | #define MPU9150_ACCEL_CONFIG_REG 0x1C |
mpetovello | 0:3bffc1862262 | 28 | #define MPU9150_INT_STATUS_REG 0x3A |
mpetovello | 0:3bffc1862262 | 29 | #define MPU9150_INT_PIN_CFG_REG 0x37 |
mpetovello | 0:3bffc1862262 | 30 | #define MPU9150_PWR_MGMT_1_REG 0x6B |
mpetovello | 0:3bffc1862262 | 31 | #define MPU9150_WHO_AM_I_REG 0x75 |
mpetovello | 0:3bffc1862262 | 32 | |
mpetovello | 0:3bffc1862262 | 33 | /** |
mpetovello | 0:3bffc1862262 | 34 | * Define Macros: Measurement Data Registers |
mpetovello | 0:3bffc1862262 | 35 | */ |
mpetovello | 0:3bffc1862262 | 36 | #define MPU9150_ACCEL_XOUT_H_REG 0x3B |
mpetovello | 0:3bffc1862262 | 37 | #define MPU9150_ACCEL_YOUT_H_REG 0x3D |
mpetovello | 0:3bffc1862262 | 38 | #define MPU9150_ACCEL_ZOUT_H_REG 0x3F |
mpetovello | 0:3bffc1862262 | 39 | |
mpetovello | 0:3bffc1862262 | 40 | /** |
mpetovello | 0:3bffc1862262 | 41 | * Define Macros: IMU Definitions |
mpetovello | 0:3bffc1862262 | 42 | */ |
mpetovello | 0:3bffc1862262 | 43 | #define MPU9150_SLEEP_BIT 6 |
mpetovello | 0:3bffc1862262 | 44 | #define MPU9150_ACCEL_RANGE_2G 0 |
mpetovello | 0:3bffc1862262 | 45 | #define MPU9150_ACCEL_RANGE_4G 1 |
mpetovello | 0:3bffc1862262 | 46 | #define MPU9150_ACCEL_RANGE_8G 2 |
mpetovello | 0:3bffc1862262 | 47 | #define MPU9150_ACCEL_RANGE_16G 3 |
mpetovello | 0:3bffc1862262 | 48 | #define MPU9150_I_AM 0x68 |
mpetovello | 0:3bffc1862262 | 49 | #define MPU9150_I2C_FAST_MODE 400000 |
mpetovello | 0:3bffc1862262 | 50 | #define MPU9150_I2C_STD_MODE 100000 |
mpetovello | 0:3bffc1862262 | 51 | |
mpetovello | 0:3bffc1862262 | 52 | /** |
mpetovello | 0:3bffc1862262 | 53 | * Class |
mpetovello | 0:3bffc1862262 | 54 | * A class to handle MPU-9150 9-DOF sensor |
mpetovello | 0:3bffc1862262 | 55 | */ |
mpetovello | 0:3bffc1862262 | 56 | class ENGO333_MPU9150 |
mpetovello | 0:3bffc1862262 | 57 | { |
mpetovello | 0:3bffc1862262 | 58 | private: |
mpetovello | 0:3bffc1862262 | 59 | ENGO333_I2C i2c; |
mpetovello | 0:3bffc1862262 | 60 | float measAccel[3]; // Measured acceleration values in units of m/s/s |
mpetovello | 0:3bffc1862262 | 61 | |
mpetovello | 0:3bffc1862262 | 62 | public: |
mpetovello | 0:3bffc1862262 | 63 | /** |
mpetovello | 0:3bffc1862262 | 64 | * Default Constructor |
mpetovello | 0:3bffc1862262 | 65 | * Once called, triggering device initialization and set data variables to |
mpetovello | 0:3bffc1862262 | 66 | * zero. Accelerometer is set to +-2G by default. |
mpetovello | 0:3bffc1862262 | 67 | */ |
mpetovello | 0:3bffc1862262 | 68 | ENGO333_MPU9150(); |
mpetovello | 0:3bffc1862262 | 69 | |
mpetovello | 0:3bffc1862262 | 70 | /** |
mpetovello | 0:3bffc1862262 | 71 | * Function : |
mpetovello | 0:3bffc1862262 | 72 | * Enable/disable device sleep mode |
mpetovello | 0:3bffc1862262 | 73 | * |
mpetovello | 0:3bffc1862262 | 74 | * Argument : |
mpetovello | 0:3bffc1862262 | 75 | * state = TRUE/FALSE |
mpetovello | 0:3bffc1862262 | 76 | * |
mpetovello | 0:3bffc1862262 | 77 | * Return : |
mpetovello | 0:3bffc1862262 | 78 | * NONE |
mpetovello | 0:3bffc1862262 | 79 | */ |
mpetovello | 0:3bffc1862262 | 80 | void setSleepMode(bool state); |
mpetovello | 0:3bffc1862262 | 81 | |
mpetovello | 0:3bffc1862262 | 82 | /** |
mpetovello | 0:3bffc1862262 | 83 | * Function : |
mpetovello | 0:3bffc1862262 | 84 | * Test device's accelerometer connection |
mpetovello | 0:3bffc1862262 | 85 | * |
mpetovello | 0:3bffc1862262 | 86 | * Argument : |
mpetovello | 0:3bffc1862262 | 87 | * NONE |
mpetovello | 0:3bffc1862262 | 88 | * |
mpetovello | 0:3bffc1862262 | 89 | * Return : |
mpetovello | 0:3bffc1862262 | 90 | * Return TRUE if connection is good, otherwise FALSSE |
mpetovello | 0:3bffc1862262 | 91 | */ |
mpetovello | 0:3bffc1862262 | 92 | virtual bool TestConnection(); |
mpetovello | 0:3bffc1862262 | 93 | |
mpetovello | 0:3bffc1862262 | 94 | /** |
mpetovello | 0:3bffc1862262 | 95 | * Function : |
mpetovello | 0:3bffc1862262 | 96 | * Set accelerometer full scale range, see MPU9150_ACCEL_RANGE_XG |
mpetovello | 0:3bffc1862262 | 97 | * |
mpetovello | 0:3bffc1862262 | 98 | * Argument : |
mpetovello | 0:3bffc1862262 | 99 | * range = values of MPU9150_ACCEL_RANGE_XG |
mpetovello | 0:3bffc1862262 | 100 | * |
mpetovello | 0:3bffc1862262 | 101 | * Return : |
mpetovello | 0:3bffc1862262 | 102 | * NONE |
mpetovello | 0:3bffc1862262 | 103 | */ |
mpetovello | 0:3bffc1862262 | 104 | void setAccelRange(char range); |
mpetovello | 0:3bffc1862262 | 105 | |
mpetovello | 0:3bffc1862262 | 106 | /** |
mpetovello | 0:3bffc1862262 | 107 | * Function : |
mpetovello | 0:3bffc1862262 | 108 | * Read raw accelerometer data, 3 axes |
mpetovello | 0:3bffc1862262 | 109 | * |
mpetovello | 0:3bffc1862262 | 110 | * Argument : |
mpetovello | 0:3bffc1862262 | 111 | * NONE |
mpetovello | 0:3bffc1862262 | 112 | * |
mpetovello | 0:3bffc1862262 | 113 | * Return : |
mpetovello | 0:3bffc1862262 | 114 | * NONE |
mpetovello | 0:3bffc1862262 | 115 | */ |
mpetovello | 0:3bffc1862262 | 116 | virtual void ReadAccelerometers(); |
mpetovello | 0:3bffc1862262 | 117 | |
mpetovello | 0:3bffc1862262 | 118 | /** |
mpetovello | 0:3bffc1862262 | 119 | * Function : |
mpetovello | 0:3bffc1862262 | 120 | * Get raw X-axis acceleration |
mpetovello | 0:3bffc1862262 | 121 | * |
mpetovello | 0:3bffc1862262 | 122 | * Argument : |
mpetovello | 0:3bffc1862262 | 123 | * NONE |
mpetovello | 0:3bffc1862262 | 124 | * |
mpetovello | 0:3bffc1862262 | 125 | * Return : |
mpetovello | 0:3bffc1862262 | 126 | * Raw X-axis acceleration |
mpetovello | 0:3bffc1862262 | 127 | */ |
mpetovello | 0:3bffc1862262 | 128 | virtual float GetAccelX() const; |
mpetovello | 0:3bffc1862262 | 129 | |
mpetovello | 0:3bffc1862262 | 130 | /** |
mpetovello | 0:3bffc1862262 | 131 | * Function : |
mpetovello | 0:3bffc1862262 | 132 | * Get raw Y-axis acceleration |
mpetovello | 0:3bffc1862262 | 133 | * |
mpetovello | 0:3bffc1862262 | 134 | * Argument : |
mpetovello | 0:3bffc1862262 | 135 | * NONE |
mpetovello | 0:3bffc1862262 | 136 | * |
mpetovello | 0:3bffc1862262 | 137 | * Return : |
mpetovello | 0:3bffc1862262 | 138 | * Raw Y-axis acceleration |
mpetovello | 0:3bffc1862262 | 139 | */ |
mpetovello | 0:3bffc1862262 | 140 | virtual float GetAccelY() const; |
mpetovello | 0:3bffc1862262 | 141 | |
mpetovello | 0:3bffc1862262 | 142 | /** |
mpetovello | 0:3bffc1862262 | 143 | * Function : |
mpetovello | 0:3bffc1862262 | 144 | * Get raw Z-axis acceleration |
mpetovello | 0:3bffc1862262 | 145 | * |
mpetovello | 0:3bffc1862262 | 146 | * Argument : |
mpetovello | 0:3bffc1862262 | 147 | * NONE |
mpetovello | 0:3bffc1862262 | 148 | * |
mpetovello | 0:3bffc1862262 | 149 | * Return : |
mpetovello | 0:3bffc1862262 | 150 | * Raw Z-axis acceleration |
mpetovello | 0:3bffc1862262 | 151 | */ |
mpetovello | 0:3bffc1862262 | 152 | virtual float GetAccelZ() const; |
mpetovello | 0:3bffc1862262 | 153 | |
mpetovello | 0:3bffc1862262 | 154 | }; |
mpetovello | 0:3bffc1862262 | 155 | |
mpetovello | 0:3bffc1862262 | 156 | #endif |
mpetovello | 0:3bffc1862262 | 157 | |
mpetovello | 0:3bffc1862262 | 158 |