Boboter
Loading...
Searching...
No Matches
encoders.h
Go to the documentation of this file.
1
7
8#pragma once
9
10#include <atomic>
11#include <esp_attr.h>
12#include <soc/gpio_num.h>
13#include "include/log_sources.h"
14
15class Robot;
16
17namespace Device {
18 class Encoders {
19 private:
20 static constexpr const char* TAG = "Device::Encoders";
21 static constexpr log_source LOG_SOURCE = LOG_SOURCE_DEVICE_ENCODERS;
22
23 static constexpr uint8_t NUM_ENCODERS = 2;
24 static constexpr gpio_num_t LEFT_ENCODER_PIN = GPIO_NUM_27;
25 static constexpr gpio_num_t RIGHT_ENCODER_PIN = GPIO_NUM_14;
26
27 struct isr_context_t {
28 Encoders* instance;
29 uint8_t encoder_index;
30 };
31
32 Robot& robot;
33 std::atomic<int32_t> pulse_count[NUM_ENCODERS];
34 isr_context_t isr_contexts[NUM_ENCODERS];
35
36 public:
37 static void IRAM_ATTR encoder_isr_handler(void* arg);
38
39 enum class encoder_id_t : uint8_t {
40 LEFT = 0,
41 RIGHT = 1,
42 };
43
44 public:
45 explicit Encoders(Robot& robot);
46 ~Encoders();
47
51 void initialize();
52
60 [[nodiscard]] int32_t get_pulse_count(encoder_id_t encoder_id) const {
61 return pulse_count[static_cast<uint8_t>(encoder_id)];
62 }
63
70 pulse_count[static_cast<uint8_t>(encoder_id)] = 0;
71 }
72 };
73}
encoder_id_t
Definition encoders.h:39
@ RIGHT
Definition encoders.h:41
@ LEFT
Definition encoders.h:40
int32_t get_pulse_count(encoder_id_t encoder_id) const
Gets the pulse count of the given encoder.
Definition encoders.h:60
Encoders(Robot &robot)
Definition encoders.cpp:15
void reset_pulse_count(encoder_id_t encoder_id)
Resets the pulse count of the given encoder back to zero.
Definition encoders.h:69
~Encoders()
Definition encoders.cpp:22
static void IRAM_ATTR encoder_isr_handler(void *arg)
Definition encoders.cpp:26
void initialize()
Sets up the necessary GPIO pins.
Definition encoders.cpp:33
Class containing all the components of the robot.
Definition robot.h:32
log_source
Definition log_sources.h:12
@ LOG_SOURCE_DEVICE_ENCODERS
Definition log_sources.h:26
Definition battery.cpp:14