Tool Libs
UMIT-TIROL Institute of Automation and Control Engineering library collection
 
Loading...
Searching...
No Matches
BMI160.h
Go to the documentation of this file.
1
5#pragma once
6
7#include <cstdint>
8#include "sys/i2c.h"
9
12 enum ADDR {
13 DEFAULT = 0b1101000,
14 ALT = 0b1101001,
15 };
16 struct Axes {
17 double x, y, z;
18 };
19
20 BMI160(Sink<I2C::Request> &bus, enum ADDR addr=DEFAULT)
21 : I2C::Device(bus, addr) {
22 writeReg(0x7e, 0x11); // enable accelerometer
23 writeReg(0x7e, 0x15); // enable gyroscope
24 bus.trypush({
25 .dev = this,
26 .data = {0xc}, // set read pointer to gyro data
27 });
28 }
29
33 void measure() {
34 bus.trypush({
35 .dev = this,
36 .data = 12,
37 .opts = {
38 .read = true,
39 },
40 });
41 }
42
43 Axes acc{}, gyro{};
44 // TODO: make ranges configurable
45 double factor_a{9.81 / 16384}, factor_g{3.14 / 180 / 16.4};
46
47 void writeReg(uint8_t reg, uint8_t val) {
48 bus.trypush({
49 .dev = this,
50 .data = {val},
51 .opts = {
52 .mem = true,
53 },
54 .mem = reg,
55 });
56 }
57
58 void callback(const I2C::Request &rq) override {
59 if (rq.data.size != 12) return;
60 auto view = (int16_t*)rq.data.buf;
61 Axes g{
62 .x = view[0] * factor_g,
63 .y = view[1] * factor_g,
64 .z = view[2] * factor_g,
65 };
66 Axes a{
67 .x = view[3] * factor_a,
68 .y = view[4] * factor_a,
69 .z = view[5] * factor_a,
70 };
71 this->gyro = g;
72 this->acc = a;
73 }
74};
Copyright (c) 2023 IACE.
Implementation of the BMI160 6axis Inertial Measurement Unit.
Definition BMI160.h:11
void measure()
start async read-out of sensor data
Definition BMI160.h:33
void callback(const I2C::Request &rq) override
callback.
Definition BMI160.h:58
size_t size
total capacity of buffer
Definition buffer.h:23
generic I2C device wrapper.
Definition i2c.h:21
struct defining an I2C request.
Definition i2c.h:37
Buffer< uint8_t > data
transfer data
Definition i2c.h:39
generic object sink, i.e.
Definition streams.h:15
void trypush(T &&t)
use this if you don't care if it's going to be successful. data gets discarded if sink is full.
Definition streams.h:28