From ebd3ef4909c1104921fb9fa25ed64f7686d4f71a Mon Sep 17 00:00:00 2001 From: Franco Cipollone <53065142+francocipollone@users.noreply.github.com> Date: Fri, 2 Feb 2024 09:09:25 -0300 Subject: [PATCH 1/2] Adds reference to andino_gz repo. (#216) Signed-off-by: Franco Cipollone --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ddff076d..8f20609b 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ _Note: For videos go to [Media](#selfie-media) section._ - :hammer_and_pick: [`andino_firmware`](./andino_firmware): Contains the code be run in the microcontroller for interfacing low level hardware with the SBC. - :gear: [`andino_base`](./andino_base): [ROS Control hardware interface](https://control.ros.org/master/doc/ros2_control/hardware_interface/doc/writing_new_hardware_interface.html) is implemented. - :control_knobs: [`andino_control`](./andino_control/): It launches the [`controller_manager`](https://control.ros.org/humble/doc/ros2_control/controller_manager/doc/userdoc.html) along with the [ros2 controllers](https://control.ros.org/master/doc/ros2_controllers/doc/controllers_index.html): [diff_drive_controller](https://control.ros.org/master/doc/ros2_controllers/diff_drive_controller/doc/userdoc.html) and the [joint_state_broadcaster](https://control.ros.org/master/doc/ros2_controllers/joint_state_broadcaster/doc/userdoc.html). -- :computer: [`andino_gz_classic`](./andino_gz_classic/): Gazebo simulation of the `andino` robot. +- :computer: [`andino_gz_classic`](./andino_gz_classic/): [Gazebo Classic](https://classic.gazebosim.org/) simulation of the `andino` robot. - :world_map: [`andino_slam`](./andino_slam/): Provides support for SLAM with your `andino` robot. - :compass: [`andino_navigation`](./andino_navigation/): Navigation stack based on `nav2`. @@ -36,8 +36,9 @@ _Note: For videos go to [Media](#selfie-media) section._ Other projects built upon Andino! :rocket: -- :test_tube: [`andino_integration_tests`](https://github.com/Ekumen-OS/andino_integration_tests): Extension to the Andino robot showing how to build integration tests. +- :computer: [`andino_gz`](https://github.com/Ekumen-OS/andino_gz): [Gazebo](https://gazebosim.org/home)(non-classic) simulation of the `andino` robot. - :lady_beetle: [`andino_webots`](https://github.com/Ekumen-OS/andino_webots): [Webots](https://github.com/cyberbotics/webots) simulation of the Andino robot fully integrated with ROS 2. +- :test_tube: [`andino_integration_tests`](https://github.com/Ekumen-OS/andino_integration_tests): Extension to the Andino robot showing how to build integration tests. ## :pick: Robot Assembly From 4064e50a4c289fe42d21f2a48e644abc39e0d36d Mon Sep 17 00:00:00 2001 From: Javier Balloffet Date: Fri, 2 Feb 2024 10:35:48 -0300 Subject: [PATCH 2/2] Modify Motor class API to allow dependency injection (#210) Signed-off-by: Javier Balloffet --- andino_firmware/src/app.cpp | 24 ++++++++++++++------- andino_firmware/src/app.h | 12 ++++++++++- andino_firmware/src/motor.cpp | 23 +++++++++++---------- andino_firmware/src/motor.h | 39 +++++++++++++++++++++-------------- 4 files changed, 63 insertions(+), 35 deletions(-) diff --git a/andino_firmware/src/app.cpp b/andino_firmware/src/app.cpp index 139f9b59..c23d8ebb 100644 --- a/andino_firmware/src/app.cpp +++ b/andino_firmware/src/app.cpp @@ -68,20 +68,29 @@ #include "commands.h" #include "constants.h" +#include "digital_out_arduino.h" #include "encoder.h" #include "hw.h" #include "motor.h" #include "pid.h" +#include "pwm_out_arduino.h" #include "shell.h" namespace andino { Shell App::shell_; -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); @@ -104,9 +113,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 03064719..dbe07795 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" #include "shell.h" namespace andino { @@ -82,8 +84,16 @@ class App { /// Application command shell. static Shell shell_; - /// 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