40 uint16_t TargetCopter;
41 uint16_t TargetMountType;
44 uint16_t DataCheckFreq;
45 uint16_t SendProtocol[8];
47 float SensorPosXYZ[3];
49 float SensorAngEuler[3];
50 float SensorAngQuat[4];
51 float otherParams[16];
68 for (
int i = 0; i < 3; i++)
71 SensorAngEuler[i] = 0;
73 for (
int i = 0; i < 16; i++)
77 for (
int i = 0; i < 4; i++)
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++)
93 SensorPosXYZ[i] = vsr.SensorPosXYZ[i];
94 SensorAngEuler[i] = vsr.SensorAngEuler[i];
96 for (
int i = 0; i < 16; i++)
98 otherParams[i] = vsr.otherParams[i];
100 for (
int i = 0; i < 4; i++)
102 SensorAngQuat[i] = vsr.SensorAngQuat[i];
113 std::string TargetIP;
116 int simTimeLastInt=0;
117 bool hasImgRead=
false;
119 void sendVisionSensorReq()
121 this->udp.SendTo((
const char *)&this->vis,
sizeof(this->vis), this->TargetIP, 20010 + this->windID);
125 void updateImg(
double t, uint8_T *data_total){
127 bool Success = CreateShareMemory(vis.SeqID, vis.TypeID, vis.DataWidth, vis.DataHeight);
130 std::cout <<
"CurTimeInt!=simTimeLastInt" << std::endl;
131 int CurTimeInt = int(t);
132 if (CurTimeInt!=simTimeLastInt){
133 sendVisionSensorReq();
134 simTimeLastInt=CurTimeInt;
139 GetImgBbrToRgb(data_total);
162 bool CreateShareMemory(
int SeqID,
int typeID,
int imageWidth,
int imageheight)
165 this->typeID = typeID;
167 this->DataWidth = imageWidth;
168 this->DataHeight = imageheight;
171 wchar_t full_shm_name[256];
172 swprintf(full_shm_name, 256, L
"RflySim3DImg_%d", SeqID);
174 if (hMapFile == NULL)
177 hMapFile = OpenFileMappingW(FILE_MAP_ALL_ACCESS, FALSE, full_shm_name);
178 if (hMapFile == NULL)
180 std::cerr <<
"Failed to open UE shared memory" << std::endl;
185 char full_shm_name[256];
186 snprintf(full_shm_name, 256,
"RflySim3DImg_%d", SeqID);
189 shm_fd = shm_open(full_shm_name, O_RDONLY, 0666);
192 std::cerr <<
"Failed to open UE shared memory" << std::endl;
202 unsigned char *ReadImageData()
205 LPVOID lpMapAddress = MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0);
206 if (lpMapAddress == NULL)
208 std::cerr <<
"Failed to map image shared memory" << std::endl;
211 BYTE *mm =
static_cast<BYTE *
>(lpMapAddress);
214 DWORD size = GetFileSize(hMapFile, NULL);
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)
220 std::cerr <<
"Failed to map image shared memory" << std::endl;
225 unsigned char *pImageData =
static_cast<unsigned char *
>(mm);
226 unsigned char busyFlag = pImageData[0];
237 else if (typeID == 2 || typeID == 3)
243 dataSize = DataWidth * DataHeight * dim * dimSize;
245 if (dataSize != lastDataSize && data !=
nullptr)
252 data =
new unsigned char[dataSize];
259 for (
size_t i = 0; i < dataSize; ++i)
261 data[i] = pImageData[i + offset];
267 UnmapViewOfFile(lpMapAddress);
269 munmap(mm, DataWidth * DataHeight * dim * dimSize);
275 void CloseShareMemory()
280 UnmapViewOfFile(data);
284 if (hMapFile != NULL)
286 CloseHandle(hMapFile);
292 munmap(data, dataSize);
303 if (imageData != NULL)
310 bool GetImgBbrToRgb(uint8_t *data_total)
312 unsigned char *data = ReadImageData();
315 std::cerr <<
"Failed to read image data" << std::endl;
320 if (dataSize != lastDataSize && imageData !=
nullptr)
325 if (imageData == NULL){
326 imageData =
new unsigned char[dataSize];
327 lastDataSize = dataSize;
331 int Width = this->DataWidth;
332 int Height = this->DataHeight;
334 int Imgwh = Width * Height;
335 const int newWidth = Height;
336 const int newHeight = Width;
337 const int newDepth = Dim;
340 memcpy(data_total, data, dataSize);
343 for (
int i = 0; i < Dim; ++i)
349 for (
int h = 0; h < Height; ++h)
351 for (
int w = 0; w < Width; ++w)
353 imageData[i * Width * Height + (h * Width + w)] = data[(Dim * (h * Width + w)) + (Dim - i - 1)];
358 for (
int i = 0; i < Dim; ++i)
360 for (
int w = Width - 1; w >= 0; --w)
362 for (
int h = Height - 1; h >= 0; --h)
365 data_total[i * Width * Height + (Width - 1 - w) * Height + (Height - 1 - h)] = imageData[(i * Width * Height) + ((Height - 1 - h) * Width + w)];
375 long dataSize,lastDataSize;
377 int DataHeight = 480;
382 unsigned char *imageData =
nullptr;