RoveSoSimulator
Loading...
Searching...
No Matches
RoveCommTCPWrapper.h
1#pragma once
2
3#include "CoreMinimal.h"
4#include <shared_mutex>
5#include <functional>
7#include "UObject/NoExportTypes.h"
8#include "Async/Async.h"
9#include "RoveCommTCPWrapper.generated.h"
10
11// Forward declaration of your RoveCommTCP class from the RoveComm library
12namespace rovecomm {
13 class RoveCommTCP;
14}
15
16// Create events for when a packet is received.
17DECLARE_DYNAMIC_MULTICAST_DELEGATE(FDriveDataReceivedTCP);
18DECLARE_DYNAMIC_MULTICAST_DELEGATE(FLEDPanelDataReceivedTCP);
19
20/**
21 * @brief Blueprintable wrapper class for RoveCommTCP to allow usage in Unreal Engine Blueprints.
22 */
23UCLASS(Blueprintable, BlueprintType)
24class ROVESOSIMULATOR_API URoveCommTCPWrapper : public UObject
25{
26 GENERATED_BODY()
27
28public:
29 // Constructor
30 URoveCommTCPWrapper();
31
32 // Destructor
33 virtual ~URoveCommTCPWrapper();
34
35 // Initialize the RoveComm TCP connection on a specified port
36 UFUNCTION(BlueprintCallable, Category = "RoveComm TCP")
37 bool InitTCPSocket(const FString& IPAddress, int32 Port);
38
39 // Send a TCP packet
40 UFUNCTION(BlueprintCallable, Category = "RoveComm TCP")
41 int SendTCPPacket(URoveCommPacketWrapper* Packet, const FString& IPAddress, int32 Port);
42
43 // Close the TCP socket
44 UFUNCTION(BlueprintCallable, Category = "RoveComm TCP")
45 void CloseTCPSocket();
46
47 // Getter methods with shared lock
48 UFUNCTION(BlueprintCallable, Category = "RoveComm TCP")
49 TArray<float> GetDrivePowersCopyTCP();
50 UFUNCTION(BlueprintCallable, Category = "RoveComm TCP")
51 TArray<uint8> GetLEDPanelRGBColorsCopyTCP();
52
53 // Getter method for RoveComm FPS.
54 UFUNCTION(BlueprintCallable, Category = "RoveComm TCP")
55 int32 GetRoveCommTCPFPS();
56
57 // Delegates.
58 UPROPERTY(BlueprintAssignable, Category = "RoveComm TCP")
59 FDriveDataReceivedTCP OnDriveDataReceived;
60 UPROPERTY(BlueprintAssignable, Category = "RoveComm TCP")
61 FLEDPanelDataReceivedTCP OnLEDPanelDataReceived;
62
63protected:
64 // Instance of the RoveCommTCP class
65 rovecomm::RoveCommTCP* RoveCommTCPInstance;
66
67private:
68 // Declare member variables for drive powers and LED panel.
69 TArray<float> DrivePowers;
70 TArray<uint8> LEDPanelRGBColors;
71
72 // Mutexes for the member variables. These are needed because the RoveComm callbacks run on a separate thread.
73 std::shared_mutex DrivePowersMutex;
74 std::shared_mutex LEDPanelRGBColorsMutex;
75
76 /////////////////////////////////////////
77 // RoveComm Callbacks.
78 /////////////////////////////////////////
79
80 /******************************************************************************
81 * @brief Callback function that is called whenever RoveComm receives new Accuracy data.
82 *
83 *
84 * @author clayjay3 (claytonraycowen@gmail.com)
85 * @date 2024-03-03
86 ******************************************************************************/
87 const std::function<void(const rovecomm::RoveCommPacket<float>&)> ProcessDriveDataTCP =
88 [this](const rovecomm::RoveCommPacket<float>& stPacket)
89 {
90 // Assuming stPacket.vData contains two elements: left and right drive power
91 if (stPacket.vData.size() >= 2)
92 {
93 // Lock the mutex.
95 // Clear the drive powers.
97 // Add the new drive powers.
100
101 // Unlock the mutex.
102 Lock.unlock();
103 }
104
105 // Trigger the OnDriveDataReceived event on the game thread.
107 {
109 });
110 };
111
112 /******************************************************************************
113 * @brief Callback function that is called whenever RoveComm receives new RGB data.
114 *
115 *
116 * @author clayjay3 (claytonraycowen@gmail.com)
117 * @date 2024-03-03
118 ******************************************************************************/
119 const std::function<void(const rovecomm::RoveCommPacket<uint8_t>&)> ProcessRGBDataTCP =
120 [this](const rovecomm::RoveCommPacket<uint8_t>& stPacket)
121 {
122 // Lock the mutex.
124
125 // Clear the LED panel colors.
127 // Store the first three indexes from vData in the LEDPanelRGBColors.
131
132 // Unlock the mutex.
133 Lock.unlock();
134
135 // Trigger the OnLEDPanelDataReceived event on the game thread.
137 {
139 });
140 };
141
142 /******************************************************************************
143 * @brief Callback function that is called whenever RoveComm receives new StateDisplay data.
144 *
145 *
146 * @author clayjay3 (claytonraycowen@gmail.com)
147 * @date 2024-03-03
148 ******************************************************************************/
149 const std::function<void(const rovecomm::RoveCommPacket<uint8_t>&)> ProcessStateDisplayDataTCP =
150 [this](const rovecomm::RoveCommPacket<uint8_t>& stPacket)
151 {
152 // Determine display state.
153 switch (stPacket.vData[0])
154 {
155 case 0:
156 {
157 // Teleop Blue
162 LEDPanelRGBColors.Add(255); // RGB for Blue
163 // Unlock the mutex.
164 Lock.unlock();
165 break;
166 }
167 case 1:
168 {
169 // Autonomy Red
174 LEDPanelRGBColors.Add(0); // RGB for Red
175 // Unlock the mutex.
176 Lock.unlock();
177 break;
178 }
179 case 2:
180 {
181 // Goal Green
186 LEDPanelRGBColors.Add(0); // RGB for Green
187 // Unlock the mutex.
188 Lock.unlock();
189 break;
190 }
191 default:
192 break;
193 }
194
195 // Trigger the OnLEDPanelDataReceived event on the game thread.
197 {
199 });
200 };
201};
Blueprintable wrapper class for RoveCommTCP to allow usage in Unreal Engine Blueprints.
Definition RoveCommTCPWrapper.h:25