uas-ugv/main/ugv_io_mpu.hh

103 lines
1.7 KiB
C++
Raw Normal View History

2019-01-17 19:44:14 -08:00
#pragma once
#include <stdint.h>
#include <MPU.hpp>
namespace ugv {
namespace io {
2019-01-17 19:59:13 -08:00
struct Vec3f {
float x;
float y;
float z;
Vec3f();
Vec3f(float x, float y, float z);
2019-04-10 20:47:21 -07:00
Vec3f(const mpud::float_axes_t& axes);
2019-05-09 21:16:20 -07:00
Vec3f operator*(float s) const { return {s * x, s * y, s * z}; }
Vec3f& operator*=(float s) {
x *= s;
y *= s;
z *= s;
return *this;
}
2019-04-10 20:47:21 -07:00
Vec3f operator+(const Vec3f& a) const { return {x + a.x, y + a.y, z + a.z}; }
Vec3f& operator+=(const Vec3f& a) {
x += a.x;
y += a.y;
z += a.z;
return *this;
}
2019-05-09 21:16:20 -07:00
float dot(const Vec3f& v) const {
return x * v.x + y * v.y + z * v.z;
}
2019-04-10 20:47:21 -07:00
};
struct Mat3f {
2019-05-09 21:16:20 -07:00
union {
Vec3f rx;
float xx, xy, xz;
};
union {
Vec3f ry;
float yx, yy, yz;
};
union {
Vec3f rz;
float zx, zy, zz;
};
Mat3f(Vec3f rx_, Vec3f ry_, Vec3f rz_)
: rx(rx_), ry(ry_), rz(rz_) {}
Mat3f(float xx, float xy, float xz, float yx, float yy, float yz, float zx,
float zy, float zz)
: rx(xx, xy, xz), ry(yx, yy, yz), rz(zx, zy, zz) {}
2019-04-10 20:47:21 -07:00
Vec3f operator*(const Vec3f& v) const {
2019-05-09 21:16:20 -07:00
return {rx.dot(v), ry.dot(v), rz.dot(v)};
}
Mat3f transpose() const {
return {xx, yx, zx, xy, yy, zy, xz, yz, zz};
}
Mat3f operator*(const Mat3f& m) const {
Mat3f mt = m.transpose();
return {mt * rx, mt * ry, mt * rz};
2019-04-10 20:47:21 -07:00
}
2019-01-17 19:59:13 -08:00
};
2019-01-17 19:44:14 -08:00
struct MpuData {
// G's
2019-01-17 19:59:13 -08:00
Vec3f accel;
2019-01-17 19:44:14 -08:00
// degrees/s
2019-01-17 19:59:13 -08:00
Vec3f gyro_rate;
2019-01-17 19:44:14 -08:00
// flux density uT
2019-01-17 19:59:13 -08:00
Vec3f mag;
2019-01-17 19:44:14 -08:00
};
class MPU {
public:
explicit MPU();
~MPU();
void Init();
2019-01-30 19:50:51 -08:00
void Calibrate();
2019-01-17 19:44:14 -08:00
2019-04-10 20:47:21 -07:00
void GetData(MpuData& data);
2019-01-17 19:44:14 -08:00
private:
2019-04-10 20:47:21 -07:00
mpud::mpu_bus_t* mpu_bus_;
mpud::MPU* mpu_;
2019-01-17 19:44:14 -08:00
mpud::raw_axes_t accel_, gyro_, mag_;
};
} // namespace io
} // namespace ugv