UdonLibrary 1.0.0
機械システム研究部 C++ ライブラリ
読み取り中…
検索中…
一致する文字列を見つけられません
Encoder.hpp
[詳解]
1//
2// 通信経由ロータリーエンコーダー
3//
4// Copyright (c) 2022-2024 udonrobo
5//
6
7#pragma once
8
10#include <algorithm>
11
15
18
19namespace Udon
20{
21
24 template <template <typename> class Reader>
26 {
27 static_assert(Traits::IsReader<Reader>::value, "Reader is not reader");
28
29 using ReaderType = Reader<Message::Encoder>;
30
31 ReaderType reader;
32
33 Udon::DeltaTime deltaTime;
34
35 bool direction;
36
37 int32_t count{};
38 int32_t deltaCount{};
39 int32_t offsetCount{};
40 double speed{};
41
42 public:
46 EncoderBy(ReaderType&& reader, bool direction)
47 : reader(std::move(reader))
48 , deltaTime()
49 , direction(direction)
50 {
51 }
52
54 void update()
55 {
57
58 const auto prev = getCount();
59
60 if (const auto countOpt = reader.getMessage())
61 {
62 count = countOpt->count * (direction ? 1 : -1);
63 }
64
65 const auto curr = getCount();
66
67 deltaCount = curr - prev;
68
69 speed = deltaCount / deltaTime.update().getDeltaTimeS();
70 }
71
74 void setOffset(int32_t value = 0)
75 {
76 offsetCount = count - value;
77 }
78
81 int32_t getCount() const
82 {
83 return count - offsetCount;
84 }
85
88 int32_t getDeltaCount() const
89 {
90 return deltaCount;
91 }
92
95 double getSpeed() const
96 {
97 return speed;
98 }
99
100#ifdef ARDUINO
102 void show() const
103 {
104 Serial.print(getCount());
105 Serial.print('\t');
106 }
107#endif
108 };
109
110} // namespace Udon
変化時間計測クラス
Definition DeltaTime.hpp:16
double getDeltaTimeS() const
経過時間を取得
Definition DeltaTime.hpp:48
DeltaTime & update()
更新
Definition DeltaTime.hpp:32
通信経由ロータリーエンコーダー
Definition Encoder.hpp:26
void update()
更新
Definition Encoder.hpp:54
EncoderBy(ReaderType &&reader, bool direction)
コンストラクタ
Definition Encoder.hpp:46
int32_t getCount() const
カウント値を取得
Definition Encoder.hpp:81
void setOffset(int32_t value=0)
カウント値オフセット
Definition Encoder.hpp:74
int32_t getDeltaCount() const
カウント値の差分を取得
Definition Encoder.hpp:88
double getSpeed() const
速度を取得
Definition Encoder.hpp:95
void MaybeInvokeUpdate(HasMemberFunctionUpdate &rhs)
T に T::update 関数が存在する場合呼び出す。それ以外の場合何もしない。
Definition HasMemberFunction.hpp:111
Definition Bit.hpp:12
Definition Typedef.hpp:94
送信クラスであるか判定
Definition ReaderWriterTraits.hpp:42