UdonLibrary
1.0.0
機械システム研究部 C++ ライブラリ
Toggle main menu visibility
読み取り中…
検索中…
一致する文字列を見つけられません
Encoder.hpp
[詳解]
1
//
2
// 通信経由ロータリーエンコーダー
3
//
4
// Copyright (c) 2022 udonrobo
5
//
6
7
#pragma once
8
9
#include <
Udon/Stl/EnableSTL.hpp
>
10
#include <algorithm>
11
12
#include <
Udon/Algorithm/DeltaTime.hpp
>
13
#include <
Udon/Com/Message/Encoder.hpp
>
14
#include <
Udon/Traits/HasMemberFunction.hpp
>
15
#include <
Udon/Traits/ReaderWriterTraits.hpp
>
16
#include <
Udon/Utility/Printf.hpp
>
17
#include <
Udon/Types/Direction.hpp
>
18
19
namespace
Udon
20
{
21
24
template
<
template
<
typename
>
class
Reader>
25
class
EncoderBy
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
Udon::Direction
direction;
36
37
int32_t count{};
38
int32_t deltaCount{};
39
int32_t offsetCount{};
40
double
speed{};
41
42
public
:
46
EncoderBy
(ReaderType&& reader,
Udon::Direction
direction =
Udon::Direction::Forward
)
47
: reader(
std
::move(reader))
48
, deltaTime()
49
, direction(direction)
50
{
51
}
52
54
void
update
()
55
{
56
Udon::Traits::MaybeInvokeUpdate
(reader);
57
58
const
auto
prev =
getCount
();
59
60
if
(
const
auto
countOpt = reader.getMessage())
61
{
62
count = countOpt->count *
Udon::DirectionToSign
(direction);
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
DeltaTime.hpp
Direction.hpp
EnableSTL.hpp
HasMemberFunction.hpp
Encoder.hpp
Printf.hpp
ReaderWriterTraits.hpp
Udon::DeltaTime
変化時間計測クラス
Definition
DeltaTime.hpp:16
Udon::EncoderBy::update
void update()
更新
Definition
Encoder.hpp:54
Udon::EncoderBy::EncoderBy
EncoderBy(ReaderType &&reader, Udon::Direction direction=Udon::Direction::Forward)
コンストラクタ
Definition
Encoder.hpp:46
Udon::EncoderBy::getCount
int32_t getCount() const
カウント値を取得
Definition
Encoder.hpp:81
Udon::EncoderBy::setOffset
void setOffset(int32_t value=0)
カウント値オフセット
Definition
Encoder.hpp:74
Udon::EncoderBy::getDeltaCount
int32_t getDeltaCount() const
カウント値の差分を取得
Definition
Encoder.hpp:88
Udon::EncoderBy::getSpeed
double getSpeed() const
速度を取得
Definition
Encoder.hpp:95
Udon::Traits::MaybeInvokeUpdate
void MaybeInvokeUpdate(HasMemberFunctionUpdate &rhs)
T に T::update 関数が存在する場合呼び出す。それ以外の場合何もしない。
Definition
HasMemberFunction.hpp:111
Udon
Definition
Bit.hpp:12
Udon::DirectionToSign
int DirectionToSign(Direction direction)
Definition
Direction.hpp:14
Udon::Direction
Direction
方向
Definition
Direction.hpp:9
Udon::Direction::Forward
@ Forward
Definition
Direction.hpp:10
std
Definition
Typedef.hpp:94
Udon::Traits::IsReader
送信クラスであるか判定
Definition
ReaderWriterTraits.hpp:42