main.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 = 0;
  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=on") == 0) {
  33. resend = 1;
  34. } else if(strcmp(argv[i], "-h") == 0) {
  35. LOGI("Usage: SimpleView_TriggerDelay [-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. TY_COMPONENT_ID allComps;
  55. ASSERT_OK( TYGetComponentIDs(hDevice, &allComps) );
  56. if(allComps & TY_COMPONENT_RGB_CAM && color) {
  57. LOGD("=== Has RGB camera, open RGB cam");
  58. ASSERT_OK( TYEnableComponents(hDevice, TY_COMPONENT_RGB_CAM) );
  59. }
  60. TY_COMPONENT_ID componentIDs = 0;
  61. LOGD("=== Configure components, open depth cam");
  62. if (depth) {
  63. componentIDs = TY_COMPONENT_DEPTH_CAM;
  64. }
  65. if (ir) {
  66. componentIDs |= TY_COMPONENT_IR_CAM_LEFT;
  67. }
  68. ASSERT_OK( TYEnableComponents(hDevice, componentIDs) );
  69. //try to enable depth map
  70. LOGD("Configure components, open depth cam");
  71. DepthViewer depthViewer("Depth");
  72. if (allComps & TY_COMPONENT_DEPTH_CAM && depth) {
  73. TY_IMAGE_MODE image_mode;
  74. ASSERT_OK(get_default_image_mode(hDevice, TY_COMPONENT_DEPTH_CAM, image_mode));
  75. LOGD("Select Depth Image Mode: %dx%d", TYImageWidth(image_mode), TYImageHeight(image_mode));
  76. ASSERT_OK(TYSetEnum(hDevice, TY_COMPONENT_DEPTH_CAM, TY_ENUM_IMAGE_MODE, image_mode));
  77. ASSERT_OK(TYEnableComponents(hDevice, TY_COMPONENT_DEPTH_CAM));
  78. //depth map pixel format is uint16_t ,which default unit is 1 mm
  79. //the acutal depth (mm)= PixelValue * ScaleUnit
  80. float scale_unit = 1.;
  81. TYGetFloat(hDevice, TY_COMPONENT_DEPTH_CAM, TY_FLOAT_SCALE_UNIT, &scale_unit);
  82. depthViewer.depth_scale_unit = scale_unit;
  83. }
  84. LOGD("=== Prepare image buffer");
  85. uint32_t frameSize;
  86. ASSERT_OK( TYGetFrameBufferSize(hDevice, &frameSize) );
  87. LOGD(" - Get size of framebuffer, %d", frameSize);
  88. LOGD(" - Allocate & enqueue buffers");
  89. char* frameBuffer[2];
  90. frameBuffer[0] = new char[frameSize];
  91. frameBuffer[1] = new char[frameSize];
  92. LOGD(" - Enqueue buffer (%p, %d)", frameBuffer[0], frameSize);
  93. ASSERT_OK( TYEnqueueBuffer(hDevice, frameBuffer[0], frameSize) );
  94. LOGD(" - Enqueue buffer (%p, %d)", frameBuffer[1], frameSize);
  95. ASSERT_OK( TYEnqueueBuffer(hDevice, frameBuffer[1], frameSize) );
  96. LOGD("=== Register event callback");
  97. ASSERT_OK(TYRegisterEventCallback(hDevice, eventCallback, NULL));
  98. LOGD("=== Set trigger to slave mode");
  99. TY_TRIGGER_PARAM trigger;
  100. trigger.mode = TY_TRIGGER_MODE_SLAVE;
  101. ASSERT_OK(TYSetStruct(hDevice, TY_COMPONENT_DEVICE, TY_STRUCT_TRIGGER_PARAM, &trigger, sizeof(trigger)));
  102. //for network only
  103. LOGD("=== resend: %d", resend);
  104. if (resend) {
  105. bool hasResend;
  106. ASSERT_OK(TYHasFeature(hDevice, TY_COMPONENT_DEVICE, TY_BOOL_GVSP_RESEND, &hasResend));
  107. if (hasResend) {
  108. LOGD("=== Open resend");
  109. ASSERT_OK(TYSetBool(hDevice, TY_COMPONENT_DEVICE, TY_BOOL_GVSP_RESEND, true));
  110. } else {
  111. LOGD("=== Not support feature TY_BOOL_GVSP_RESEND");
  112. }
  113. }
  114. //notice: trigger delay only be enabled in trigger salve mode and only work for hardware trigger.
  115. // delay time unit is microsecond, the maximum value is 1.3s
  116. int32_t time = 1000;
  117. ASSERT_OK(TYSetInt(hDevice, TY_COMPONENT_DEVICE, TY_INT_TRIGGER_DELAY_US, time));
  118. LOGD("=== Start capture");
  119. ASSERT_OK( TYStartCapture(hDevice) );
  120. LOGD("=== While loop to fetch frame");
  121. bool exit_main = false;
  122. TY_FRAME_DATA frame;
  123. int index = 0;
  124. while(!exit_main) {
  125. int err = TYFetchFrame(hDevice, &frame, 20000);
  126. if( err == TY_STATUS_OK ) {
  127. LOGD("=== Get frame %d", ++index);
  128. int fps = get_fps();
  129. if (fps > 0){
  130. LOGI("fps: %d", fps);
  131. }
  132. cv::Mat depth, irl, irr, color;
  133. parseFrame(frame, &depth, &irl, &irr, &color);
  134. if(!depth.empty()){
  135. depthViewer.show(depth);
  136. }
  137. if(!irl.empty()){ cv::imshow("LeftIR", irl); }
  138. if(!irr.empty()){ cv::imshow("RightIR", irr); }
  139. if(!color.empty()){ cv::imshow("Color", color); }
  140. int key = cv::waitKey(1);
  141. switch(key & 0xff) {
  142. case 0xff:
  143. break;
  144. case 'q':
  145. exit_main = true;
  146. break;
  147. default:
  148. LOGD("Unmapped key %d", key);
  149. }
  150. LOGD("=== Re-enqueue buffer(%p, %d)"
  151. , frame.userBuffer, frame.bufferSize);
  152. ASSERT_OK( TYEnqueueBuffer(hDevice, frame.userBuffer, frame.bufferSize) );
  153. }
  154. }
  155. ASSERT_OK( TYStopCapture(hDevice) );
  156. ASSERT_OK( TYCloseDevice(hDevice) );
  157. ASSERT_OK( TYCloseInterface(hIface) );
  158. ASSERT_OK( TYDeinitLib() );
  159. delete frameBuffer[0];
  160. delete frameBuffer[1];
  161. LOGD("=== Main done!");
  162. return 0;
  163. }