main.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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_TriggerMode21 [-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 21");
  120. TY_TRIGGER_PARAM trigger;
  121. trigger.mode = TY_TRIGGER_MODE_TIMER_PERIOD;
  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 period");
  143. TY_TRIGGER_TIMER_PERIOD period_timer;
  144. period_timer.start_time_us = (getSystemTime() + 3000) * 1000;
  145. period_timer.trigger_count = 10;
  146. period_timer.period_us = 1000000;
  147. ASSERT_OK(TYSetStruct(hDevice, TY_COMPONENT_DEVICE, TY_STRUCT_TRIGGER_TIMER_PERIOD, &period_timer, sizeof(period_timer)));
  148. while (!exit_main) {
  149. int err = TYFetchFrame(hDevice, &frame, 20000);
  150. if (err == TY_STATUS_OK) {
  151. LOGD("=== Get frame %d (%" PRIu64 ")", ++index, frame.image[0].timestamp);
  152. #if 0
  153. int fps = get_fps();
  154. if (fps > 0) {
  155. LOGI("fps: %d", fps);
  156. }
  157. cv::Mat depth, irl, irr, color;
  158. parseFrame(frame, &depth, &irl, &irr, &color);
  159. if (!depth.empty()) {
  160. depthViewer.show(depth);
  161. }
  162. if (!irl.empty()) { cv::imshow("LeftIR", irl); }
  163. if (!irr.empty()) { cv::imshow("RightIR", irr); }
  164. if (!color.empty()) { cv::imshow("Color", color); }
  165. int key = cv::waitKey(1);
  166. switch (key & 0xff) {
  167. case 0xff:
  168. break;
  169. case 'q':
  170. exit_main = true;
  171. break;
  172. default:
  173. LOGD("Unmapped key %d", key);
  174. }
  175. #endif
  176. LOGD("=== Re-enqueue buffer(%p, %d)"
  177. , frame.userBuffer, frame.bufferSize);
  178. ASSERT_OK(TYEnqueueBuffer(hDevice, frame.userBuffer, frame.bufferSize));
  179. cnt++;
  180. } else {
  181. LOGD("=== TIMEOUT DROP FRAME!!!!!");
  182. }
  183. if (cnt == period_timer.trigger_count) {
  184. LOGD("=== Set trigger timer period");
  185. period_timer.start_time_us = (getSystemTime() + 3000) * 1000;
  186. period_timer.trigger_count = 10;
  187. period_timer.period_us = 1000000;
  188. ASSERT_OK(TYSetStruct(hDevice, TY_COMPONENT_DEVICE, TY_STRUCT_TRIGGER_TIMER_PERIOD, &period_timer, sizeof(period_timer)));
  189. cnt = 0;
  190. }
  191. }
  192. ASSERT_OK( TYStopCapture(hDevice) );
  193. ASSERT_OK( TYCloseDevice(hDevice) );
  194. ASSERT_OK( TYCloseInterface(hIface) );
  195. ASSERT_OK( TYDeinitLib() );
  196. delete frameBuffer[0];
  197. delete frameBuffer[1];
  198. LOGD("=== Main done!");
  199. return 0;
  200. }