Start of the code for point estimation (cannot be tested yet du to the lack of raspi)

This commit is contained in:
2024-04-23 10:51:10 +02:00
parent c4d72c22e2
commit a23bccf7f3
4 changed files with 148 additions and 3 deletions

View File

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

View File

@@ -1,6 +1,28 @@
#include <atomic>
#include <iostream>
#include "tcp/MyClient.h"
std::atomic<bool> 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;
}

62
tcp/MyClient.cpp Normal file
View File

@@ -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<std::string> 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<double>(2, 0);
double secondMatrixElement0 = pos.second.at<double>(0, 0);
double secondMatrixElement1 = pos.second.at<double>(1, 0);
double secondMatrixElement2 = pos.second.at<double>(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;
}

55
tcp/MyClient.h Normal file
View File

@@ -0,0 +1,55 @@
//
// Created by BreizhHardware on 23/04/2024.
//
#ifndef MYCLIENT_H
#define MYCLIENT_H
#pragma once
#include <TCPSocket/TCPClient.hpp>
#include <TCPSocket/TCPUtils.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/aruco.hpp>
enum ArucoTagType
{
FLOWER,
SOLAR_PANEL
};
class ArucoTag {
int id;
std::string name;
float length;
cv::Mat objectRepresenation;
ArucoTagType type;
std::vector<std::pair<ArucoTag, std::pair<cv::Mat, cv::Mat>>> 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<cv::Vec3f>(0)[0] = cv::Vec3f(-length/2.f, length/2.f, 0);
this->objectRepresenation.ptr<cv::Vec3f>(0)[1] = cv::Vec3f(length/2.f, length/2.f, 0);
this->objectRepresenation.ptr<cv::Vec3f>(0)[2] = cv::Vec3f(length/2.f, -length/2.f, 0);
this->objectRepresenation.ptr<cv::Vec3f>(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