main.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #include "../common/common.hpp"
  2. #include <signal.h>
  3. static bool exit_main = false;
  4. static bool capture_started = false;
  5. struct CamInfo
  6. {
  7. char sn[32];
  8. std::string tag;
  9. TY_INTERFACE_HANDLE hIface;
  10. TY_DEV_HANDLE hDev;
  11. std::vector<char> fb[6];
  12. TY_FRAME_DATA frame;
  13. int idx;
  14. DepthRender render;
  15. CamInfo() : hDev(0), idx(0) {}
  16. };
  17. void frameHandler(TY_FRAME_DATA* frame, void* userdata)
  18. {
  19. CamInfo* pData = (CamInfo*)userdata;
  20. cv::Mat depth, irl, irr, color;
  21. parseFrame(*frame, &depth, &irl, &irr, &color);
  22. char win[64];
  23. if (!depth.empty()) {
  24. cv::Mat colorDepth = pData->render.Compute(depth);
  25. sprintf(win, "depth-%s", pData->tag.c_str());
  26. cv::imshow(win, colorDepth);
  27. }
  28. if (!irl.empty()) {
  29. sprintf(win, "LeftIR-%s", pData->tag.c_str());
  30. cv::imshow(win, irl);
  31. }
  32. if (!irr.empty()) {
  33. sprintf(win, "RightIR-%s", pData->tag.c_str());
  34. cv::imshow(win, irr);
  35. }
  36. if (!color.empty()) {
  37. sprintf(win, "color-%s", pData->tag.c_str());
  38. cv::imshow(win, color);
  39. }
  40. pData->idx++;
  41. LOGD("=== Re-enqueue buffer(%p, %d)", frame->userBuffer, frame->bufferSize);
  42. ASSERT_OK(TYEnqueueBuffer(pData->hDev, frame->userBuffer, frame->bufferSize));
  43. }
  44. void eventCallback(TY_EVENT_INFO *event_info, void *userdata)
  45. {
  46. if (event_info->eventId == TY_EVENT_DEVICE_OFFLINE) {
  47. LOGD("=== Event Callback: Device Offline!");
  48. // Note:
  49. // Please set TY_BOOL_KEEP_ALIVE_ONOFF feature to false if you need to debug with breakpoint!
  50. }
  51. else if (event_info->eventId == TY_EVENT_LICENSE_ERROR) {
  52. LOGD("=== Event Callback: License Error!");
  53. }
  54. }
  55. void signalHandle(int signum)
  56. {
  57. LOGD("Interrupt signal %d received", signum);
  58. signal(SIGINT, signalHandle);
  59. if (capture_started) {
  60. exit_main = true;
  61. } else {
  62. ASSERT_OK(TYDeinitLib());
  63. exit(0);
  64. }
  65. }
  66. int main(int argc, char* argv[])
  67. {
  68. int32_t resend = 1;
  69. int32_t deviceType = TY_INTERFACE_ETHERNET | TY_INTERFACE_USB;
  70. std::vector<char*> list;
  71. int32_t cam_size = 0;
  72. int32_t found;
  73. bool trigger_mode = false;
  74. if ((argc < 2) || (strcmp(argv[1], "-list") != 0)) {
  75. LOGI("Usage: %s -list masterSN [slaveSN ......]", argv[0]);
  76. return 0;
  77. }
  78. for(int i = 0; i < argc; i++) {
  79. if (strcmp(argv[i], "-h") == 0) {
  80. LOGI("Usage: %s -list [xxx, xxx, ....] [-h]", argv[0]);
  81. return 0;
  82. }
  83. else if (strcmp(argv[i], "-list") == 0) {
  84. if (argc == i + 1) {
  85. LOGI("===== no list input");
  86. return 0;
  87. }
  88. int32_t j;
  89. for (j = 0; j < argc - (i + 1); j++) {
  90. list.push_back(argv[i + 1 + j]);
  91. LOGI("===== Select device %s", list[j]);
  92. }
  93. }
  94. }
  95. signal(SIGINT, signalHandle);
  96. LOGD("=== Init lib");
  97. ASSERT_OK(TYInitLib());
  98. TY_VERSION_INFO ver;
  99. ASSERT_OK(TYLibVersion(&ver));
  100. LOGD(" - lib version: %d.%d.%d", ver.major, ver.minor, ver.patch);
  101. std::vector<TY_DEVICE_BASE_INFO> selected;
  102. ASSERT_OK( selectDevice(deviceType, "", "", 100, selected) );
  103. //printf("size: %d\n", list.size());
  104. if (list.size()) {
  105. cam_size = list.size();
  106. } else {
  107. cam_size = selected.size();
  108. }
  109. std::vector<CamInfo> cams(cam_size);
  110. int32_t count = 0;
  111. for(uint32_t i = 0; i < selected.size(); i++){
  112. if (list.size()) {
  113. found = 0;
  114. for (uint32_t j = 0; j < list.size(); j++) {
  115. LOGD("=== check device: %s, %s", selected[i].id, list[j]);
  116. if (strcmp(selected[i].id, list[j]) == 0) {
  117. LOGD("=== check device success");
  118. found = 1;
  119. continue;
  120. }
  121. }
  122. if (!found) {
  123. continue;
  124. }
  125. }
  126. LOGD("=== Open device: %s", selected[i].id);
  127. strncpy(cams[count].sn, selected[i].id, sizeof(cams[count].sn));
  128. ASSERT_OK( TYOpenInterface(selected[i].iface.id, &cams[count].hIface) );
  129. ASSERT_OK( TYOpenDevice(cams[count].hIface, selected[i].id, &cams[count].hDev) );
  130. LOGD("=== Configure components, open depth cam");
  131. int componentIDs = TY_COMPONENT_DEPTH_CAM;
  132. ASSERT_OK( TYEnableComponents(cams[count].hDev, componentIDs) );
  133. //try to enable depth map
  134. LOGD("Configure components, open depth cam");
  135. if (componentIDs & TY_COMPONENT_DEPTH_CAM) {
  136. TY_IMAGE_MODE image_mode;
  137. ASSERT_OK(get_default_image_mode(cams[count].hDev, TY_COMPONENT_DEPTH_CAM, image_mode));
  138. LOGD("Select Depth Image Mode: %dx%d", TYImageWidth(image_mode), TYImageHeight(image_mode));
  139. ASSERT_OK(TYSetEnum(cams[count].hDev, TY_COMPONENT_DEPTH_CAM, TY_ENUM_IMAGE_MODE, image_mode));
  140. ASSERT_OK(TYEnableComponents(cams[count].hDev, TY_COMPONENT_DEPTH_CAM));
  141. }
  142. LOGD("=== Prepare image buffer");
  143. uint32_t frameSize;
  144. ASSERT_OK( TYGetFrameBufferSize(cams[count].hDev, &frameSize) );
  145. LOGD(" - Get size of framebuffer, %d", frameSize);
  146. LOGD(" - Allocate & enqueue buffers");
  147. for (int i = 0; i < 6; i++) {
  148. cams[count].fb[i].resize(frameSize);
  149. LOGD(" - Enqueue buffer (%p, %d)", cams[count].fb[i].data(), frameSize);
  150. ASSERT_OK(TYEnqueueBuffer(cams[count].hDev, cams[count].fb[i].data(), frameSize));
  151. }
  152. LOGD("=== Register event callback");
  153. ASSERT_OK(TYRegisterEventCallback(cams[count].hDev, eventCallback, NULL));
  154. LOGD("=== Set trigger mode");
  155. if (((strcmp(selected[i].id, list[0]) == 0) && (list.size() > 0))
  156. || ((count == 0) && (list.size() == 0))) {
  157. LOGD("=== set master device, id: %s", cams[count].sn);
  158. cams[count].tag = std::string(cams[count].sn) + "_master";
  159. TY_TRIGGER_PARAM param;
  160. param.mode = TY_TRIGGER_MODE_M_SIG;
  161. ASSERT_OK(TYSetStruct(cams[count].hDev, TY_COMPONENT_DEVICE, TY_STRUCT_TRIGGER_PARAM, (void*)&param, sizeof(param)));
  162. } else {
  163. cams[count].tag = std::string(cams[count].sn) + "_slave";
  164. TY_TRIGGER_PARAM param;
  165. param.mode = TY_TRIGGER_MODE_SLAVE;
  166. ASSERT_OK(TYSetStruct(cams[count].hDev, TY_COMPONENT_DEVICE, TY_STRUCT_TRIGGER_PARAM, (void*)&param, sizeof(param)));
  167. }
  168. //for network only
  169. LOGD("=== resend: %d", resend);
  170. if (resend) {
  171. bool hasResend;
  172. ASSERT_OK(TYHasFeature(cams[count].hDev, TY_COMPONENT_DEVICE, TY_BOOL_GVSP_RESEND, &hasResend));
  173. if (hasResend) {
  174. LOGD("=== Open resend");
  175. ASSERT_OK(TYSetBool(cams[count].hDev, TY_COMPONENT_DEVICE, TY_BOOL_GVSP_RESEND, true));
  176. }
  177. else {
  178. LOGD("=== Not support feature TY_BOOL_GVSP_RESEND");
  179. }
  180. }
  181. //LOGD("=== Start capture");
  182. //ASSERT_OK( TYStartCapture(cams[count].hDev) );
  183. count++;
  184. }
  185. if (count != cam_size) {
  186. LOGD("Invalid ids in input id list");
  187. return 0;
  188. }
  189. int cam_index = 0;
  190. LOGD("=== Start capture for salve");
  191. for (uint32_t i = 0; i < cams.size(); i++) {
  192. if (cams[i].tag.compare(13, 6, "master") != 0) {
  193. ASSERT_OK(TYStartCapture(cams[i].hDev));
  194. }
  195. }
  196. LOGD("=== Start capture for master");
  197. for (uint32_t i = 0; i < cams.size(); i++) {
  198. if (cams[i].tag.compare(13, 6, "master") == 0) {
  199. ASSERT_OK(TYStartCapture(cams[i].hDev));
  200. cam_index = i;
  201. }
  202. }
  203. MSLEEP(1000);
  204. LOGD("=== While loop to fetch frame");
  205. capture_started = true;
  206. exit_main = false;
  207. while (!exit_main) {
  208. if (cams[cam_index].tag.compare(13, 6, "master") == 0) {
  209. while(TY_STATUS_BUSY == TYSendSoftTrigger(cams[cam_index].hDev));
  210. }
  211. int err = TYFetchFrame(cams[cam_index].hDev, &cams[cam_index].frame, 20000);
  212. if (err != TY_STATUS_OK) {
  213. LOGD("cam %s %d ... Drop one frame", cams[cam_index].sn, cams[cam_index].idx);
  214. }
  215. else {
  216. LOGD("cam %s %d got one frame", cams[cam_index].sn, cams[cam_index].idx);
  217. frameHandler(&cams[cam_index].frame, &cams[cam_index]);
  218. int key = cv::waitKey(1);
  219. switch (key & 0xff) {
  220. case 0xff:
  221. break;
  222. case 'q':
  223. exit_main = true;
  224. break;
  225. default:
  226. LOGD("Unmapped key %d", key);
  227. }
  228. }
  229. cam_index = (cam_index + 1) % cams.size();
  230. }
  231. for (uint32_t i = 0; i < cams.size(); i++) {
  232. ASSERT_OK(TYStopCapture(cams[i].hDev));
  233. ASSERT_OK(TYCloseDevice(cams[i].hDev));
  234. ASSERT_OK(TYCloseInterface(cams[i].hIface));
  235. }
  236. ASSERT_OK(TYDeinitLib());
  237. LOGD("=== Main done!");
  238. return 0;
  239. }