-
Notifications
You must be signed in to change notification settings - Fork 631
/
main.cpp
301 lines (275 loc) · 10.8 KB
/
main.cpp
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
/*! @file advanced-sensing/main.cpp
* @version 4.0.0
* @date Sep 15 2017
*
* @brief
* Logging API usage in a Linux environment.
* Shows example usage of various advanced sensing APIs and controls.
*
* @Copyright (c) 2017 DJI
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
#include "dji_linux_helpers.hpp"
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include "dji_vehicle.hpp"
using namespace DJI::OSDK;
#define IMAGE_FILE_PATH "./image"
#define IMAGE_FILE_PATH_LEN (64)
#define IMAGE_INFO_LEN (sizeof(DUSS_MSG_OSDK_IMAGE_INFO_t))
#define IMAGE_MAX_DIRECTION_NUM (6)
#ifdef OPEN_CV_INSTALLED
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#else
int writePictureData(const uint8_t *data, uint32_t len) {
DIR *dirp = NULL;
FILE *fp = NULL;
char fileName[IMAGE_FILE_PATH_LEN] = {0};
size_t size = 0;
static uint32_t index = 0;
dirp = opendir(IMAGE_FILE_PATH);
if(dirp == NULL && mkdir(IMAGE_FILE_PATH, S_IRWXU | S_IRGRP | S_IROTH | S_IXOTH)) {
return -1;
}
closedir(dirp);
snprintf(fileName, IMAGE_FILE_PATH_LEN, "%s/%d.raw", IMAGE_FILE_PATH, index);
fp = fopen(fileName, "w+");
if(fp == NULL) {
return -1;
}
size = fwrite(data, 1, len, fp);
if(size != len) {
return -1;
}
if(fp) {
fclose(fp);
}
index++;
return 0;
}
#endif
typedef struct StereoImagePacketType {
Perception::ImageInfoType info;
uint8_t *imageRawBuffer;
T_OsdkMutexHandle mutex;
bool gotData;
} StereoImagePacketType;
void PerceptionImageCB(Perception::ImageInfoType info, uint8_t *imageRawBuffer,
int bufferLen, void *userData) {
DSTATUS("image info : dataId(%d) seq(%d) timestamp(%llu) datatype(%d) index(%d) h(%d) w(%d) dir(%d) "
"bpp(%d) bufferlen(%d)", info.dataId, info.sequence, info.timeStamp, info.dataType,
info.rawInfo.index, info.rawInfo.height, info.rawInfo.width, info.rawInfo.direction,
info.rawInfo.bpp, bufferLen);
if (imageRawBuffer && userData) {
StereoImagePacketType *pack = (StereoImagePacketType*)userData;
OsdkOsal_MutexLock(pack->mutex);
pack->info = info;
if (pack->imageRawBuffer) OsdkOsal_Free(pack->imageRawBuffer);
pack->imageRawBuffer = (uint8_t *)OsdkOsal_Malloc(bufferLen);
memcpy(pack->imageRawBuffer, imageRawBuffer, bufferLen);
pack->gotData = true;
OsdkOsal_MutexUnlock(pack->mutex);
}
}
void PerceptionCamParamCB(Perception::CamParamPacketType pack,
void *userData) {
DSTATUS("stereo cam parameters : timestamp(%d) dirNum(%d)", pack.timeStamp, pack.directionNum);
if ((pack.directionNum > 0) && (pack.directionNum <= IMAGE_MAX_DIRECTION_NUM))
for (int i = 0 ; i < pack.directionNum; i++) {
DSTATUS("dir[%d] parameters :", pack.cameraParam[i].direction);
DSTATUS("\tleftIntrinsics \t= {%f, %f, %f, %f, %f, %f, %f, %f, %f }",
pack.cameraParam[i].leftIntrinsics[0],
pack.cameraParam[i].leftIntrinsics[1],
pack.cameraParam[i].leftIntrinsics[2],
pack.cameraParam[i].leftIntrinsics[3],
pack.cameraParam[i].leftIntrinsics[4],
pack.cameraParam[i].leftIntrinsics[5],
pack.cameraParam[i].leftIntrinsics[6],
pack.cameraParam[i].leftIntrinsics[7],
pack.cameraParam[i].leftIntrinsics[8]);
DSTATUS("\trightIntrinsics \t= {%f, %f, %f, %f, %f, %f, %f, %f, %f }",
pack.cameraParam[i].rightIntrinsics[0],
pack.cameraParam[i].rightIntrinsics[1],
pack.cameraParam[i].rightIntrinsics[2],
pack.cameraParam[i].rightIntrinsics[3],
pack.cameraParam[i].rightIntrinsics[4],
pack.cameraParam[i].rightIntrinsics[5],
pack.cameraParam[i].rightIntrinsics[6],
pack.cameraParam[i].rightIntrinsics[7],
pack.cameraParam[i].rightIntrinsics[8]);
DSTATUS("\trotaionLeftInRight \t= {%f, %f, %f, %f, %f, %f, %f, %f, %f }",
pack.cameraParam[i].rotaionLeftInRight[0],
pack.cameraParam[i].rotaionLeftInRight[1],
pack.cameraParam[i].rotaionLeftInRight[2],
pack.cameraParam[i].rotaionLeftInRight[3],
pack.cameraParam[i].rotaionLeftInRight[4],
pack.cameraParam[i].rotaionLeftInRight[5],
pack.cameraParam[i].rotaionLeftInRight[6],
pack.cameraParam[i].rotaionLeftInRight[7],
pack.cameraParam[i].rotaionLeftInRight[8]);
DSTATUS("\ttranslationLeftInRight \t= {%f, %f, %f }",
pack.cameraParam[i].translationLeftInRight[0],
pack.cameraParam[i].translationLeftInRight[1],
pack.cameraParam[i].translationLeftInRight[2]);
}
}
static void *stereoImagesDisplayTask(void *arg) {
while (1) {
OsdkOsal_TaskSleepMs(1);
if (arg) {
StereoImagePacketType *pack = (StereoImagePacketType*)arg;
#ifdef OPEN_CV_INSTALLED
char name[20] = {0};
/*! Get data here */
OsdkOsal_MutexLock(pack->mutex);
if (!pack->gotData) {
OsdkOsal_MutexUnlock(pack->mutex);
continue;
}
cv::Mat cv_img_stereo = cv::Mat(pack->info.rawInfo.height, pack->info.rawInfo.width, CV_8U);
int copySize = pack->info.rawInfo.height * pack->info.rawInfo.width;
if (pack->imageRawBuffer) {
memcpy(cv_img_stereo.data, pack->imageRawBuffer, copySize);
OsdkOsal_Free(pack->imageRawBuffer);
pack->imageRawBuffer = NULL;
}
sprintf(name, "Image_dataType : %d", pack->info.dataType);
pack->gotData = false;
OsdkOsal_MutexUnlock(pack->mutex);
/*! Using Opencv display here */
cv::imshow(name, cv_img_stereo);
cv::waitKey(1);
#else
OsdkOsal_MutexLock(pack->mutex);
if (!pack->gotData) {
OsdkOsal_MutexUnlock(pack->mutex);
continue;
}
int copySize = pack->info.rawInfo.height * pack->info.rawInfo.width;
uint8_t *imageRawBuffer = (uint8_t *)OsdkOsal_Malloc(copySize);
memcpy(imageRawBuffer, pack->imageRawBuffer, copySize);
OsdkOsal_MutexUnlock(pack->mutex);
DSTATUS("Save images at local path.");
writePictureData(imageRawBuffer, copySize);
OsdkOsal_Free(imageRawBuffer);
#endif
}
}
}
int
main(int argc, char** argv)
{
LinuxSetup linuxEnvironment(argc, argv);
Vehicle *vehicle = linuxEnvironment.getVehicle();
if (vehicle == NULL) {
std::cout << "Vehicle not initialized, exiting. \n";
return -1;
}
/*! Image Data Container */
StereoImagePacketType stereoImagePacket = {
.info = { 0 },
.imageRawBuffer = NULL,
.mutex = NULL,
.gotData = false };
/*! Display Thread */
static T_OsdkTaskHandle stereoImageThread;
OsdkOsal_MutexCreate(&stereoImagePacket.mutex);
OsdkOsal_TaskCreate(&stereoImageThread, stereoImagesDisplayTask,
OSDK_TASK_STACK_SIZE_DEFAULT, &stereoImagePacket);
char inputChar;
while(1) {
// Display interactive prompt
std::cout
<< "| Available commands: |"
<< std::endl;
std::cout
<< "| [a] Subscribe down stereo camera pair images |"
<< std::endl;
std::cout
<< "| [b] Subscribe front stereo camera pair images |"
<< std::endl;
std::cout
<< "| [c] Subscribe rear stereo camera pair images |"
<< std::endl;
std::cout
<< "| [d] Subscribe up stereo camera pair images |"
<< std::endl;
std::cout
<< "| [e] Subscribe left stereo camera pair images |"
<< std::endl;
std::cout
<< "| [f] Subscribe right stereo camera pair images |"
<< std::endl;
std::cout
<< "| [g] get the parameters of all the stereo cameras |"
<< std::endl;
std::cout
<< "| [q] quit |"
<< std::endl;
Perception::DirectionType direction = (Perception::DirectionType)0xFF;
std::cin >> inputChar;
switch (inputChar) {
case 'a':
DSTATUS("Subscribe down stereo camera pair images.");
direction = Perception::DirectionType::RECTIFY_DOWN;
break;
case 'b':
DSTATUS("Subscribe front stereo camera pair images.");
direction = Perception::DirectionType::RECTIFY_FRONT;
break;
case 'c':
DSTATUS("Subscribe rear stereo camera pair images.");
direction = Perception::DirectionType::RECTIFY_REAR;
break;
case 'd':
DSTATUS("Subscribe up stereo camera pair images.");
direction = Perception::DirectionType::RECTIFY_UP;
break;
case 'e':
DSTATUS("Subscribe left stereo camera pair images.");
direction = Perception::DirectionType::RECTIFY_LEFT;
break;
case 'f':
DSTATUS("Subscribe right stereo camera pair images.");
direction = Perception::DirectionType::RECTIFY_RIGHT;
break;
case 'g':
DSTATUS("Do stereo camera parameters subscription");
vehicle->advancedSensing->setStereoCamParamsObserver(PerceptionCamParamCB, NULL);
vehicle->advancedSensing->triggerStereoCamParamsPushing();
break;
case 'q':return 0;
default:break;
}
if (direction != (Perception::DirectionType)0xFF) {
OsdkOsal_TaskSleepMs(1000);
DSTATUS("Do stereo camera imagines subscription");
vehicle->advancedSensing->subscribePerceptionImage(direction, PerceptionImageCB, &stereoImagePacket);
OsdkOsal_TaskSleepMs(10000);
vehicle->advancedSensing->unsubscribePerceptionImage(direction);
OsdkOsal_TaskSleepMs(1000);
}
}
OsdkOsal_MutexDestroy(stereoImagePacket.mutex);
return 0;
}