main.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. //Notice: This sample is used for part of percipio camera
  14. int main(int argc, char* argv[])
  15. {
  16. std::string ID, IP;
  17. TY_INTERFACE_HANDLE hIface = NULL;
  18. TY_DEV_HANDLE hDevice = NULL;
  19. int32_t color, ir, depth;
  20. color = ir = depth = 1;
  21. int duty = 2;
  22. for(int i = 1; i < argc; i++) {
  23. if(strcmp(argv[i], "-id") == 0){
  24. ID = argv[++i];
  25. } else if(strcmp(argv[i], "-ip") == 0) {
  26. IP = argv[++i];
  27. } else if(strcmp(argv[i], "-color=off") == 0) {
  28. color = 0;
  29. } else if(strcmp(argv[i], "-depth=off") == 0) {
  30. depth = 0;
  31. } else if(strcmp(argv[i], "-ir=off") == 0) {
  32. ir = 0;
  33. } else if(strcmp(argv[i], "-duty") == 0) {
  34. duty = atoi(argv[++i]);
  35. } else if(strcmp(argv[i], "-h") == 0) {
  36. LOGI("Usage: SimpleView_Triggermode18 [-h]");
  37. return 0;
  38. }
  39. }
  40. if (!color && !depth && !ir) {
  41. LOGD("=== At least one component need to be on");
  42. return -1;
  43. }
  44. LOGD("=== Init lib");
  45. ASSERT_OK( TYInitLib() );
  46. TY_VERSION_INFO ver;
  47. ASSERT_OK( TYLibVersion(&ver) );
  48. LOGD(" - lib version: %d.%d.%d", ver.major, ver.minor, ver.patch);
  49. std::vector<TY_DEVICE_BASE_INFO> selected;
  50. ASSERT_OK( selectDevice(TY_INTERFACE_ALL, ID, IP, 1, selected) );
  51. ASSERT(selected.size() > 0);
  52. TY_DEVICE_BASE_INFO& selectedDev = selected[0];
  53. ASSERT_OK( TYOpenInterface(selectedDev.iface.id, &hIface) );
  54. ASSERT_OK( TYOpenDevice(hIface, selectedDev.id, &hDevice) );
  55. TY_COMPONENT_ID allComps;
  56. ASSERT_OK( TYGetComponentIDs(hDevice, &allComps) );
  57. if(allComps & TY_COMPONENT_RGB_CAM && color) {
  58. LOGD("=== Has RGB camera, open RGB cam");
  59. ASSERT_OK( TYEnableComponents(hDevice, TY_COMPONENT_RGB_CAM) );
  60. }
  61. TY_COMPONENT_ID componentIDs = 0;
  62. LOGD("=== Configure components, open depth cam");
  63. if (depth) {
  64. componentIDs = TY_COMPONENT_DEPTH_CAM;
  65. }
  66. if (ir) {
  67. componentIDs |= TY_COMPONENT_IR_CAM_LEFT;
  68. }
  69. ASSERT_OK( TYEnableComponents(hDevice, componentIDs) );
  70. //try to enable depth map
  71. LOGD("Configure components, open depth cam");
  72. DepthViewer depthViewer("Depth");
  73. if (allComps & TY_COMPONENT_DEPTH_CAM && depth) {
  74. TY_IMAGE_MODE image_mode;
  75. ASSERT_OK(get_default_image_mode(hDevice, TY_COMPONENT_DEPTH_CAM, image_mode));
  76. LOGD("Select Depth Image Mode: %dx%d", TYImageWidth(image_mode), TYImageHeight(image_mode));
  77. ASSERT_OK(TYSetEnum(hDevice, TY_COMPONENT_DEPTH_CAM, TY_ENUM_IMAGE_MODE, image_mode));
  78. ASSERT_OK(TYEnableComponents(hDevice, TY_COMPONENT_DEPTH_CAM));
  79. //depth map pixel format is uint16_t ,which default unit is 1 mm
  80. //the acutal depth (mm)= PixelValue * ScaleUnit
  81. float scale_unit = 1.;
  82. TYGetFloat(hDevice, TY_COMPONENT_DEPTH_CAM, TY_FLOAT_SCALE_UNIT, &scale_unit);
  83. depthViewer.depth_scale_unit = scale_unit;
  84. }
  85. LOGD("=== Prepare image buffer");
  86. uint32_t frameSize;
  87. ASSERT_OK( TYGetFrameBufferSize(hDevice, &frameSize) );
  88. LOGD(" - Get size of framebuffer, %d", frameSize);
  89. LOGD(" - Allocate & enqueue buffers");
  90. std::vector<char *> frameBuffer(duty + 2);
  91. for (int i=0; i<duty + 2; i++) {
  92. frameBuffer[i] = new char[frameSize];
  93. LOGD(" - Enqueue buffer (%p, %d)", frameBuffer[i], frameSize);
  94. ASSERT_OK(TYEnqueueBuffer(hDevice, frameBuffer[i], frameSize));
  95. }
  96. LOGD("=== Register event callback");
  97. ASSERT_OK(TYRegisterEventCallback(hDevice, eventCallback, NULL));
  98. LOGD("=== Set trigger to trig mode 18");
  99. TY_TRIGGER_PARAM_EX trigger;
  100. trigger.mode = TY_TRIGGER_MODE_SIG_PASS;
  101. trigger.fps = 10; // [1, 15]
  102. trigger.duty = duty;
  103. trigger.laser_stream = TY_COMPONENT_DEPTH_CAM | TY_COMPONENT_RGB_CAM;
  104. trigger.led_stream = TY_COMPONENT_IR_CAM_LEFT | TY_COMPONENT_RGB_CAM;
  105. trigger.led_expo = 1088; // [3, 1088]
  106. trigger.led_gain = 32; // [0, 255]
  107. ASSERT_OK(TYSetStruct(hDevice, TY_COMPONENT_DEVICE, TY_STRUCT_TRIGGER_PARAM_EX, &trigger, sizeof(trigger)));
  108. LOGD("=== Enable Resend Option");
  109. ASSERT_OK(TYSetBool(hDevice, TY_COMPONENT_DEVICE, TY_BOOL_GVSP_RESEND, true));
  110. LOGD("=== Start capture");
  111. ASSERT_OK( TYStartCapture(hDevice) );
  112. LOGD("=== While loop to fetch frame");
  113. bool exit_main = false;
  114. TY_FRAME_DATA frame;
  115. int index = 0;
  116. int cnt = 0;
  117. LOGD("=== Send Trigger Command");
  118. while(TY_STATUS_BUSY == TYSendSoftTrigger(hDevice));
  119. while(!exit_main) {
  120. int err = TYFetchFrame(hDevice, &frame, 20000);
  121. if (err == TY_STATUS_OK) {
  122. LOGD("=== Get frame %d", ++index);
  123. for (int32_t i=0; i<frame.validCount; i++) {
  124. LOGD(" image [%d] width %4d height %4d (Laser_val %d)", i,
  125. frame.image[i].width, frame.image[i].height, frame.image[i].reserved[0]); // laser_val: frame.image[i].reserved[0]
  126. }
  127. int fps = get_fps();
  128. if (fps > 0) {
  129. LOGI("fps: %d", fps);
  130. }
  131. cv::Mat depth, irl, irr, color;
  132. parseFrame(frame, &depth, &irl, &irr, &color);
  133. if (!depth.empty()) {
  134. depthViewer.show(depth);
  135. }
  136. if (!irl.empty()) { cv::imshow("LeftIR", irl); }
  137. if (!irr.empty()) { cv::imshow("RightIR", irr); }
  138. if (!color.empty()) { cv::imshow("Color", color); }
  139. int key = cv::waitKey(1);
  140. switch (key & 0xff) {
  141. case 0xff:
  142. break;
  143. case 'q':
  144. exit_main = true;
  145. break;
  146. default:
  147. LOGD("Unmapped key %d", key);
  148. }
  149. LOGD("=== Re-enqueue buffer(%p, %d)"
  150. , frame.userBuffer, frame.bufferSize);
  151. ASSERT_OK(TYEnqueueBuffer(hDevice, frame.userBuffer, frame.bufferSize));
  152. cnt++;
  153. } else {
  154. LOGD("FetchFrame err %d %s, Exit!", err, TYErrorString(err));
  155. exit_main = true;
  156. }
  157. if (cnt == duty + 1) {
  158. LOGD("=== Send Trigger Command");
  159. while(TY_STATUS_BUSY == TYSendSoftTrigger(hDevice));
  160. cnt = 0;
  161. }
  162. }
  163. ASSERT_OK( TYStopCapture(hDevice) );
  164. ASSERT_OK( TYCloseDevice(hDevice) );
  165. ASSERT_OK( TYCloseInterface(hIface) );
  166. ASSERT_OK( TYDeinitLib() );
  167. delete frameBuffer[0];
  168. delete frameBuffer[1];
  169. LOGD("=== Main done!");
  170. return 0;
  171. }