-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTCPServer.h
More file actions
324 lines (224 loc) · 7.24 KB
/
TCPServer.h
File metadata and controls
324 lines (224 loc) · 7.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#pragma once
#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <cstring>
#include <thread>
#include <utility>
#include <vector>
#include <algorithm>
#include <atomic>
#include <fstream>
#include <optional>
#include "utils.h"
#define MAX_SPEED 200
#define MIN_SPEED 150
struct ClientTCP
{
std::string name;
int socket = -1;
bool isReady = false;
ClientTCP() = default;
explicit ClientTCP(std::string name, int socket = -1) : name(std::move(name)), socket(socket) {}
};
enum Team {
BLUE,
YELLOW,
TEST,
};
enum StratPattern {
TURN_SOLAR_PANNEL_1,
TURN_SOLAR_PANNEL_2,
TURN_SOLAR_PANNEL_3,
TAKE_FLOWER_BOTTOM,
TAKE_FLOWER_TOP,
DROP_PURPLE_FLOWER,
DROP_WHITE_FLOWER_J1,
DROP_WHITE_FLOWER_J2,
GO_END,
GET_LIDAR_POS,
CHECKPOINT_MIDDLE,
CHECKPOINT_TRANSITION_SOLAR_PANEL_FLOWER,
TAKE_3_PLANT_BOTTOM_1,
TAKE_3_PLANT_BOTTOM_2,
TAKE_3_PLANT_TOP_1,
TAKE_3_PLANT_TOP_2,
DROP_FLOWER_J1,
DROP_FLOWER_J2,
REMOVE_POT_J2,
DROP_FLOWER_BASE_1,
DROP_FLOWER_BASE_2,
SLEEP_1S,
SLEEP_5S,
SLEEP_10S,
ROTATE_0,
ROTATE_270,
};
class TCPServer; // Forward declaration
class ClientHandler {
private:
int clientSocket;
TCPServer* server; // Reference to the TCPServer instance
public:
explicit ClientHandler(int clientSocket, TCPServer* server);
void handle();
void processMessage(const std::string& message);
void closeConnection();
};
class TCPServer {
private:
int serverSocket;
std::vector<std::thread> clientThreads;
std::vector<int> clientSockets; // Store connected client sockets
int connectedClients = 0; // Track the number of connected clients
std::atomic<bool> _shouldStop = false; // Flag to indicate if the server should stop
std::vector<ClientTCP> clients; // Store connected clients
std::array<PinceState, 3> pinceState = {NONE, NONE, NONE};
int isRobotIdle = 0;
bool gameStarted = false;
std::chrono::time_point<std::chrono::system_clock> gameStart;
int speed = 0;
struct Position {
struct {
float x;
float y;
} pos;
float theta;
};
Position robotPose{};
Position initRobotPose{};
Position endRobotPose{};
Position lidarCalculatePos{};
std::vector<ArucoTag> arucoTags;
Team team;
std::vector<StratPattern> stratPatterns = {
TURN_SOLAR_PANNEL_1,
TURN_SOLAR_PANNEL_2,
TURN_SOLAR_PANNEL_3,
CHECKPOINT_TRANSITION_SOLAR_PANEL_FLOWER,
/*TAKE_FLOWER_BOTTOM,
TAKE_FLOWER_BOTTOM,
TAKE_FLOWER_BOTTOM,*/
/*TAKE_3_PLANT_BOTTOM_1,
DROP_FLOWER_BASE_2,*/
TAKE_3_PLANT_BOTTOM_1,
GET_LIDAR_POS,
REMOVE_POT_J2,
DROP_FLOWER_J2,
ROTATE_270,
/*DROP_PURPLE_FLOWER,
DROP_WHITE_FLOWER_J1,*/
// GET_LIDAR_POS,
/*TAKE_FLOWER_TOP,
TAKE_FLOWER_TOP,
TAKE_FLOWER_TOP,*/
TAKE_3_PLANT_TOP_1,
GET_LIDAR_POS,
DROP_FLOWER_J1,
/*DROP_PURPLE_FLOWER,
DROP_WHITE_FLOWER_J2,*/
// GET_LIDAR_POS,
/*TAKE_FLOWER_TOP,
TAKE_FLOWER_TOP,
TAKE_FLOWER_TOP,*/
//TAKE_3_PLANT_TOP,
/*DROP_WHITE_FLOWER_J2,
DROP_PURPLE_FLOWER,*/
TAKE_3_PLANT_TOP_2,
DROP_FLOWER_BASE_1,
GO_END
};
// This is the index of the current pattern
int whereAmI = 0;
bool stopEmergency = false;
bool handleEmergencyFlag = false;
bool awaitForLidar = false;
std::thread gameThread;
int lidarSocket = -1;
int arduinoSocket = -1;
int lidarGetPosTimeout = 0;
std::string lastArduinoCommand{};
public:
explicit TCPServer(int port);
void start();
void acceptConnections();
// Broadcast message to all connected clients
void broadcastMessage(const char* message, int senderSocket = -1); // Modified method signature
void broadcastMessage(const std::string &message, int senderSocket = -1); // Modified method signature
void sendToClient(const char* message, int clientSocket); // New method to send message to a specific client
void sendToClient(const std::string &message, int clientSocket); // New method to send message to a specific client
void sendToClient(const char* message, const std::string& clientName); // New method to send message to a specific client
void sendToClient(const std::string &message, const std::string& clientName); // New method to send message to a specific client
void handleMessage(const std::string& message, int clientSocket = -1);
void clientDisconnected(int clientSocket); // New method to handle client disconnection
void stop();
[[nodiscard]] size_t nbClients() const;
void checkIfAllClientsReady();
void startGame();
void startGameTest();
void goToAruco(const ArucoTag &arucoTag, int pince);
void askArduinoPos();
[[nodiscard]] bool shouldStop() const;
int awaitRobotIdle();
void handleArucoTag(const ArucoTag &tag);
std::optional<ArucoTag> getBiggestArucoTag(float borneMinX, float borneMaxX, float borneMinY, float borneMaxY);
std::optional<ArucoTag> getMostCenteredArucoTag(float borneMinX, float borneMaxX, float borneMinY, float borneMaxY);
std::vector<PinceState> getNotFallenFlowers() const;
void handleEmergency(int distance, double angle);
/*
* Start Strategy function
*/
void goAndTurnSolarPanel(StratPattern sp);
void findAndGoFlower(StratPattern sp);
void goEnd();
void dropPurpleFlowers();
void dropWhiteFlowers(StratPattern sp);
void getLidarPos();
void checkpoint(StratPattern sp);
void dropJardiniereFlowers(StratPattern sp);
void dropBaseFlowers(StratPattern sp);
void go3Plants(StratPattern sp);
void removePot(StratPattern sp);
/*
* End Strategy function
*/
void startTestAruco(int pince);
// Call to broadcast
void setSpeed(int speed);
void setMaxSpeed();
void setMinSpeed();
template<class X, class Y>
void go(X x, Y y);
template<class X>
void go(std::array<X, 2> data);
template<class X>
void rotate(X angle);
template<class X, class Y>
void transit(X x,Y y, int endSpeed);
template<class X>
void transit(std::array<X, 2> data, int endSpeed);
template<class X, class Y, class Z>
void setPosition(X x, Y y, Z theta, int clientSocket = -1);
template<class X>
void setPosition(std::array<X, 3> data, int clientSocket = -1);
void setPosition(Position pos, int clientSocket = -1);
template<class X, class Y, class Z>
void setPosition(X x, Y y, Z theta, const std::string &toSend);
template<class X>
void setPosition(std::array<X, 3> data, const std::string &toSend);
void setPosition(Position pos, const std::string &toSend);
void openPince(int pince);
void fullyOpenPince(int pince);
void middlePince(int pince);
void closePince(int pince);
void baisserBras();
void leverBras();
void transportBras();
void checkPanneau(int servo_moteur);
void uncheckPanneau(int servo_moteur);
void askLidarPosition();
void sendPoint(int point);
void setTeam(Team team);
~TCPServer();
};