diff --git a/CMakeLists.txt b/CMakeLists.txt index 7ac4f1c..2bf6d25 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,15 @@ project(socketServer) set(CMAKE_CXX_STANDARD 17) +find_package(Modelec COMPONENTS + Utils + REQUIRED +) + add_executable(socketServer main.cpp TCPServer.cpp - utils.cpp ) + +target_link_libraries(socketServer + Modelec::Utils +) \ No newline at end of file diff --git a/TCPServer.cpp b/TCPServer.cpp index f4bdcd3..5e5e810 100644 --- a/TCPServer.cpp +++ b/TCPServer.cpp @@ -18,7 +18,7 @@ void ClientHandler::handle() { break; } - std::vector messages = TCPUtils::split(buffer, "\n"); + std::vector messages = Modelec::split(buffer, "\n"); for (const std::string& message : messages) { processMessage(message); } @@ -92,7 +92,7 @@ void TCPServer::acceptConnections() std::cerr << "Accepting connection failed" << std::endl; continue; } - std::cout << "Connection accepted" << std::endl; + std::cout << "Connection accepted." << clientSocket << std::endl; // Add the client socket to the list @@ -112,15 +112,15 @@ void TCPServer::handleMessage(const std::string& message, int clientSocket) { // std::cout << message << std::endl; - std::vector tokens = TCPUtils::split(message, ";"); + std::vector tokens = Modelec::split(message, ";"); if (tokens.size() != 4) { std::cerr << "Invalid message format, token size : " << std::to_string(tokens.size()) << " from message : " << message << std::endl; return; } - if (TCPUtils::contains(tokens[2], "stop proximity")) { - std::vector args = TCPUtils::split(tokens[3], ","); + if (Modelec::contains(tokens[2], "stop proximity")) { + std::vector args = Modelec::split(tokens[3], ","); lidarDecetionDistance = stoi(args[0]); // TODO distance de detection proportionnelle a la vitesse @@ -165,14 +165,14 @@ void TCPServer::handleMessage(const std::string& message, int clientSocket) } else if (tokens[0] == "gc") { if (tokens[2] == "axis") { - std::vector args = TCPUtils::split(tokens[3], ","); + std::vector args = Modelec::split(tokens[3], ","); double value = std::stod(args[1]); if (value > -1000 && value < 1000) { value = 0; } if (args[0] == "0") { if (!handleEmergecnyFlag) { - int angle = static_cast(TCPUtils::mapValue(value, -32767.0, 32768.0, - PI / 2, PI / 2)); + int angle = static_cast(Modelec::mapValue(value, -32767.0, 32768.0, - PI / 2, PI / 2)); this->broadcastMessage("strat;arduino;angle;" + std::to_string(angle) + "\n"); } } @@ -182,11 +182,11 @@ void TCPServer::handleMessage(const std::string& message, int clientSocket) value = -value; if (value < 0) { - speed = static_cast(TCPUtils::mapValue(value, -32767.0, 0.0, -310.0, -70.0)); + speed = static_cast(Modelec::mapValue(value, -32767.0, 0.0, -310.0, -70.0)); } else if (value == 0) { speed = 0; } else { - speed = static_cast(TCPUtils::mapValue(value, 0.0, 32768.0, 70.0, 310.0)); + speed = static_cast(Modelec::mapValue(value, 0.0, 32768.0, 70.0, 310.0)); } if (!handleEmergecnyFlag) { @@ -202,7 +202,7 @@ void TCPServer::handleMessage(const std::string& message, int clientSocket) } } else if (args[0] == "2") { - int speed = static_cast(TCPUtils::mapValue(value, -32767.0, 32768.0, -310.0, 310.0)); + int speed = static_cast(Modelec::mapValue(value, -32767.0, 32768.0, -310.0, 310.0)); this->broadcastMessage("start;arduino;rotate;" + std::to_string(speed)); } } @@ -236,7 +236,7 @@ void TCPServer::handleMessage(const std::string& message, int clientSocket) } else if (tokens[2] == "trigger") { - std::vector args = TCPUtils::split(tokens[3], ","); + std::vector args = Modelec::split(tokens[3], ","); int nb = std::stoi(args[0]); diff --git a/TCPServer.h b/TCPServer.h index c3830b2..00ed3c8 100644 --- a/TCPServer.h +++ b/TCPServer.h @@ -12,8 +12,16 @@ #include #include #include +#include -#include "utils.h" +#include + +enum PinceState { + WHITE_FLOWER, + PURPLE_FLOWER, + FLOWER, + NONE +}; struct ClientTCP { diff --git a/utils.cpp b/utils.cpp deleted file mode 100644 index ae92670..0000000 --- a/utils.cpp +++ /dev/null @@ -1,98 +0,0 @@ -#include "utils.h" - -bool TCPUtils::startWith(const std::string& str, const std::string& start) -{ - return str.rfind(start, 0) == 0; -} - -bool TCPUtils::endWith(const std::string& str, const std::string& end) -{ - if (str.length() >= end.length()) - { - return (0 == str.compare(str.length() - end.length(), end.length(), end)); - } - return false; -} - -bool TCPUtils::contains(const std::string& str, const std::string& sub) -{ - return str.find(sub) != std::string::npos; -} - -std::vector TCPUtils::split(const std::string& str, const std::string& delimiter) -{ - std::vector tokens; - size_t prev = 0, pos = 0; - do - { - pos = str.find(delimiter, prev); - if (pos == std::string::npos) pos = str.length(); - std::string token = str.substr(prev, pos - prev); - if (!token.empty()) tokens.push_back(token); - prev = pos + delimiter.length(); - } while (pos < str.length() && prev < str.length()); - return tokens; -} - - -ArucoTag::ArucoTag(int id, std::string name, std::array pos, std::array rot) : _id(id), _name(std::move(name)), _pos(pos), _rot(rot) {} - -int ArucoTag::id() const { - return _id; -} - -std::string ArucoTag::name() const { - return _name; -} - -std::array ArucoTag::pos() const { - return _pos; -} - -std::array ArucoTag::rot() const { - return _rot; -} - -void ArucoTag::setId(int id) { - _id = id; -} - -void ArucoTag::setName(const std::string& name) { - _name = name; -} - -void ArucoTag::setPos(float x, float y) { - this->_pos[0] = x; - this->_pos[1] = y; -} - -void ArucoTag::setRot(float x, float y, float z) { - this->_rot[0] = x; - this->_rot[1] = y; - this->_rot[2] = z; -} - -ArucoTag& ArucoTag::operator=(const ArucoTag& tag) { - _id = tag.id(); - _name = tag.name(); - _pos = tag.pos(); - _rot = tag.rot(); - return *this; -} - -void ArucoTag::find() { - nbFind++; -} - -int ArucoTag::getNbFind() const { - return nbFind; -} - -std::ostream& operator<<(std::ostream& os, const ArucoTag& tag) { - os << "ArucoTag{id=" << tag.id() << ", name=" << tag.name() << ", pos=[" << tag.pos()[0] << ", " << tag.pos()[1] << "], rot=[" << tag.rot()[0] << ", " << tag.rot()[1] << ", " << tag.rot()[2] << "]}"; - return os; -} - -double distanceToTag(const ArucoTag& tag) { - return std::sqrt(pow(tag.pos()[0], 2) + pow(tag.pos()[1], 2)); -} diff --git a/utils.h b/utils.h deleted file mode 100644 index 5db47f0..0000000 --- a/utils.h +++ /dev/null @@ -1,75 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include -#include - -#define PI 3.14159265358979323846 - -enum PinceState { - WHITE_FLOWER, - PURPLE_FLOWER, - FLOWER, - NONE -}; - -namespace TCPUtils { - bool startWith(const std::string& str, const std::string& start); - - bool endWith(const std::string& str, const std::string& end); - - bool contains(const std::string& str, const std::string& sub); - - std::vector split(const std::string& str, const std::string& delimiter); - - template - T mapValue(T v, T v_min, T v_max, T v_min_prime, T v_max_prime) { - return v_min_prime + (v - v_min) * (v_max_prime - v_min_prime) / (v_max - v_min); - } -} - -class ArucoTag { - -public: - ArucoTag(int id, std::string name, std::array pos, std::array rot); - - ArucoTag() = default; - - ArucoTag(const ArucoTag& other) = default; - - [[nodiscard]] int id() const; - - [[nodiscard]] std::string name() const; - - [[nodiscard]] std::array pos() const; - - [[nodiscard]] std::array rot() const; - - void setId(int id); - - void setName(const std::string& name); - - void setPos(float x, float y); - - void setRot(float x, float y, float z); - - ArucoTag& operator=(const ArucoTag& tag); - - void find(); - - [[nodiscard]] int getNbFind() const; - - friend std::ostream& operator<<(std::ostream& os, const ArucoTag& tag); - -private: - int _id = -1; - std::string _name; - std::array _pos = {0, 0}; - std::array _rot = {0, 0, 0}; - int nbFind = 1; -}; - -double distanceToTag(const ArucoTag& tag); \ No newline at end of file