RflySimSDK v3.05
RflySimSDK说明文档
载入中...
搜索中...
未找到
rfly_sensor.h
1#ifndef RFLYSENSOR_H
2#define RFLYSENSOR_H
3
4#include <cstdint>
5#include <vector>
6#include <iostream>
7#include "rfly_udp.h"
8#ifdef _WIN32
9#include <windows.h>
10#else
11#include <fcntl.h>
12#include <sys/mman.h>
13#include <unistd.h>
14#endif
15
16// 这是旧版本协议
17// struct VisionSensorReq {
18// uint16_t checksum; //数据校验位,12345
19// uint16_t SeqID; //内存序号ID
20// uint16_t TypeID; //传感器类型ID
21// uint16_t TargetCopter; //绑定的目标飞机 //可改变
22// uint16_t TargetMountType; //绑定的类型 //可改变
23// uint16_t DataWidth; //数据或图像宽度
24// uint16_t DataHeight; //数据或图像高度
25// uint16_t DataCheckFreq; //检查数据更新频率
26// uint16_t SendProtocol[8]; //传输类型(共享内存、UDP传输无压缩、UDP视频串流),IP地址,端口号,...
27// float CameraFOV; //相机视场角(仅限视觉类传感器) //可改变
28// float SensorPosXYZ[3]; // 传感器安装位置 //可改变
29// float SensorAngEular[3]; //传感器安装角度 //可改变
30// float otherParams[8]; //预留的8位数据位
31// };
32
33// 这是新版本协议
34
36{
37 uint16_t checksum; // 数据校验位,12345
38 uint16_t SeqID; // 内存序号ID
39 uint16_t TypeID; // 传感器类型ID
40 uint16_t TargetCopter; // 绑定的目标飞机 //可改变
41 uint16_t TargetMountType; // 绑定的类型 //可改变
42 uint16_t DataWidth; // 数据或图像宽度
43 uint16_t DataHeight; // 数据或图像高度
44 uint16_t DataCheckFreq; // 检查数据更新频率
45 uint16_t SendProtocol[8]; // 传输类型(共享内存、UDP传输无压缩、UDP视频串流),IP地址,端口号,...
46 float CameraFOV; // 相机视场角(仅限视觉类传感器) //可改变
47 float SensorPosXYZ[3]; // 传感器安装位置 //可改变
48 float EulerOrQuat; // 选择欧拉角或四元数方式,大于0.5就是四元数
49 float SensorAngEuler[3]; // 传感器安装角度 //可改变
50 float SensorAngQuat[4]; // 传感器安装四元数 //可改变
51 float otherParams[16]; // 预留的16位数据位
53 {
54 reset();
55 }
56 void reset()
57 {
58 checksum = 12345;
59 SeqID = 0;
60 TypeID = 0;
61 TargetCopter = 0;
62 TargetMountType = 0;
63 DataWidth = 0;
64 DataHeight = 0;
65 DataCheckFreq = 0;
66 CameraFOV = 0;
67 EulerOrQuat = 0;
68 for (int i = 0; i < 3; i++)
69 {
70 SensorPosXYZ[i] = 0;
71 SensorAngEuler[i] = 0;
72 }
73 for (int i = 0; i < 16; i++)
74 {
75 otherParams[i] = 0;
76 }
77 for (int i = 0; i < 4; i++)
78 {
79 SensorAngQuat[i] = 0;
80 }
81 }
82 void UpdateData(VisionSensorReq vsr)
83 {
84 checksum = vsr.checksum;
85 TargetCopter = vsr.TargetCopter;
86 TargetMountType = vsr.TargetMountType;
87 DataWidth = vsr.DataWidth;
88 DataHeight = vsr.DataHeight;
89 DataCheckFreq = vsr.DataCheckFreq;
90 CameraFOV = vsr.CameraFOV;
91 for (int i = 0; i < 3; i++)
92 {
93 SensorPosXYZ[i] = vsr.SensorPosXYZ[i];
94 SensorAngEuler[i] = vsr.SensorAngEuler[i];
95 }
96 for (int i = 0; i < 16; i++)
97 {
98 otherParams[i] = vsr.otherParams[i];
99 }
100 for (int i = 0; i < 4; i++)
101 {
102 SensorAngQuat[i] = vsr.SensorAngQuat[i];
103 }
104 }
105};
106
107
109{
110
111public:
112 VisionSensorReq vis;
113 std::string TargetIP;
114 int windID;
115 UDPSocket udp;
116 int simTimeLastInt=0;
117 bool hasImgRead=false;
118
119 void sendVisionSensorReq()
120 {
121 this->udp.SendTo((const char *)&this->vis, sizeof(this->vis), this->TargetIP, 20010 + this->windID);
122 }
123
124
125 void updateImg(double t, uint8_T *data_total){
126
127 bool Success = CreateShareMemory(vis.SeqID, vis.TypeID, vis.DataWidth, vis.DataHeight);
128
129 if(!Success){
130 std::cout << "CurTimeInt!=simTimeLastInt" << std::endl;
131 int CurTimeInt = int(t);
132 if (CurTimeInt!=simTimeLastInt){
133 sendVisionSensorReq(); // send img req every 1 seconds
134 simTimeLastInt=CurTimeInt;
135 }
136 }
137
138 if(Success){
139 GetImgBbrToRgb(data_total);
140 }
141
142 }
143
144 // std::vector<VisionSensorReq> VisionSensorReqVec; //存储所有传感器信息
145public:
146 RflySensor()
147 {
148 data = nullptr;
149 dim = 1;
150 dimSize = 1;
151#ifdef _WIN32
152 hMapFile = NULL;
153#else
154 shm_fd = -1;
155#endif
156 }
158 {
159 CloseShareMemory();
160 }
161
162 bool CreateShareMemory(int SeqID, int typeID, int imageWidth, int imageheight)
163 {
164 this->seqID = seqID;
165 this->typeID = typeID;
166
167 this->DataWidth = imageWidth;
168 this->DataHeight = imageheight;
169
170#ifdef _WIN32
171 wchar_t full_shm_name[256];
172 swprintf(full_shm_name, 256, L"RflySim3DImg_%d", SeqID);
173
174 if (hMapFile == NULL)
175 {
176 // 打开内存共享
177 hMapFile = OpenFileMappingW(FILE_MAP_ALL_ACCESS, FALSE, full_shm_name);
178 if (hMapFile == NULL)
179 {
180 std::cerr << "Failed to open UE shared memory" << std::endl;
181 return false;
182 }
183 }
184#else
185 char full_shm_name[256];
186 snprintf(full_shm_name, 256, "RflySim3DImg_%d", SeqID);
187 if (shm_fd == -1)
188 {
189 shm_fd = shm_open(full_shm_name, O_RDONLY, 0666);
190 if (shm_fd == -1)
191 {
192 std::cerr << "Failed to open UE shared memory" << std::endl;
193 return false;
194 }
195 }
196
197#endif
198
199 return true;
200 }
201
202 unsigned char *ReadImageData()
203 {
204#ifdef _WIN32
205 LPVOID lpMapAddress = MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0);
206 if (lpMapAddress == NULL)
207 {
208 std::cerr << "Failed to map image shared memory" << std::endl;
209 return nullptr;
210 }
211 BYTE *mm = static_cast<BYTE *>(lpMapAddress);
212
213 // 获取文件映射对象的大小
214 DWORD size = GetFileSize(hMapFile, NULL);
215
216#else
217 unsigned char *mm = static_cast<unsigned char *>(mmap(NULL, DataWidth * DataHeight * dim * dimSize, PROT_READ, MAP_SHARED, shm_fd, 0));
218 if (mm == MAP_FAILED)
219 {
220 std::cerr << "Failed to map image shared memory" << std::endl;
221 return nullptr;
222 }
223#endif
224
225 unsigned char *pImageData = static_cast<unsigned char *>(mm);
226 unsigned char busyFlag = pImageData[0];
227
228 if (busyFlag == 2)
229 {
230 pImageData[0] = 3;
231 // 根据不同的图片类型计算图像数据的大小
232 if (typeID == 1)
233 {
234 dim = 3;
235 dimSize = 1;
236 }
237 else if (typeID == 2 || typeID == 3)
238 {
239 dim = 1;
240 dimSize = 1;
241 }
242
243 dataSize = DataWidth * DataHeight * dim * dimSize;
244
245 if (dataSize != lastDataSize && data != nullptr)
246 {
247 delete[] data;
248 }
249
250 if (data == nullptr)
251 {
252 data = new unsigned char[dataSize];
253 // lastDataSize = dataSize;
254 }
255
256
257
258 int offset = 1 + 8; // 根据需要调整偏移量的计算方式
259 for (size_t i = 0; i < dataSize; ++i)
260 {
261 data[i] = pImageData[i + offset];
262 }
263 pImageData[0] = 4;
264 }
265
266#ifdef _WIN32
267 UnmapViewOfFile(lpMapAddress);
268#else
269 munmap(mm, DataWidth * DataHeight * dim * dimSize);
270#endif
271
272 return data;
273 }
274
275 void CloseShareMemory()
276 {
277#ifdef _WIN32
278 if (data != NULL)
279 {
280 UnmapViewOfFile(data);
281 delete data;
282 data = nullptr;
283 }
284 if (hMapFile != NULL)
285 {
286 CloseHandle(hMapFile);
287 hMapFile = NULL;
288 }
289#else
290 if (data != NULL)
291 {
292 munmap(data, dataSize);
293 delete data;
294 data = nullptr;
295 }
296 if (shm_fd != -1)
297 {
298 close(shm_fd);
299 shm_fd = -1;
300 }
301#endif
302
303 if (imageData != NULL)
304 {
305 delete imageData;
306 imageData = nullptr;
307 }
308 }
309
310 bool GetImgBbrToRgb(uint8_t *data_total)
311 {
312 unsigned char *data = ReadImageData();
313 if (data == nullptr)
314 {
315 std::cerr << "Failed to read image data" << std::endl;
316 return false;
317 }
318 // static unsigned char* imageData = nullptr;
319
320 if (dataSize != lastDataSize && imageData != nullptr)
321 {
322 delete[] imageData;
323 }
324
325 if (imageData == NULL){
326 imageData = new unsigned char[dataSize];
327 lastDataSize = dataSize;
328 }
329
330
331 int Width = this->DataWidth;
332 int Height = this->DataHeight;
333 int Dim = this->dim;
334 int Imgwh = Width * Height;
335 const int newWidth = Height;
336 const int newHeight = Width;
337 const int newDepth = Dim;
338
339 if (Dim == 1)
340 memcpy(data_total, data, dataSize);
341 else if (Dim == 3)
342 {
343 for (int i = 0; i < Dim; ++i)
344 {
345 // for (int j = 0; j < Imgwh; ++j) {
346 // data_total[i * Imgwh + j] = data[(Dim * j) + (Dim - i - 1)];
347 // }
348
349 for (int h = 0; h < Height; ++h)
350 {
351 for (int w = 0; w < Width; ++w)
352 {
353 imageData[i * Width * Height + (h * Width + w)] = data[(Dim * (h * Width + w)) + (Dim - i - 1)];
354 }
355 }
356 }
357 // for (int i = 0; i < Dim; ++i) {
358 for (int i = 0; i < Dim; ++i)
359 {
360 for (int w = Width - 1; w >= 0; --w)
361 {
362 for (int h = Height - 1; h >= 0; --h)
363 {
364 // data_total[i * Width * Height + ((Height-1-h) * Width + (Width-1-w))] = imageData[(Dim * (h * Width + w)) + (Dim - i - 1)];
365 data_total[i * Width * Height + (Width - 1 - w) * Height + (Height - 1 - h)] = imageData[(i * Width * Height) + ((Height - 1 - h) * Width + w)];
366 }
367 }
368 }
369 }
370 return true;
371 }
372
373private:
374 unsigned char *data;
375 long dataSize,lastDataSize;
376 int DataWidth = 640;
377 int DataHeight = 480;
378 int typeID;
379 int seqID;
380 int dim;
381 int dimSize;
382 unsigned char *imageData = nullptr;
383 // int* pData;
384
385#ifdef _WIN32
386 HANDLE hMapFile;
387 HANDLE UEhMapFile;
388#else
389 int shm_fd;
390 int ue_shm_fd;
391#endif
392};
393
394#endif // RFLYSENSOR_H
定义 rfly_sensor.h:109
定义 rfly_udp.h:107
定义 rfly_sensor.h:36