usage of Modelec lib

This commit is contained in:
ackimixs
2024-05-20 12:52:38 +02:00
parent 56bf715ba8
commit 27487d0f2d
5 changed files with 29 additions and 186 deletions

View File

@@ -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
)

View File

@@ -18,7 +18,7 @@ void ClientHandler::handle() {
break;
}
std::vector<std::string> messages = TCPUtils::split(buffer, "\n");
std::vector<std::string> 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<std::string> tokens = TCPUtils::split(message, ";");
std::vector<std::string> 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<std::string> args = TCPUtils::split(tokens[3], ",");
if (Modelec::contains(tokens[2], "stop proximity")) {
std::vector<std::string> 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<std::string> args = TCPUtils::split(tokens[3], ",");
std::vector<std::string> 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<int>(TCPUtils::mapValue(value, -32767.0, 32768.0, - PI / 2, PI / 2));
int angle = static_cast<int>(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<int>(TCPUtils::mapValue(value, -32767.0, 0.0, -310.0, -70.0));
speed = static_cast<int>(Modelec::mapValue(value, -32767.0, 0.0, -310.0, -70.0));
} else if (value == 0) {
speed = 0;
} else {
speed = static_cast<int>(TCPUtils::mapValue(value, 0.0, 32768.0, 70.0, 310.0));
speed = static_cast<int>(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<int>(TCPUtils::mapValue(value, -32767.0, 32768.0, -310.0, 310.0));
int speed = static_cast<int>(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<std::string> args = TCPUtils::split(tokens[3], ",");
std::vector<std::string> args = Modelec::split(tokens[3], ",");
int nb = std::stoi(args[0]);

View File

@@ -12,8 +12,16 @@
#include <atomic>
#include <fstream>
#include <optional>
#include <array>
#include "utils.h"
#include <Modelec/Utils.h>
enum PinceState {
WHITE_FLOWER,
PURPLE_FLOWER,
FLOWER,
NONE
};
struct ClientTCP
{

View File

@@ -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<std::string> TCPUtils::split(const std::string& str, const std::string& delimiter)
{
std::vector<std::string> 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<float, 2> pos, std::array<float, 3> 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<float, 2> ArucoTag::pos() const {
return _pos;
}
std::array<float, 3> 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));
}

75
utils.h
View File

@@ -1,75 +0,0 @@
#pragma once
#include <array>
#include <utility>
#include <vector>
#include <string>
#include <ostream>
#include <cmath>
#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<std::string> split(const std::string& str, const std::string& delimiter);
template<typename T>
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<float, 2> pos, std::array<float, 3> rot);
ArucoTag() = default;
ArucoTag(const ArucoTag& other) = default;
[[nodiscard]] int id() const;
[[nodiscard]] std::string name() const;
[[nodiscard]] std::array<float, 2> pos() const;
[[nodiscard]] std::array<float, 3> 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<float, 2> _pos = {0, 0};
std::array<float, 3> _rot = {0, 0, 0};
int nbFind = 1;
};
double distanceToTag(const ArucoTag& tag);