diff --git a/include/mgos_imu.h b/include/mgos_imu.h new file mode 100644 index 0000000..998a62b --- /dev/null +++ b/include/mgos_imu.h @@ -0,0 +1,128 @@ +/* + * Copyright 2018 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include "mgos.h" + +struct mgos_imu_mag; +struct mgos_imu_acc; +struct mgos_imu_gyro; + +typedef bool (*mgos_imu_mag_detect_fn)(struct mgos_imu_mag *dev); +typedef bool (*mgos_imu_mag_start_fn)(struct mgos_imu_mag *dev); +typedef bool (*mgos_imu_mag_read_fn)(struct mgos_imu_mag *dev); +typedef bool (*mgos_imu_acc_detect_fn)(struct mgos_imu_acc *dev); +typedef bool (*mgos_imu_acc_start_fn)(struct mgos_imu_acc *dev); +typedef bool (*mgos_imu_acc_read_fn)(struct mgos_imu_acc *dev); +typedef bool (*mgos_imu_gyro_detect_fn)(struct mgos_imu_gyro *dev); +typedef bool (*mgos_imu_gyro_start_fn)(struct mgos_imu_gyro *dev); +typedef bool (*mgos_imu_gyro_read_fn)(struct mgos_imu_gyro *dev); + +enum mgos_imu_acc_type { + ACC_NONE = 0, + ACC_MPU9250 +}; + +enum mgos_imu_gyro_type { + GYRO_NONE = 0, + GYRO_MPU9250 +}; + +enum mgos_imu_mag_type { + MAG_NONE = 0, + MAG_AK8963 +}; + +struct mgos_imu { + struct mgos_imu_mag *mag; + struct mgos_imu_acc *acc; + struct mgos_imu_gyro *gyro; +}; + +struct mgos_imu_mag { + mgos_imu_mag_detect_fn detect; + mgos_imu_mag_start_fn start; + mgos_imu_mag_read_fn read; + + struct mgos_i2c *i2c; + uint8_t i2caddr; + enum mgos_imu_mag_type type; + + float gain[3]; + int16_t data[3]; +}; + +struct mgos_imu_acc { + mgos_imu_acc_detect_fn detect; + mgos_imu_acc_start_fn start; + mgos_imu_acc_read_fn read; + + struct mgos_i2c *i2c; + uint8_t i2caddr; + enum mgos_imu_acc_type type; + + float scale; + int16_t data[3]; +}; + +struct mgos_imu_gyro { + mgos_imu_gyro_detect_fn detect; + mgos_imu_gyro_start_fn start; + mgos_imu_gyro_read_fn read; + + struct mgos_i2c *i2c; + uint8_t i2caddr; + enum mgos_imu_gyro_type type; + + float scale; + float bias[3]; + int16_t data[3]; + int16_t temperature; +}; + + +struct mgos_imu *mgos_imu_create_i2c(struct mgos_i2c *i2c); +bool mgos_imu_add_gyroscope_i2c(struct mgos_imu *sensor, uint8_t i2caddr, enum mgos_imu_gyro_type type); +bool mgos_imu_add_accelerometer_i2c(struct mgos_imu *sensor, uint8_t i2caddr, enum mgos_imu_acc_type type); +bool mgos_imu_add_magnetometer_i2c(struct mgos_imu *sensor, uint8_t i2caddr, enum mgos_imu_mag_type type); + +void mgos_imu_destroy(struct mgos_imu **sensor); + +bool mgos_imu_has_accelerometer(struct mgos_imu *sensor); +bool mgos_imu_has_gyroscope(struct mgos_imu *sensor); +bool mgos_imu_has_magnetometer(struct mgos_imu *sensor); + +/* Read all available sensor data in the IMU */ +bool mgos_imu_read(struct mgos_imu *sensor); + +/* Return accelerometer data in units of G */ +bool mgos_imu_get_accelerometer(struct mgos_imu *sensor, float *x, float *y, float *z); + +/* Return accelerometer data in units of deg/sec rotation rate */ +bool mgos_imu_get_gyroscope(struct mgos_imu *sensor, float *x, float *y, float *z); + +/* Return magnetometer data in units of microTesla */ +bool mgos_imu_get_magnetometer(struct mgos_imu *sensor, float *x, float *y, float *z); + +/* + * Initialization function for MGOS -- currently a noop. + */ +bool mgos_imu_init(void); + + #ifdef __cplusplus +} +#endif diff --git a/src/main.c b/src/main.c index 8519030..bdee7be 100644 --- a/src/main.c +++ b/src/main.c @@ -22,6 +22,7 @@ #include "mgos_mcp9808.h" #include "mgos_ccs811.h" #include "mgos_mpu9250.h" +#include "mgos_imu.h" #include #include