main.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #include "../common/common.hpp"
  2. void eventCallback(TY_EVENT_INFO *event_info, void *userdata)
  3. {
  4. if (event_info->eventId == TY_EVENT_DEVICE_OFFLINE) {
  5. LOGD("=== Event Callback: Device Offline!");
  6. // Note:
  7. // Please set TY_BOOL_KEEP_ALIVE_ONOFF feature to false if you need to debug with breakpoint!
  8. }
  9. else if (event_info->eventId == TY_EVENT_LICENSE_ERROR) {
  10. LOGD("=== Event Callback: License Error!");
  11. }
  12. }
  13. int main(int argc, char* argv[])
  14. {
  15. std::string ID, IP;
  16. TY_INTERFACE_HANDLE hIface = NULL;
  17. TY_DEV_HANDLE hDevice = NULL;
  18. int32_t color, ir, depth;
  19. color = ir = depth = 1;
  20. int32_t resend = 1;
  21. for(int i = 1; i < argc; i++) {
  22. if(strcmp(argv[i], "-id") == 0){
  23. ID = argv[++i];
  24. } else if(strcmp(argv[i], "-ip") == 0) {
  25. IP = argv[++i];
  26. } else if(strcmp(argv[i], "-color=off") == 0) {
  27. color = 0;
  28. } else if(strcmp(argv[i], "-depth=off") == 0) {
  29. depth = 0;
  30. } else if(strcmp(argv[i], "-ir=off") == 0) {
  31. ir = 0;
  32. } else if (strcmp(argv[i], "-resend=off") == 0) {
  33. resend = 0;
  34. } else if(strcmp(argv[i], "-h") == 0) {
  35. LOGI("Usage: SimpleView_TriggerMode20 [-h] [-id <ID>] [-ip <IP>]");
  36. return 0;
  37. }
  38. }
  39. if (!color && !depth && !ir) {
  40. LOGD("=== At least one component need to be on");
  41. return -1;
  42. }
  43. LOGD("=== Init lib");
  44. ASSERT_OK( TYInitLib() );
  45. TY_VERSION_INFO ver;
  46. ASSERT_OK( TYLibVersion(&ver) );
  47. LOGD(" - lib version: %d.%d.%d", ver.major, ver.minor, ver.patch);
  48. std::vector<TY_DEVICE_BASE_INFO> selected;
  49. ASSERT_OK( selectDevice(TY_INTERFACE_ALL, ID, IP, 1, selected) );
  50. ASSERT(selected.size() > 0);
  51. TY_DEVICE_BASE_INFO& selectedDev = selected[0];
  52. ASSERT_OK( TYOpenInterface(selectedDev.iface.id, &hIface) );
  53. ASSERT_OK( TYOpenDevice(hIface, selectedDev.id, &hDevice) );
  54. LOGD("Set type of time sync mechanism");
  55. ASSERT_OK(TYSetEnum(hDevice, TY_COMPONENT_DEVICE, TY_ENUM_TIME_SYNC_TYPE, TY_TIME_SYNC_TYPE_HOST));
  56. LOGD("Wait for time sync ready");
  57. while (1) {
  58. bool sync_ready;
  59. ASSERT_OK(TYGetBool(hDevice, TY_COMPONENT_DEVICE, TY_BOOL_TIME_SYNC_READY, &sync_ready));
  60. if (sync_ready) {
  61. break;
  62. }
  63. MSLEEP(10);
  64. }
  65. ASSERT_OK(TYSetEnum(hDevice, TY_COMPONENT_DEVICE, TY_ENUM_TIME_SYNC_TYPE, TY_TIME_SYNC_TYPE_PTP));
  66. LOGD("Wait for time sync ready");
  67. while (1) {
  68. bool sync_ready;
  69. ASSERT_OK(TYGetBool(hDevice, TY_COMPONENT_DEVICE, TY_BOOL_TIME_SYNC_READY, &sync_ready));
  70. if (sync_ready) {
  71. break;
  72. }
  73. MSLEEP(10);
  74. }
  75. TY_COMPONENT_ID allComps;
  76. ASSERT_OK( TYGetComponentIDs(hDevice, &allComps) );
  77. if(allComps & TY_COMPONENT_RGB_CAM && color) {
  78. LOGD("=== Has RGB camera, open RGB cam");
  79. ASSERT_OK( TYEnableComponents(hDevice, TY_COMPONENT_RGB_CAM) );
  80. }
  81. TY_COMPONENT_ID componentIDs = 0;
  82. LOGD("=== Configure components, open depth cam");
  83. if (depth) {
  84. componentIDs = TY_COMPONENT_DEPTH_CAM;
  85. }
  86. if (ir) {
  87. componentIDs |= TY_COMPONENT_IR_CAM_LEFT;
  88. }
  89. ASSERT_OK( TYEnableComponents(hDevice, componentIDs) );
  90. //try to enable depth map
  91. LOGD("Configure components, open depth cam");
  92. DepthViewer depthViewer("Depth");
  93. if (allComps & TY_COMPONENT_DEPTH_CAM && depth) {
  94. TY_IMAGE_MODE image_mode;
  95. ASSERT_OK(get_default_image_mode(hDevice, TY_COMPONENT_DEPTH_CAM, image_mode));
  96. LOGD("Select Depth Image Mode: %dx%d", TYImageWidth(image_mode), TYImageHeight(image_mode));
  97. ASSERT_OK(TYSetEnum(hDevice, TY_COMPONENT_DEPTH_CAM, TY_ENUM_IMAGE_MODE, image_mode));
  98. ASSERT_OK(TYEnableComponents(hDevice, TY_COMPONENT_DEPTH_CAM));
  99. //depth map pixel format is uint16_t ,which default unit is 1 mm
  100. //the acutal depth (mm)= PixelValue * ScaleUnit
  101. float scale_unit = 1.;
  102. TYGetFloat(hDevice, TY_COMPONENT_DEPTH_CAM, TY_FLOAT_SCALE_UNIT, &scale_unit);
  103. depthViewer.depth_scale_unit = scale_unit;
  104. }
  105. LOGD("=== Prepare image buffer");
  106. uint32_t frameSize;
  107. ASSERT_OK( TYGetFrameBufferSize(hDevice, &frameSize) );
  108. LOGD(" - Get size of framebuffer, %d", frameSize);
  109. LOGD(" - Allocate & enqueue buffers");
  110. char* frameBuffer[2];
  111. frameBuffer[0] = new char[frameSize];
  112. frameBuffer[1] = new char[frameSize];
  113. LOGD(" - Enqueue buffer (%p, %d)", frameBuffer[0], frameSize);
  114. ASSERT_OK( TYEnqueueBuffer(hDevice, frameBuffer[0], frameSize) );
  115. LOGD(" - Enqueue buffer (%p, %d)", frameBuffer[1], frameSize);
  116. ASSERT_OK( TYEnqueueBuffer(hDevice, frameBuffer[1], frameSize) );
  117. LOGD("=== Register event callback");
  118. ASSERT_OK(TYRegisterEventCallback(hDevice, eventCallback, NULL));
  119. LOGD("=== Set trigger to trig mode 20");
  120. TY_TRIGGER_PARAM trigger;
  121. trigger.mode = TY_TRIGGER_MODE_TIMER_LIST;
  122. ASSERT_OK(TYSetStruct(hDevice, TY_COMPONENT_DEVICE, TY_STRUCT_TRIGGER_PARAM, &trigger, sizeof(trigger)));
  123. //for network only
  124. LOGD("=== resend: %d", resend);
  125. if (resend) {
  126. bool hasResend;
  127. ASSERT_OK(TYHasFeature(hDevice, TY_COMPONENT_DEVICE, TY_BOOL_GVSP_RESEND, &hasResend));
  128. if (hasResend) {
  129. LOGD("=== Open resend");
  130. ASSERT_OK(TYSetBool(hDevice, TY_COMPONENT_DEVICE, TY_BOOL_GVSP_RESEND, true));
  131. } else {
  132. LOGD("=== Not support feature TY_BOOL_GVSP_RESEND");
  133. }
  134. }
  135. LOGD("=== Start capture");
  136. ASSERT_OK( TYStartCapture(hDevice) );
  137. LOGD("=== While loop to fetch frame");
  138. bool exit_main = false;
  139. TY_FRAME_DATA frame;
  140. int index = 0;
  141. int cnt = 0;
  142. LOGD("=== Set trigger timer list");
  143. TY_TRIGGER_TIMER_LIST list_timer;
  144. list_timer.start_time_us = (getSystemTime() + 3000) * 1000;
  145. list_timer.offset_us_count = 4;
  146. list_timer.offset_us_list[0] = 1000000;
  147. list_timer.offset_us_list[1] = 1000000;
  148. list_timer.offset_us_list[2] = 1000000;
  149. list_timer.offset_us_list[3] = 1000000;
  150. ASSERT_OK(TYSetStruct(hDevice, TY_COMPONENT_DEVICE, TY_STRUCT_TRIGGER_TIMER_LIST, &list_timer, sizeof(list_timer)));
  151. while (!exit_main) {
  152. int err = TYFetchFrame(hDevice, &frame, 20000);
  153. if (err == TY_STATUS_OK) {
  154. LOGD("=== Get frame %d (%" PRIu64 ")", ++index, frame.image[0].timestamp);
  155. #if 0
  156. int fps = get_fps();
  157. if (fps > 0) {
  158. LOGI("fps: %d", fps);
  159. }
  160. cv::Mat depth, irl, irr, color;
  161. parseFrame(frame, &depth, &irl, &irr, &color);
  162. if (!depth.empty()) {
  163. depthViewer.show(depth);
  164. }
  165. if (!irl.empty()) { cv::imshow("LeftIR", irl); }
  166. if (!irr.empty()) { cv::imshow("RightIR", irr); }
  167. if (!color.empty()) { cv::imshow("Color", color); }
  168. int key = cv::waitKey(1);
  169. switch (key & 0xff) {
  170. case 0xff:
  171. break;
  172. case 'q':
  173. exit_main = true;
  174. break;
  175. default:
  176. LOGD("Unmapped key %d", key);
  177. }
  178. #endif
  179. LOGD("=== Re-enqueue buffer(%p, %d)"
  180. , frame.userBuffer, frame.bufferSize);
  181. ASSERT_OK(TYEnqueueBuffer(hDevice, frame.userBuffer, frame.bufferSize));
  182. cnt++;
  183. } else {
  184. LOGD("=== TIMEOUT DROP FRAME!!!!!");
  185. }
  186. if (cnt == list_timer.offset_us_count + 1) {
  187. LOGD("=== Set trigger timer list");
  188. list_timer.start_time_us = (getSystemTime() + 3000) * 1000;
  189. list_timer.offset_us_count = 4;
  190. list_timer.offset_us_list[0] = 1000000;
  191. list_timer.offset_us_list[1] = 1000000;
  192. list_timer.offset_us_list[2] = 1000000;
  193. list_timer.offset_us_list[3] = 1000000;
  194. ASSERT_OK(TYSetStruct(hDevice, TY_COMPONENT_DEVICE, TY_STRUCT_TRIGGER_TIMER_LIST, &list_timer, sizeof(list_timer)));
  195. cnt = 0;
  196. }
  197. }
  198. ASSERT_OK( TYStopCapture(hDevice) );
  199. ASSERT_OK( TYCloseDevice(hDevice) );
  200. ASSERT_OK( TYCloseInterface(hIface) );
  201. ASSERT_OK( TYDeinitLib() );
  202. delete frameBuffer[0];
  203. delete frameBuffer[1];
  204. LOGD("=== Main done!");
  205. return 0;
  206. }