tutrc_harurobo_lib
読み取り中…
検索中…
一致する文字列を見つけられません
bno055.hpp
[詳解]
1#pragma once
2
3#include <array>
4#include <cstdint>
5
6#include "cmsis_os2.h"
7
8#include "uart.hpp"
9
11
12class BNO055 {
13public:
21 bool init(UART *uart, uint32_t timeout = 500) {
22 uart_ = uart;
23 uint32_t start = osKernelGetTickCount();
24 while (osKernelGetTickCount() - start < timeout) {
25 uint8_t data = 0x00;
26 if (!write_reg(0x3D, &data, 1)) {
27 continue;
28 }
29 data = 0x04;
30 if (!write_reg(0x3B, &data, 1)) {
31 continue;
32 }
33 data = 0x08;
34 if (!write_reg(0x3D, &data, 1)) {
35 continue;
36 }
37 return true;
38 }
39 return false;
40 }
41
45 void update() {
46 std::array<int16_t, 3> data;
47 if (read_reg(0x1A, reinterpret_cast<uint8_t *>(data.data()), 6)) {
48 euler_x_ = data[0] / 900.0f;
49 euler_y_ = data[1] / 900.0f;
50 euler_z_ = data[2] / 900.0f;
51 }
52 }
53
57 float get_euler_x() { return euler_x_; }
58
62 float get_euler_y() { return euler_y_; }
63
67 float get_euler_z() { return euler_z_; }
68
69private:
70 UART *uart_;
71 float euler_x_ = 0;
72 float euler_y_ = 0;
73 float euler_z_ = 0;
74
75 bool write_reg(uint8_t addr, uint8_t *data, uint8_t size) {
76 std::array<uint8_t, 4> buf = {0xAA, 0x00, addr, size};
77 uart_->flush();
78 if (!uart_->transmit(buf.data(), 4)) {
79 return false;
80 }
81 if (!uart_->transmit(data, size)) {
82 return false;
83 }
84 if (!uart_->receive(buf.data(), 2, 5)) {
85 return false;
86 }
87 return buf[0] == 0xEE && buf[1] == 0x01;
88 }
89
90 bool read_reg(uint8_t addr, uint8_t *data, uint8_t size) {
91 std::array<uint8_t, 4> buf = {0xAA, 0x01, addr, size};
92 uart_->flush();
93 if (!uart_->transmit(buf.data(), 4)) {
94 return false;
95 }
96 if (!uart_->receive(buf.data(), 2, 5)) {
97 return false;
98 }
99 if (buf[0] != 0xBB || buf[1] != size) {
100 return false;
101 }
102 return uart_->receive(data, size, 5);
103 }
104};
105
106} // namespace tutrc_harurobo_lib
Definition bno055.hpp:12
void update()
Definition bno055.hpp:45
float get_euler_z()
Definition bno055.hpp:67
float get_euler_x()
Definition bno055.hpp:57
float get_euler_y()
Definition bno055.hpp:62
bool init(UART *uart, uint32_t timeout=500)
Definition bno055.hpp:21
Definition uart.hpp:56
bool transmit(const uint8_t *data, size_t size)
bool receive(uint8_t *data, size_t size, uint32_t timeout)
Definition bno055.hpp:10