diff --git a/CMakeLists.txt b/CMakeLists.txt index 40f9c27..04b8477 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,12 @@ -cmake_minimum_required(VERSION 3.28) +cmake_minimum_required(VERSION 3.25) project(estimation_points) set(CMAKE_CXX_STANDARD 17) -add_executable(estimation_points main.cpp) +find_package(PkgConfig REQUIRED) +pkg_check_modules(TCPSocket REQUIRED TCPSocket) + +add_executable(estimation_points main.cpp + tcp/MyClient.cpp + tcp/MyClient.h + tcp/MyClient.h) diff --git a/main.cpp b/main.cpp index bc8f460..19c6512 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,28 @@ +#include #include +#include "tcp/MyClient.h" + +std::atomic stopRequested(false); + +void userInputThread() { + // Wait for the user to press Enter + std::cout << "Press Enter to stop the program..." << std::endl; + std::cin.ignore(); + stopRequested = true; +} int main() { - std::cout << "Hello, World!" << std::endl; + MyClient client("127.0.0.1", 8080); + + client.start(); + + client.sendMessage("point;start;ready;1"); + + while (true) { + if(stopRequested) { + break; + } + } + return 0; } diff --git a/tcp/MyClient.cpp b/tcp/MyClient.cpp new file mode 100644 index 0000000..f5bd463 --- /dev/null +++ b/tcp/MyClient.cpp @@ -0,0 +1,62 @@ +// +// Created by BreizhHardware on 23/04/2024. +// + +#include "MyClient.h" + +MyClient::~MyClient() { + this -> stop(); +} + +MyClient::MyClient(const char* ip, const int &port) : TCPClient(ip, port), points(0) { + +} + +void MyClient::handleMessage(const std::string&message) { + std::vector messageSplited = TCPSocket::split(message, ";"); + if (messageSplited[1] == "point" || messageSplited[1] == "all") { + if (messageSplited[2] == "ping") { + this->sendMessage("point;ihm;pong;1"); + } + } + if(messageSplited[0] == "aruco") { + if(messageSplited[1] == "strat") { + if(messageSplited[2] == "get aruco") { + if(messageSplited[3] == "404") { + return; + } + else { + for (auto& [tag, pos] : arucoTags) { + double firstMatrixElement = pos.first.at(2, 0); + + double secondMatrixElement0 = pos.second.at(0, 0); + double secondMatrixElement1 = pos.second.at(1, 0); + double secondMatrixElement2 = pos.second.at(2, 0); + + // Check if the tag is in the depot area + if (firstMatrixElement < 0.5) { + if (secondMatrixElement0 > 0.5 && secondMatrixElement0 < 1.5) { + if (secondMatrixElement1 > 0.5 && secondMatrixElement1 < 1.5) { + if (secondMatrixElement2 > 0.5 && secondMatrixElement2 < 1.5) { + // The tag is in the depot area so add 1 point + this->setPoints(this->getPoints() + 1); + // Send the new points to the IHM + this->sendMessage("point;ihm;update;" + std::to_string(this->getPoints())); + } + } + } + } + } + } + } + } + } +} + +int MyClient::getPoints() const { + return this->points; +} + +void MyClient::setPoints(const int &points) { + this->points = points; +} diff --git a/tcp/MyClient.h b/tcp/MyClient.h new file mode 100644 index 0000000..51909f1 --- /dev/null +++ b/tcp/MyClient.h @@ -0,0 +1,55 @@ +// +// Created by BreizhHardware on 23/04/2024. +// + +#ifndef MYCLIENT_H +#define MYCLIENT_H +#pragma once + +#include +#include +#include +#include + +enum ArucoTagType +{ + FLOWER, + SOLAR_PANEL +}; + +class ArucoTag { + int id; + std::string name; + float length; + cv::Mat objectRepresenation; + ArucoTagType type; + std::vector>> arucoTags; + ArucoTag(const int id, std::string name, const float length, const ArucoTagType type) : id(id), name(std::move(name)), length(length), type(type) + { + this->objectRepresenation = cv::Mat(4, 1, CV_32FC3); + this->objectRepresenation.ptr(0)[0] = cv::Vec3f(-length/2.f, length/2.f, 0); + this->objectRepresenation.ptr(0)[1] = cv::Vec3f(length/2.f, length/2.f, 0); + this->objectRepresenation.ptr(0)[2] = cv::Vec3f(length/2.f, -length/2.f, 0); + this->objectRepresenation.ptr(0)[3] = cv::Vec3f(-length/2.f, -length/2.f, 0); + } +}; + +class MyClient : public TCPClient { +protected: + int points; + +public: + + ~MyClient() override; + + explicit MyClient(const char* ip = "127.0.0.1", const int &port = 8080); + + void handleMessage(const std::string &message) override; + + int getPoints() const; + + void setPoints(const int &points); + +}; + +#endif //MYCLIENT_H