Skip to content

Commit

Permalink
Modify Motor class API to allow dependency injection
Browse files Browse the repository at this point in the history
Signed-off-by: Javier Balloffet <[email protected]>
  • Loading branch information
jballoffet committed Jan 17, 2024
1 parent 5088a58 commit 712b847
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 35 deletions.
23 changes: 16 additions & 7 deletions andino_firmware/src/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@

#include "commands.h"
#include "constants.h"
#include "digital_out_arduino.h"
#include "encoder.h"
#include "hw.h"
#include "motor.h"
Expand Down Expand Up @@ -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);
Expand All @@ -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());
Expand Down
12 changes: 11 additions & 1 deletion andino_firmware/src/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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).
Expand Down
23 changes: 12 additions & 11 deletions andino_firmware/src/motor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,19 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "motor.h"

#include <Arduino.h>
#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;

Expand All @@ -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);
}
}

Expand Down
39 changes: 23 additions & 16 deletions andino_firmware/src/motor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
///
Expand All @@ -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

0 comments on commit 712b847

Please sign in to comment.