From 712b84733d738d7f22c7a44288f30eba5ad79e56 Mon Sep 17 00:00:00 2001 From: Javier Balloffet Date: Sun, 14 Jan 2024 20:39:43 +0100 Subject: [PATCH] Modify Motor class API to allow dependency injection Signed-off-by: Javier Balloffet --- andino_firmware/src/app.cpp | 23 ++++++++++++++------- andino_firmware/src/app.h | 12 ++++++++++- andino_firmware/src/motor.cpp | 23 +++++++++++---------- andino_firmware/src/motor.h | 39 +++++++++++++++++++++-------------- 4 files changed, 62 insertions(+), 35 deletions(-) diff --git a/andino_firmware/src/app.cpp b/andino_firmware/src/app.cpp index aa606aae..80c8aa9e 100644 --- a/andino_firmware/src/app.cpp +++ b/andino_firmware/src/app.cpp @@ -68,6 +68,7 @@ #include "commands.h" #include "constants.h" +#include "digital_out_arduino.h" #include "encoder.h" #include "hw.h" #include "motor.h" @@ -96,10 +97,17 @@ char argv2[16]; namespace andino { -Motor App::left_motor_(Hw::kLeftMotorEnableGpioPin, Hw::kLeftMotorForwardGpioPin, - Hw::kLeftMotorBackwardGpioPin); -Motor App::right_motor_(Hw::kRightMotorEnableGpioPin, Hw::kRightMotorForwardGpioPin, - Hw::kRightMotorBackwardGpioPin); +DigitalOutArduino App::left_motor_enable_digital_out_(Hw::kLeftMotorEnableGpioPin); +PwmOutArduino App::left_motor_forward_pwm_out_(Hw::kLeftMotorForwardGpioPin); +PwmOutArduino App::left_motor_backward_pwm_out_(Hw::kLeftMotorBackwardGpioPin); +Motor App::left_motor_(&left_motor_enable_digital_out_, &left_motor_forward_pwm_out_, + &left_motor_backward_pwm_out_); + +DigitalOutArduino App::right_motor_enable_digital_out_(Hw::kRightMotorEnableGpioPin); +PwmOutArduino App::right_motor_forward_pwm_out_(Hw::kRightMotorForwardGpioPin); +PwmOutArduino App::right_motor_backward_pwm_out_(Hw::kRightMotorBackwardGpioPin); +Motor App::right_motor_(&right_motor_enable_digital_out_, &right_motor_forward_pwm_out_, + &right_motor_backward_pwm_out_); Encoder App::left_encoder_(Hw::kLeftEncoderChannelAGpioPin, Hw::kLeftEncoderChannelBGpioPin); Encoder App::right_encoder_(Hw::kRightEncoderChannelAGpioPin, Hw::kRightEncoderChannelBGpioPin); @@ -118,9 +126,10 @@ void App::setup() { left_encoder_.init(); right_encoder_.init(); - // Enable motors. - left_motor_.set_state(true); - right_motor_.set_state(true); + left_motor_.begin(); + left_motor_.enable(true); + right_motor_.begin(); + right_motor_.enable(true); left_pid_controller_.reset(left_encoder_.read()); right_pid_controller_.reset(right_encoder_.read()); diff --git a/andino_firmware/src/app.h b/andino_firmware/src/app.h index b0745f6e..d03a9ded 100644 --- a/andino_firmware/src/app.h +++ b/andino_firmware/src/app.h @@ -29,9 +29,11 @@ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #pragma once +#include "digital_out_arduino.h" #include "encoder.h" #include "motor.h" #include "pid.h" +#include "pwm_out_arduino.h" namespace andino { @@ -88,8 +90,16 @@ class App { // TODO(jballoffet): Parse arguments within callback method. static void cmd_set_pid_tuning_gains(const char* arg1, const char* arg2); - /// Motors (one per wheel). + /// Left wheel motor. + static DigitalOutArduino left_motor_enable_digital_out_; + static PwmOutArduino left_motor_forward_pwm_out_; + static PwmOutArduino left_motor_backward_pwm_out_; static Motor left_motor_; + + /// Right wheel motor. + static DigitalOutArduino right_motor_enable_digital_out_; + static PwmOutArduino right_motor_forward_pwm_out_; + static PwmOutArduino right_motor_backward_pwm_out_; static Motor right_motor_; /// Encoders (one per wheel). diff --git a/andino_firmware/src/motor.cpp b/andino_firmware/src/motor.cpp index 1720bdab..3fe73c1d 100644 --- a/andino_firmware/src/motor.cpp +++ b/andino_firmware/src/motor.cpp @@ -64,18 +64,19 @@ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "motor.h" -#include +#include "digital_out.h" +#include "pwm_out.h" namespace andino { -void Motor::set_state(bool enabled) { - if (enabled) { - digitalWrite(enable_gpio_pin_, HIGH); - } else { - digitalWrite(enable_gpio_pin_, LOW); - } +void Motor::begin() { + enable_digital_out_->begin(); + forward_pwm_out_->begin(); + backward_pwm_out_->begin(); } +void Motor::enable(bool enabled) { enable_digital_out_->write(enabled ? 1 : 0); } + void Motor::set_speed(int speed) { bool forward = true; @@ -89,11 +90,11 @@ void Motor::set_speed(int speed) { // The motor speed is controlled by sending a PWM wave to the corresponding pin. if (forward) { - analogWrite(forward_gpio_pin_, speed); - analogWrite(backward_gpio_pin_, 0); + forward_pwm_out_->write(speed); + backward_pwm_out_->write(0); } else { - analogWrite(backward_gpio_pin_, speed); - analogWrite(forward_gpio_pin_, 0); + backward_pwm_out_->write(speed); + forward_pwm_out_->write(0); } } diff --git a/andino_firmware/src/motor.h b/andino_firmware/src/motor.h index ae675163..df28f57d 100644 --- a/andino_firmware/src/motor.h +++ b/andino_firmware/src/motor.h @@ -64,6 +64,9 @@ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #pragma once +#include "digital_out.h" +#include "pwm_out.h" + namespace andino { /// @brief This class allows to control a DC motor by enabling it and setting its speed. The @@ -73,18 +76,22 @@ class Motor { public: /// @brief Constructs a new Motor object. /// - /// @param enable_gpio_pin Motor enable GPIO pin. - /// @param forward_gpio_pin Motor forward GPIO pin. - /// @param backward_gpio_pin Motor backward GPIO pin. - Motor(int enable_gpio_pin, int forward_gpio_pin, int backward_gpio_pin) - : enable_gpio_pin_(enable_gpio_pin), - forward_gpio_pin_(forward_gpio_pin), - backward_gpio_pin_(backward_gpio_pin) {} + /// @param enable_digital_out Digital output connected to motor enable pin. + /// @param forward_pwm_out PWM output connected to motor forward pin. + /// @param backward_pwm_out PWM output connected to motor backward pin. + Motor(const DigitalOut* enable_digital_out, const PwmOut* forward_pwm_out, + const PwmOut* backward_pwm_out) + : enable_digital_out_(enable_digital_out), + forward_pwm_out_(forward_pwm_out), + backward_pwm_out_(backward_pwm_out) {} + + /// @brief Initializes the motor. + void begin(); - /// @brief Sets the motor state. + /// @brief Enables the motor. /// - /// @param enabled Motor state. - void set_state(bool enabled); + /// @param enabled True to enable the motor, false otherwise. + void enable(bool enabled); /// @brief Sets the motor speed. /// @@ -98,14 +105,14 @@ class Motor { /// Maximum speed value. static constexpr int kMaxSpeed{255}; - /// Motor enable GPIO pin. - int enable_gpio_pin_; + /// Digital output connected to motor enable pin. + const DigitalOut* enable_digital_out_; - /// Motor forward GPIO pin. - int forward_gpio_pin_; + /// PWM output connected to motor forward pin. + const PwmOut* forward_pwm_out_; - /// Motor backward GPIO pin. - int backward_gpio_pin_; + /// PWM output connected to motor backward pin. + const PwmOut* backward_pwm_out_; }; } // namespace andino