Skip to content

Commit

Permalink
mpu9250 misc cleanup (clang-tidy, posix fixes, header split)
Browse files Browse the repository at this point in the history
  • Loading branch information
dagar committed Sep 30, 2018
1 parent 9664fed commit 3cddd01
Show file tree
Hide file tree
Showing 12 changed files with 183 additions and 146 deletions.
1 change: 1 addition & 0 deletions cmake/configs/posix_sitl_default.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ set(config_module_list
drivers/batt_smbus
drivers/camera_trigger
drivers/gps
drivers/imu/mpu9250
drivers/linux_gpio
drivers/pwm_out_sim
drivers/vmount
Expand Down
11 changes: 11 additions & 0 deletions src/drivers/imu/mpu9250/AK8963.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

// ak8963 register address and bit definitions
#define AK8963_I2C_ADDR 0x0C
#define AK8963_DEVICE_ID 0x48
#define AK8963REG_WIA 0x00
#define AK8963REG_ST1 0x02
#define AK8963REG_CNTL1_ADDR 0x0A
#define AK8963REG_CNTL2_ADDR 0x0B
#define AK8963_CONTINUOUS_16BIT 0x16
#define AK8963_SELFTEST_MODE 0x08
#define AK8963_RESET 0x01
50 changes: 19 additions & 31 deletions src/drivers/imu/mpu9250/mpu9250.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,10 @@ MPU9250::MPU9250(mpu9250::mpu9250_bus_option &bus, enum Rotation rotation) :
_fifo_maxed(perf_alloc(PC_COUNT, "mpu9250_fifo_maxed")),

_rotation(rotation)

{
if (_interface->init() != OK) {
delete _interface;
warnx("No device on bus %u.", (unsigned)bus.busid);
PX4_ERR("No device on bus %u.", (unsigned)bus.busid);
}

probe();
Expand All @@ -70,8 +69,10 @@ MPU9250::MPU9250(mpu9250::mpu9250_bus_option &bus, enum Rotation rotation) :
_mag = new Mag(bus.mag_path, _interface, DRV_MAG_DEVTYPE_MPU9250);
}

#ifdef CONFIG_FAT_DMAMEMORY
// Allocate 512 bytes for dma FIFO transfer.
_dma_data_buffer = (uint8_t *)fat_dma_alloc(_dma_buffer_size);
#endif
}

MPU9250::~MPU9250()
Expand All @@ -84,7 +85,9 @@ MPU9250::~MPU9250()
delete _mag;
}

#ifdef CONFIG_FAT_DMAMEMORY
fat_dma_free(_dma_data_buffer, _dma_buffer_size);
#endif

hrt_cancel(&_hrt_call);
px4_sem_destroy(&_data_semaphore);
Expand All @@ -96,21 +99,19 @@ MPU9250::~MPU9250()

