From 9863a4e0a13f4e3739652105ac7c728857f1cc4f Mon Sep 17 00:00:00 2001 From: Emilio Corigliano <emilio.corigliano@skywarder.eu> Date: Sat, 1 Jul 2023 08:22:41 +0000 Subject: [PATCH] [Stepper] Updated Stepper with a state getter --- cmake/boardcore.cmake | 4 +- .../actuators/{ => stepper}/Stepper.cpp | 17 +++++- src/shared/actuators/{ => stepper}/Stepper.h | 14 +++++ src/shared/actuators/stepper/StepperData.h | 54 +++++++++++++++++++ .../actuators/{ => stepper}/StepperPWM.cpp | 2 +- .../actuators/{ => stepper}/StepperPWM.h | 0 src/shared/logger/LogTypes.h | 2 + src/tests/actuators/test-stepper-pwm.cpp | 2 +- src/tests/actuators/test-stepper.cpp | 2 +- 9 files changed, 91 insertions(+), 6 deletions(-) rename src/shared/actuators/{ => stepper}/Stepper.cpp (91%) rename src/shared/actuators/{ => stepper}/Stepper.h (94%) create mode 100644 src/shared/actuators/stepper/StepperData.h rename src/shared/actuators/{ => stepper}/StepperPWM.cpp (98%) rename src/shared/actuators/{ => stepper}/StepperPWM.h (100%) diff --git a/cmake/boardcore.cmake b/cmake/boardcore.cmake index 85fa903a8..6fd4ba2d0 100644 --- a/cmake/boardcore.cmake +++ b/cmake/boardcore.cmake @@ -30,8 +30,8 @@ foreach(OPT_BOARD ${BOARDS}) # Actuators ${SBS_BASE}/src/shared/actuators/HBridge/HBridge.cpp ${SBS_BASE}/src/shared/actuators/Servo/Servo.cpp - ${SBS_BASE}/src/shared/actuators/Stepper.cpp - ${SBS_BASE}/src/shared/actuators/StepperPWM.cpp + ${SBS_BASE}/src/shared/actuators/stepper/Stepper.cpp + ${SBS_BASE}/src/shared/actuators/stepper/StepperPWM.cpp # Algorithms ${SBS_BASE}/src/shared/algorithms/ADA/ADA.cpp diff --git a/src/shared/actuators/Stepper.cpp b/src/shared/actuators/stepper/Stepper.cpp similarity index 91% rename from src/shared/actuators/Stepper.cpp rename to src/shared/actuators/stepper/Stepper.cpp index 00a2166cd..d76f6dfeb 100644 --- a/src/shared/actuators/Stepper.cpp +++ b/src/shared/actuators/stepper/Stepper.cpp @@ -52,6 +52,7 @@ void Stepper::enable() { enablePin.high(); } + enabled = true; } void Stepper::disable() @@ -64,6 +65,7 @@ void Stepper::disable() { enablePin.low(); } + enabled = false; } void Stepper::setDirection() @@ -111,7 +113,7 @@ void Stepper::setDirection() void Stepper::move(int16_t steps) { - if (speed == 0) + if (!enabled || speed == 0 || steps == 0) return; unsigned int halfStepDelay = 1e6 / (speed * 360 / stepAngle * microStep); @@ -160,4 +162,17 @@ void Stepper::move(int16_t steps) currentPositionDeg += steps * stepAngle / microStep; } +bool Stepper::isEnabled() { return enabled; } + +StepperData Stepper::getState(float moveDeg) +{ + return {TimestampTimer::getTimestamp(), + static_cast<unsigned int>(stepPin.getPort()), + stepPin.getNumber(), + enabled, + getCurrentDegPosition(), + speed, + moveDeg}; +} + } // namespace Boardcore \ No newline at end of file diff --git a/src/shared/actuators/Stepper.h b/src/shared/actuators/stepper/Stepper.h similarity index 94% rename from src/shared/actuators/Stepper.h rename to src/shared/actuators/stepper/Stepper.h index d93a5c7dc..6e9d25dca 100644 --- a/src/shared/actuators/Stepper.h +++ b/src/shared/actuators/stepper/Stepper.h @@ -22,10 +22,13 @@ #pragma once +#include <drivers/timer/TimestampTimer.h> #include <interfaces-impl/gpio_impl.h> #include <interfaces/delays.h> #include <utils/TestUtils/MockGpioPin.h> +#include "StepperData.h" + namespace Boardcore { @@ -115,6 +118,16 @@ public: */ virtual float getCurrentDegPosition(); + /** + * @brief Returns whether the stepper is enabled or not. + */ + bool isEnabled(); + + /** + * @brief Returns the current position and the current timestamp. + */ + StepperData getState(float moveDeg); + protected: /** * @brief Sets the directionPin to the right value to go in the direction @@ -138,6 +151,7 @@ protected: miosix::GpioPin directionPin; float speed; // [rev/s] float stepAngle; // [deg/step] + bool enabled = false; bool revertDirection; uint16_t microStep; PinConfiguration pinConfig; diff --git a/src/shared/actuators/stepper/StepperData.h b/src/shared/actuators/stepper/StepperData.h new file mode 100644 index 000000000..096f48f2c --- /dev/null +++ b/src/shared/actuators/stepper/StepperData.h @@ -0,0 +1,54 @@ +/* Copyright (c) 2022 Skyward Experimental Rocketry + * Author: Emilio Corigliano + * + * 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 + +#include <ostream> + +namespace Boardcore +{ + +struct StepperData +{ + uint64_t timestamp; + unsigned int pulsePinPort; + unsigned int pulsePinNumber; + bool enabled; + float positionDeg; + float speed; + float moveDeg; + + static std::string header() + { + return "timestamp,pulsePinPort,pulsePinNumber,enabled,positionDeg," + "speed,moveDeg\n"; + } + + void print(std::ostream& os) const + { + os << timestamp << "," << pulsePinPort << "," << pulsePinNumber << "," + << enabled << "," << positionDeg << "," << speed << "," << moveDeg + << "\n"; + } +}; + +} // namespace Boardcore diff --git a/src/shared/actuators/StepperPWM.cpp b/src/shared/actuators/stepper/StepperPWM.cpp similarity index 98% rename from src/shared/actuators/StepperPWM.cpp rename to src/shared/actuators/stepper/StepperPWM.cpp index e9eccf5ff..4f04532df 100644 --- a/src/shared/actuators/StepperPWM.cpp +++ b/src/shared/actuators/stepper/StepperPWM.cpp @@ -62,7 +62,7 @@ void StepperPWM::setMicroStepping(uint16_t microStep) void StepperPWM::move(int16_t steps) { - if (speed == 0 || steps == 0) + if (!enabled || speed == 0 || steps == 0) return; // First update currentPositionDeg. This method corrects the initial diff --git a/src/shared/actuators/StepperPWM.h b/src/shared/actuators/stepper/StepperPWM.h similarity index 100% rename from src/shared/actuators/StepperPWM.h rename to src/shared/actuators/stepper/StepperPWM.h diff --git a/src/shared/logger/LogTypes.h b/src/shared/logger/LogTypes.h index be18505e7..4d36569c9 100644 --- a/src/shared/logger/LogTypes.h +++ b/src/shared/logger/LogTypes.h @@ -23,6 +23,7 @@ #pragma once #include <actuators/Servo/ServoData.h> +#include <actuators/stepper/StepperData.h> #include <algorithms/ADA/ADAData.h> #include <algorithms/NAS/NASState.h> #include <diagnostic/CpuMeter/CpuMeter.h> @@ -80,6 +81,7 @@ namespace LogTypes void registerTypes(Deserializer& ds) { + ds.registerType<StepperData>(); ds.registerType<ServoData>(); ds.registerType<ADAState>(); ds.registerType<NASState>(); diff --git a/src/tests/actuators/test-stepper-pwm.cpp b/src/tests/actuators/test-stepper-pwm.cpp index f1a2101a4..a9e23e585 100644 --- a/src/tests/actuators/test-stepper-pwm.cpp +++ b/src/tests/actuators/test-stepper-pwm.cpp @@ -20,7 +20,7 @@ * THE SOFTWARE. */ -#include <actuators/StepperPWM.h> +#include <actuators/stepper/StepperPWM.h> #include <drivers/timer/CountedPWM.h> #include <miosix.h> diff --git a/src/tests/actuators/test-stepper.cpp b/src/tests/actuators/test-stepper.cpp index 6ee595f8c..91caa0bb3 100644 --- a/src/tests/actuators/test-stepper.cpp +++ b/src/tests/actuators/test-stepper.cpp @@ -20,7 +20,7 @@ * THE SOFTWARE. */ -#include <actuators/Stepper.h> +#include <actuators/stepper/Stepper.h> using namespace miosix; using namespace Boardcore; -- GitLab