main.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. #include "../common/common.hpp"
  2. #include <inttypes.h>
  3. void eventCallback(TY_EVENT_INFO* event_info, void* userdata)
  4. {
  5. if (event_info->eventId == TY_EVENT_DEVICE_OFFLINE) {
  6. LOGD("=== Event Callback: Device Offline!");
  7. // Note:
  8. // Please set TY_BOOL_KEEP_ALIVE_ONOFF feature to false if you need to debug with breakpoint!
  9. }
  10. else if (event_info->eventId == TY_EVENT_LICENSE_ERROR) {
  11. LOGD("=== Event Callback: License Error!");
  12. }
  13. }
  14. void imuCallback(TY_IMU_DATA* imu_data, void* userdata)
  15. {
  16. static int imu_data_count = 0;
  17. imu_data_count++;
  18. if ((imu_data_count % 400) == 0) {
  19. LOGD("[COUNT] %d [ACC] %4.2f %4.2f %4.2f [GYRO] %4.2f %4.2f %4.2f [TEMP] %4.2f [TS] %" PRIu64 "",
  20. imu_data_count,
  21. imu_data->acc_x,
  22. imu_data->acc_y,
  23. imu_data->acc_z,
  24. imu_data->gyro_x,
  25. imu_data->gyro_y,
  26. imu_data->gyro_z,
  27. imu_data->temperature,
  28. imu_data->timestamp);
  29. }
  30. }
  31. int main(int argc, char* argv[])
  32. {
  33. std::string ID, IP;
  34. TY_INTERFACE_HANDLE hIface = NULL;
  35. TY_ISP_HANDLE hColorIspHandle = NULL;
  36. TY_DEV_HANDLE hDevice = NULL;
  37. int32_t color, ir, depth;
  38. color = ir = depth = 1;
  39. for (int i = 1; i < argc; i++) {
  40. if (strcmp(argv[i], "-id") == 0) {
  41. ID = argv[++i];
  42. }
  43. else if (strcmp(argv[i], "-ip") == 0) {
  44. IP = argv[++i];
  45. }
  46. else if (strcmp(argv[i], "-color=off") == 0) {
  47. color = 0;
  48. }
  49. else if (strcmp(argv[i], "-depth=off") == 0) {
  50. depth = 0;
  51. }
  52. else if (strcmp(argv[i], "-ir=off") == 0) {
  53. ir = 0;
  54. }
  55. else if (strcmp(argv[i], "-h") == 0) {
  56. LOGI("Usage: SimpleView_ImuData [-h] [-id <ID>] [-ip <IP>]");
  57. return 0;
  58. }
  59. }
  60. if (!color && !depth && !ir) {
  61. LOGD("At least one component need to be on");
  62. return -1;
  63. }
  64. LOGD("Init lib");
  65. ASSERT_OK(TYInitLib());
  66. TY_VERSION_INFO ver;
  67. ASSERT_OK(TYLibVersion(&ver));
  68. LOGD(" - lib version: %d.%d.%d", ver.major, ver.minor, ver.patch);
  69. std::vector<TY_DEVICE_BASE_INFO> selected;
  70. ASSERT_OK(selectDevice(TY_INTERFACE_ALL, ID, IP, 1, selected));
  71. ASSERT(selected.size() > 0);
  72. TY_DEVICE_BASE_INFO& selectedDev = selected[0];
  73. ASSERT_OK(TYOpenInterface(selectedDev.iface.id, &hIface));
  74. ASSERT_OK(TYOpenDevice(hIface, selectedDev.id, &hDevice));
  75. TY_COMPONENT_ID allComps;
  76. ASSERT_OK(TYGetComponentIDs(hDevice, &allComps));
  77. ///try to enable color camera
  78. if (allComps & TY_COMPONENT_RGB_CAM && color) {
  79. LOGD("Has RGB camera, open RGB cam");
  80. ASSERT_OK(TYEnableComponents(hDevice, TY_COMPONENT_RGB_CAM));
  81. //create a isp handle to convert raw image(color bayer format) to rgb image
  82. ASSERT_OK(TYISPCreate(&hColorIspHandle));
  83. //Init code can be modified in common.hpp
  84. //NOTE: Should set RGB image format & size before init ISP
  85. ASSERT_OK(ColorIspInitSetting(hColorIspHandle, hDevice));
  86. //You can call follow function to show color isp supported features
  87. #if 0
  88. ColorIspShowSupportedFeatures(hColorIspHandle);
  89. #endif
  90. //You can turn on auto exposure function as follow ,but frame rate may reduce .
  91. //Device may be casually stucked 1~2 seconds while software is trying to adjust device exposure time value
  92. #if 0
  93. ASSERT_OK(ColorIspInitAutoExposure(hColorIspHandle, hDevice));
  94. #endif
  95. }
  96. if (allComps & TY_COMPONENT_IR_CAM_LEFT && ir) {
  97. LOGD("Has IR left camera, open IR left cam");
  98. ASSERT_OK(TYEnableComponents(hDevice, TY_COMPONENT_IR_CAM_LEFT));
  99. }
  100. if (allComps & TY_COMPONENT_IR_CAM_RIGHT && ir) {
  101. LOGD("Has IR right camera, open IR right cam");
  102. ASSERT_OK(TYEnableComponents(hDevice, TY_COMPONENT_IR_CAM_RIGHT));
  103. }
  104. //try to enable depth map
  105. LOGD("Configure components, open depth cam");
  106. DepthViewer depthViewer("Depth");
  107. if (allComps & TY_COMPONENT_DEPTH_CAM && depth) {
  108. TY_IMAGE_MODE image_mode;
  109. ASSERT_OK(get_default_image_mode(hDevice, TY_COMPONENT_DEPTH_CAM, image_mode));
  110. LOGD("Select Depth Image Mode: %dx%d", TYImageWidth(image_mode), TYImageHeight(image_mode));
  111. ASSERT_OK(TYSetEnum(hDevice, TY_COMPONENT_DEPTH_CAM, TY_ENUM_IMAGE_MODE, image_mode));
  112. ASSERT_OK(TYEnableComponents(hDevice, TY_COMPONENT_DEPTH_CAM));
  113. //depth map pixel format is uint16_t ,which default unit is 1 mm
  114. //the acutal depth (mm)= PixelValue * ScaleUnit
  115. float scale_unit = 1.;
  116. TYGetFloat(hDevice, TY_COMPONENT_DEPTH_CAM, TY_FLOAT_SCALE_UNIT, &scale_unit);
  117. depthViewer.depth_scale_unit = scale_unit;
  118. }
  119. LOGD("Prepare image buffer");
  120. uint32_t frameSize;
  121. ASSERT_OK(TYGetFrameBufferSize(hDevice, &frameSize));
  122. LOGD(" - Get size of framebuffer, %d", frameSize);
  123. LOGD(" - Allocate & enqueue buffers");
  124. char* frameBuffer[2];
  125. frameBuffer[0] = new char[frameSize];
  126. frameBuffer[1] = new char[frameSize];
  127. LOGD(" - Enqueue buffer (%p, %d)", frameBuffer[0], frameSize);
  128. ASSERT_OK(TYEnqueueBuffer(hDevice, frameBuffer[0], frameSize));
  129. LOGD(" - Enqueue buffer (%p, %d)", frameBuffer[1], frameSize);
  130. ASSERT_OK(TYEnqueueBuffer(hDevice, frameBuffer[1], frameSize));
  131. LOGD("Register event callback");
  132. ASSERT_OK(TYRegisterEventCallback(hDevice, eventCallback, NULL));
  133. bool hasTrigger;
  134. ASSERT_OK(TYHasFeature(hDevice, TY_COMPONENT_DEVICE, TY_STRUCT_TRIGGER_PARAM, &hasTrigger));
  135. if (hasTrigger) {
  136. LOGD("Disable trigger mode");
  137. TY_TRIGGER_PARAM trigger;
  138. trigger.mode = TY_TRIGGER_MODE_OFF;
  139. ASSERT_OK(TYSetStruct(hDevice, TY_COMPONENT_DEVICE, TY_STRUCT_TRIGGER_PARAM, &trigger, sizeof(trigger)));
  140. }
  141. LOGD("=== Set IMU data onoff");
  142. ASSERT_OK(TYSetBool(hDevice, TY_COMPONENT_IMU, TY_BOOL_IMU_DATA_ONOFF, true));
  143. LOGD("=== Register IMU callback");
  144. ASSERT_OK(TYRegisterImuCallback(hDevice, imuCallback, NULL));
  145. TY_ACC_BIAS acc_bias;
  146. ASSERT_OK(TYGetStruct(hDevice, TY_COMPONENT_IMU, TY_STRUCT_IMU_ACC_BIAS
  147. , &acc_bias, sizeof(TY_ACC_BIAS)));
  148. LOGD("acc_bias: %f %f %f",
  149. acc_bias.data[0],
  150. acc_bias.data[1],
  151. acc_bias.data[2]);
  152. TY_ACC_MISALIGNMENT acc_misalignment;
  153. ASSERT_OK(TYGetStruct(hDevice, TY_COMPONENT_IMU, TY_STRUCT_IMU_ACC_MISALIGNMENT
  154. , &acc_misalignment, sizeof(TY_ACC_MISALIGNMENT)));
  155. LOGD("acc_misalignment: %f %f %f %f %f %f %f %f %f",
  156. acc_misalignment.data[0],
  157. acc_misalignment.data[1],
  158. acc_misalignment.data[2],
  159. acc_misalignment.data[3],
  160. acc_misalignment.data[4],
  161. acc_misalignment.data[5],
  162. acc_misalignment.data[6],
  163. acc_misalignment.data[7],
  164. acc_misalignment.data[8]);
  165. TY_ACC_SCALE acc_scale;
  166. ASSERT_OK(TYGetStruct(hDevice, TY_COMPONENT_IMU, TY_STRUCT_IMU_ACC_SCALE
  167. , &acc_scale, sizeof(TY_ACC_SCALE)));
  168. LOGD("acc_scale: %f %f %f %f %f %f %f %f %f",
  169. acc_scale.data[0],
  170. acc_scale.data[1],
  171. acc_scale.data[2],
  172. acc_scale.data[3],
  173. acc_scale.data[4],
  174. acc_scale.data[5],
  175. acc_scale.data[6],
  176. acc_scale.data[7],
  177. acc_scale.data[8]);
  178. TY_GYRO_BIAS gyro_bias;
  179. ASSERT_OK(TYGetStruct(hDevice, TY_COMPONENT_IMU, TY_STRUCT_IMU_GYRO_BIAS
  180. , &gyro_bias, sizeof(TY_GYRO_BIAS)));
  181. LOGD("gyro_bias: %f %f %f",
  182. gyro_bias.data[0],
  183. gyro_bias.data[1],
  184. gyro_bias.data[2]);
  185. TY_GYRO_MISALIGNMENT gyro_misalignment;
  186. ASSERT_OK(TYGetStruct(hDevice, TY_COMPONENT_IMU, TY_STRUCT_IMU_GYRO_MISALIGNMENT
  187. , &gyro_misalignment, sizeof(TY_GYRO_MISALIGNMENT)));
  188. LOGD("gyro_misalignment: %f %f %f %f %f %f %f %f %f",
  189. gyro_misalignment.data[0],
  190. gyro_misalignment.data[1],
  191. gyro_misalignment.data[2],
  192. gyro_misalignment.data[3],
  193. gyro_misalignment.data[4],
  194. gyro_misalignment.data[5],
  195. gyro_misalignment.data[6],
  196. gyro_misalignment.data[7],
  197. gyro_misalignment.data[8]);
  198. TY_GYRO_SCALE gyro_scale;
  199. ASSERT_OK(TYGetStruct(hDevice, TY_COMPONENT_IMU, TY_STRUCT_IMU_GYRO_SCALE
  200. , &gyro_scale, sizeof(TY_GYRO_SCALE)));
  201. LOGD("gyro_scale: %f %f %f %f %f %f %f %f %f",
  202. gyro_scale.data[0],
  203. gyro_scale.data[1],
  204. gyro_scale.data[2],
  205. gyro_scale.data[3],
  206. gyro_scale.data[4],
  207. gyro_scale.data[5],
  208. gyro_scale.data[6],
  209. gyro_scale.data[7],
  210. gyro_scale.data[8]);
  211. TY_CAMERA_TO_IMU cam_to_imu;
  212. ASSERT_OK(TYGetStruct(hDevice, TY_COMPONENT_IMU, TY_STRUCT_IMU_CAM_TO_IMU
  213. , &cam_to_imu, sizeof(TY_CAMERA_TO_IMU)));
  214. LOGD("cam_to_imu: %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f",
  215. cam_to_imu.data[0],
  216. cam_to_imu.data[1],
  217. cam_to_imu.data[2],
  218. cam_to_imu.data[3],
  219. cam_to_imu.data[4],
  220. cam_to_imu.data[5],
  221. cam_to_imu.data[6],
  222. cam_to_imu.data[7],
  223. cam_to_imu.data[8],
  224. cam_to_imu.data[9],
  225. cam_to_imu.data[10],
  226. cam_to_imu.data[11],
  227. cam_to_imu.data[12],
  228. cam_to_imu.data[13],
  229. cam_to_imu.data[14],
  230. cam_to_imu.data[15]);
  231. LOGD("Start capture");
  232. ASSERT_OK(TYStartCapture(hDevice));
  233. LOGD("While loop to fetch frame");
  234. bool exit_main = false;
  235. TY_FRAME_DATA frame;
  236. int index = 0;
  237. while (!exit_main) {
  238. int err = TYFetchFrame(hDevice, &frame, -1);
  239. if (err == TY_STATUS_OK) {
  240. LOGD("=== Get frame %d(%" PRIu64 ")", ++index, frame.image[0].timestamp);
  241. int fps = get_fps();
  242. if (fps > 0) {
  243. LOGI("fps: %d", fps);
  244. }
  245. cv::Mat depth, irl, irr, color;
  246. parseFrame(frame, &depth, &irl, &irr, &color, hColorIspHandle);
  247. if (!depth.empty()) {
  248. depthViewer.show(depth);
  249. }
  250. if (!irl.empty()) { cv::imshow("LeftIR", irl); }
  251. if (!irr.empty()) { cv::imshow("RightIR", irr); }
  252. if (!color.empty()) { cv::imshow("Color", color); }
  253. int key = cv::waitKey(1);
  254. switch (key & 0xff) {
  255. case 0xff:
  256. break;
  257. case 'q':
  258. exit_main = true;
  259. break;
  260. default:
  261. LOGD("Unmapped key %d", key);
  262. }
  263. TYISPUpdateDevice(hColorIspHandle);
  264. ASSERT_OK(TYEnqueueBuffer(hDevice, frame.userBuffer, frame.bufferSize));
  265. }
  266. }
  267. ASSERT_OK(TYStopCapture(hDevice));
  268. ASSERT_OK(TYCloseDevice(hDevice));
  269. ASSERT_OK(TYCloseInterface(hIface));
  270. ASSERT_OK(TYISPRelease(&hColorIspHandle));
  271. ASSERT_OK(TYDeinitLib());
  272. delete frameBuffer[0];
  273. delete frameBuffer[1];
  274. LOGD("Main done!");
  275. return 0;
  276. }