From 6f468eb88021dadfb0d50272d75095369bfdc6e6 Mon Sep 17 00:00:00 2001 From: Leonetienne Date: Sun, 7 Dec 2025 20:56:43 +0100 Subject: [PATCH] implement basic motor class --- MotionCore/MotionCore.vcxproj | 6 +++ MotionCore/MotionCore.vcxproj.filters | 6 +++ MotionCore/Motor.cpp | 67 +++++++++++++++++++++++++++ MotionCore/Motor.h | 36 ++++++++++++++ MotionCore/OSerialBus.cpp | 10 ---- MotionCore/OSerialBus.h | 4 +- MotionCore/main.cpp | 40 +++++++++++++--- 7 files changed, 151 insertions(+), 18 deletions(-) create mode 100755 MotionCore/Motor.cpp create mode 100755 MotionCore/Motor.h diff --git a/MotionCore/MotionCore.vcxproj b/MotionCore/MotionCore.vcxproj index dd3d49f..0e37b7e 100755 --- a/MotionCore/MotionCore.vcxproj +++ b/MotionCore/MotionCore.vcxproj @@ -88,6 +88,7 @@ true WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions);WINDOWS true + stdcpp20 Console @@ -102,6 +103,7 @@ true WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions);WINDOWS true + stdcpp20 Console @@ -116,6 +118,7 @@ true _DEBUG;_CONSOLE;%(PreprocessorDefinitions);WINDOWS true + stdcpp20 Console @@ -130,6 +133,7 @@ true NDEBUG;_CONSOLE;%(PreprocessorDefinitions);WINDOWS true + stdcpp20 Console @@ -141,10 +145,12 @@ + + diff --git a/MotionCore/MotionCore.vcxproj.filters b/MotionCore/MotionCore.vcxproj.filters index ce23d70..7b89b20 100755 --- a/MotionCore/MotionCore.vcxproj.filters +++ b/MotionCore/MotionCore.vcxproj.filters @@ -24,6 +24,9 @@ Quelldateien + + Quelldateien + @@ -32,5 +35,8 @@ Headerdateien + + Headerdateien + \ No newline at end of file diff --git a/MotionCore/Motor.cpp b/MotionCore/Motor.cpp new file mode 100755 index 0000000..7bc5bee --- /dev/null +++ b/MotionCore/Motor.cpp @@ -0,0 +1,67 @@ +#include "Motor.h" +#include + +Motor::Motor( + const std::size_t index, + OSerialBus& serialBus, + const double initialPosition, + const double mechanicalMinDeg, + const double mechanicalMaxDeg, + const double homePosition +) noexcept: + index(index), + serialBus(serialBus), + currentPosition(initialPosition), + mechanicalMinDeg(mechanicalMinDeg), + mechanicalMaxDeg(mechanicalMaxDeg), + homePosition(homePosition) +{ +} + +Motor::Motor(Motor&& other) noexcept: + index(other.index), + serialBus(other.serialBus), + currentPosition(other.currentPosition), + mechanicalMinDeg(other.mechanicalMinDeg), + mechanicalMaxDeg(other.mechanicalMaxDeg), + homePosition(other.homePosition) +{ + +} + +bool Motor::moveTo(const double theta) noexcept +{ + if (theta < mechanicalMinDeg || theta > mechanicalMaxDeg) { + return false; + } + + char buffer[32]; + snprintf(buffer, sizeof(buffer), "MA %d %d", (int)index, (int)theta); + bool result = serialBus.sendl(buffer); + if (result) { + currentPosition = theta; + } + return result; +} + +bool Motor::moveBy(const double theta) noexcept +{ + const double acuteTarget = currentPosition + theta; + + if (acuteTarget < mechanicalMinDeg || acuteTarget > mechanicalMaxDeg) { + return false; + } + + char buffer[32]; + snprintf(buffer, sizeof(buffer), "MA %d %d", (int)index, (int)acuteTarget); + bool result = serialBus.sendl(buffer); + if (result) { + currentPosition = acuteTarget; + } + return result; +} + +bool Motor::moveHome() noexcept +{ + return moveTo(homePosition); +} diff --git a/MotionCore/Motor.h b/MotionCore/Motor.h new file mode 100755 index 0000000..8ce8730 --- /dev/null +++ b/MotionCore/Motor.h @@ -0,0 +1,36 @@ +#pragma once +#include "OSerialBus.h" + +class Motor +{ +public: + Motor( + const std::size_t index, + OSerialBus& serialBus, + const double initialPosition, + const double mechanicalMinDeg, + const double mechanicalMaxDeg, + const double homePosition + ) noexcept; + + Motor(const Motor& other) = delete; + Motor(Motor&& other) noexcept; + + bool moveTo(const double theta) noexcept; + bool moveBy(const double theta) noexcept; + bool moveHome() noexcept; + + std::size_t getIndex() const { return index; }; + double getCurrentPosition() const { return currentPosition; }; + double getMechanicalMinDeg() const { return mechanicalMinDeg; }; + double getMechanicalMaxDeg() const { return mechanicalMaxDeg; }; + double getHomePosition() const { return homePosition; }; + +private: + const std::size_t index; + OSerialBus& serialBus; + double currentPosition = 0; + const double mechanicalMinDeg; + const double mechanicalMaxDeg; + const double homePosition; +}; diff --git a/MotionCore/OSerialBus.cpp b/MotionCore/OSerialBus.cpp index 09ef234..f3d6e58 100755 --- a/MotionCore/OSerialBus.cpp +++ b/MotionCore/OSerialBus.cpp @@ -112,16 +112,6 @@ bool OSerialBus::sendl(const std::string& line) noexcept return send("\n"); } -int OSerialBus::getBaudRate() const noexcept -{ - return baudRate; -} - -const std::wstring& OSerialBus::getComPort() const noexcept -{ - return comPort; -} - OSerialBus& OSerialBus::operator<<(const std::string& in) { if (!send(in)) { diff --git a/MotionCore/OSerialBus.h b/MotionCore/OSerialBus.h index b42610f..90932cc 100755 --- a/MotionCore/OSerialBus.h +++ b/MotionCore/OSerialBus.h @@ -16,8 +16,8 @@ public: bool send(const std::string& line) noexcept; bool sendl(const std::string& line) noexcept; - int getBaudRate() const noexcept; - const std::wstring& getComPort() const noexcept; + int getBaudRate() const noexcept { return baudRate; }; + const std::wstring& getComPort() const noexcept { return comPort; }; // Alias for send() OSerialBus& operator<<(const std::string& in); diff --git a/MotionCore/main.cpp b/MotionCore/main.cpp index c3ec6bd..af6fd8a 100755 --- a/MotionCore/main.cpp +++ b/MotionCore/main.cpp @@ -1,6 +1,9 @@ #include #include "MainLoop.h" #include "OSerialBus.h" +#include "Motor.h" +#include +#include int main() { @@ -10,12 +13,37 @@ int main() return -1; } - if (!bus.send("MA 1 60\n")) { - std::cerr << "Could not send command!" << std::endl; - return -1; - } - - + Motor j0( + 0, + bus, + 125.0, + 30.0, + 180.0, + 125.0 + ); + j0.moveHome(); + + Motor j1( + 1, + bus, + 51.0, + 0.0, + 120.0, + 51.0 + ); + j1.moveHome(); + + Motor j2( + 2, + bus, + 115.0, + 0.0, + 180.0, + 115.0 + ); + j2.moveHome(); + + bus.close(); MainLoop mainloop;