From 26530f9dee82c791510f4a6f2313deeccc0bbf9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niccol=C3=B2=20Betto?= <niccolo.betto@skywarder.eu> Date: Thu, 6 Mar 2025 13:28:08 +0100 Subject: [PATCH] [RIGv2] Improve documentation of actuators initialization macros --- src/RIGv2/Actuators/Actuators.cpp | 74 +-------------- src/RIGv2/Actuators/ActuatorsMacros.h | 128 ++++++++++++++++++++++++++ 2 files changed, 130 insertions(+), 72 deletions(-) create mode 100644 src/RIGv2/Actuators/ActuatorsMacros.h diff --git a/src/RIGv2/Actuators/Actuators.cpp b/src/RIGv2/Actuators/Actuators.cpp index 0b6de233d..412562582 100644 --- a/src/RIGv2/Actuators/Actuators.cpp +++ b/src/RIGv2/Actuators/Actuators.cpp @@ -29,6 +29,8 @@ #include <events/EventBroker.h> #include <interfaces-impl/hwmapping.h> +#include "ActuatorsMacros.h" + using namespace Boardcore; using namespace miosix; using namespace Common; @@ -113,78 +115,6 @@ bool Actuators::ServoInfo::setOpeningTime(uint32_t time) return true; } -/** - * @brief Shorthand to create a ServoInfo struct from the servo name - */ -#define MAKE_SERVO(name) \ - ServoInfo \ - { \ - std::make_unique<Servo>( \ - MIOSIX_SERVOS_##name##_TIM, \ - TimerUtils::Channel::MIOSIX_SERVOS_##name##_CHANNEL, \ - Config::Servos::MIN_PULSE, Config::Servos::MAX_PULSE, \ - Config::Servos::FREQUENCY), \ - ServoInfo::ServoConfig \ - { \ - .limit = Config::Servos::name##_LIMIT, \ - .flipped = Config::Servos::name##_FLIPPED, \ - .defaultOpeningTime = \ - Config::Servos::DEFAULT_##name##_OPENING_TIME, \ - .defaultMaxAperture = \ - Config::Servos::DEFAULT_##name##_MAX_APERTURE, \ - .openingEvent = MOTOR_##name##_OPEN, \ - .closingEvent = MOTOR_##name##_CLOSE, \ - .openingTimeRegKey = CONFIG_ID_##name##_OPENING_TIME, \ - .maxApertureRegKey = CONFIG_ID_##name##_MAX_APERTURE \ - } \ - } - -/** - * @brief Shorthand to create a detach ServoInfo struct from the servo name - */ -#define MAKE_DETACH_SERVO(name) \ - ServoInfo \ - { \ - std::make_unique<Servo>( \ - MIOSIX_SERVOS_##name##_TIM, \ - TimerUtils::Channel::MIOSIX_SERVOS_##name##_CHANNEL, \ - Config::Servos::DETACH_MIN_PULSE, \ - Config::Servos::DETACH_MAX_PULSE, Config::Servos::FREQUENCY), \ - ServoInfo::ServoConfig \ - { \ - .limit = Config::Servos::name##_LIMIT, \ - .flipped = Config::Servos::name##_FLIPPED, \ - .defaultOpeningTime = \ - Config::Servos::DEFAULT_##name##_OPENING_TIME, \ - .defaultMaxAperture = \ - Config::Servos::DEFAULT_##name##_MAX_APERTURE, \ - .openingEvent = MOTOR_##name##_OPEN, \ - .closingEvent = MOTOR_##name##_CLOSE, \ - .openingTimeRegKey = CONFIG_ID_##name##_OPENING_TIME, \ - .maxApertureRegKey = CONFIG_ID_##name##_MAX_APERTURE \ - } \ - } - -/** - * @brief Shorthand to create a non-atomic ServoInfo struct from the servo name - */ -#define MAKE_SIMPLE_SERVO(name) \ - ServoInfo \ - { \ - std::make_unique<Servo>( \ - MIOSIX_SERVOS_##name##_TIM, \ - TimerUtils::Channel::MIOSIX_SERVOS_##name##_CHANNEL, \ - Config::Servos::MIN_PULSE, Config::Servos::MAX_PULSE, \ - Config::Servos::FREQUENCY), \ - ServoInfo::ServoConfig \ - { \ - .limit = Config::Servos::name##_LIMIT, \ - .flipped = Config::Servos::name##_FLIPPED, \ - .openingEvent = MOTOR_##name##_OPEN, \ - .closingEvent = MOTOR_##name##_CLOSE, \ - } \ - } - Actuators::Actuators() : infos{ MAKE_SERVO(OX_FIL), MAKE_SERVO(OX_REL), diff --git a/src/RIGv2/Actuators/ActuatorsMacros.h b/src/RIGv2/Actuators/ActuatorsMacros.h new file mode 100644 index 000000000..3fa941e94 --- /dev/null +++ b/src/RIGv2/Actuators/ActuatorsMacros.h @@ -0,0 +1,128 @@ +/* Copyright (c) 2025 Skyward Experimental Rocketry + * Author: Niccolò Betto + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#pragma once + +/** + * @file ActuatorsMacros.h + * @brief Macros for the Actuators module + * + * This file contains macros to simplify the creation of ServoInfo structs + * for the Actuators module. + * + * The hardware mapping (hwmapping.h), the actuators configuration + * (ActuatorsConfig.h) and the registry keys (Registry.h) have been created with + * a specific naming convention to allow the use of these macros, to reduce the + * amount of code repetition and to make the code more readable during servo + * creation. + * + * The following naming convention is used: + * - The servo name is an uppercase string code (e.g. OX_FIL) + * - Constants are expected to be in the Config::Servos namespace + * - The servo name is used to create the following constants: + * - MIOSIX_SERVOS_{servo}_TIM (hwmapping) + * - MIOSIX_SERVOS_{servo}_CHANNEL (hwmapping) + * - {servo}_LIMIT (ActuatorsConfig) + * - {servo}_FLIPPED (ActuatorsConfig) + * - Common::MOTOR_{servo}_OPEN (Events) + * - Common::MOTOR_{servo}_CLOSE (Events) + * - DEFAULT_{servo}_OPENING_TIME (ActuatorsConfig) + * - DEFAULT_{servo}_MAX_APERTURE (ActuatorsConfig) + * - CONFIG_ID_{servo}_OPENING_TIME (Registry) + * - CONFIG_ID_{servo}_MAX_APERTURE (Registry) + * - The following constants are used for all servos: + * - MIN_PULSE + * - MAX_PULSE + * - FREQUENCY + */ + +/** + * @brief Shorthand to create a ServoInfo struct from the servo name + */ +#define MAKE_SERVO(name) \ + ServoInfo \ + { \ + std::make_unique<Servo>( \ + MIOSIX_SERVOS_##name##_TIM, \ + TimerUtils::Channel::MIOSIX_SERVOS_##name##_CHANNEL, \ + Config::Servos::MIN_PULSE, Config::Servos::MAX_PULSE, \ + Config::Servos::FREQUENCY), \ + ServoInfo::ServoConfig \ + { \ + .limit = Config::Servos::name##_LIMIT, \ + .flipped = Config::Servos::name##_FLIPPED, \ + .defaultOpeningTime = \ + Config::Servos::DEFAULT_##name##_OPENING_TIME, \ + .defaultMaxAperture = \ + Config::Servos::DEFAULT_##name##_MAX_APERTURE, \ + .openingEvent = Common::MOTOR_##name##_OPEN, \ + .closingEvent = Common::MOTOR_##name##_CLOSE, \ + .openingTimeRegKey = CONFIG_ID_##name##_OPENING_TIME, \ + .maxApertureRegKey = CONFIG_ID_##name##_MAX_APERTURE \ + } \ + } + +/** + * @brief Shorthand to create a detach ServoInfo struct from the servo name + */ +#define MAKE_DETACH_SERVO(name) \ + ServoInfo \ + { \ + std::make_unique<Servo>( \ + MIOSIX_SERVOS_##name##_TIM, \ + TimerUtils::Channel::MIOSIX_SERVOS_##name##_CHANNEL, \ + Config::Servos::DETACH_MIN_PULSE, \ + Config::Servos::DETACH_MAX_PULSE, Config::Servos::FREQUENCY), \ + ServoInfo::ServoConfig \ + { \ + .limit = Config::Servos::name##_LIMIT, \ + .flipped = Config::Servos::name##_FLIPPED, \ + .defaultOpeningTime = \ + Config::Servos::DEFAULT_##name##_OPENING_TIME, \ + .defaultMaxAperture = \ + Config::Servos::DEFAULT_##name##_MAX_APERTURE, \ + .openingEvent = Common::MOTOR_##name##_OPEN, \ + .closingEvent = Common::MOTOR_##name##_CLOSE, \ + .openingTimeRegKey = CONFIG_ID_##name##_OPENING_TIME, \ + .maxApertureRegKey = CONFIG_ID_##name##_MAX_APERTURE \ + } \ + } + +/** + * @brief Shorthand to create a non-atomic ServoInfo struct from the servo name + */ +#define MAKE_SIMPLE_SERVO(name) \ + ServoInfo \ + { \ + std::make_unique<Servo>( \ + MIOSIX_SERVOS_##name##_TIM, \ + TimerUtils::Channel::MIOSIX_SERVOS_##name##_CHANNEL, \ + Config::Servos::MIN_PULSE, Config::Servos::MAX_PULSE, \ + Config::Servos::FREQUENCY), \ + ServoInfo::ServoConfig \ + { \ + .limit = Config::Servos::name##_LIMIT, \ + .flipped = Config::Servos::name##_FLIPPED, \ + .openingEvent = Common::MOTOR_##name##_OPEN, \ + .closingEvent = Common::MOTOR_##name##_CLOSE, \ + } \ + } -- GitLab