2015-01-28 09:15:27 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Freie Universität Berlin
|
2019-08-23 13:06:13 +02:00
|
|
|
* 2019 HAW Hamburg
|
2015-01-28 09:15:27 +01:00
|
|
|
*
|
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
|
|
* directory for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2019-08-23 13:06:13 +02:00
|
|
|
* @defgroup drivers_mpu9x50 MPU-9X50 (MPU9150 and MPU9250) accelerometer/magnetometer/gyroscope
|
2015-09-25 21:06:17 +02:00
|
|
|
* @ingroup drivers_sensors
|
2018-06-12 10:35:56 +02:00
|
|
|
* @ingroup drivers_saul
|
2019-08-23 13:06:13 +02:00
|
|
|
* @brief Device driver interface for the MPU-9X50 (MPU9150 and MPU9250)
|
2018-06-12 10:35:56 +02:00
|
|
|
*
|
|
|
|
* This driver provides @ref drivers_saul capabilities.
|
2015-01-28 09:15:27 +01:00
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
2019-08-23 13:06:13 +02:00
|
|
|
* @brief Device driver interface for the MPU-9X50 (MPU9150 and MPU9250)
|
2015-01-28 09:15:27 +01:00
|
|
|
*
|
|
|
|
* @author Fabian Nack <nack@inf.fu-berlin.de>
|
2019-08-23 13:06:13 +02:00
|
|
|
* @author Jannes Volkens <jannes.volkens@haw-hamburg.de>
|
2015-01-28 09:15:27 +01:00
|
|
|
*/
|
|
|
|
|
2019-08-23 13:06:13 +02:00
|
|
|
#ifndef MPU9X50_H
|
|
|
|
#define MPU9X50_H
|
2015-01-28 09:15:27 +01:00
|
|
|
|
|
|
|
#include "periph/i2c.h"
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
2017-08-29 18:00:46 +02:00
|
|
|
* @name Sample rate macro definitions
|
2015-01-28 09:15:27 +01:00
|
|
|
* @{
|
|
|
|
*/
|
2019-08-23 13:06:13 +02:00
|
|
|
#define MPU9X50_MIN_SAMPLE_RATE (4)
|
|
|
|
#define MPU9X50_MAX_SAMPLE_RATE (1000)
|
|
|
|
#define MPU9X50_DEFAULT_SAMPLE_RATE (50)
|
|
|
|
#define MPU9X50_MIN_COMP_SMPL_RATE (1)
|
|
|
|
#define MPU9X50_MAX_COMP_SMPL_RATE (100)
|
2015-01-28 09:15:27 +01:00
|
|
|
/** @} */
|
|
|
|
|
|
|
|
/**
|
2017-08-29 18:00:46 +02:00
|
|
|
* @brief Power enum values
|
2015-01-28 09:15:27 +01:00
|
|
|
*/
|
|
|
|
typedef enum {
|
2019-08-23 13:06:13 +02:00
|
|
|
MPU9X50_SENSOR_PWR_OFF = 0x00,
|
|
|
|
MPU9X50_SENSOR_PWR_ON = 0x01,
|
|
|
|
} mpu9x50_pwr_t;
|
2015-01-28 09:15:27 +01:00
|
|
|
|
|
|
|
/**
|
2019-08-23 13:06:13 +02:00
|
|
|
* @brief Possible MPU-9X50 hardware addresses (wiring specific)
|
2015-01-28 09:15:27 +01:00
|
|
|
*/
|
|
|
|
typedef enum {
|
2019-08-23 13:06:13 +02:00
|
|
|
MPU9X50_HW_ADDR_HEX_68 = 0x68,
|
|
|
|
MPU9X50_HW_ADDR_HEX_69 = 0x69,
|
|
|
|
} mpu9x50_hw_addr_t;
|
2015-01-28 09:15:27 +01:00
|
|
|
|
|
|
|
/**
|
2017-08-29 18:00:46 +02:00
|
|
|
* @brief Possible compass addresses (wiring specific)
|
2015-01-28 09:15:27 +01:00
|
|
|
*/
|
|
|
|
typedef enum {
|
2019-08-23 13:06:13 +02:00
|
|
|
MPU9X50_COMP_ADDR_HEX_0C = 0x0C,
|
|
|
|
MPU9X50_COMP_ADDR_HEX_0D = 0x0D,
|
|
|
|
MPU9X50_COMP_ADDR_HEX_0E = 0x0E,
|
|
|
|
MPU9X50_COMP_ADDR_HEX_0F = 0x0F,
|
|
|
|
} mpu9x50_comp_addr_t;
|
2015-01-28 09:15:27 +01:00
|
|
|
|
|
|
|
/**
|
2017-08-29 18:00:46 +02:00
|
|
|
* @brief Possible full scale ranges for the gyroscope
|
2015-01-28 09:15:27 +01:00
|
|
|
*/
|
|
|
|
typedef enum {
|
2019-08-23 13:06:13 +02:00
|
|
|
MPU9X50_GYRO_FSR_250DPS = 0x00,
|
|
|
|
MPU9X50_GYRO_FSR_500DPS = 0x01,
|
|
|
|
MPU9X50_GYRO_FSR_1000DPS = 0x02,
|
|
|
|
MPU9X50_GYRO_FSR_2000DPS = 0x03,
|
|
|
|
} mpu9x50_gyro_ranges_t;
|
2015-01-28 09:15:27 +01:00
|
|
|
|
|
|
|
/**
|
2017-08-29 18:00:46 +02:00
|
|
|
* @brief Possible full scale ranges for the accelerometer
|
2015-01-28 09:15:27 +01:00
|
|
|
*/
|
|
|
|
typedef enum {
|
2019-08-23 13:06:13 +02:00
|
|
|
MPU9X50_ACCEL_FSR_2G = 0x00,
|
|
|
|
MPU9X50_ACCEL_FSR_4G = 0x01,
|
|
|
|
MPU9X50_ACCEL_FSR_8G = 0x02,
|
|
|
|
MPU9X50_ACCEL_FSR_16G = 0x03,
|
|
|
|
} mpu9x50_accel_ranges_t;
|
2015-01-28 09:15:27 +01:00
|
|
|
|
|
|
|
/**
|
2017-08-29 18:00:46 +02:00
|
|
|
* @brief Possible low pass filter values
|
2015-01-28 09:15:27 +01:00
|
|
|
*/
|
|
|
|
typedef enum {
|
2019-08-23 13:06:13 +02:00
|
|
|
MPU9X50_FILTER_188HZ = 0x01,
|
|
|
|
MPU9X50_FILTER_98HZ = 0x02,
|
|
|
|
MPU9X50_FILTER_42HZ = 0x03,
|
|
|
|
MPU9X50_FILTER_20HZ = 0x04,
|
|
|
|
MPU9X50_FILTER_10HZ = 0x05,
|
|
|
|
MPU9X50_FILTER_5HZ = 0x06,
|
|
|
|
} mpu9x50_lpf_t;
|
2015-01-28 09:15:27 +01:00
|
|
|
|
|
|
|
/**
|
2019-08-23 13:06:13 +02:00
|
|
|
* @brief MPU-9X50 result vector struct
|
2015-01-28 09:15:27 +01:00
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
int16_t x_axis; /**< X-Axis measurement result */
|
|
|
|
int16_t y_axis; /**< Y-Axis measurement result */
|
|
|
|
int16_t z_axis; /**< Z-Axis measurement result */
|
2019-08-23 13:06:13 +02:00
|
|
|
} mpu9x50_results_t;
|
2015-01-28 09:15:27 +01:00
|
|
|
|
|
|
|
/**
|
2019-08-23 13:06:13 +02:00
|
|
|
* @brief Configuration struct for the MPU-9X50 sensor
|
2015-01-28 09:15:27 +01:00
|
|
|
*/
|
|
|
|
typedef struct {
|
2019-08-23 13:06:13 +02:00
|
|
|
mpu9x50_pwr_t accel_pwr; /**< Accel power status (on/off) */
|
|
|
|
mpu9x50_pwr_t gyro_pwr; /**< Gyro power status (on/off) */
|
|
|
|
mpu9x50_pwr_t compass_pwr; /**< Compass power status (on/off) */
|
|
|
|
mpu9x50_gyro_ranges_t gyro_fsr; /**< Configured gyro full-scale range */
|
|
|
|
mpu9x50_accel_ranges_t accel_fsr; /**< Configured accel full-scale range */
|
2015-01-28 09:15:27 +01:00
|
|
|
uint16_t sample_rate; /**< Configured sample rate for accel and gyro */
|
|
|
|
uint8_t compass_sample_rate; /**< Configured compass sample rate */
|
|
|
|
uint8_t compass_x_adj; /**< Compass X-Axis sensitivity adjustment value */
|
|
|
|
uint8_t compass_y_adj; /**< Compass Y-Axis sensitivity adjustment value */
|
|
|
|
uint8_t compass_z_adj; /**< Compass Z-Axis sensitivity adjustment value */
|
2019-08-23 13:06:13 +02:00
|
|
|
} mpu9x50_status_t;
|
2015-01-28 09:15:27 +01:00
|
|
|
|
|
|
|
/**
|
2017-11-07 22:55:56 +01:00
|
|
|
* @brief Device initialization parameters
|
2015-01-28 09:15:27 +01:00
|
|
|
*/
|
|
|
|
typedef struct {
|
2017-11-07 22:55:56 +01:00
|
|
|
i2c_t i2c; /**< I2C device which is used */
|
2019-08-23 13:06:13 +02:00
|
|
|
uint8_t addr; /**< Hardware address of the MPU-9X50 */
|
|
|
|
uint8_t comp_addr; /**< Address of the MPU-9X50s compass */
|
2017-11-07 22:55:56 +01:00
|
|
|
uint16_t sample_rate; /**< Sample rate */
|
2019-08-23 13:06:13 +02:00
|
|
|
} mpu9x50_params_t;
|
2017-11-07 22:55:56 +01:00
|
|
|
|
|
|
|
/**
|
2019-08-23 13:06:13 +02:00
|
|
|
* @brief Device descriptor for the MPU9X50 sensor
|
2017-11-07 22:55:56 +01:00
|
|
|
*/
|
|
|
|
typedef struct {
|
2019-08-23 13:06:13 +02:00
|
|
|
mpu9x50_params_t params; /**< Device initialization parameters */
|
|
|
|
mpu9x50_status_t conf; /**< Device configuration */
|
|
|
|
} mpu9x50_t;
|
2015-01-28 09:15:27 +01:00
|
|
|
|
|
|
|
/**
|
2019-08-23 13:06:13 +02:00
|
|
|
* @brief Initialize the given MPU9X50 device
|
2015-01-28 09:15:27 +01:00
|
|
|
*
|
2019-08-23 13:06:13 +02:00
|
|
|
* @param[out] dev Initialized device descriptor of MPU9X50 device
|
2017-11-07 22:55:56 +01:00
|
|
|
* @param[in] params Initialization parameters
|
2015-01-28 09:15:27 +01:00
|
|
|
*
|
|
|
|
* @return 0 on success
|
|
|
|
* @return -1 if given I2C is not enabled in board config
|
|
|
|
*/
|
2019-08-23 13:06:13 +02:00
|
|
|
int mpu9x50_init(mpu9x50_t *dev, const mpu9x50_params_t *params);
|
2015-01-28 09:15:27 +01:00
|
|
|
|
|
|
|
/**
|
2017-08-29 18:00:46 +02:00
|
|
|
* @brief Enable or disable accelerometer power
|
2015-01-28 09:15:27 +01:00
|
|
|
*
|
2019-08-23 13:06:13 +02:00
|
|
|
* @param[in] dev Device descriptor of MPU9X50 device
|
2015-01-28 09:15:27 +01:00
|
|
|
* @param[in] pwr_conf Target power setting: PWR_ON or PWR_OFF
|
|
|
|
*
|
|
|
|
* @return 0 on success
|
|
|
|
* @return -1 if given I2C is not enabled in board config
|
|
|
|
*/
|
2019-08-23 13:06:13 +02:00
|
|
|
int mpu9x50_set_accel_power(mpu9x50_t *dev, mpu9x50_pwr_t pwr_conf);
|
2015-01-28 09:15:27 +01:00
|
|
|
|
|
|
|
/**
|
2017-08-29 18:00:46 +02:00
|
|
|
* @brief Enable or disable gyroscope power
|
2015-01-28 09:15:27 +01:00
|
|
|
*
|
2019-08-23 13:06:13 +02:00
|
|
|
* @param[in] dev Device descriptor of MPU9X50 device
|
2015-01-28 09:15:27 +01:00
|
|
|
* @param[in] pwr_conf Target power setting: PWR_ON or PWR_OFF
|
|
|
|
*
|
|
|
|
* @return 0 on success
|
|
|
|
* @return -1 if given I2C is not enabled in board config
|
|
|
|
*/
|
2019-08-23 13:06:13 +02:00
|
|
|
int mpu9x50_set_gyro_power(mpu9x50_t *dev, mpu9x50_pwr_t pwr_conf);
|
2015-01-28 09:15:27 +01:00
|
|
|
|
|
|
|
/**
|
2017-08-29 18:00:46 +02:00
|
|
|
* @brief Enable or disable compass power
|
2015-01-28 09:15:27 +01:00
|
|
|
*
|
2019-08-23 13:06:13 +02:00
|
|
|
* @param[in] dev Device descriptor of MPU9X50 device
|
2015-01-28 09:15:27 +01:00
|
|
|
* @param[in] pwr_conf Target power setting: PWR_ON or PWR_OFF
|
|
|
|
*
|
|
|
|
* @return 0 on success
|
|
|
|
* @return -1 if given I2C is not enabled in board config
|
|
|
|
*/
|
2019-08-23 13:06:13 +02:00
|
|
|
int mpu9x50_set_compass_power(mpu9x50_t *dev, mpu9x50_pwr_t pwr_conf);
|
2015-01-28 09:15:27 +01:00
|
|
|
|
|
|
|
/**
|
2019-08-23 13:06:13 +02:00
|
|
|
* @brief Read angular speed values from the given MPU9X50 device, returned in dps
|
2015-01-28 09:15:27 +01:00
|
|
|
*
|
|
|
|
* The raw gyroscope data is read from the sensor and normalized with respect to
|
|
|
|
* the configured gyroscope full-scale range.
|
|
|
|
*
|
2019-08-23 13:06:13 +02:00
|
|
|
* @param[in] dev Device descriptor of MPU9X50 device to read from
|
2015-01-28 09:15:27 +01:00
|
|
|
* @param[out] output Result vector in dps per axis
|
|
|
|
*
|
|
|
|
* @return 0 on success
|
|
|
|
* @return -1 if device's I2C is not enabled in board config
|
|
|
|
* @return -2 if gyro full-scale range is configured wrong
|
|
|
|
*/
|
2019-08-23 13:06:13 +02:00
|
|
|
int mpu9x50_read_gyro(const mpu9x50_t *dev, mpu9x50_results_t *output);
|
2015-01-28 09:15:27 +01:00
|
|
|
|
|
|
|
/**
|
2019-08-23 13:06:13 +02:00
|
|
|
* @brief Read acceleration values from the given MPU9X50 device, returned in mG
|
2015-01-28 09:15:27 +01:00
|
|
|
*
|
|
|
|
* The raw acceleration data is read from the sensor and normalized with respect to
|
|
|
|
* the configured accelerometer full-scale range.
|
|
|
|
*
|
2019-08-23 13:06:13 +02:00
|
|
|
* @param[in] dev Device descriptor of MPU9X50 device to read from
|
2015-01-28 09:15:27 +01:00
|
|
|
* @param[out] output Result vector in mG per axis
|
|
|
|
*
|
|
|
|
* @return 0 on success
|
|
|
|
* @return -1 if device's I2C is not enabled in board config
|
|
|
|
* @return -2 if accel full-scale range is configured wrong
|
|
|
|
*/
|
2019-08-23 13:06:13 +02:00
|
|
|
int mpu9x50_read_accel(const mpu9x50_t *dev, mpu9x50_results_t *output);
|
2015-01-28 09:15:27 +01:00
|
|
|
|
|
|
|
/**
|
2019-08-23 13:06:13 +02:00
|
|
|
* @brief Read magnetic field values from the given MPU9X50 device, returned in mikroT
|
2015-01-28 09:15:27 +01:00
|
|
|
*
|
|
|
|
* The raw compass data is read from the sensor and normalized with respect to
|
|
|
|
* the compass full-scale range (which can not be configured).
|
|
|
|
*
|
2019-08-23 13:06:13 +02:00
|
|
|
* @param[in] dev Device descriptor of MPU9X50 device to read from
|
2015-01-28 09:15:27 +01:00
|
|
|
* @param[out] output Result vector in mikroT per axis
|
|
|
|
*
|
|
|
|
* @return 0 on success
|
|
|
|
* @return -1 if device's I2C is not enabled in board config
|
|
|
|
*/
|
2019-08-23 13:06:13 +02:00
|
|
|
int mpu9x50_read_compass(const mpu9x50_t *dev, mpu9x50_results_t *output);
|
2015-01-28 09:15:27 +01:00
|
|
|
|
|
|
|
/**
|
2019-08-23 13:06:13 +02:00
|
|
|
* @brief Read temperature value from the given MPU9X50 device, returned in m°C
|
2015-01-28 09:15:27 +01:00
|
|
|
*
|
|
|
|
* @note
|
|
|
|
* The measured temperature is slightly higher than the real room temperature.
|
|
|
|
* Tests showed that the offset varied around 2-3 °C (but no warranties here).
|
|
|
|
*
|
2019-08-23 13:06:13 +02:00
|
|
|
* @param[in] dev Device descriptor of MPU9X50 device to read from
|
2015-01-28 09:15:27 +01:00
|
|
|
* @param[out] output Temperature in m°C
|
|
|
|
*
|
|
|
|
* @return 0 on success
|
|
|
|
* @return -1 if device's I2C is not enabled in board config
|
|
|
|
*/
|
2019-08-23 13:06:13 +02:00
|
|
|
int mpu9x50_read_temperature(const mpu9x50_t *dev, int32_t *output);
|
2015-01-28 09:15:27 +01:00
|
|
|
|
|
|
|
/**
|
2017-08-29 18:00:46 +02:00
|
|
|
* @brief Set the full-scale range for raw gyroscope data
|
2015-01-28 09:15:27 +01:00
|
|
|
*
|
2019-08-23 13:06:13 +02:00
|
|
|
* @param[in] dev Device descriptor of MPU9X50 device
|
2015-01-28 09:15:27 +01:00
|
|
|
* @param[in] fsr Target full-scale range
|
|
|
|
*
|
|
|
|
* @return 0 on success
|
|
|
|
* @return -1 if device's I2C is not enabled in board config
|
|
|
|
* @return -2 if given full-scale target value is not valid
|
|
|
|
*/
|
2019-08-23 13:06:13 +02:00
|
|
|
int mpu9x50_set_gyro_fsr(mpu9x50_t *dev, mpu9x50_gyro_ranges_t fsr);
|
2015-01-28 09:15:27 +01:00
|
|
|
|
|
|
|
/**
|
2017-08-29 18:00:46 +02:00
|
|
|
* @brief Set the full-scale range for raw accelerometer data
|
2015-01-28 09:15:27 +01:00
|
|
|
*
|
2019-08-23 13:06:13 +02:00
|
|
|
* @param[in] dev Device descriptor of MPU9X50 device
|
2015-01-28 09:15:27 +01:00
|
|
|
* @param[in] fsr Target full-scale range
|
|
|
|
*
|
|
|
|
* @return 0 on success
|
|
|
|
* @return -1 if device's I2C is not enabled in board config
|
|
|
|
* @return -2 if given full-scale target value is not valid
|
|
|
|
*/
|
2019-08-23 13:06:13 +02:00
|
|
|
int mpu9x50_set_accel_fsr(mpu9x50_t *dev, mpu9x50_accel_ranges_t fsr);
|
2015-01-28 09:15:27 +01:00
|
|
|
|
|
|
|
/**
|
2017-08-29 18:00:46 +02:00
|
|
|
* @brief Set the rate at which the gyroscope and accelerometer data is sampled
|
2015-01-28 09:15:27 +01:00
|
|
|
*
|
|
|
|
* Sample rate can be chosen between 4 Hz and 1kHz. The actual set value might
|
|
|
|
* slightly differ. If necessary, check the actual set value in the device's
|
|
|
|
* config member afterwards.
|
|
|
|
*
|
2019-08-23 13:06:13 +02:00
|
|
|
* @param[in] dev Device descriptor of MPU9X50 device
|
2015-01-28 09:15:27 +01:00
|
|
|
* @param[in] rate Target sample rate in Hz
|
|
|
|
*
|
|
|
|
* @return 0 on success
|
|
|
|
* @return -1 if device's I2C is not enabled in board config
|
|
|
|
* @return -2 if given target sample rate is not valid
|
|
|
|
*/
|
2019-08-23 13:06:13 +02:00
|
|
|
int mpu9x50_set_sample_rate(mpu9x50_t *dev, uint16_t rate);
|
2015-01-28 09:15:27 +01:00
|
|
|
|
|
|
|
/**
|
2017-08-29 18:00:46 +02:00
|
|
|
* @brief Set the rate at which the compass data is sampled
|
2015-01-28 09:15:27 +01:00
|
|
|
*
|
|
|
|
* Sample rate can be chosen between 1 Hz and 100 Hz but has to be a fraction
|
|
|
|
* of the configured accel/gyro sample rate. The actual set value might
|
|
|
|
* slightly differ. If necessary, check the actual set value in the device's
|
|
|
|
* config member afterwards.
|
|
|
|
*
|
2019-08-23 13:06:13 +02:00
|
|
|
* @param[in] dev Device descriptor of MPU9X50 device
|
2015-01-28 09:15:27 +01:00
|
|
|
* @param[in] rate Target sample rate in Hz
|
|
|
|
*
|
|
|
|
* @return 0 on success
|
|
|
|
* @return -1 if device's I2C is not enabled in board config
|
|
|
|
* @return -2 if given target sample rate is not valid
|
|
|
|
*/
|
2019-08-23 13:06:13 +02:00
|
|
|
int mpu9x50_set_compass_sample_rate(mpu9x50_t *dev, uint8_t rate);
|
2015-01-28 09:15:27 +01:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-08-23 13:06:13 +02:00
|
|
|
#endif /* MPU9X50_H */
|
2015-01-28 09:15:27 +01:00
|
|
|
/** @} */
|