mpu9250::mpu9250_bus_option &MPU9250::initialize_bus(mpu9250::MPU9250_BUS bus_id)
{
uint8_t i = 0;

for (i = 0; i < NUM_BUS_OPTIONS; i++) {
for (uint8_t i = 0; i < NUM_BUS_OPTIONS; i++) {
if (bus_id == mpu9250::g_bus_options[i].busid) {
return mpu9250::g_bus_options[i];
}
}

PX4_ERR("Could not find bus.");
return mpu9250::g_bus_options[i];
return mpu9250::g_bus_options[0];
}

MPU9250 *MPU9250::instantiate(int argc, char *argv[])
{
int ch;
int ch = 0;
int myoptind = 1;
const char *myoptarg = nullptr;

Expand Down Expand Up @@ -159,7 +160,6 @@ MPU9250 *MPU9250::instantiate(int argc, char *argv[])
return mpu9250;
}


int MPU9250::task_spawn(int argc, char *argv[])
{
int task_id = px4_task_spawn_cmd("mpu9250", SCHED_DEFAULT,
Expand All @@ -171,7 +171,6 @@ int MPU9250::task_spawn(int argc, char *argv[])

int MPU9250::custom_command(int argc, char *argv[])
{

return 0;
}

Expand Down Expand Up @@ -262,8 +261,6 @@ void MPU9250::clean_reset()
write_reg(USER_CTRL_ADDR, USER_CTRL_FIFO_EN);
}



int MPU9250::probe()
{
_whoami = read_reg(WHOAMI_ADDR);
Expand All @@ -281,7 +278,6 @@ int MPU9250::probe()
return PX4_ERROR;
}


int MPU9250::self_test()
{
// if (perf_event_count(_sample_perf) == 0) {
Expand All @@ -295,7 +291,7 @@ int MPU9250::self_test()

uint8_t MPU9250::read_reg(unsigned reg)
{
uint8_t buf[2] = {0};
uint8_t buf[2] = {};

_interface->read(reg, buf, 2);

Expand All @@ -304,10 +300,7 @@ uint8_t MPU9250::read_reg(unsigned reg)

void MPU9250::write_reg(unsigned reg, uint8_t value)
{
uint8_t buf[2] = {0};

buf[0] = value;

uint8_t buf[2] = { value, 0 };
_interface->write(reg, buf, 2);
}

Expand Down Expand Up @@ -367,13 +360,13 @@ void MPU9250::run()

unsigned samples = fifo_count / 12;

float accel_x = 0;
float accel_y = 0;
float accel_z = 0;
float accel_x = 0.0f;
float accel_y = 0.0f;
float accel_z = 0.0f;

float gyro_x = 0;
float gyro_y = 0;
float gyro_z = 0;
float gyro_x = 0.0f;
float gyro_y = 0.0f;
float gyro_z = 0.0f;

for (unsigned i = 0; i < samples; i++) {
accel_x += int16_t(_dma_data_buffer[_offset + 0 + i * 12] << 8 | _dma_data_buffer[_offset + 1 + i * 12]);
Expand Down Expand Up @@ -407,9 +400,9 @@ void MPU9250::run()

_interface->read(EXT_SENS_DATA_00, _dma_data_buffer, 8 + _offset);

float x = (uint16_t)(_dma_data_buffer[3] << 8 | _dma_data_buffer[2]);
float y = (uint16_t)(_dma_data_buffer[5] << 8 | _dma_data_buffer[4]);
float z = (uint16_t)(_dma_data_buffer[7] << 8 | _dma_data_buffer[6]);
float x = (uint16_t)(_dma_data_buffer[3] << 8 | _dma_data_buffer[2]);
float y = (uint16_t)(_dma_data_buffer[5] << 8 | _dma_data_buffer[4]);
float z = (uint16_t)(_dma_data_buffer[7] << 8 | _dma_data_buffer[6]);

// Now bring the mag into same reference frame as the accel and gyro.
int16_t temp = x;
Expand All @@ -426,18 +419,13 @@ void MPU9250::run()

perf_end(_cycle);
}

}



int MPU9250::print_status()
{

return PX4_OK;
}


int MPU9250::print_usage()
{
PRINT_MODULE_DESCRIPTION(
Expand Down
39 changes: 12 additions & 27 deletions src/drivers/imu/mpu9250/mpu9250.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@
#pragma once

#include <drivers/drv_hrt.h>
#include <drivers/accelerometer/Accel.hpp>
#include <drivers/gyroscope/Gyro.hpp>
#include <drivers/magnetometer/Mag.hpp>

#include <lib/drivers/accelerometer/Accel.hpp>
#include <lib/drivers/gyroscope/Gyro.hpp>
#include <lib/drivers/magnetometer/Mag.hpp>
#include <ecl/geo/geo.h>

#include <nuttx/fs/fat.h>

#include <perf/perf_counter.h>

#include <px4_module_multi.h>
#include <px4_getopt.h>

#include "AK8963.h"

#ifdef CONFIG_FAT_DMAMEMORY
#include <nuttx/fs/fat.h>
#endif

// MPU9250 registers
#define WHOAMI_ADDR 0x75
Expand Down Expand Up @@ -121,19 +121,6 @@

#define EXT_SENS_DATA_00 0x49


// ak8963 register address and bit definitions
#define AK8963_I2C_ADDR 0x0C
#define AK8963_DEVICE_ID 0x48
#define AK8963REG_WIA 0x00
#define AK8963REG_ST1 0x02
#define AK8963REG_CNTL1_ADDR 0x0A
#define AK8963REG_CNTL2_ADDR 0x0B
#define AK8963_CONTINUOUS_16BIT 0x16
#define AK8963_SELFTEST_MODE 0x08
#define AK8963_RESET 0x01


#define ACCEL_SAMPLE_RATE 1000
#define ACCEL_FILTER_FREQ 30
#define ACCEL_FS_RANGE_M_S2 32.0f * CONSTANTS_ONE_G
Expand All @@ -147,6 +134,7 @@
#define MAG_FS_RANGE_UT 9600.0f



#define MPU_DEVICE_PATH_ACCEL "/dev/mpu9250_accel"
#define MPU_DEVICE_PATH_GYRO "/dev/mpu9250_gyro"
#define MPU_DEVICE_PATH_MAG "/dev/mpu9250_mag"
Expand Down Expand Up @@ -261,7 +249,7 @@ class MPU9250 : public ModuleBaseMulti<MPU9250>
void init();

/** @brief Starts the driver and kicks off the main loop. */
void run();
void run() override;

/**
* @brief Prints the status of the driver.
Expand All @@ -275,7 +263,6 @@ class MPU9250 : public ModuleBaseMulti<MPU9250>
*/
int probe();


private:

device::Device *_interface = nullptr;
Expand All @@ -289,15 +276,15 @@ class MPU9250 : public ModuleBaseMulti<MPU9250>

uint8_t _whoami = 0;

struct hrt_call _hrt_call;
struct hrt_call _hrt_call {};

perf_counter_t _spi_transfer;
perf_counter_t _cycle;
perf_counter_t _fifo_maxed;

unsigned _offset = 1;

uint8_t *_dma_data_buffer;
uint8_t *_dma_data_buffer{nullptr};

static constexpr unsigned _dma_buffer_size = 544; // 32B block size

Expand All @@ -312,15 +299,13 @@ class MPU9250 : public ModuleBaseMulti<MPU9250>
*/
uint8_t read_reg(unsigned reg);


/**
* @brief Write a register.
* @param The register to write.
* @param The value to write.
*/
void write_reg(unsigned reg, uint8_t value);


/**
* @brief Measurement self test
* @return PX4_OK on success, PX4_ERROR on failure
Expand Down
33 changes: 5 additions & 28 deletions src/drivers/imu/mpu9250/mpu9250_i2c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,13 @@ class MPU9250_I2C : public device::I2C
{
public:
MPU9250_I2C(int bus, uint32_t address);
virtual ~MPU9250_I2C() = default;
~MPU9250_I2C() override = default;

virtual int read(unsigned address, void *data, unsigned count);
virtual int write(unsigned address, void *data, unsigned count);

virtual int ioctl(unsigned operation, unsigned &arg);
int read(unsigned address, void *data, unsigned count) override;
int write(unsigned address, void *data, unsigned count) override;

protected:
virtual int probe();
int probe() override;

};

Expand All @@ -77,33 +75,12 @@ MPU9250_I2C_interface(int bus, uint32_t address)
MPU9250_I2C::MPU9250_I2C(int bus, uint32_t address) :
I2C("MPU9250_I2C", nullptr, bus, address, 400000)
{
_device_id.devid_s.devtype = DRV_ACC_DEVTYPE_MPU9250;
}

int
MPU9250_I2C::ioctl(unsigned operation, unsigned &arg)
{
int ret = PX4_ERROR;

switch (operation) {

case ACCELIOCGEXTERNAL:
return external();

case DEVIOCGDEVICEID:
return CDev::ioctl(nullptr, operation, arg);

default:
ret = -EINVAL;
}

return ret;
}

int
MPU9250_I2C::write(unsigned reg_speed, void *data, unsigned count)
{
uint8_t cmd[2];
uint8_t cmd[2] {};

PX4_INFO("what am i doing here");

Expand Down
Loading

0 comments on commit 3cddd01

Please sign in to comment.