mirs_esp_pio
読み取り中…
検索中…
一致する文字列を見つけられません
Encoder.cpp
[詳解]
1#include "Encoder.hpp"
2
3#include "esp_err.h"
4
5namespace {
6constexpr uint32_t kPcntGlitchFilterNs = 1250;
7}
8
9Encoder::Encoder(uint8_t pin_a, uint8_t pin_b)
10 : pin_a_(pin_a), pin_b_(pin_b)
11{
12}
13
15 if (initialized_) {
16 return;
17 }
18
19 pinMode(pin_a_, INPUT_PULLUP);
20 pinMode(pin_b_, INPUT_PULLUP);
21
22 const pcnt_unit_config_t unit_config = {
23 .low_limit = -PCNT_LIMIT,
24 .high_limit = PCNT_LIMIT,
25 .intr_priority = 0,
26 .flags = {.accum_count = 1},
27 };
28 ESP_ERROR_CHECK(pcnt_new_unit(&unit_config, &unit_));
29 ESP_ERROR_CHECK(pcnt_unit_add_watch_point(unit_, -PCNT_LIMIT));
30 ESP_ERROR_CHECK(pcnt_unit_add_watch_point(unit_, PCNT_LIMIT));
31
32 const pcnt_glitch_filter_config_t filter_config = {
33 .max_glitch_ns = kPcntGlitchFilterNs,
34 };
35 ESP_ERROR_CHECK(pcnt_unit_set_glitch_filter(unit_, &filter_config));
36
37 // A相エッジを数え、B相がLOWの間は方向を反転する
38 const pcnt_chan_config_t channel_a_config = {
39 .edge_gpio_num = pin_a_,
40 .level_gpio_num = pin_b_,
41 };
42 ESP_ERROR_CHECK(pcnt_new_channel(unit_, &channel_a_config, &channel_a_));
43 ESP_ERROR_CHECK(pcnt_channel_set_edge_action(
44 channel_a_, PCNT_CHANNEL_EDGE_ACTION_INCREASE, PCNT_CHANNEL_EDGE_ACTION_DECREASE));
45 ESP_ERROR_CHECK(pcnt_channel_set_level_action(
46 channel_a_, PCNT_CHANNEL_LEVEL_ACTION_KEEP, PCNT_CHANNEL_LEVEL_ACTION_INVERSE));
47
48 // B相も計数し、A相がHIGHの間は方向を反転してX4計数する
49 const pcnt_chan_config_t channel_b_config = {
50 .edge_gpio_num = pin_b_,
51 .level_gpio_num = pin_a_,
52 };
53 ESP_ERROR_CHECK(pcnt_new_channel(unit_, &channel_b_config, &channel_b_));
54 ESP_ERROR_CHECK(pcnt_channel_set_edge_action(
55 channel_b_, PCNT_CHANNEL_EDGE_ACTION_INCREASE, PCNT_CHANNEL_EDGE_ACTION_DECREASE));
56 ESP_ERROR_CHECK(pcnt_channel_set_level_action(
57 channel_b_, PCNT_CHANNEL_LEVEL_ACTION_INVERSE, PCNT_CHANNEL_LEVEL_ACTION_KEEP));
58
59 ESP_ERROR_CHECK(pcnt_unit_clear_count(unit_));
60 ESP_ERROR_CHECK(pcnt_unit_enable(unit_));
61 ESP_ERROR_CHECK(pcnt_unit_start(unit_));
62 initialized_ = true;
63}
64
65int64_t Encoder::getCount() const {
66 if (!initialized_) {
67 return 0;
68 }
69
70 int count = 0;
71 ESP_ERROR_CHECK(pcnt_unit_get_count(unit_, &count));
72
73 // The driver accumulator is int-sized; extend it to int64 across its wrap.
74 int64_t delta = static_cast<int64_t>(count) - last_pcnt_count_;
75 if (delta > INT32_MAX) {
76 delta -= (INT64_C(1) << 32);
77 } else if (delta < INT32_MIN) {
78 delta += (INT64_C(1) << 32);
79 }
80 extended_count_ += delta;
81 last_pcnt_count_ = static_cast<int32_t>(count);
82 return extended_count_;
83}
84
86 if (!initialized_) {
87 return;
88 }
89
90 ESP_ERROR_CHECK(pcnt_unit_stop(unit_));
91 ESP_ERROR_CHECK(pcnt_unit_clear_count(unit_));
92 extended_count_ = 0;
93 last_pcnt_count_ = 0;
94 ESP_ERROR_CHECK(pcnt_unit_start(unit_));
95}
ESP32のパルス計数機能で左右のエンコーダ信号を数える。
int64_t getCount() const
static constexpr int16_t PCNT_LIMIT
void begin()
void reset()
Encoder(uint8_t pin_a, uint8_t pin_b)