Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
natanaeljr committed Mar 3, 2018
2 parents 62882e1 + 0128240 commit dfe1298
Show file tree
Hide file tree
Showing 8 changed files with 239 additions and 3 deletions.
6 changes: 6 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Examples

To run this examples, do not forget to add `I2Cbus/SPIbus` library path in the Makefile of each example.

+ **MPU-I2C**: Setup MPU through I2C for basic usage.
+ **MPU-SPI**: Setup MPU through SPI for basic usage.
11 changes: 11 additions & 0 deletions examples/mpu_i2c/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Project Makefile #

PROJECT_NAME := mpu-i2c

# Path to MPU Driver
EXTRA_COMPONENT_DIRS += $(abspath ../..)
# Path to I2Cbus
EXTRA_COMPONENT_DIRS += ${HOME}/esp/libraries/I2Cbus


include $(IDF_PATH)/make/project.mk
Empty file.
94 changes: 94 additions & 0 deletions examples/mpu_i2c/main/mpu_i2c.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// =========================================================================
// Released under the MIT License
// Copyright 2017-2018 Natanael Josue Rabello. All rights reserved.
// For the license information refer to LICENSE file in root directory.
// =========================================================================

/**
* @file mpu_i2c.cpp
* Example on how to setup MPU through I2C for basic usage.
*/

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#include "driver/gpio.h"
#include "driver/i2c.h"
#include "esp_err.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/portmacro.h"
#include "freertos/task.h"
#include "sdkconfig.h"

#include "I2Cbus.hpp"
#include "MPU.hpp"
#include "mpu/math.hpp"
#include "mpu/types.hpp"

static const char* TAG = "example";

static constexpr gpio_num_t SDA = GPIO_NUM_22;
static constexpr gpio_num_t SCL = GPIO_NUM_23;
static constexpr uint32_t CLOCK_SPEED = 400000; // range from 100 KHz ~ 400Hz

