main.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. *(bool*)userdata = true;
  7. }
  8. else if (event_info->eventId == TY_EVENT_LICENSE_ERROR) {
  9. LOGD("=== Event Callback: License Error!");
  10. }
  11. }
  12. int main(int argc, char* argv[])
  13. {
  14. std::string ID, IP;
  15. for(int i = 1; i < argc; i++){
  16. if(strcmp(argv[i], "-id") == 0){
  17. ID = argv[++i];
  18. } else if(strcmp(argv[i], "-ip") == 0) {
  19. IP = argv[++i];
  20. }else if(strcmp(argv[i], "-h") == 0){
  21. LOGI("Usage: LoopDetect [-h] [-id <ID>]");
  22. return 0;
  23. }
  24. }
  25. LOGD("=== Init lib");
  26. int loop_index = 1;
  27. bool loop_exit = false;
  28. ASSERT_OK( TYInitLib() );
  29. TY_VERSION_INFO ver;
  30. ASSERT_OK( TYLibVersion(&ver) );
  31. LOGD(" - lib version: %d.%d.%d", ver.major, ver.minor, ver.patch);
  32. while(!loop_exit) {
  33. LOGD("==========================");
  34. LOGD("========== loop %d", loop_index++);
  35. LOGD("==========================");
  36. TY_INTERFACE_HANDLE hIface;
  37. TY_DEV_HANDLE hDevice;
  38. std::vector<TY_DEVICE_BASE_INFO> selected;
  39. int ret = 0;
  40. ret = selectDevice(TY_INTERFACE_ALL, ID, IP, 100, selected);
  41. if (ret < 0) {
  42. MSleep(2000);
  43. continue;
  44. }
  45. ASSERT(selected.size() > 0);
  46. TY_DEVICE_BASE_INFO& selectedDev = selected[0];
  47. LOGD("=== Open interface: %s", selectedDev.iface.id);
  48. ASSERT_OK(TYOpenInterface(selectedDev.iface.id, &hIface));
  49. LOGD("=== Open device: %s", selectedDev.id);
  50. ret = TYOpenDevice(hIface, selectedDev.id, &hDevice);
  51. if (ret < 0) {
  52. LOGD("Open device %s failed!", selectedDev.id);
  53. ASSERT_OK(TYCloseInterface(hIface));
  54. continue;
  55. }
  56. TY_COMPONENT_ID allComps;
  57. ret = TYGetComponentIDs(hDevice, &allComps);
  58. if (ret < 0) {
  59. LOGD("Get component failed!");
  60. ASSERT_OK(TYCloseDevice(hDevice));
  61. ASSERT_OK(TYCloseInterface(hIface));
  62. continue;
  63. }
  64. if(allComps & TY_COMPONENT_RGB_CAM) {
  65. LOGD("=== Has RGB camera, open RGB cam");
  66. ret = TYEnableComponents(hDevice, TY_COMPONENT_RGB_CAM);
  67. if (ret < 0) {
  68. LOGD("Enable RGB component failed!");
  69. ASSERT_OK(TYCloseDevice(hDevice));
  70. ASSERT_OK(TYCloseInterface(hIface));
  71. continue;
  72. }
  73. }
  74. if (allComps & TY_COMPONENT_DEPTH_CAM) {
  75. LOGD("=== Has depth camera, open depth cam");
  76. ret = TYEnableComponents(hDevice, TY_COMPONENT_DEPTH_CAM);
  77. if (ret < 0) {
  78. LOGD("Enable depth component failed!");
  79. ASSERT_OK(TYCloseDevice(hDevice));
  80. ASSERT_OK(TYCloseInterface(hIface));
  81. continue;
  82. }
  83. }
  84. LOGD("=== Prepare image buffer");
  85. uint32_t frameSize;
  86. ret = TYGetFrameBufferSize(hDevice, &frameSize);
  87. if (ret < 0) {
  88. LOGD("Get frame buffer size failed!");
  89. ASSERT_OK(TYCloseDevice(hDevice));
  90. ASSERT_OK(TYCloseInterface(hIface));
  91. continue;
  92. }
  93. LOGD(" - Allocate & enqueue buffers");
  94. char* frameBuffer[2];
  95. frameBuffer[0] = new char[frameSize];
  96. frameBuffer[1] = new char[frameSize];
  97. LOGD(" - Enqueue buffer (%p, %d)", frameBuffer[0], frameSize);
  98. ASSERT_OK(TYEnqueueBuffer(hDevice, frameBuffer[0], frameSize));
  99. LOGD(" - Enqueue buffer (%p, %d)", frameBuffer[1], frameSize);
  100. ASSERT_OK(TYEnqueueBuffer(hDevice, frameBuffer[1], frameSize));
  101. bool device_offline = false;;
  102. LOGD("=== Register event callback");
  103. LOGD("Note: Callback may block internal data receiving,");
  104. LOGD(" so that user should not do long time work in callback.");
  105. ASSERT_OK(TYRegisterEventCallback(hDevice, eventCallback, &device_offline));
  106. bool hasTrigger;
  107. ASSERT_OK(TYHasFeature(hDevice, TY_COMPONENT_DEVICE, TY_STRUCT_TRIGGER_PARAM, &hasTrigger));
  108. if (hasTrigger) {
  109. LOGD("=== Disable trigger mode");
  110. TY_TRIGGER_PARAM trigger;
  111. trigger.mode = TY_TRIGGER_MODE_OFF;
  112. ret = TYSetStruct(hDevice, TY_COMPONENT_DEVICE, TY_STRUCT_TRIGGER_PARAM, &trigger, sizeof(trigger));
  113. if (ret < 0) {
  114. LOGD("Set trigger mode failed!");
  115. ASSERT_OK(TYCloseDevice(hDevice));
  116. ASSERT_OK(TYCloseInterface(hIface));
  117. delete frameBuffer[0];
  118. delete frameBuffer[1];
  119. continue;
  120. }
  121. }
  122. LOGD("=== Start capture");
  123. ret = TYStartCapture(hDevice);
  124. if (ret < 0) {
  125. LOGD("Start capture failed!");
  126. ASSERT_OK(TYCloseDevice(hDevice));
  127. ASSERT_OK(TYCloseInterface(hIface));
  128. delete frameBuffer[0];
  129. delete frameBuffer[1];
  130. continue;
  131. }
  132. bool saveFrame = false;
  133. int saveIdx = 0;
  134. cv::Mat depth;
  135. cv::Mat leftIR;
  136. cv::Mat rightIR;
  137. cv::Mat color;
  138. LOGD("=== Wait for callback");
  139. bool exit_main = false;
  140. DepthViewer depthViewer("Depth");
  141. int count = 0;
  142. TY_FRAME_DATA frame;
  143. while(!exit_main){
  144. ret = TYFetchFrame(hDevice, &frame, 1000);
  145. if( ret == TY_STATUS_OK ) {
  146. LOGD("=== Get frame %d", ++count);
  147. parseFrame(frame, &depth, &leftIR, &rightIR, &color);
  148. if(!color.empty()){
  149. LOGI("Color format is %s", colorFormatName(TYImageInFrame(frame, TY_COMPONENT_RGB_CAM)->pixelFormat));
  150. }
  151. LOGD("=== Callback: Re-enqueue buffer(%p, %d)", frame.userBuffer, frame.bufferSize);
  152. ASSERT_OK(TYEnqueueBuffer(hDevice, frame.userBuffer, frame.bufferSize));
  153. if(!depth.empty()){
  154. depthViewer.show(depth);
  155. }
  156. if(!leftIR.empty()){
  157. cv::imshow("LeftIR", leftIR);
  158. }
  159. if(!rightIR.empty()){
  160. cv::imshow("RightIR", rightIR);
  161. }
  162. if(!color.empty()){
  163. cv::imshow("color", color);
  164. }
  165. if(saveFrame && !depth.empty() && !leftIR.empty() && !rightIR.empty()){
  166. LOGI(">>>> save frame %d", saveIdx);
  167. char f[32];
  168. sprintf(f, "%d.img", saveIdx++);
  169. FILE* fp = fopen(f, "wb");
  170. fwrite(depth.data, 2, depth.size().area(), fp);
  171. fwrite(color.data, 3, color.size().area(), fp);
  172. fclose(fp);
  173. saveFrame = false;
  174. }
  175. }
  176. if(device_offline){
  177. LOGI("Found device offline");
  178. break;
  179. }
  180. int key = cv::waitKey(10);
  181. switch(key & 0xff){
  182. case 0xff:
  183. break;
  184. case 'q':
  185. exit_main = true;
  186. break;
  187. case 's':
  188. saveFrame = true;
  189. break;
  190. case 'x':
  191. exit_main = true;
  192. loop_exit = true;
  193. break;
  194. default:
  195. LOGD("Unmapped key %d", key);
  196. }
  197. }
  198. if(device_offline) {
  199. LOGI("device offline release resource");
  200. } else {
  201. LOGI("normal exit");
  202. }
  203. ASSERT_OK(TYStopCapture(hDevice));
  204. ASSERT_OK(TYCloseDevice(hDevice));
  205. ASSERT_OK(TYCloseInterface(hIface));
  206. delete frameBuffer[0];
  207. delete frameBuffer[1];
  208. }
  209. LOGD("=== Deinit lib");
  210. ASSERT_OK( TYDeinitLib() );
  211. LOGD("=== Main done!");
  212. return 0;
  213. }