/* mpu6050.c */ #include "sensors.h" #include "mpu6050.h" #include "i2c.h" #include "uart.h" #include "dcm.h" #include "fisqrt.h" #include "stick.h" #include "watchdog.h" #include "status.h" #include "abs.h" #include "event.h" #include "timer.h" #include "panic.h" #include "log.h" i2c_result mpu6050_result; unsigned int mpu6050_generation; signed int gyro_zero_roll; signed int gyro_zero_pitch; signed int gyro_zero_yaw; #define GYRO_ZERO_COUNT 1000 unsigned int mpu6050_gyro_zero_count; unsigned char mpu6050_sample_data[14]; /*unsigned char mpu6050_whoami_command[1] = {0x75}; */ unsigned char mpu6050_whoami_command[1] = {0x6B}; struct i2c_transaction mpu6050_whoami_transaction2; struct i2c_transaction mpu6050_whoami_transaction = { (0x68 << 1) + 0, /* write */ 1, mpu6050_whoami_command, &mpu6050_result, EVENT_MPU6050_I2C_COMPLETE, &mpu6050_whoami_transaction2 }; struct i2c_transaction mpu6050_whoami_transaction2 = { (0x68 << 1) + 1, /* read */ 1, mpu6050_sample_data, &mpu6050_result, EVENT_MPU6050_I2C_COMPLETE, NULL }; /* Accelerometer scaling */ #define AFS_SEL 2 unsigned char mpu6050_init_command[] = {0x6B, 0x01}; unsigned char mpu6050_accel_init_command[] = {0x1c, (AFS_SEL<<3)}; struct i2c_transaction mpu6050_accel_init_transaction = { (0x68 << 1) + 0, /* write */ 2, mpu6050_accel_init_command, &mpu6050_result, EVENT_MPU6050_I2C_COMPLETE, NULL }; struct i2c_transaction mpu6050_init_transaction = { (0x68 << 1) + 0, /* write */ 2, mpu6050_init_command, &mpu6050_result, EVENT_MPU6050_I2C_COMPLETE, &mpu6050_accel_init_transaction }; unsigned char mpu6050_sample_command[] = {0x3B}; struct i2c_transaction mpu6050_sample_transaction2; struct i2c_transaction mpu6050_sample_transaction = { (0x68 << 1) + 0, /* write */ 1, mpu6050_sample_command, &mpu6050_result, EVENT_MPU6050_I2C_COMPLETE, &mpu6050_sample_transaction2 }; struct i2c_transaction mpu6050_sample_transaction2 = { (0x68 << 1) + 1, /* read */ 14, mpu6050_sample_data, &mpu6050_result, EVENT_MPU6050_I2C_COMPLETE, NULL }; void mpu6050_event_handler(void); bool mpu6050_init(void) { event_register(EVENT_MPU6050_I2C_COMPLETE, mpu6050_event_handler); if (!i2c_start_transaction(&mpu6050_init_transaction)) return FALSE; while (i2c_busy()) ; return (mpu6050_result == I2C_SUCCESS); } /* LSB / g */ /* 1 for +- 2g */ /* 2 for +- 4g */ /* 4 for +- 8g */ /* 8 for +- 16g */ #define ACCEL_STEP (16384.0 / (float)(1<