extern "C" void app_main() {
printf("$ MPU Driver Example: MPU-I2C\n");
fflush(stdout);

// Initialize I2C on port 0 using I2Cbus interface
i2c0.begin(SDA, SCL, CLOCK_SPEED);

// Or directly with esp-idf API
/*
i2c_config_t conf;
conf.mode = I2C_MODE_MASTER;
conf.sda_io_num = SDA;
conf.scl_io_num = SCL;
conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
conf.master.clk_speed = CLOCK_SPEED;
ESP_ERROR_CHECK(i2c_param_config(I2C_NUM_0, &conf));
ESP_ERROR_CHECK(i2c_driver_install(I2C_NUM_0, conf.mode, 0, 0, 0));
*/

MPU_t MPU; // create a default MPU object
MPU.setBus(i2c0); // set bus port, not really needed since default is i2c0
MPU.setAddr(mpud::MPU_I2CADDRESS_AD0_LOW); // set address, default is AD0_LOW

// Great! Let's verify the communication
// (this also check if the connected MPU supports the implementation of chip selected in the component menu)
while (esp_err_t err = MPU.testConnection()) {
ESP_LOGE(TAG, "Failed to connect to the MPU, error=%#X", err);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
ESP_LOGI(TAG, "MPU connection successful!");

// Initialize
ESP_ERROR_CHECK(MPU.initialize()); // initialize the chip and set initial configurations
// Setup with your configurations
// ESP_ERROR_CHECK(MPU.setSampleRate(50)); // set sample rate to 50 Hz
// ESP_ERROR_CHECK(MPU.setGyroFullScale(mpud::GYRO_FS_500DPS));
// ESP_ERROR_CHECK(MPU.setAccelFullScale(mpud::ACCEL_FS_4G));

// Reading sensor data
printf("Reading sensor data:\n");
mpud::raw_axes_t accelRaw; // x, y, z axes as int16
mpud::raw_axes_t gyroRaw; // x, y, z axes as int16
mpud::float_axes_t accelG; // accel axes in (g) gravity format
mpud::float_axes_t gyroDPS; // gyro axes in (DPS) º/s format
while (true) {
// Read
MPU.acceleration(&accelRaw); // fetch raw data from the registers
MPU.rotation(&gyroRaw); // fetch raw data from the registers
// MPU.motion(&accelRaw, &gyroRaw); // read both in one shot
// Convert
accelG = mpud::accelGravity(accelRaw, mpud::ACCEL_FS_4G);
gyroDPS = mpud::gyroDegPerSec(gyroRaw, mpud::GYRO_FS_500DPS);
// Debug
printf("accel: [%+6.2f %+6.2f %+6.2f ] (G) \t", accelG.x, accelG.y, accelG.z);
printf("gyro: [%+7.2f %+7.2f %+7.2f ] (º/s)\n", gyroDPS[0], gyroDPS[1], gyroDPS[2]);
vTaskDelay(100 / portTICK_PERIOD_MS);
}
}
11 changes: 11 additions & 0 deletions examples/mpu_spi/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Project Makefile #

PROJECT_NAME := mpu-spi

# Path to MPU Driver
EXTRA_COMPONENT_DIRS += $(abspath ../..)
# Path to SPIbus
EXTRA_COMPONENT_DIRS += ${HOME}/esp/libraries/SPIbus


include $(IDF_PATH)/make/project.mk
Empty file.
112 changes: 112 additions & 0 deletions examples/mpu_spi/main/mpu_spi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// =========================================================================
// Released under the MIT License
// Copyright 2017-2018 Natanael Josue Rabello. All rights reserved.
// For the license information refer to LICENSE file in root directory.
// =========================================================================

/**
* @file mpu_spi.cpp
* Example on how to setup MPU through SPI for basic usage.
*/

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#include "driver/gpio.h"
#include "driver/spi_master.h"
#include "esp_err.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/portmacro.h"
#include "freertos/task.h"
#include "sdkconfig.h"

#include "MPU.hpp"
#include "SPIbus.hpp"
#include "mpu/math.hpp"
#include "mpu/types.hpp"

static const char* TAG = "example";

static constexpr int MOSI = 22;
static constexpr int MISO = 21;
static constexpr int SCLK = 23;
static constexpr int CS = 16;
static constexpr uint32_t CLOCK_SPEED = 1000000; // up to 1MHz for all registers, and 20MHz for sensor data registers only

extern "C" void app_main() {
printf("$ MPU Driver Example: MPU-SPI\n");
fflush(stdout);

spi_device_handle_t mpu_spi_handle;
// Initialize SPI on HSPI host through SPIbus interface:
hspi.begin(MOSI, MISO, SCLK);
hspi.addDevice(0, CLOCK_SPEED, CS, &mpu_spi_handle);

// Or directly with esp-idf API:
/*
spi_bus_config_t spi_config;
spi_config.mosi_io_num = MOSI;
spi_config.miso_io_num = MISO;
spi_config.sclk_io_num = SCLK;
spi_config.quadwp_io_num = -1;
spi_config.quadhd_io_num = -1;
spi_config.max_transfer_sz = SPI_MAX_DMA_LEN;
ESP_ERROR_CHECK(spi_bus_initialize(HSPI_HOST, &spi_config, 0));
spi_device_interface_config_t dev_config;
dev_config.command_bits = 0;
dev_config.address_bits = 8;
dev_config.dummy_bits = 0;
dev_config.mode = 0;
dev_config.duty_cycle_pos = 128;
dev_config.cs_ena_pretrans = 0;
dev_config.cs_ena_posttrans = 0;
dev_config.clock_speed_hz = CLOCK_SPEED;
dev_config.spics_io_num = CS;
dev_config.flags = 0;
dev_config.queue_size = 1;
dev_config.pre_cb = NULL;
dev_config.post_cb = NULL;
ESP_ERROR_CHECK(spi_bus_add_device(HSPI_HOST, &dev_config, &mpu_spi_handle));
*/

MPU_t MPU; // create a default MPU object
MPU.setBus(hspi); // set bus port, not really needed here since default is HSPI
MPU.setAddr(mpu_spi_handle); // set spi_device_handle, always needed!

// Great! Let's verify the communication
// (this also check if the connected MPU supports the implementation of chip selected in the component menu)
while (esp_err_t err = MPU.testConnection()) {
ESP_LOGE(TAG, "Failed to connect to the MPU, error=%#X", err);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
ESP_LOGI(TAG, "MPU connection successful!");

// Initialize
ESP_ERROR_CHECK(MPU.initialize()); // initialize the chip and set initial configurations
// Setup with your configurations
// ESP_ERROR_CHECK(MPU.setSampleRate(50)); // set sample rate to 50 Hz
// ESP_ERROR_CHECK(MPU.setGyroFullScale(mpud::GYRO_FS_500DPS));
// ESP_ERROR_CHECK(MPU.setAccelFullScale(mpud::ACCEL_FS_4G));

// Reading sensor data
printf("Reading sensor data:\n");
mpud::raw_axes_t accelRaw; // x, y, z axes as int16
mpud::raw_axes_t gyroRaw; // x, y, z axes as int16
mpud::float_axes_t accelG; // accel axes in (g) gravity format
mpud::float_axes_t gyroDPS; // gyro axes in (DPS) º/s format
while (true) {
// Read
MPU.acceleration(&accelRaw); // fetch raw data from the registers
MPU.rotation(&gyroRaw); // fetch raw data from the registers
// MPU.motion(&accelRaw, &gyroRaw); // read both in one shot
// Convert
accelG = mpud::accelGravity(accelRaw, mpud::ACCEL_FS_4G);
gyroDPS = mpud::gyroDegPerSec(gyroRaw, mpud::GYRO_FS_500DPS);
// Debug
printf("accel: [%+6.2f %+6.2f %+6.2f ] (G) \t", accelG.x, accelG.y, accelG.z);
printf("gyro: [%+7.2f %+7.2f %+7.2f ] (º/s)\n", gyroDPS[0], gyroDPS[1], gyroDPS[2]);
vTaskDelay(100 / portTICK_PERIOD_MS);
}
}
8 changes: 5 additions & 3 deletions include/mpu/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -533,9 +533,11 @@ static constexpr mag_stat1_t MAG_STAT1_DATA_OVERRUN{1 << regs::mag::STATUS1_DATA

/*! Magnetometer sensor status 2 */
typedef uint8_t mag_stat2_t;
static constexpr mag_stat2_t MAG_STAT1_SENSOR_OVERFLOW{1 << regs::mag::STATUS2_OVERFLOW_BIT};
#ifdef CONFIG_MPU_AK8975
static constexpr mag_stat2_t MAG_STAT1_DATA_OVERRUN{1 << regs::mag::STATUS1_DATA_OVERRUN_BIT};
static constexpr mag_stat2_t MAG_STAT2_SENSOR_OVERFLOW{1 << regs::mag::STATUS2_OVERFLOW_BIT};
#ifdef CONFIG_MPU_AK8963
static constexpr mag_stat2_t MAG_STAT2_BIT_OUTPUT_SETTING{1 << regs::mag::STATUS2_BIT_OUTPUT_M_BIT};
#elif CONFIG_MPU_AK8975
static constexpr mag_stat2_t MAG_STAT2_DATA_ERROR{1 << regs::mag::STATUS2_DATA_ERROR_BIT};
#endif

#ifdef CONFIG_MPU_AK8963
Expand Down

0 comments on commit dfe1298

Please sign in to comment.