tutrc_harurobo_lib
読み取り中…
検索中…
一致する文字列を見つけられません
c610.hpp
[詳解]
1#pragma once
2
3#include <array>
4#include <cstddef>
5#include <cstdint>
6
7#include "can_base.hpp"
8#include "utility.hpp"
9
10namespace tutrc_harurobo_lib {
11
51class C610 {
52public:
53 enum class ID {
54 ID1,
55 ID2,
56 ID3,
57 ID4,
58 ID5,
59 ID6,
60 ID7,
61 ID8,
62 };
63
64 bool init(CANBase *can) {
65 can_ = can;
66 return true;
67 }
68
69 void update() {
70 CANMessage msg;
71 for (size_t i = can_->available(); i > 0; --i) {
72 if (can_->receive(&msg, 0)) {
73 for (size_t i = 0; i < 8; ++i) {
74 if (msg.id == 0x201 + i) {
75 int16_t angle =
76 static_cast<int16_t>(msg.data[0] << 8 | msg.data[1]);
77 int16_t delta = angle - prev_angle_[i];
78 if (delta > 4096) {
79 delta -= 8192;
80 } else if (delta < -4096) {
81 delta += 8192;
82 }
83 position_[i] += delta;
84 prev_angle_[i] = angle;
85
86 rpm_[i] = static_cast<int16_t>(msg.data[2] << 8 | msg.data[3]);
87 current_[i] = static_cast<int16_t>(msg.data[4] << 8 | msg.data[5]);
88 break;
89 }
90 }
91 }
92 }
93
95 msg.id = 0x200;
96 msg.dlc = 8;
97 for (size_t i = 0; i < 4; ++i) {
98 msg.data[i * 2] = current_target_[i] >> 8;
99 msg.data[i * 2 + 1] = current_target_[i];
100 }
101 can_->transmit(&msg);
102 msg.id = 0x1FF;
103 for (size_t i = 0; i < 4; ++i) {
104 msg.data[i * 2] = current_target_[i + 4] >> 8;
105 msg.data[i * 2 + 1] = current_target_[i + 4];
106 }
107 can_->transmit(&msg);
108 }
109
110 float get_rpm(ID id) { return rpm_[to_underlying(id)]; }
111
112 float get_rps(ID id) { return get_rpm(id) / 60.0f; }
113
114 float get_position(ID id) { return position_[to_underlying(id)] / 8192.0f; }
115
116 void set_position(ID id, float position) {
117 position_[to_underlying(id)] = position * 8192;
118 }
119
120 // -10000 ~ 10000 mA
121 int16_t get_current(ID id) { return current_[to_underlying(id)]; }
122
123 void set_current(ID id, int16_t current) {
124 current_target_[to_underlying(id)] = current;
125 }
126
127private:
128 CANBase *can_;
129
130 std::array<int16_t, 8> prev_angle_ = {};
131 std::array<int64_t, 8> position_ = {};
132 std::array<int16_t, 8> rpm_ = {};
133 std::array<int16_t, 8> current_ = {};
134 std::array<int16_t, 8> current_target_ = {};
135};
136
137} // namespace tutrc_harurobo_lib
Definition c610.hpp:51
bool init(CANBase *can)
Definition c610.hpp:64
void set_position(ID id, float position)
Definition c610.hpp:116
float get_rpm(ID id)
Definition c610.hpp:110
int16_t get_current(ID id)
Definition c610.hpp:121
float get_rps(ID id)
Definition c610.hpp:112
void update()
Definition c610.hpp:69
float get_position(ID id)
Definition c610.hpp:114
ID
Definition c610.hpp:53
void set_current(ID id, int16_t current)
Definition c610.hpp:123
Definition can_base.hpp:20
virtual bool receive(CANMessage *msg, uint32_t timeout)=0
virtual size_t available()=0
virtual bool transmit(const CANMessage *msg)=0
Definition bno055.hpp:10
constexpr std::underlying_type_t< T > to_underlying(T value) noexcept
Definition utility.hpp:16
Definition can_base.hpp:13
uint8_t dlc
Definition can_base.hpp:16
uint32_t id
Definition can_base.hpp:15
uint8_t data[8]
Definition can_base.hpp:17
CANIDType id_type
Definition can_base.hpp:14