main.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. for(int i = 1; i < argc; i++) {
  21. if(strcmp(argv[i], "-id") == 0){
  22. ID = argv[++i];
  23. } else if(strcmp(argv[i], "-ip") == 0) {
  24. IP = argv[++i];
  25. } else if(strcmp(argv[i], "-color=off") == 0) {
  26. color = 0;
  27. } else if(strcmp(argv[i], "-depth=off") == 0) {
  28. depth = 0;
  29. } else if(strcmp(argv[i], "-ir=off") == 0) {
  30. ir = 0;
  31. } else if(strcmp(argv[i], "-h") == 0) {
  32. LOGI("Usage: SimpleView_TriggerMode0 [-h] [-id <ID>] [-ip <IP>]");
  33. return 0;
  34. }
  35. }
  36. if (!color && !depth && !ir) {
  37. LOGD("=== At least one component need to be on");
  38. return -1;
  39. }
  40. LOGD("=== Init lib");
  41. ASSERT_OK( TYInitLib() );
  42. TY_VERSION_INFO ver;
  43. ASSERT_OK( TYLibVersion(&ver) );
  44. LOGD(" - lib version: %d.%d.%d", ver.major, ver.minor, ver.patch);
  45. std::vector<TY_DEVICE_BASE_INFO> selected;
  46. ASSERT_OK( selectDevice(TY_INTERFACE_ALL, ID, IP, 1, selected) );
  47. ASSERT(selected.size() > 0);
  48. TY_DEVICE_BASE_INFO& selectedDev = selected[0];
  49. ASSERT_OK( TYOpenInterface(selectedDev.iface.id, &hIface) );
  50. ASSERT_OK( TYOpenDevice(hIface, selectedDev.id, &hDevice) );
  51. TY_COMPONENT_ID allComps;
  52. ASSERT_OK( TYGetComponentIDs(hDevice, &allComps) );
  53. if(allComps & TY_COMPONENT_RGB_CAM && color) {
  54. LOGD("=== Has RGB camera, open RGB cam");
  55. ASSERT_OK( TYEnableComponents(hDevice, TY_COMPONENT_RGB_CAM) );
  56. }
  57. TY_COMPONENT_ID componentIDs = 0;
  58. LOGD("=== Configure components, open depth cam");
  59. if (depth) {
  60. componentIDs = TY_COMPONENT_DEPTH_CAM;
  61. }
  62. if (ir) {
  63. componentIDs |= TY_COMPONENT_IR_CAM_LEFT;
  64. }
  65. ASSERT_OK( TYEnableComponents(hDevice, componentIDs) );
  66. //try to enable depth map
  67. LOGD("Configure components, open depth cam");
  68. DepthViewer depthViewer("Depth");
  69. if (allComps & TY_COMPONENT_DEPTH_CAM && depth) {
  70. TY_IMAGE_MODE image_mode;
  71. ASSERT_OK(get_default_image_mode(hDevice, TY_COMPONENT_DEPTH_CAM, image_mode));
  72. LOGD("Select Depth Image Mode: %dx%d", TYImageWidth(image_mode), TYImageHeight(image_mode));
  73. ASSERT_OK(TYSetEnum(hDevice, TY_COMPONENT_DEPTH_CAM, TY_ENUM_IMAGE_MODE, image_mode));
  74. ASSERT_OK(TYEnableComponents(hDevice, TY_COMPONENT_DEPTH_CAM));
  75. //depth map pixel format is uint16_t ,which default unit is 1 mm
  76. //the acutal depth (mm)= PixelValue * ScaleUnit
  77. float scale_unit = 1.;
  78. TYGetFloat(hDevice, TY_COMPONENT_DEPTH_CAM, TY_FLOAT_SCALE_UNIT, &scale_unit);
  79. depthViewer.depth_scale_unit = scale_unit;
  80. }
  81. LOGD("=== Prepare image buffer");
  82. uint32_t frameSize;
  83. ASSERT_OK( TYGetFrameBufferSize(hDevice, &frameSize) );
  84. LOGD(" - Get size of framebuffer, %d", frameSize);
  85. LOGD(" - Allocate & enqueue buffers");
  86. char* frameBuffer[2];
  87. frameBuffer[0] = new char[frameSize];
  88. frameBuffer[1] = new char[frameSize];
  89. LOGD(" - Enqueue buffer (%p, %d)", frameBuffer[0], frameSize);
  90. ASSERT_OK( TYEnqueueBuffer(hDevice, frameBuffer[0], frameSize) );
  91. LOGD(" - Enqueue buffer (%p, %d)", frameBuffer[1], frameSize);
  92. ASSERT_OK( TYEnqueueBuffer(hDevice, frameBuffer[1], frameSize) );
  93. LOGD("=== Register event callback");
  94. ASSERT_OK(TYRegisterEventCallback(hDevice, eventCallback, NULL));
  95. LOGD("=== Disable trigger mode");
  96. TY_TRIGGER_PARAM trigger;
  97. trigger.mode = TY_TRIGGER_MODE_OFF;
  98. ASSERT_OK(TYSetStruct(hDevice, TY_COMPONENT_DEVICE, TY_STRUCT_TRIGGER_PARAM, &trigger, sizeof(trigger)));
  99. LOGD("=== Start capture");
  100. ASSERT_OK( TYStartCapture(hDevice) );
  101. LOGD("=== While loop to fetch frame");
  102. bool exit_main = false;
  103. TY_FRAME_DATA frame;
  104. int index = 0;
  105. while(!exit_main) {
  106. int err = TYFetchFrame(hDevice, &frame, -1);
  107. if( err == TY_STATUS_OK ) {
  108. LOGD("=== Get frame %d", ++index);
  109. int fps = get_fps();
  110. if (fps > 0){
  111. LOGI("fps: %d", fps);
  112. }
  113. cv::Mat depth, irl, irr, color;
  114. parseFrame(frame, &depth, &irl, &irr, &color);
  115. if(!depth.empty()){
  116. depthViewer.show(depth);
  117. }
  118. if(!irl.empty()){ cv::imshow("LeftIR", irl); }
  119. if(!irr.empty()){ cv::imshow("RightIR", irr); }
  120. if(!color.empty()){ cv::imshow("Color", color); }
  121. int key = cv::waitKey(1);
  122. switch(key & 0xff) {
  123. case 0xff:
  124. break;
  125. case 'q':
  126. exit_main = true;
  127. break;
  128. default:
  129. LOGD("Unmapped key %d", key);
  130. }
  131. LOGD("=== Re-enqueue buffer(%p, %d)"
  132. , frame.userBuffer, frame.bufferSize);
  133. ASSERT_OK( TYEnqueueBuffer(hDevice, frame.userBuffer, frame.bufferSize) );
  134. }
  135. }
  136. ASSERT_OK( TYStopCapture(hDevice) );
  137. ASSERT_OK( TYCloseDevice(hDevice) );
  138. ASSERT_OK( TYCloseInterface(hIface) );
  139. ASSERT_OK( TYDeinitLib() );
  140. delete frameBuffer[0];
  141. delete frameBuffer[1];
  142. LOGD("=== Main done!");
  143. return 0;
  144. }