RoveSoSimulator
Loading...
Searching...
No Matches
RoveCommUDPWrapper.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 "RoveCommUDPWrapper.generated.h"
10
11// Forward declaration of your RoveCommUDP class from the RoveComm library
12namespace rovecomm {
13 class RoveCommUDP;
14}
15
16// Create events for when a packet is received.
17DECLARE_DYNAMIC_MULTICAST_DELEGATE(FDriveDataReceived);
18DECLARE_DYNAMIC_MULTICAST_DELEGATE(FLEDPanelDataReceived);
19
20/**
21 * @brief Blueprintable wrapper class for RoveCommUDP to allow usage in Unreal Engine Blueprints.
22 */
23UCLASS(Blueprintable, BlueprintType)
24class ROVESOSIMULATOR_API URoveCommUDPWrapper : public UObject
25{
26 GENERATED_BODY()
27
28public:
29 // Constructor
30 URoveCommUDPWrapper();
31
32 // Destructor
33 virtual ~URoveCommUDPWrapper();
34
35 // Initialize the RoveComm UDP connection on a specified port
36 UFUNCTION(BlueprintCallable, Category = "RoveComm UDP")
37 bool InitUDPSocket(int32 Port);
38
39 // Send a UDP packet
40 UFUNCTION(BlueprintCallable, Category = "RoveComm UDP")
41 int SendPacket(URoveCommPacketWrapper* Packet, const FString& IPAddress, int32 Port);
42
43 // Close the UDP socket
44 UFUNCTION(BlueprintCallable, Category = "RoveComm UDP")
45 void CloseUDPSocket();
46
47 // Getter methods with shared lock
48 UFUNCTION(BlueprintCallable, Category = "RoveComm UDP")
49 TArray<float> GetDrivePowersCopy();
50 UFUNCTION(BlueprintCallable, Category = "RoveComm UDP")
51 TArray<uint8> GetLEDPanelRGBColorsCopy();
52
53 // Getter method for RoveComm FPS.
54 UFUNCTION(BlueprintCallable, Category = "RoveComm UDP")
55 int32 GetRoveCommFPS();
56
57 // Delegates.
58 UPROPERTY(BlueprintAssignable, Category = "RoveComm UDP")
59 FDriveDataReceived OnDriveDataReceived;
60 UPROPERTY(BlueprintAssignable, Category = "RoveComm UDP")
61 FLEDPanelDataReceived OnLEDPanelDataReceived;
62
63protected:
64 // Instance of the RoveCommUDP class
65 rovecomm::RoveCommUDP* RoveCommUDPInstance;
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>&, const sockaddr_in&)> ProcessDriveData =
88 [this](const rovecomm::RoveCommPacket<float>& stPacket, const sockaddr_in& stdAddr)
89 {
90 // Not using this.
91 (void) stdAddr;
92
93 // Assuming stPacket.vData contains two elements: left and right drive power
94 if (stPacket.vData.size() >= 2)
95 {
96 // Lock the mutex.
98 // Clear the drive powers.
100 // Add the new drive powers.
103
104 // Unlock the mutex.
105 Lock.unlock();
106 }
107
108 // Trigger the OnDriveDataReceived event on the game thread.
110 {
112 });
113 };
114
115 /******************************************************************************
116 * @brief Callback function that is called whenever RoveComm receives new RGB data.
117 *
118 *
119 * @author clayjay3 (claytonraycowen@gmail.com)
120 * @date 2024-03-03
121 ******************************************************************************/
122 const std::function<void(const rovecomm::RoveCommPacket<uint8_t>&, const sockaddr_in&)> ProcessRGBData =
124 {
125 // Not using this.
126 (void) stdAddr;
127
128 // Lock the mutex.
130
131 // Clear the LED panel colors.
133 // Store the first three indexes from vData in the LEDPanelRGBColors.
137
138 // Unlock the mutex.
139 Lock.unlock();
140
141 // Trigger the OnLEDPanelDataReceived event on the game thread.
143 {
145 });
146 };
147
148 /******************************************************************************
149 * @brief Callback function that is called whenever RoveComm receives new StateDisplay data.
150 *
151 *
152 * @author clayjay3 (claytonraycowen@gmail.com)
153 * @date 2024-03-03
154 ******************************************************************************/
155 const std::function<void(const rovecomm::RoveCommPacket<uint8_t>&, const sockaddr_in&)> ProcessStateDisplayData =
157 {
158 // Not using this.
159 (void) stdAddr;
160
161 // Determine display state.
162 switch (stPacket.vData[0])
163 {
164 case 0:
165 {
166 // Teleop Blue
171 LEDPanelRGBColors.Add(255); // RGB for Blue
172 // Unlock the mutex.
173 Lock.unlock();
174 break;
175 }
176 case 1:
177 {
178 // Autonomy Red
183 LEDPanelRGBColors.Add(0); // RGB for Red
184 // Unlock the mutex.
185 Lock.unlock();
186 break;
187 }
188 case 2:
189 {
190 // Goal Green
195 LEDPanelRGBColors.Add(0); // RGB for Green
196 // Unlock the mutex.
197 Lock.unlock();
198 break;
199 }
200 default:
201 break;
202 }
203
204 // Trigger the OnLEDPanelDataReceived event on the game thread.
206 {
208 });
209 };
210};
Blueprintable wrapper class for RoveCommUDP to allow usage in Unreal Engine Blueprints.
Definition RoveCommUDPWrapper.h:25