├── library.properties ├── README.md ├── examples └── read9axis │ └── read9axis.ino ├── keywords.txt ├── src ├── FaBo9Axis_MPU9250.h └── FaBo9Axis_MPU9250.cpp └── LICENSE /library.properties: -------------------------------------------------------------------------------- 1 | name=FaBo 202 9Axis MPU9250 2 | version=1.0.1 3 | author=FaBo 4 | maintainer=Akira Sasaki 5 | sentence=A library for FaBo 9Axis I2C Brick 6 | paragraph=MPU9250 is 9-Axis sensor. 7 | category=Sensors 8 | url=https://github.com/FaBoPlatform/FaBo9AXIS-MPU9250-Library 9 | architectures=avr,esp32 -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # FaBo9AXIS-MPU9250-Library 2 | 3 | This is a library for FaBo #202 9Axis I2C Brick. 4 | 5 | ## FaBo 9Axis I2C Brick 6 | 7 | [#202 9Axis I2C Brick](http://fabo.io/202.html) 8 | 9 | ## MPU-9250 10 | 11 | MPU9250 is 9-Axis sensor. 12 | 13 | ### MPU-9250 Datasheet 14 | 15 | [MPU9250 Datasheet](http://43zrtwysvxb2gf29r5o0athu.wpengine.netdna-cdn.com/wp-content/uploads/2015/02/MPU-9250-Datasheet.pdf) 16 | [MPU9250 Register map](http://43zrtwysvxb2gf29r5o0athu.wpengine.netdna-cdn.com/wp-content/uploads/2015/02/MPU-9250-Register-Map.pdf) 17 | 18 | ## Releases 19 | 20 | - 1.0.0 Initial release. 21 | - 1.0.1 minimal fix 22 | 23 | ## How to install. 24 | 25 | [Installing Additional Arduino Libraries](https://www.arduino.cc/en/Guide/Libraries) 26 | -------------------------------------------------------------------------------- /examples/read9axis/read9axis.ino: -------------------------------------------------------------------------------- 1 | /** 2 | @file read9axis.ino 3 | @brief This is an Example for the FaBo 9Axis I2C Brick. 4 | 5 | http://fabo.io/202.html 6 | 7 | Released under APACHE LICENSE, VERSION 2.0 8 | 9 | http://www.apache.org/licenses/ 10 | 11 | @author FaBo 12 | */ 13 | 14 | #include 15 | #include 16 | 17 | FaBo9Axis fabo_9axis; 18 | 19 | void setup() { 20 | Serial.begin(115200); 21 | Serial.println("RESET"); 22 | Serial.println(); 23 | 24 | Serial.println("configuring device."); 25 | 26 | if (fabo_9axis.begin()) { 27 | Serial.println("configured FaBo 9Axis I2C Brick"); 28 | } else { 29 | Serial.println("device error"); 30 | while(1); 31 | } 32 | } 33 | 34 | void loop() { 35 | float ax,ay,az; 36 | float gx,gy,gz; 37 | float mx,my,mz; 38 | float temp; 39 | 40 | fabo_9axis.readAccelXYZ(&ax,&ay,&az); 41 | fabo_9axis.readGyroXYZ(&gx,&gy,&gz); 42 | fabo_9axis.readMagnetXYZ(&mx,&my,&mz); 43 | fabo_9axis.readTemperature(&temp); 44 | 45 | Serial.print("ax: "); 46 | Serial.print(ax); 47 | Serial.print(" ay: "); 48 | Serial.print(ay); 49 | Serial.print(" az: "); 50 | Serial.println(az); 51 | 52 | Serial.print("gx: "); 53 | Serial.print(gx); 54 | Serial.print(" gy: "); 55 | Serial.print(gy); 56 | Serial.print(" gz: "); 57 | Serial.println(gz); 58 | 59 | Serial.print("mx: "); 60 | Serial.print(mx); 61 | Serial.print(" my: "); 62 | Serial.print(my); 63 | Serial.print(" mz: "); 64 | Serial.println(mz); 65 | 66 | Serial.print("temp: "); 67 | Serial.println(temp); 68 | 69 | delay(1000); 70 | } 71 | -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map For FaBo9AXIS-MPU9250-Library 3 | ####################################### 4 | 5 | ####################################### 6 | # Class (KEYWORD1) 7 | ####################################### 8 | 9 | FaBo9Axis KEYWORD1 10 | 11 | ####################################### 12 | # Methods and Functions (KEYWORD2) 13 | ####################################### 14 | 15 | begin KEYWORD2 16 | searchDevice KEYWORD2 17 | configMPU9250 KEYWORD2 18 | configAK8963 KEYWORD2 19 | checkDataReady KEYWORD2 20 | readAccelXYZ KEYWORD2 21 | readGyroXYZ KEYWORD2 22 | readMagnetXYZ KEYWORD2 23 | readTemperature KEYWORD2 24 | 25 | ####################################### 26 | # Constants (LITERAL1) 27 | ####################################### 28 | 29 | MPU9250_SLAVE_ADDRESS LITERAL1 30 | AK8963_SLAVE_ADDRESS LITERAL1 31 | MPU9250_SELF_TEST_X_GYRO LITERAL1 32 | MPU9250_SELF_TEST_Y_GYRO LITERAL1 33 | MPU9250_SELF_TEST_Z_GYRO LITERAL1 34 | MPU9250_SELF_TEST_X_ACCEL LITERAL1 35 | MPU9250_SELF_TEST_Y_ACCEL LITERAL1 36 | MPU9250_SELF_TEST_Z_ACCEL LITERAL1 37 | MPU9250_XG_OFFSET_H LITERAL1 38 | MPU9250_XG_OFFSET_L LITERAL1 39 | MPU9250_YG_OFFSET_H LITERAL1 40 | MPU9250_YG_OFFSET_L LITERAL1 41 | MPU9250_ZG_OFFSET_H LITERAL1 42 | MPU9250_ZG_OFFSET_L LITERAL1 43 | MPU9250_SMPLRT_DIV LITERAL1 44 | MPU9250_CONFIG LITERAL1 45 | MPU9250_GYRO_CONFIG LITERAL1 46 | MPU9250_ACCEL_CONFIG LITERAL1 47 | MPU9250_ACCEL_CONFIG_2 LITERAL1 48 | MPU9250_LP_ACCEL_ODR LITERAL1 49 | MPU9250_WOM_THR LITERAL1 50 | MPU9250_FIFO_EN LITERAL1 51 | MPU9250_I2C_MST_CTRL LITERAL1 52 | MPU9250_I2C_SLV0_ADDR LITERAL1 53 | MPU9250_I2C_SLV0_REG LITERAL1 54 | MPU9250_I2C_SLV0_CTRL LITERAL1 55 | MPU9250_I2C_SLV1_ADDR LITERAL1 56 | MPU9250_I2C_SLV1_REG LITERAL1 57 | MPU9250_I2C_SLV1_CTRL LITERAL1 58 | MPU9250_I2C_SLV2_ADDR LITERAL1 59 | MPU9250_I2C_SLV2_REG LITERAL1 60 | MPU9250_I2C_SLV2_CTRL LITERAL1 61 | MPU9250_I2C_SLV3_ADDR LITERAL1 62 | MPU9250_I2C_SLV3_REG LITERAL1 63 | MPU9250_I2C_SLV3_CTRL LITERAL1 64 | MPU9250_I2C_SLV4_ADDR LITERAL1 65 | MPU9250_I2C_SLV4_REG LITERAL1 66 | MPU9250_I2C_SLV4_DO LITERAL1 67 | MPU9250_I2C_SLV4_CTRL LITERAL1 68 | MPU9250_I2C_SLV4_DI LITERAL1 69 | MPU9250_I2C_MST_STATUS LITERAL1 70 | MPU9250_INT_PIN_CFG LITERAL1 71 | MPU9250_INT_ENABLE LITERAL1 72 | MPU9250_INT_STATUS LITERAL1 73 | MPU9250_ACCEL_XOUT_H LITERAL1 74 | MPU9250_ACCEL_XOUT_L LITERAL1 75 | MPU9250_ACCEL_YOUT_H LITERAL1 76 | MPU9250_ACCEL_YOUT_L LITERAL1 77 | MPU9250_ACCEL_ZOUT_H LITERAL1 78 | MPU9250_ACCEL_ZOUT_L LITERAL1 79 | MPU9250_TEMP_OUT_H LITERAL1 80 | MPU9250_TEMP_OUT_L LITERAL1 81 | MPU9250_GYRO_XOUT_H LITERAL1 82 | MPU9250_GYRO_XOUT_L LITERAL1 83 | MPU9250_GYRO_YOUT_H LITERAL1 84 | MPU9250_GYRO_YOUT_L LITERAL1 85 | MPU9250_GYRO_ZOUT_H LITERAL1 86 | MPU9250_GYRO_ZOUT_L LITERAL1 87 | MPU9250_EXT_SENS_DATA_00 LITERAL1 88 | MPU9250_EXT_SENS_DATA_01 LITERAL1 89 | MPU9250_EXT_SENS_DATA_02 LITERAL1 90 | MPU9250_EXT_SENS_DATA_03 LITERAL1 91 | MPU9250_EXT_SENS_DATA_04 LITERAL1 92 | MPU9250_EXT_SENS_DATA_05 LITERAL1 93 | MPU9250_EXT_SENS_DATA_06 LITERAL1 94 | MPU9250_EXT_SENS_DATA_07 LITERAL1 95 | MPU9250_EXT_SENS_DATA_08 LITERAL1 96 | MPU9250_EXT_SENS_DATA_09 LITERAL1 97 | MPU9250_EXT_SENS_DATA_10 LITERAL1 98 | MPU9250_EXT_SENS_DATA_11 LITERAL1 99 | MPU9250_EXT_SENS_DATA_12 LITERAL1 100 | MPU9250_EXT_SENS_DATA_13 LITERAL1 101 | MPU9250_EXT_SENS_DATA_14 LITERAL1 102 | MPU9250_EXT_SENS_DATA_15 LITERAL1 103 | MPU9250_EXT_SENS_DATA_16 LITERAL1 104 | MPU9250_EXT_SENS_DATA_17 LITERAL1 105 | MPU9250_EXT_SENS_DATA_18 LITERAL1 106 | MPU9250_EXT_SENS_DATA_19 LITERAL1 107 | MPU9250_EXT_SENS_DATA_20 LITERAL1 108 | MPU9250_EXT_SENS_DATA_21 LITERAL1 109 | MPU9250_EXT_SENS_DATA_22 LITERAL1 110 | MPU9250_EXT_SENS_DATA_23 LITERAL1 111 | MPU9250_I2C_SLV0_DO LITERAL1 112 | MPU9250_I2C_SLV1_DO LITERAL1 113 | MPU9250_I2C_SLV2_DO LITERAL1 114 | MPU9250_I2C_SLV3_DO LITERAL1 115 | MPU9250_I2C_MST_DELAY_CTRL LITERAL1 116 | MPU9250_SIGNAL_PATH_RESET LITERAL1 117 | MPU9250_MOT_DETECT_CTRL LITERAL1 118 | MPU9250_USER_CTRL LITERAL1 119 | MPU9250_PWR_MGMT_1 LITERAL1 120 | MPU9250_PWR_MGMT_2 LITERAL1 121 | MPU9250_FIFO_COUNTH LITERAL1 122 | MPU9250_FIFO_COUNTL LITERAL1 123 | MPU9250_FIFO_R_W LITERAL1 124 | MPU9250_WHO_AM_I LITERAL1 125 | MPU9250_XA_OFFSET_H LITERAL1 126 | MPU9250_XA_OFFSET_L LITERAL1 127 | MPU9250_YA_OFFSET_H LITERAL1 128 | MPU9250_YA_OFFSET_L LITERAL1 129 | MPU9250_ZA_OFFSET_H LITERAL1 130 | MPU9250_ZA_OFFSET_L LITERAL1 131 | MPU9250_GFS_250 LITERAL1 132 | MPU9250_GFS_500 LITERAL1 133 | MPU9250_GFS_1000 LITERAL1 134 | MPU9250_GFS_2000 LITERAL1 135 | MPU9250_AFS_2G LITERAL1 136 | MPU9250_AFS_4G LITERAL1 137 | MPU9250_AFS_8G LITERAL1 138 | MPU9250_AFS_16G LITERAL1 139 | AK8963_WIA LITERAL1 140 | AK8963_INFO LITERAL1 141 | AK8963_ST1 LITERAL1 142 | AK8963_HXL LITERAL1 143 | AK8963_HXH LITERAL1 144 | AK8963_HYL LITERAL1 145 | AK8963_HYH LITERAL1 146 | AK8963_HZL LITERAL1 147 | AK8963_HZH LITERAL1 148 | AK8963_ST2 LITERAL1 149 | AK8963_CNTL1 LITERAL1 150 | AK8963_CNTL2 LITERAL1 151 | AK8963_ASTC LITERAL1 152 | AK8963_TS1 LITERAL1 153 | AK8963_TS2 LITERAL1 154 | AK8963_I2CDIS LITERAL1 155 | AK8963_ASAX LITERAL1 156 | AK8963_ASAY LITERAL1 157 | AK8963_ASAZ LITERAL1 158 | AK8963_RSV LITERAL1 159 | AK8963_MODE_DOWN LITERAL1 160 | AK8963_MODE_ONE LITERAL1 161 | AK8963_MODE_C8HZ LITERAL1 162 | AK8963_MODE_C100HZ LITERAL1 163 | AK8963_MODE_TRIG LITERAL1 164 | AK8963_MODE_TEST LITERAL1 165 | AK8963_MODE_FUSE LITERAL1 166 | AK8963_BIT_14 LITERAL1 167 | AK8963_BIT_16 LITERAL1 168 | -------------------------------------------------------------------------------- /src/FaBo9Axis_MPU9250.h: -------------------------------------------------------------------------------- 1 | /** 2 | @file FaBo9Axis_MPU9250.h 3 | @brief This is a library for the FaBo 9Axis I2C Brick. 4 | 5 | http://fabo.io/202.html 6 | 7 | Released under APACHE LICENSE, VERSION 2.0 8 | 9 | http://www.apache.org/licenses/ 10 | 11 | @author FaBo 12 | */ 13 | 14 | #ifndef FABO9AXIS_MPU9250_H 15 | #define FABO9AXIS_MPU9250_H 16 | 17 | #include 18 | #include 19 | 20 | 21 | #define MPU9250_SLAVE_ADDRESS 0x68 ///< MPU9250 Default I2C slave address 22 | #define AK8963_SLAVE_ADDRESS 0x0C ///< AK8963 I2C slave address 23 | 24 | /// @name MPU-9250 Register Addresses 25 | /// @{ 26 | #define MPU9250_SELF_TEST_X_GYRO 0x00 27 | #define MPU9250_SELF_TEST_Y_GYRO 0x01 28 | #define MPU9250_SELF_TEST_Z_GYRO 0x02 29 | #define MPU9250_SELF_TEST_X_ACCEL 0x0D 30 | #define MPU9250_SELF_TEST_Y_ACCEL 0x0E 31 | #define MPU9250_SELF_TEST_Z_ACCEL 0x0F 32 | #define MPU9250_XG_OFFSET_H 0x13 33 | #define MPU9250_XG_OFFSET_L 0x14 34 | #define MPU9250_YG_OFFSET_H 0x15 35 | #define MPU9250_YG_OFFSET_L 0x16 36 | #define MPU9250_ZG_OFFSET_H 0x17 37 | #define MPU9250_ZG_OFFSET_L 0x18 38 | #define MPU9250_SMPLRT_DIV 0x19 39 | #define MPU9250_CONFIG 0x1A 40 | #define MPU9250_GYRO_CONFIG 0x1B 41 | #define MPU9250_ACCEL_CONFIG 0x1C 42 | #define MPU9250_ACCEL_CONFIG_2 0x1D 43 | #define MPU9250_LP_ACCEL_ODR 0x1E 44 | #define MPU9250_WOM_THR 0x1F 45 | #define MPU9250_FIFO_EN 0x23 46 | #define MPU9250_I2C_MST_CTRL 0x24 47 | #define MPU9250_I2C_SLV0_ADDR 0x25 48 | #define MPU9250_I2C_SLV0_REG 0x26 49 | #define MPU9250_I2C_SLV0_CTRL 0x27 50 | #define MPU9250_I2C_SLV1_ADDR 0x28 51 | #define MPU9250_I2C_SLV1_REG 0x29 52 | #define MPU9250_I2C_SLV1_CTRL 0x2A 53 | #define MPU9250_I2C_SLV2_ADDR 0x2B 54 | #define MPU9250_I2C_SLV2_REG 0x2C 55 | #define MPU9250_I2C_SLV2_CTRL 0x2D 56 | #define MPU9250_I2C_SLV3_ADDR 0x2E 57 | #define MPU9250_I2C_SLV3_REG 0x2F 58 | #define MPU9250_I2C_SLV3_CTRL 0x30 59 | #define MPU9250_I2C_SLV4_ADDR 0x31 60 | #define MPU9250_I2C_SLV4_REG 0x32 61 | #define MPU9250_I2C_SLV4_DO 0x33 62 | #define MPU9250_I2C_SLV4_CTRL 0x34 63 | #define MPU9250_I2C_SLV4_DI 0x35 64 | #define MPU9250_I2C_MST_STATUS 0x36 65 | #define MPU9250_INT_PIN_CFG 0x37 66 | #define MPU9250_INT_ENABLE 0x38 67 | #define MPU9250_INT_STATUS 0x3A 68 | #define MPU9250_ACCEL_XOUT_H 0x3B 69 | #define MPU9250_ACCEL_XOUT_L 0x3C 70 | #define MPU9250_ACCEL_YOUT_H 0x3D 71 | #define MPU9250_ACCEL_YOUT_L 0x3E 72 | #define MPU9250_ACCEL_ZOUT_H 0x3F 73 | #define MPU9250_ACCEL_ZOUT_L 0x40 74 | #define MPU9250_TEMP_OUT_H 0x41 75 | #define MPU9250_TEMP_OUT_L 0x42 76 | #define MPU9250_GYRO_XOUT_H 0x43 77 | #define MPU9250_GYRO_XOUT_L 0x44 78 | #define MPU9250_GYRO_YOUT_H 0x45 79 | #define MPU9250_GYRO_YOUT_L 0x46 80 | #define MPU9250_GYRO_ZOUT_H 0x47 81 | #define MPU9250_GYRO_ZOUT_L 0x48 82 | #define MPU9250_EXT_SENS_DATA_00 0x49 83 | #define MPU9250_EXT_SENS_DATA_01 0x4A 84 | #define MPU9250_EXT_SENS_DATA_02 0x4B 85 | #define MPU9250_EXT_SENS_DATA_03 0x4C 86 | #define MPU9250_EXT_SENS_DATA_04 0x4D 87 | #define MPU9250_EXT_SENS_DATA_05 0x4E 88 | #define MPU9250_EXT_SENS_DATA_06 0x4F 89 | #define MPU9250_EXT_SENS_DATA_07 0x50 90 | #define MPU9250_EXT_SENS_DATA_08 0x51 91 | #define MPU9250_EXT_SENS_DATA_09 0x52 92 | #define MPU9250_EXT_SENS_DATA_10 0x53 93 | #define MPU9250_EXT_SENS_DATA_11 0x54 94 | #define MPU9250_EXT_SENS_DATA_12 0x55 95 | #define MPU9250_EXT_SENS_DATA_13 0x56 96 | #define MPU9250_EXT_SENS_DATA_14 0x57 97 | #define MPU9250_EXT_SENS_DATA_15 0x58 98 | #define MPU9250_EXT_SENS_DATA_16 0x59 99 | #define MPU9250_EXT_SENS_DATA_17 0x5A 100 | #define MPU9250_EXT_SENS_DATA_18 0x5B 101 | #define MPU9250_EXT_SENS_DATA_19 0x5C 102 | #define MPU9250_EXT_SENS_DATA_20 0x5D 103 | #define MPU9250_EXT_SENS_DATA_21 0x5E 104 | #define MPU9250_EXT_SENS_DATA_22 0x5F 105 | #define MPU9250_EXT_SENS_DATA_23 0x60 106 | #define MPU9250_I2C_SLV0_DO 0x63 107 | #define MPU9250_I2C_SLV1_DO 0x64 108 | #define MPU9250_I2C_SLV2_DO 0x65 109 | #define MPU9250_I2C_SLV3_DO 0x66 110 | #define MPU9250_I2C_MST_DELAY_CTRL 0x67 111 | #define MPU9250_SIGNAL_PATH_RESET 0x68 112 | #define MPU9250_MOT_DETECT_CTRL 0x69 113 | #define MPU9250_USER_CTRL 0x6A 114 | #define MPU9250_PWR_MGMT_1 0x6B 115 | #define MPU9250_PWR_MGMT_2 0x6C 116 | #define MPU9250_FIFO_COUNTH 0x72 117 | #define MPU9250_FIFO_COUNTL 0x73 118 | #define MPU9250_FIFO_R_W 0x74 119 | #define MPU9250_WHO_AM_I 0x75 120 | #define MPU9250_XA_OFFSET_H 0x77 121 | #define MPU9250_XA_OFFSET_L 0x78 122 | #define MPU9250_YA_OFFSET_H 0x7A 123 | #define MPU9250_YA_OFFSET_L 0x7B 124 | #define MPU9250_ZA_OFFSET_H 0x7D 125 | #define MPU9250_ZA_OFFSET_L 0x7E 126 | /// @} 127 | 128 | /// @name Gyro Full Scale Select 129 | /// @{ 130 | #define MPU9250_GFS_250 0x0 ///< +250dps 131 | #define MPU9250_GFS_500 0x1 ///< +500dps 132 | #define MPU9250_GFS_1000 0x2 ///< +1000dps 133 | #define MPU9250_GFS_2000 0x3 ///< +2000dps 134 | /// @} 135 | 136 | /// @name Accel Full Scale Select 137 | /// @{ 138 | #define MPU9250_AFS_2G 0x0 ///< 2g 139 | #define MPU9250_AFS_4G 0x1 ///< 4g 140 | #define MPU9250_AFS_8G 0x2 ///< 8g 141 | #define MPU9250_AFS_16G 0x3 ///< 16g 142 | /// @} 143 | 144 | 145 | /// @name AK8963 Register Addresses 146 | /// @{ 147 | #define AK8963_WIA 0x00 148 | #define AK8963_INFO 0x01 149 | #define AK8963_ST1 0x02 150 | #define AK8963_HXL 0x03 151 | #define AK8963_HXH 0x04 152 | #define AK8963_HYL 0x05 153 | #define AK8963_HYH 0x06 154 | #define AK8963_HZL 0x07 155 | #define AK8963_HZH 0x08 156 | #define AK8963_ST2 0x09 157 | #define AK8963_CNTL1 0x0A 158 | #define AK8963_CNTL2 0x0B 159 | #define AK8963_ASTC 0x0C 160 | #define AK8963_TS1 0x0D 161 | #define AK8963_TS2 0x0E 162 | #define AK8963_I2CDIS 0x0F 163 | #define AK8963_ASAX 0x10 164 | #define AK8963_ASAY 0x11 165 | #define AK8963_ASAZ 0x12 166 | #define AK8963_RSV 0x13 167 | /// @} 168 | 169 | 170 | /// @name CNTL1 Mode select 171 | /// @{ 172 | #define AK8963_MODE_DOWN 0x00 ///< Power down mode 173 | #define AK8963_MODE_ONE 0x01 ///< One shot data output 174 | #define AK8963_MODE_C8HZ 0x02 ///< Continous data output 8Hz 175 | #define AK8963_MODE_C100HZ 0x06 ///< Continous data output 100Hz 176 | #define AK8963_MODE_TRIG 0x04 ///< External trigger data output 177 | #define AK8963_MODE_TEST 0x08 ///< Self test 178 | #define AK8963_MODE_FUSE 0x0F ///< Fuse ROM access 179 | /// @} 180 | 181 | /// @name Magneto Scale Select 182 | /// @{ 183 | #define AK8963_BIT_14 0x0 ///< 14bit output 184 | #define AK8963_BIT_16 0x1 ///< 16bit output 185 | /// @} 186 | 187 | 188 | /** 189 | * @class FaBo9Axis 190 | * @brief FaBo 9Axis I2C Controll class 191 | */ 192 | class FaBo9Axis { 193 | public: 194 | FaBo9Axis(uint8_t addr = MPU9250_SLAVE_ADDRESS); 195 | bool begin(void); 196 | bool searchDevice(void); 197 | void configMPU9250(uint8_t gfs = MPU9250_GFS_250, uint8_t afs = MPU9250_AFS_2G); 198 | void configAK8963(uint8_t mode = AK8963_MODE_C8HZ, uint8_t mfs = AK8963_BIT_16); 199 | bool checkDataReady(); 200 | void readAccelXYZ(float * ax, float * ay, float * az); 201 | void readGyroXYZ(float * gx, float * gy, float * gz); 202 | void readMagnetXYZ(float * mx, float * my, float * mz); 203 | void readTemperature(float * temperature); 204 | void dumpConfig(void); 205 | private: 206 | uint8_t _mpu9250addr; 207 | float _gres, _ares, _mres; 208 | float _magXcoef, _magYcoef, _magZcoef; 209 | void writeI2c(byte address, uint8_t register_addr, uint8_t data); 210 | void readI2c(uint8_t address, uint8_t register_addr, uint8_t num, uint8_t * buffer); 211 | }; 212 | 213 | #endif // FABO9AXIS_MPU9250_H 214 | -------------------------------------------------------------------------------- /src/FaBo9Axis_MPU9250.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | @file FaBo9Axis_MPU9250.cpp 3 | @brief This is a library for the FaBo 9Axis I2C Brick. 4 | 5 | http://fabo.io/202.html 6 | 7 | Released under APACHE LICENSE, VERSION 2.0 8 | 9 | http://www.apache.org/licenses/ 10 | 11 | @author FaBo 12 | */ 13 | 14 | #include "FaBo9Axis_MPU9250.h" 15 | 16 | /** 17 | @brief Constructor 18 | @param [in] addr MPU-9250 I2C slave address 19 | */ 20 | FaBo9Axis::FaBo9Axis(uint8_t addr) { 21 | _mpu9250addr = addr; 22 | Wire.begin(); 23 | } 24 | 25 | /** 26 | @brief Begin Device 27 | @retval true normaly done 28 | @retval false device error 29 | */ 30 | bool FaBo9Axis::begin() { 31 | if (searchDevice()) { 32 | configMPU9250(); 33 | configAK8963(); 34 | return true; 35 | } else { 36 | return false; 37 | } 38 | } 39 | 40 | /** 41 | @brief Search Device 42 | @retval true device connected 43 | @retval false device error 44 | */ 45 | bool FaBo9Axis::searchDevice() { 46 | uint8_t whoami; 47 | readI2c(_mpu9250addr, MPU9250_WHO_AM_I, 1, &whoami); 48 | if(whoami == 0x71){ 49 | return true; 50 | } else{ 51 | return false; 52 | } 53 | } 54 | 55 | /** 56 | @brief Configure MPU-9250 57 | @param [in] gfs Gyro Full Scale Select(default:MPU9250_GFS_250[+250dps]) 58 | @param [in] afs Accel Full Scale Select(default:MPU9250_AFS_2G[2g]) 59 | */ 60 | void FaBo9Axis::configMPU9250(uint8_t gfs, uint8_t afs) { 61 | switch(gfs) { 62 | case MPU9250_GFS_250: 63 | _gres = 250.0/32768.0; 64 | break; 65 | case MPU9250_GFS_500: 66 | _gres = 500.0/32768.0; 67 | break; 68 | case MPU9250_GFS_1000: 69 | _gres = 1000.0/32768.0; 70 | break; 71 | case MPU9250_GFS_2000: 72 | _gres = 2000.0/32768.0; 73 | break; 74 | } 75 | switch(afs) { 76 | case MPU9250_AFS_2G: 77 | _ares = 2.0/32768.0; 78 | break; 79 | case MPU9250_AFS_4G: 80 | _ares = 4.0/32768.0; 81 | break; 82 | case MPU9250_AFS_8G: 83 | _ares = 8.0/32768.0; 84 | break; 85 | case MPU9250_AFS_16G: 86 | _ares = 16.0/32768.0; 87 | break; 88 | } 89 | // sleep off 90 | writeI2c(_mpu9250addr, MPU9250_PWR_MGMT_1, 0x00); 91 | delay(100); 92 | // auto select clock source 93 | writeI2c(_mpu9250addr, MPU9250_PWR_MGMT_1, 0x01); 94 | delay(200); 95 | // DLPF_CFG 96 | writeI2c(_mpu9250addr, MPU9250_CONFIG, 0x03); 97 | // sample rate divider 98 | writeI2c(_mpu9250addr, MPU9250_SMPLRT_DIV, 0x04); 99 | // gyro full scale select 100 | writeI2c(_mpu9250addr, MPU9250_GYRO_CONFIG, gfs << 3); 101 | // accel full scale select 102 | writeI2c(_mpu9250addr, MPU9250_ACCEL_CONFIG, afs << 3); 103 | // A_DLPFCFG 104 | writeI2c(_mpu9250addr, MPU9250_ACCEL_CONFIG_2, 0x03); 105 | // BYPASS_EN 106 | writeI2c(_mpu9250addr, MPU9250_INT_PIN_CFG, 0x02); 107 | delay(100); 108 | } 109 | 110 | void FaBo9Axis::dumpConfig() { 111 | uint8_t c; 112 | readI2c(_mpu9250addr, MPU9250_GYRO_CONFIG, 1, &c); 113 | Serial.println(c,HEX); 114 | readI2c(_mpu9250addr, MPU9250_ACCEL_CONFIG, 1, &c); 115 | Serial.println(c,HEX); 116 | readI2c(_mpu9250addr, MPU9250_ACCEL_CONFIG_2, 1, &c); 117 | Serial.println(c,HEX); 118 | readI2c(AK8963_SLAVE_ADDRESS, AK8963_CNTL1, 1, &c); 119 | Serial.println(c,HEX); 120 | } 121 | 122 | /** 123 | @brief Configure AK8963 124 | @param [in] mode Magneto Mode Select(default:AK8963_MODE_C8HZ[Continous 8Hz]) 125 | @param [in] mfs Magneto Scale Select(default:AK8963_BIT_16[16bit]) 126 | */ 127 | void FaBo9Axis::configAK8963(uint8_t mode, uint8_t mfs) { 128 | uint8_t data[3]; 129 | 130 | switch(mfs) { 131 | case AK8963_BIT_14: 132 | _mres = 4912.0/8190.0; 133 | break; 134 | case AK8963_BIT_16: 135 | _mres = 4912.0/32760.0; 136 | break; 137 | } 138 | 139 | // set software reset 140 | // writeI2c(AK8963_SLAVE_ADDRESS, AK8963_CNTL2, 0x01); 141 | // delay(100); 142 | // set power down mode 143 | writeI2c(AK8963_SLAVE_ADDRESS, AK8963_CNTL1, 0x00); 144 | delay(1); 145 | // set read FuseROM mode 146 | writeI2c(AK8963_SLAVE_ADDRESS, AK8963_CNTL1, 0x0F); 147 | delay(1); 148 | // read coef data 149 | readI2c(AK8963_SLAVE_ADDRESS, AK8963_ASAX, 3, data); 150 | _magXcoef = (float)(data[0] - 128) / 256.0 + 1.0; 151 | _magYcoef = (float)(data[1] - 128) / 256.0 + 1.0; 152 | _magZcoef = (float)(data[2] - 128) / 256.0 + 1.0; 153 | // set power down mode 154 | writeI2c(AK8963_SLAVE_ADDRESS, AK8963_CNTL1, 0x00); 155 | delay(1); 156 | // set scale&continous mode 157 | writeI2c(AK8963_SLAVE_ADDRESS, AK8963_CNTL1, (mfs<<4|mode)); 158 | delay(1); 159 | } 160 | 161 | /** 162 | @brief Check data ready 163 | @retval true data is ready 164 | @retval false data is not ready 165 | */ 166 | bool FaBo9Axis::checkDataReady() { 167 | uint8_t drdy; 168 | readI2c(_mpu9250addr, MPU9250_INT_STATUS, 1, &drdy); 169 | if ( drdy & 0x01 ) { 170 | return true; 171 | } else { 172 | return false; 173 | } 174 | } 175 | 176 | /** 177 | @brief Read accelerometer 178 | @param [out] ax X-accel(g) 179 | @param [out] ay Y-accel(g) 180 | @param [out] az Z-accel(g) 181 | */ 182 | void FaBo9Axis::readAccelXYZ(float * ax, float * ay, float * az) { 183 | uint8_t data[6]; 184 | int16_t axc, ayc, azc; 185 | readI2c(_mpu9250addr, MPU9250_ACCEL_XOUT_H, 6, data); 186 | axc = ((int16_t)data[0] << 8) | data[1]; 187 | ayc = ((int16_t)data[2] << 8) | data[3]; 188 | azc = ((int16_t)data[4] << 8) | data[5]; 189 | *ax = (float)axc * _ares; 190 | *ay = (float)ayc * _ares; 191 | *az = (float)azc * _ares; 192 | } 193 | 194 | /** 195 | @brief Read gyro 196 | @param [out] gx X-gyro(degrees/sec) 197 | @param [out] gy Y-gyro(degrees/sec) 198 | @param [out] gz Z-gyro(degrees/sec) 199 | */ 200 | void FaBo9Axis::readGyroXYZ(float * gx, float * gy, float * gz) { 201 | uint8_t data[6]; 202 | int16_t gxc, gyc, gzc; 203 | readI2c(_mpu9250addr, MPU9250_GYRO_XOUT_H, 6, data); 204 | gxc = ((int16_t)data[0] << 8) | data[1]; 205 | gyc = ((int16_t)data[2] << 8) | data[3]; 206 | gzc = ((int16_t)data[4] << 8) | data[5]; 207 | *gx = (float)gxc * _gres; 208 | *gy = (float)gyc * _gres; 209 | *gz = (float)gzc * _gres; 210 | } 211 | 212 | /** 213 | @brief Read magneto 214 | @param [out] mx X-magneto(uT) 215 | @param [out] my Y-magneto(uT) 216 | @param [out] mz Z-magneto(uT) 217 | */ 218 | void FaBo9Axis::readMagnetXYZ(float * mx, float * my, float * mz) { 219 | uint8_t data[7]; 220 | uint8_t drdy; 221 | int16_t mxc, myc, mzc; 222 | readI2c(AK8963_SLAVE_ADDRESS, AK8963_ST1, 1, &drdy); 223 | if ( drdy & 0x01 ) { // check data ready 224 | readI2c(AK8963_SLAVE_ADDRESS, AK8963_HXL, 7, data); 225 | if ( !(data[6] & 0x08) ) { // check overflow 226 | mxc = ((int16_t)data[1] << 8) | data[0]; 227 | myc = ((int16_t)data[3] << 8) | data[2]; 228 | mzc = ((int16_t)data[5] << 8) | data[4]; 229 | *mx = (float)mxc * _mres * _magXcoef; 230 | *my = (float)myc * _mres * _magYcoef; 231 | *mz = (float)mzc * _mres * _magZcoef; 232 | } 233 | } 234 | } 235 | 236 | /** 237 | @brief Read temperature 238 | @param [out] temperature temperature(degrees C) 239 | */ 240 | void FaBo9Axis::readTemperature(float * temperature) { 241 | uint8_t data[2]; 242 | int16_t tmc; 243 | readI2c(_mpu9250addr, MPU9250_TEMP_OUT_H, 2, data); 244 | tmc = ((int16_t)data[0] << 8) | data[1]; 245 | *temperature = ((float)tmc) / 333.87 + 21.0; 246 | } 247 | 248 | ///////////////////////////////////////////////////////////////////////////////////////////////////// 249 | 250 | /** 251 | @brief Write I2C 252 | @param [in] address I2C slave address 253 | @param [in] register_addr register address 254 | @param [in] data write data 255 | */ 256 | void FaBo9Axis::writeI2c(uint8_t address, uint8_t register_addr, uint8_t data) { 257 | Wire.beginTransmission(address); 258 | Wire.write(register_addr); 259 | Wire.write(data); 260 | Wire.endTransmission(); 261 | } 262 | 263 | /** 264 | @brief Read I2C 265 | @param [in] address I2C slave address 266 | @param [in] register_addr register address 267 | @param [in] num read length 268 | @param [out] buffer read data 269 | */ 270 | void FaBo9Axis::readI2c(uint8_t address, uint8_t register_addr, uint8_t num, uint8_t * buffer) { 271 | Wire.beginTransmission(address); 272 | Wire.write(register_addr); 273 | Wire.endTransmission(); 274 | uint8_t i = 0; 275 | Wire.requestFrom(address, num); 276 | while( Wire.available() ) { 277 | buffer[i++] = Wire.read(); 278 | } 279 | } 280 | 281 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------