Skip to content

Commit

Permalink
feat(motorgo): Add APIs for more customized use (single motor, delaye…
Browse files Browse the repository at this point in the history
…d motor init, etc.) (#241)
  • Loading branch information
finger563 authored May 17, 2024
1 parent ceb3090 commit a27a668
Showing 1 changed file with 62 additions and 5 deletions.
67 changes: 62 additions & 5 deletions components/motorgo-mini/include/motorgo-mini.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,34 @@ class MotorGoMini : public BaseComponent {
using Encoder = espp::Mt6701<espp::Mt6701Interface::SSI>;
using BldcMotor = espp::BldcMotor<espp::BldcDriver, Encoder>;

/// Configuration for the MotorGo-Mini
struct Config {
bool auto_init = true; ///< If true, the MotorGo-Mini will initialize itself in the constructor
espp::Logger::Verbosity verbosity =
espp::Logger::Verbosity::WARN; ///< The verbosity level for
///< the logger of the MotorGo-Mini
///< and its components
};

/// Constructor
/// \param verbosity The verbosity level for the logger of the MotorGo-Mini
/// and its components
explicit MotorGoMini(espp::Logger::Verbosity verbosity = espp::Logger::Verbosity::WARN)
: BaseComponent("MotorGo-Mini", verbosity) {
always_init();
init();
}

/// Constructor
/// \param config The configuration for the MotorGo-Mini
explicit MotorGoMini(const Config &config)
: BaseComponent("MotorGo-Mini", config.verbosity) {
always_init();
if (config.auto_init) {
init();
}
}

/// Get a reference to the external I2C bus
/// \return A reference to the external I2C bus
I2c &get_external_i2c() { return external_i2c_; }
Expand Down Expand Up @@ -116,6 +136,34 @@ class MotorGoMini : public BaseComponent {
led_.set_duty(led_channels_[1].channel, 0.0f);
}

/// 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.
void init_motor_channel_1() {
bool run_task = true;
std::error_code ec;
encoder1_.initialize(run_task, ec);
if (ec) {
logger_.error("Could not initialize encoder1: {}", ec.message());
return;
}
motor1_.initialize();
}

/// Initialize the MotorGo-Mini's components for motor channel 2
/// \details This function initializes the encoder and motor for motor channel
/// 2. This consists of initializing encoder2 and motor2.
void init_motor_channel_2() {
bool run_task = true;
std::error_code ec;
encoder2_.initialize(run_task, ec);
if (ec) {
logger_.error("Could not initialize encoder2: {}", ec.message());
return;
}
motor2_.initialize();
}

/// Get a reference to the encoder 1
/// \return A reference to the encoder 1
Encoder &encoder1() { return encoder1_; }
Expand Down Expand Up @@ -206,9 +254,12 @@ class MotorGoMini : public BaseComponent {
// TODO: figure this out and update it :)
static constexpr float CURRENT_SENSE_MV_TO_A = 1.0f;

void init() {
void always_init() {
start_breathing();
init_spi();
}

void init() {
init_encoders();
init_motors();
}
Expand Down Expand Up @@ -388,8 +439,11 @@ class MotorGoMini : public BaseComponent {
.kv_rating = 320,
.current_limit = 1.0f,
.foc_type = espp::detail::FocType::SPACE_VECTOR_PWM,
.driver = std::shared_ptr<espp::BldcDriver>(&motor1_driver_),
.sensor = std::shared_ptr<Encoder>(&encoder1_),
// create shared_ptr from raw pointer to ensure shared_ptr doesn't delete the object
.driver =
std::shared_ptr<espp::BldcDriver>(std::shared_ptr<espp::BldcDriver>{}, &motor1_driver_),
// create shared_ptr from raw pointer to ensure shared_ptr doesn't delete the object
.sensor = std::shared_ptr<Encoder>(std::shared_ptr<Encoder>{}, &encoder1_),
.velocity_pid_config =
{
.kp = 0.010f,
Expand Down Expand Up @@ -421,8 +475,11 @@ class MotorGoMini : public BaseComponent {
.kv_rating = 320,
.current_limit = 1.0f,
.foc_type = espp::detail::FocType::SPACE_VECTOR_PWM,
.driver = std::shared_ptr<espp::BldcDriver>(&motor2_driver_),
.sensor = std::shared_ptr<Encoder>(&encoder2_),
// create shared_ptr from raw pointer to ensure shared_ptr doesn't delete the object
.driver =
std::shared_ptr<espp::BldcDriver>(std::shared_ptr<espp::BldcDriver>{}, &motor2_driver_),
// create shared_ptr from raw pointer to ensure shared_ptr doesn't delete the object
.sensor = std::shared_ptr<Encoder>(std::shared_ptr<Encoder>{}, &encoder2_),
.velocity_pid_config =
{
.kp = 0.010f,
Expand Down

0 comments on commit a27a668

Please sign in to comment.