Skip to content

Commit f055868

Browse files
authored
feat(motorgo-mini): Replace use of espp::Button with espp::Interrupt (#403)
1 parent cc77c3e commit f055868

File tree

6 files changed

+135
-48
lines changed

6 files changed

+135
-48
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
idf_component_register(
22
INCLUDE_DIRS "include"
33
SRC_DIRS "src"
4-
REQUIRES base_component button filters led math mt6701 pid task bldc_driver bldc_motor i2c adc
4+
REQUIRES base_component interrupt filters led math mt6701 pid task bldc_driver bldc_motor i2c adc
55
)

components/motorgo-mini/Kconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
menu "MotorGo Mini Configuration"
2+
config MOTORGO_MINI_INTERRUPT_STACK_SIZE
3+
int "Interrupt stack size"
4+
default 4096
5+
help
6+
Size of the stack used for the interrupt handler. Used by the
7+
button callback.
8+
endmenu

components/motorgo-mini/example/main/motorgo_mini_example.cpp

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ extern "C" void app_main(void) {
1919
motorgo_mini.init_motor_channel_2();
2020
auto &motor1 = motorgo_mini.motor1();
2121
auto &motor2 = motorgo_mini.motor2();
22-
auto &button = motorgo_mini.button();
2322

2423
static constexpr uint64_t core_update_period_us = 1000; // microseconds
2524
static constexpr float core_update_period = core_update_period_us / 1e6f; // seconds
@@ -169,30 +168,29 @@ extern "C" void app_main(void) {
169168
});
170169
target_task.start();
171170

172-
bool button_state = false;
173-
174-
while (true) {
175-
bool new_button_state = button.is_pressed();
176-
if (new_button_state != button_state) {
177-
button_state = new_button_state;
178-
if (button_state) {
179-
logger.info("Button pressed, changing motion control type");
180-
// switch between ANGLE and VELOCITY
181-
if (motion_control_type == espp::detail::MotionControlType::ANGLE ||
182-
motion_control_type == espp::detail::MotionControlType::ANGLE_OPENLOOP) {
183-
motion_control_type = espp::detail::MotionControlType::VELOCITY;
184-
target_is_angle = false;
185-
} else {
186-
motion_control_type = espp::detail::MotionControlType::ANGLE;
187-
target_is_angle = true;
188-
}
189-
initialize_target();
190-
motor1.set_motion_control_type(motion_control_type);
191-
motor2.set_motion_control_type(motion_control_type);
171+
logger.info("Initializing the button");
172+
auto on_button_pressed = [&](const auto &event) {
173+
if (event.active) {
174+
logger.info("Button pressed, changing motion control type");
175+
// switch between ANGLE and VELOCITY
176+
if (motion_control_type == espp::detail::MotionControlType::ANGLE ||
177+
motion_control_type == espp::detail::MotionControlType::ANGLE_OPENLOOP) {
178+
motion_control_type = espp::detail::MotionControlType::VELOCITY;
179+
target_is_angle = false;
192180
} else {
193-
logger.info("Button released");
181+
motion_control_type = espp::detail::MotionControlType::ANGLE;
182+
target_is_angle = true;
194183
}
184+
initialize_target();
185+
motor1.set_motion_control_type(motion_control_type);
186+
motor2.set_motion_control_type(motion_control_type);
187+
} else {
188+
logger.info("Button released");
195189
}
190+
};
191+
motorgo_mini.initialize_button(on_button_pressed);
192+
193+
while (true) {
196194
std::this_thread::sleep_for(50ms);
197195
}
198196
//! [motorgo-mini example]

components/motorgo-mini/include/motorgo-mini.hpp

Lines changed: 77 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
#include "base_component.hpp"
99
#include "bldc_driver.hpp"
1010
#include "bldc_motor.hpp"
11-
#include "button.hpp"
1211
#include "gaussian.hpp"
1312
#include "i2c.hpp"
13+
#include "interrupt.hpp"
1414
#include "led.hpp"
1515
#include "mt6701.hpp"
1616
#include "oneshot_adc.hpp"
@@ -40,7 +40,13 @@ namespace espp {
4040
/// \snippet motorgo_mini_example.cpp motorgo-mini example
4141
class MotorGoMini : public BaseComponent {
4242
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
4347
using Encoder = espp::Mt6701<espp::Mt6701Interface::SSI>;
48+
49+
/// Alias for the BLDC motor type
4450
using BldcMotor = espp::BldcMotor<espp::BldcDriver, Encoder>;
4551

4652
/// @brief Access the singleton instance of the MotorGoMini class
@@ -59,8 +65,26 @@ class MotorGoMini : public BaseComponent {
5965
/// \return A reference to the external I2C bus
6066
I2c &get_external_i2c();
6167

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+
/////////////////////////////////////////////////////////////////////////////
6488

6589
/// Get a reference to the yellow LED channel (channel 0)
6690
/// \return A reference to the yellow LED channel (channel 0)
@@ -117,6 +141,22 @@ class MotorGoMini : public BaseComponent {
117141
/// effectively turning off the LEDs.
118142
void stop_breathing();
119143

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+
120160
/// Initialize the MotorGo-Mini's components for motor channel 1
121161
/// \details This function initializes the encoder and motor for motor channel
122162
/// 1. This consists of initializing encoder1 and motor1.
@@ -127,14 +167,6 @@ class MotorGoMini : public BaseComponent {
127167
/// 2. This consists of initializing encoder2 and motor2.
128168
void init_motor_channel_2();
129169

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-
138170
/// Get a reference to the motor 1 driver
139171
/// \return A reference to the motor 1 driver
140172
espp::BldcDriver &motor1_driver();
@@ -151,13 +183,21 @@ class MotorGoMini : public BaseComponent {
151183
/// \return A reference to the motor 2
152184
BldcMotor &motor2();
153185

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+
/////////////////////////////////////////////////////////////////////////////
157189

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+
/////////////////////////////////////////////////////////////////////////////
161201

162202
/// Get the current sense value for motor 1 phase U
163203
/// \return The current sense value for motor 1 phase U in amps
@@ -388,15 +428,28 @@ class MotorGoMini : public BaseComponent {
388428
.channels = {current_sense_m2_u_},
389429
}};
390430

391-
// button
392-
espp::Button button_{{
393-
.name = "MotorGo Mini Button",
431+
// Interrupts
432+
espp::Interrupt::PinConfig button_interrupt_pin_{
394433
.gpio_num = BUTTON_GPIO,
434+
.callback =
435+
[this](const auto &event) {
436+
if (button_callback_) {
437+
button_callback_(event);
438+
}
439+
},
395440
.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};
400453

401454
// led
402455
std::vector<espp::Led::ChannelConfig> led_channels_{
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "motorgo-mini.hpp"
2+
3+
using namespace espp;
4+
5+
////////////////////////
6+
// Button Functions //
7+
////////////////////////
8+
9+
bool MotorGoMini::initialize_button(const MotorGoMini::button_callback_t &callback) {
10+
logger_.info("Initializing button");
11+
12+
// save the callback
13+
button_callback_ = callback;
14+
15+
// configure the button
16+
if (!button_initialized_) {
17+
interrupts_.add_interrupt(button_interrupt_pin_);
18+
}
19+
button_initialized_ = true;
20+
return true;
21+
}
22+
23+
bool MotorGoMini::button_state() const {
24+
if (!button_initialized_) {
25+
return false;
26+
}
27+
return interrupts_.is_active(button_interrupt_pin_);
28+
}

components/motorgo-mini/src/motorgo-mini.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ MotorGoMini::MotorGoMini()
99

1010
I2c &MotorGoMini::get_external_i2c() { return external_i2c_; }
1111

12-
espp::Button &MotorGoMini::button() { return button_; }
12+
espp::Interrupt &MotorGoMini::interrupts() { return interrupts_; }
1313

1414
espp::Led::ChannelConfig &MotorGoMini::yellow_led() { return led_channels_[0]; }
1515

0 commit comments

Comments
 (0)