8
8
#include " base_component.hpp"
9
9
#include " bldc_driver.hpp"
10
10
#include " bldc_motor.hpp"
11
- #include " button.hpp"
12
11
#include " gaussian.hpp"
13
12
#include " i2c.hpp"
13
+ #include " interrupt.hpp"
14
14
#include " led.hpp"
15
15
#include " mt6701.hpp"
16
16
#include " oneshot_adc.hpp"
@@ -40,7 +40,13 @@ namespace espp {
40
40
// / \snippet motorgo_mini_example.cpp motorgo-mini example
41
41
class MotorGoMini : public BaseComponent {
42
42
public:
43
+ // / Alias for the button callback function
44
+ using button_callback_t = espp::Interrupt::event_callback_fn;
45
+
46
+ // / Alias for the encoder type
43
47
using Encoder = espp::Mt6701<espp::Mt6701Interface::SSI>;
48
+
49
+ // / Alias for the BLDC motor type
44
50
using BldcMotor = espp::BldcMotor<espp::BldcDriver, Encoder>;
45
51
46
52
// / @brief Access the singleton instance of the MotorGoMini class
@@ -59,8 +65,26 @@ class MotorGoMini : public BaseComponent {
59
65
// / \return A reference to the external I2C bus
60
66
I2c &get_external_i2c ();
61
67
62
- // / Get a reference to the boot button
63
- espp::Button &button ();
68
+ // / Get a reference to the interrupts
69
+ // / \return A reference to the interrupts
70
+ espp::Interrupt &interrupts ();
71
+
72
+ // ///////////////////////////////////////////////////////////////////////////
73
+ // Button
74
+ // ///////////////////////////////////////////////////////////////////////////
75
+
76
+ // / Initialize the button
77
+ // / \param callback The callback function to call when the button is pressed
78
+ // / \return true if the button was successfully initialized, false otherwise
79
+ bool initialize_button (const button_callback_t &callback = nullptr );
80
+
81
+ // / Get the button state
82
+ // / \return The button state (true = button pressed, false = button released)
83
+ bool button_state () const ;
84
+
85
+ // ///////////////////////////////////////////////////////////////////////////
86
+ // LEDs
87
+ // ///////////////////////////////////////////////////////////////////////////
64
88
65
89
// / Get a reference to the yellow LED channel (channel 0)
66
90
// / \return A reference to the yellow LED channel (channel 0)
@@ -117,6 +141,22 @@ class MotorGoMini : public BaseComponent {
117
141
// / effectively turning off the LEDs.
118
142
void stop_breathing ();
119
143
144
+ // ///////////////////////////////////////////////////////////////////////////
145
+ // ADCs
146
+ // ///////////////////////////////////////////////////////////////////////////
147
+
148
+ // / Get a reference to the ADC_UNIT_1 OneshotAdc object
149
+ // / \return A reference to the ADC_UNIT_1 OneshotAdc object
150
+ espp::OneshotAdc &adc1 ();
151
+
152
+ // / Get a reference to the ADC_UNIT_2 OneshotAdc object
153
+ // / \return A reference to the ADC_UNIT_2 OneshotAdc object
154
+ espp::OneshotAdc &adc2 ();
155
+
156
+ // ///////////////////////////////////////////////////////////////////////////
157
+ // Motors
158
+ // ///////////////////////////////////////////////////////////////////////////
159
+
120
160
// / Initialize the MotorGo-Mini's components for motor channel 1
121
161
// / \details This function initializes the encoder and motor for motor channel
122
162
// / 1. This consists of initializing encoder1 and motor1.
@@ -127,14 +167,6 @@ class MotorGoMini : public BaseComponent {
127
167
// / 2. This consists of initializing encoder2 and motor2.
128
168
void init_motor_channel_2 ();
129
169
130
- // / Get a reference to the encoder 1
131
- // / \return A reference to the encoder 1
132
- Encoder &encoder1 ();
133
-
134
- // / Get a reference to the encoder 2
135
- // / \return A reference to the encoder 2
136
- Encoder &encoder2 ();
137
-
138
170
// / Get a reference to the motor 1 driver
139
171
// / \return A reference to the motor 1 driver
140
172
espp::BldcDriver &motor1_driver ();
@@ -151,13 +183,21 @@ class MotorGoMini : public BaseComponent {
151
183
// / \return A reference to the motor 2
152
184
BldcMotor &motor2 ();
153
185
154
- // / Get a reference to the ADC_UNIT_1 OneshotAdc object
155
- // / \return A reference to the ADC_UNIT_1 OneshotAdc object
156
- espp::OneshotAdc & adc1 ();
186
+ // ///////////////////////////////////////////////////////////////////////////
187
+ // Encoders
188
+ // ///////////////////////////////////////////////////////////////////////////
157
189
158
- // / Get a reference to the ADC_UNIT_2 OneshotAdc object
159
- // / \return A reference to the ADC_UNIT_2 OneshotAdc object
160
- espp::OneshotAdc &adc2 ();
190
+ // / Get a reference to the encoder 1
191
+ // / \return A reference to the encoder 1
192
+ Encoder &encoder1 ();
193
+
194
+ // / Get a reference to the encoder 2
195
+ // / \return A reference to the encoder 2
196
+ Encoder &encoder2 ();
197
+
198
+ // ///////////////////////////////////////////////////////////////////////////
199
+ // Motor Current Sense
200
+ // ///////////////////////////////////////////////////////////////////////////
161
201
162
202
// / Get the current sense value for motor 1 phase U
163
203
// / \return The current sense value for motor 1 phase U in amps
@@ -388,15 +428,28 @@ class MotorGoMini : public BaseComponent {
388
428
.channels = {current_sense_m2_u_},
389
429
}};
390
430
391
- // button
392
- espp::Button button_{{
393
- .name = " MotorGo Mini Button" ,
431
+ // Interrupts
432
+ espp::Interrupt::PinConfig button_interrupt_pin_{
394
433
.gpio_num = BUTTON_GPIO,
434
+ .callback =
435
+ [this ](const auto &event) {
436
+ if (button_callback_) {
437
+ button_callback_ (event);
438
+ }
439
+ },
395
440
.active_level = espp::Interrupt::ActiveLevel::LOW,
396
- .pullup_enabled = false ,
397
- .pulldown_enabled = false ,
398
- .log_level = espp::Logger::Verbosity::WARN,
399
- }};
441
+ .interrupt_type = espp::Interrupt::Type::ANY_EDGE,
442
+ .pullup_enabled = true };
443
+
444
+ // we'll only add each interrupt pin if the initialize method is called
445
+ espp::Interrupt interrupts_{
446
+ {.interrupts = {},
447
+ .task_config = {.name = " motorgo mini interrupts" ,
448
+ .stack_size_bytes = CONFIG_MOTORGO_MINI_INTERRUPT_STACK_SIZE}}};
449
+
450
+ // button
451
+ std::atomic<bool > button_initialized_{false };
452
+ button_callback_t button_callback_{nullptr };
400
453
401
454
// led
402
455
std::vector<espp::Led::ChannelConfig> led_channels_{
0 commit comments