implement basic motor class
This commit is contained in:
@@ -88,6 +88,7 @@
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions);WINDOWS</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<LanguageStandard>stdcpp20</LanguageStandard>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
@@ -102,6 +103,7 @@
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions);WINDOWS</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<LanguageStandard>stdcpp20</LanguageStandard>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
@@ -116,6 +118,7 @@
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions);WINDOWS</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<LanguageStandard>stdcpp20</LanguageStandard>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
@@ -130,6 +133,7 @@
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions);WINDOWS</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<LanguageStandard>stdcpp20</LanguageStandard>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
@@ -141,10 +145,12 @@
|
||||
<ItemGroup>
|
||||
<ClCompile Include="main.cpp" />
|
||||
<ClCompile Include="MainLoop.cpp" />
|
||||
<ClCompile Include="Motor.cpp" />
|
||||
<ClCompile Include="OSerialBus.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="MainLoop.h" />
|
||||
<ClInclude Include="Motor.h" />
|
||||
<ClInclude Include="OSerialBus.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
|
||||
@@ -24,6 +24,9 @@
|
||||
<ClCompile Include="OSerialBus.cpp">
|
||||
<Filter>Quelldateien</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Motor.cpp">
|
||||
<Filter>Quelldateien</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="MainLoop.h">
|
||||
@@ -32,5 +35,8 @@
|
||||
<ClInclude Include="OSerialBus.h">
|
||||
<Filter>Headerdateien</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Motor.h">
|
||||
<Filter>Headerdateien</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
Executable
+67
@@ -0,0 +1,67 @@
|
||||
#include "Motor.h"
|
||||
#include <algorithm>
|
||||
|
||||
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);
|
||||
}
|
||||
Executable
+36
@@ -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;
|
||||
};
|
||||
@@ -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)) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
+32
-4
@@ -1,6 +1,9 @@
|
||||
#include <iostream>
|
||||
#include "MainLoop.h"
|
||||
#include "OSerialBus.h"
|
||||
#include "Motor.h"
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
|
||||
int main()
|
||||
{
|
||||
@@ -10,10 +13,35 @@ 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();
|
||||
|
||||
Reference in New Issue
Block a user