RBControl
Library for the RB3201-RBControl board with the ESP32 by RoboticsBrno.
RBControl_encoder.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <atomic>
4 #include <functional>
5 
6 #include <driver/gpio.h>
7 #include <driver/pcnt.h>
8 
9 #include "RBControl_pinout.hpp"
10 #include "RBControl_util.hpp"
11 
12 namespace rb {
13 
14 class Encoder;
15 class Manager;
16 
17 class Encoder {
18  friend class Manager;
19  friend class Motor;
20 
21 public:
22  ~Encoder();
23 
33  void driveToValue(int32_t positionAbsolute, uint8_t power, std::function<void(Encoder&)> callback = nullptr);
43  void drive(int32_t positionRelative, uint8_t power, std::function<void(Encoder&)> callback = nullptr);
44 
50  int32_t value();
51 
56  float speed();
57 
58 private:
59  Encoder(Manager& man, MotorId id);
60  Encoder(const Encoder&) = delete;
61 
62  static void IRAM_ATTR isrGpio(void* cookie);
63 
64  void install();
65 
66  void onEdgeIsr(int64_t timestamp, uint8_t pinLevel);
67  void onPcntIsr(uint32_t status);
68 
69  void pcnt_init(pcnt_unit_t pcntUnit, gpio_num_t GPIO_A, gpio_num_t GPIO_B);
70 
71  Manager& m_manager;
72  MotorId m_id;
73 
74  std::atomic<int32_t> m_counter;
75 
76  std::mutex m_time_mutex;
77  int64_t m_counter_time_us_last;
78  int64_t m_counter_time_us_diff;
79  int32_t m_target;
80  int8_t m_target_direction;
81  std::function<void(Encoder&)> m_target_callback;
82 };
83 
85 class PcntInterruptHandler {
86 public:
87  static PcntInterruptHandler& get(Manager* manager);
88 
89  void enable(int index);
90 
91 private:
92  PcntInterruptHandler(Manager* manager);
93  ~PcntInterruptHandler();
94  static void IRAM_ATTR isrHandler(void* cookie);
95 };
96 
97 } // namespace rb
Definition: RBControl_encoder.hpp:17
~Encoder()
Definition: RBControl_encoder.cpp:110
float speed()
Get number of edges per one second.
Definition: RBControl_encoder.cpp:248
int32_t value()
Get number of edges from encoder.
Definition: RBControl_encoder.cpp:242
void drive(int32_t positionRelative, uint8_t power, std::function< void(Encoder &)> callback=nullptr)
Drive motor to set position (according relative value).
Definition: RBControl_encoder.cpp:284
void driveToValue(int32_t positionAbsolute, uint8_t power, std::function< void(Encoder &)> callback=nullptr)
Drive motor to set position (according absolute value).
Definition: RBControl_encoder.cpp:263
The main library class for working with the RBControl board. Call the install() method at the start o...
Definition: RBControl_manager.hpp:49
Definition: RBControl_motor.hpp:17
The base namespace. Contains some logging functions, too.
Definition: half_duplex_uart.cpp:53
MotorId
Definition: RBControl_pinout.hpp:8