tutrcos
読み取り中…
検索中…
一致する文字列を見つけられません
c6x0.hpp
[詳解]
1#pragma once
2
3#include <array>
4#include <cstddef>
5#include <cstdint>
6
8#include "tutrcos/utility.hpp"
9
10#include "encoder_base.hpp"
11
12namespace tutrcos {
13namespace module {
14
59class C6x0 : public EncoderBase {
60public:
61 enum class Type {
62 C610,
63 C620,
64 };
65
66 enum class ID {
67 _1,
68 _2,
69 _3,
70 _4,
71 _5,
72 _6,
73 _7,
74 _8,
75 };
76
77 class Manager {
78 public:
79 Manager(peripheral::CANBase &can) : can_{can} {}
80
81 bool update() {
83 while (can_.receive(msg, 0)) {
84 if (msg.id >= 0x201 && msg.id < 0x201 + 8) {
85 C6x0 *motor = motors_[msg.id - 0x201];
86 if (motor) {
87 int16_t count =
88 static_cast<int16_t>(msg.data[0] << 8 | msg.data[1]);
89 int16_t delta = count - motor->prev_count_;
90 if (delta > 4096) {
91 delta -= 8192;
92 } else if (delta < -4096) {
93 delta += 8192;
94 }
95 motor->set_count(motor->get_count() + delta);
96 motor->prev_count_ = count;
97
98 motor->rpm_ = static_cast<int16_t>(msg.data[2] << 8 | msg.data[3]);
99 motor->current_ =
100 static_cast<int16_t>(msg.data[4] << 8 | msg.data[5]);
101 }
102 }
103 }
104
105 msg.data.fill(0);
107 msg.id = 0x200;
108 msg.dlc = 8;
109 for (size_t i = 0; i < 4; ++i) {
110 if (motors_[i]) {
111 msg.data[i * 2] = motors_[i]->target_current_ >> 8;
112 msg.data[i * 2 + 1] = motors_[i]->target_current_;
113 }
114 }
115 if (!can_.transmit(msg, 0)) {
116 return false;
117 }
118
119 msg.data.fill(0);
120 msg.id = 0x1FF;
121 for (size_t i = 0; i < 4; ++i) {
122 if (motors_[i + 4]) {
123 msg.data[i * 2] = motors_[i + 4]->target_current_ >> 8;
124 msg.data[i * 2 + 1] = motors_[i + 4]->target_current_;
125 }
126 }
127 return can_.transmit(msg, 0);
128 }
129
130 private:
132 std::array<C6x0 *, 8> motors_{};
133
134 friend C6x0;
135 };
136
137 C6x0(Manager &manager, Type type, ID id)
138 : EncoderBase{8192}, manager_{manager}, type_{type}, id_{id} {
139 TUTRCOS_VERIFY(manager_.motors_[utility::to_underlying(id_)] == nullptr);
140 manager_.motors_[utility::to_underlying(id_)] = this;
141 }
142
143 ~C6x0() { manager_.motors_[utility::to_underlying(id_)] = nullptr; }
144
145 float get_rps() override { return get_rpm() / 60; }
146
147 float get_rpm() override { return rpm_; }
148
149 int16_t get_current() {
150 switch (type_) {
151 case Type::C610:
152 return current_;
153 case Type::C620:
154 return current_ * 25000 / 16384;
155 }
156 }
157
158 void set_current(int16_t current) {
159 switch (type_) {
160 case Type::C610:
161 target_current_ = current;
162 break;
163 case Type::C620:
164 target_current_ = current * 16384 / 25000;
165 break;
166 }
167 }
168
169private:
170 Manager &manager_;
171 Type type_;
172 ID id_;
173
174 int16_t prev_count_ = 0;
175 int16_t rpm_ = 0;
176 int16_t current_ = 0;
177 int16_t target_current_ = 0;
178};
179
180} // namespace module
181} // namespace tutrcos
Definition c6x0.hpp:77
bool update()
Definition c6x0.hpp:81
Manager(peripheral::CANBase &can)
Definition c6x0.hpp:79
Definition c6x0.hpp:59
float get_rps() override
Definition c6x0.hpp:145
void set_current(int16_t current)
Definition c6x0.hpp:158
Type
Definition c6x0.hpp:61
C6x0(Manager &manager, Type type, ID id)
Definition c6x0.hpp:137
float get_rpm() override
Definition c6x0.hpp:147
int16_t get_current()
Definition c6x0.hpp:149
ID
Definition c6x0.hpp:66
~C6x0()
Definition c6x0.hpp:143
Definition encoder_base.hpp:10
void set_count(int64_t count)
Definition encoder_base.hpp:28
int64_t get_count()
Definition encoder_base.hpp:16
Definition can_base.hpp:10
virtual bool transmit(const Message &msg, uint32_t timeout)=0
virtual bool receive(Message &msg, uint32_t timeout)=0
constexpr std::underlying_type_t< T > to_underlying(T value) noexcept
Definition utility.hpp:23
Definition kernel.hpp:7
Definition can_base.hpp:17
uint32_t id
Definition can_base.hpp:19
uint8_t dlc
Definition can_base.hpp:20
IDType id_type
Definition can_base.hpp:18
std::array< uint8_t, 8 > data
Definition can_base.hpp:21
#define TUTRCOS_VERIFY(expr)
Definition utility.hpp:16