Skip to content

feat(motorgo-mini): Replace use of espp::Button with espp::Interrupt #403

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/motorgo-mini/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
idf_component_register(
INCLUDE_DIRS "include"
SRC_DIRS "src"
REQUIRES base_component button filters led math mt6701 pid task bldc_driver bldc_motor i2c adc
REQUIRES base_component interrupt filters led math mt6701 pid task bldc_driver bldc_motor i2c adc
)
8 changes: 8 additions & 0 deletions components/motorgo-mini/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
menu "MotorGo Mini Configuration"
config MOTORGO_MINI_INTERRUPT_STACK_SIZE
int "Interrupt stack size"
default 4096
help
Size of the stack used for the interrupt handler. Used by the
button callback.
endmenu
42 changes: 20 additions & 22 deletions components/motorgo-mini/example/main/motorgo_mini_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ extern "C" void app_main(void) {
motorgo_mini.init_motor_channel_2();
auto &motor1 = motorgo_mini.motor1();
auto &motor2 = motorgo_mini.motor2();
auto &button = motorgo_mini.button();

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

bool button_state = false;

while (true) {
bool new_button_state = button.is_pressed();
if (new_button_state != button_state) {
button_state = new_button_state;
if (button_state) {
logger.info("Button pressed, changing motion control type");
// switch between ANGLE and VELOCITY
if (motion_control_type == espp::detail::MotionControlType::ANGLE ||
motion_control_type == espp::detail::MotionControlType::ANGLE_OPENLOOP) {
motion_control_type = espp::detail::MotionControlType::VELOCITY;
target_is_angle = false;
} else {
motion_control_type = espp::detail::MotionControlType::ANGLE;
target_is_angle = true;
}
initialize_target();
motor1.set_motion_control_type(motion_control_type);
motor2.set_motion_control_type(motion_control_type);
logger.info("Initializing the button");
auto on_button_pressed = [&](const auto &event) {
if (event.active) {
logger.info("Button pressed, changing motion control type");
// switch between ANGLE and VELOCITY
if (motion_control_type == espp::detail::MotionControlType::ANGLE ||
motion_control_type == espp::detail::MotionControlType::ANGLE_OPENLOOP) {
motion_control_type = espp::detail::MotionControlType::VELOCITY;
target_is_angle = false;
} else {
logger.info("Button released");
motion_control_type = espp::detail::MotionControlType::ANGLE;
target_is_angle = true;
}
initialize_target();
motor1.set_motion_control_type(motion_control_type);
motor2.set_motion_control_type(motion_control_type);
} else {
logger.info("Button released");
}
};
motorgo_mini.initialize_button(on_button_pressed);

while (true) {
std::this_thread::sleep_for(50ms);
}
//! [motorgo-mini example]
Expand Down
101 changes: 77 additions & 24 deletions components/motorgo-mini/include/motorgo-mini.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
#include "base_component.hpp"
#include "bldc_driver.hpp"
#include "bldc_motor.hpp"
#include "button.hpp"
#include "gaussian.hpp"
#include "i2c.hpp"
#include "interrupt.hpp"
#include "led.hpp"
#include "mt6701.hpp"
#include "oneshot_adc.hpp"
Expand Down Expand Up @@ -40,7 +40,13 @@ namespace espp {
/// \snippet motorgo_mini_example.cpp motorgo-mini example
class MotorGoMini : public BaseComponent {
public:
/// Alias for the button callback function
using button_callback_t = espp::Interrupt::event_callback_fn;

/// Alias for the encoder type
using Encoder = espp::Mt6701<espp::Mt6701Interface::SSI>;

/// Alias for the BLDC motor type
using BldcMotor = espp::BldcMotor<espp::BldcDriver, Encoder>;

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

/// Get a reference to the boot button
espp::Button &button();
/// Get a reference to the interrupts
/// \return A reference to the interrupts
espp::Interrupt &interrupts();

/////////////////////////////////////////////////////////////////////////////
// Button
/////////////////////////////////////////////////////////////////////////////

/// Initialize the button
/// \param callback The callback function to call when the button is pressed
/// \return true if the button was successfully initialized, false otherwise
bool initialize_button(const button_callback_t &callback = nullptr);

/// Get the button state
/// \return The button state (true = button pressed, false = button released)
bool button_state() const;

/////////////////////////////////////////////////////////////////////////////
// LEDs
/////////////////////////////////////////////////////////////////////////////

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

/////////////////////////////////////////////////////////////////////////////
// ADCs
/////////////////////////////////////////////////////////////////////////////

/// Get a reference to the ADC_UNIT_1 OneshotAdc object
/// \return A reference to the ADC_UNIT_1 OneshotAdc object
espp::OneshotAdc &adc1();

/// Get a reference to the ADC_UNIT_2 OneshotAdc object
/// \return A reference to the ADC_UNIT_2 OneshotAdc object
espp::OneshotAdc &adc2();

/////////////////////////////////////////////////////////////////////////////
// Motors
/////////////////////////////////////////////////////////////////////////////

/// Initialize the MotorGo-Mini's components for motor channel 1
/// \details This function initializes the encoder and motor for motor channel
/// 1. This consists of initializing encoder1 and motor1.
Expand All @@ -127,14 +167,6 @@ class MotorGoMini : public BaseComponent {
/// 2. This consists of initializing encoder2 and motor2.
void init_motor_channel_2();

/// Get a reference to the encoder 1
/// \return A reference to the encoder 1
Encoder &encoder1();

/// Get a reference to the encoder 2
/// \return A reference to the encoder 2
Encoder &encoder2();

/// Get a reference to the motor 1 driver
/// \return A reference to the motor 1 driver
espp::BldcDriver &motor1_driver();
Expand All @@ -151,13 +183,21 @@ class MotorGoMini : public BaseComponent {
/// \return A reference to the motor 2
BldcMotor &motor2();

/// Get a reference to the ADC_UNIT_1 OneshotAdc object
/// \return A reference to the ADC_UNIT_1 OneshotAdc object
espp::OneshotAdc &adc1();
/////////////////////////////////////////////////////////////////////////////
// Encoders
/////////////////////////////////////////////////////////////////////////////

/// Get a reference to the ADC_UNIT_2 OneshotAdc object
/// \return A reference to the ADC_UNIT_2 OneshotAdc object
espp::OneshotAdc &adc2();
/// Get a reference to the encoder 1
/// \return A reference to the encoder 1
Encoder &encoder1();

/// Get a reference to the encoder 2
/// \return A reference to the encoder 2
Encoder &encoder2();

/////////////////////////////////////////////////////////////////////////////
// Motor Current Sense
/////////////////////////////////////////////////////////////////////////////

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

// button
espp::Button button_{{
.name = "MotorGo Mini Button",
// Interrupts
espp::Interrupt::PinConfig button_interrupt_pin_{
.gpio_num = BUTTON_GPIO,
.callback =
[this](const auto &event) {
if (button_callback_) {
button_callback_(event);
}
},
.active_level = espp::Interrupt::ActiveLevel::LOW,
.pullup_enabled = false,
.pulldown_enabled = false,
.log_level = espp::Logger::Verbosity::WARN,
}};
.interrupt_type = espp::Interrupt::Type::ANY_EDGE,
.pullup_enabled = true};

// we'll only add each interrupt pin if the initialize method is called
espp::Interrupt interrupts_{
{.interrupts = {},
.task_config = {.name = "motorgo mini interrupts",
.stack_size_bytes = CONFIG_MOTORGO_MINI_INTERRUPT_STACK_SIZE}}};

// button
std::atomic<bool> button_initialized_{false};
button_callback_t button_callback_{nullptr};

// led
std::vector<espp::Led::ChannelConfig> led_channels_{
Expand Down
28 changes: 28 additions & 0 deletions components/motorgo-mini/src/button.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "motorgo-mini.hpp"

using namespace espp;

////////////////////////
// Button Functions //
////////////////////////

bool MotorGoMini::initialize_button(const MotorGoMini::button_callback_t &callback) {
logger_.info("Initializing button");

// save the callback
button_callback_ = callback;

// configure the button
if (!button_initialized_) {
interrupts_.add_interrupt(button_interrupt_pin_);
}
button_initialized_ = true;
return true;
}

bool MotorGoMini::button_state() const {
if (!button_initialized_) {
return false;
}
return interrupts_.is_active(button_interrupt_pin_);
}
2 changes: 1 addition & 1 deletion components/motorgo-mini/src/motorgo-mini.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ MotorGoMini::MotorGoMini()

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

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

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

Expand Down