diff --git a/cmake/boardcore.cmake b/cmake/boardcore.cmake index 85fa903a83ead157ba2b7468c5d13f60351f6912..6fd4ba2d037ed3cb1a0d003d8db8c8602d4d032f 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 00a2166cdaaa011026386b92723ef382614ac00b..d76f6dfebf538a5e39b7378b5ac5a603d01dbf1c 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 d93a5c7dcfc3da67c3b1eeb3347c5f949c3dbc23..6e9d25dca661c1868de063541d2dd39dec877d3c 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 0000000000000000000000000000000000000000..096f48f2c6ee567259e56d2a57a91ec4dc10a5a5 --- /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 e9eccf5ffa73aac7ba0c8b8ef5ca13668dfa7ff5..4f04532dff19e839c75a1a75d19a342219ce70db 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 be18505e7ed1e127478f509cb8d0fd752a2cb129..4d36569c99e8b0ec9beb8972ee11ed3db4c75379 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 f1a2101a4bb496758a57eacf6cc9f21745d4049e..a9e23e58539d34f2e00787e5a89aad15e200de85 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 6ee595f8ce2737dae6f0b4950bf44dc836d8c35a..91caa0bb36080142c03402e58f1399e1beca8907 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;