lidarmain.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #include <QCoreApplication>
  2. #include "ivdriver_lidar_rs16.h"
  3. #include <signal.h>
  4. #include <getopt.h>
  5. #include <iostream>
  6. #include <iostream>
  7. #include <fstream>
  8. #include <yaml-cpp/yaml.h>
  9. iv::ivmodule * gpivmodule;
  10. QCoreApplication * gpapp;
  11. void sigint_handler(int sig){
  12. if(sig == SIGINT){
  13. // ctrl+c退出时执行的代码
  14. delete gpivmodule;
  15. gpapp->exit(0);
  16. }
  17. }
  18. char gstr_memname[256];
  19. char gstr_rollang[256];
  20. char gstr_inclinationang_yaxis[256]; //from y axis
  21. char gstr_inclinationang_xaxis[256]; //from x axis
  22. char gstr_hostip[256];
  23. char gstr_port[256];
  24. char gstr_yaml[256];
  25. void print_useage()
  26. {
  27. std::cout<<" -m --memname $memname : share memory name. eq. -m lidar_pc"<<std::endl;
  28. std::cout<<" -r --rollang $rollang : roll angle. eq. -r 10.0"<<std::endl;
  29. std::cout<<" -x --inclinationang_xaxis $inclinationang_xaxis : inclination angle from x axis. eq. -x 0.0"<<std::endl;
  30. std::cout<<" -y --inclinationang_yaxis $inclinationang_yaxis : inclination angle from y axis. eq. -y 0.0"<<std::endl;
  31. std::cout<<" -o --hostip $hostip : host ip. eq. -o 192.168.1.111"<<std::endl;
  32. std::cout<<" -p --port $port : port . eq. -p 2368"<<std::endl;
  33. std::cout<<" -s --setyaml $yaml : port . eq. -s rs1.yaml"<<std::endl;
  34. std::cout<<" -h --help print help"<<std::endl;
  35. }
  36. int GetOptLong(int argc, char *argv[]) {
  37. int nRtn = 0;
  38. int opt; // getopt_long() 的返回值
  39. int digit_optind = 0; // 设置短参数类型及是否需要参数
  40. // 如果option_index非空,它指向的变量将记录当前找到参数符合long_opts里的
  41. // 第几个元素的描述,即是long_opts的下标值
  42. int option_index = 0;
  43. // 设置短参数类型及是否需要参数
  44. const char *optstring = "m:r:x:y:o:p:s:h";
  45. // 设置长参数类型及其简写,比如 --reqarg <==>-r
  46. /*
  47. struct option {
  48. const char * name; // 参数的名称
  49. int has_arg; // 是否带参数值,有三种:no_argument, required_argument,optional_argument
  50. int * flag; // 为空时,函数直接将 val 的数值从getopt_long的返回值返回出去,
  51. // 当非空时,val的值会被赋到 flag 指向的整型数中,而函数返回值为0
  52. int val; // 用于指定函数找到该选项时的返回值,或者当flag非空时指定flag指向的数据的值
  53. };
  54. 其中:
  55. no_argument(即0),表明这个长参数不带参数(即不带数值,如:--name)
  56. required_argument(即1),表明这个长参数必须带参数(即必须带数值,如:--name Bob)
  57. optional_argument(即2),表明这个长参数后面带的参数是可选的,(即--name和--name Bob均可)
  58. */
  59. static struct option long_options[] = {
  60. {"memname", required_argument, NULL, 'm'},
  61. {"rollang", required_argument, NULL, 'r'},
  62. {"inclinationang_xaxis", required_argument, NULL, 'x'},
  63. {"inclinationang_yaxis", required_argument, NULL, 'y'},
  64. {"hostip", required_argument, NULL, 'o'},
  65. {"port", required_argument, NULL, 'p'},
  66. {"setyaml", required_argument, NULL, 's'},
  67. {"help", no_argument, NULL, 'h'},
  68. // {"optarg", optional_argument, NULL, 'o'},
  69. {0, 0, 0, 0} // 添加 {0, 0, 0, 0} 是为了防止输入空值
  70. };
  71. while ( (opt = getopt_long(argc,
  72. argv,
  73. optstring,
  74. long_options,
  75. &option_index)) != -1) {
  76. // printf("opt = %c\n", opt); // 命令参数,亦即 -a -b -n -r
  77. // printf("optarg = %s\n", optarg); // 参数内容
  78. // printf("optind = %d\n", optind); // 下一个被处理的下标值
  79. // printf("argv[optind - 1] = %s\n", argv[optind - 1]); // 参数内容
  80. // printf("option_index = %d\n", option_index); // 当前打印参数的下标值
  81. // printf("\n");
  82. switch(opt)
  83. {
  84. case 'm':
  85. strncpy(gstr_memname,optarg,255);
  86. break;
  87. case 'r':
  88. strncpy(gstr_rollang,optarg,255);
  89. break;
  90. case 'x':
  91. strncpy(gstr_inclinationang_xaxis,optarg,255);
  92. break;
  93. case 'y':
  94. strncpy(gstr_inclinationang_yaxis,optarg,255);
  95. break;
  96. case 'o':
  97. strncpy(gstr_hostip,optarg,255);
  98. break;
  99. case 'p':
  100. strncpy(gstr_port,optarg,255);
  101. break;
  102. case 's':
  103. strncpy(gstr_yaml,optarg,255);
  104. break;
  105. case 'h':
  106. print_useage();
  107. nRtn = 1; //because use -h
  108. break;
  109. default:
  110. break;
  111. }
  112. }
  113. return nRtn;
  114. }
  115. void decodeyaml(const char * stryaml)
  116. {
  117. YAML::Node config;
  118. try
  119. {
  120. config = YAML::LoadFile(stryaml);
  121. }
  122. catch(YAML::BadFile e)
  123. {
  124. qDebug("load yaml error.");
  125. return;
  126. }
  127. if(config["memname"])
  128. {
  129. strncpy(gstr_memname,config["memname"].as<std::string>().data(),255);
  130. }
  131. if(config["rollang"])
  132. {
  133. strncpy(gstr_rollang,config["rollang"].as<std::string>().data(),255);
  134. }
  135. if(config["inclinationang_xaxis"])
  136. {
  137. strncpy(gstr_inclinationang_xaxis,config["inclinationang_xaxis"].as<std::string>().data(),255);
  138. }
  139. if(config["inclinationang_yaxis"])
  140. {
  141. strncpy(gstr_inclinationang_yaxis,config["inclinationang_yaxis"].as<std::string>().data(),255);
  142. }
  143. if(config["hostip"])
  144. {
  145. strncpy(gstr_hostip,config["hostip"].as<std::string>().data(),255);
  146. }
  147. if(config["port"])
  148. {
  149. strncpy(gstr_port,config["port"].as<std::string>().data(),255);
  150. }
  151. // std::cout<<gstr_memname<<std::endl;
  152. // std::cout<<gstr_rollang<<std::endl;
  153. // std::cout<<gstr_inclinationang_xaxis<<std::endl;
  154. // std::cout<<gstr_inclinationang_yaxis<<std::endl;
  155. // std::cout<<gstr_hostip<<std::endl;
  156. // std::cout<<gstr_port<<std::endl;
  157. }
  158. int lidarmain(iv::ivdriver_lidar * pivm,int argc, char *argv[],QCoreApplication * pa,const char * strmodulename)
  159. {
  160. snprintf(gstr_memname,255,"lidar_pc");
  161. snprintf(gstr_rollang,255,"-9.0");
  162. snprintf(gstr_inclinationang_xaxis,255,"0.0");
  163. snprintf(gstr_inclinationang_yaxis,255,"0");
  164. snprintf(gstr_hostip,255,"0.0.0.0");
  165. snprintf(gstr_port,255,"6699");//默认端口号
  166. snprintf(gstr_yaml,255,"");
  167. int nRtn = GetOptLong(argc,argv);
  168. if(nRtn == 1) //show help,so exit.
  169. {
  170. return 0;
  171. }
  172. if(strnlen(gstr_yaml,255)>0)
  173. {
  174. decodeyaml(gstr_yaml);
  175. }
  176. strncpy(pivm->mstr_memname,gstr_memname,256);
  177. strncpy(pivm->mstr_hostip,gstr_hostip,256);
  178. strncpy(pivm->mstr_port,gstr_port,256);
  179. pivm->mfrollang = atof(gstr_rollang)*M_PI/180.0;
  180. pivm->mfinclinationang_xaxis = atof(gstr_inclinationang_xaxis)*M_PI/180.0;
  181. pivm->mfinclinationang_yaxis = atof(gstr_inclinationang_yaxis)*M_PI/180.0;
  182. iv::ivmodule * pivmodule = pivm;
  183. signal(SIGINT, sigint_handler);
  184. gpivmodule = pivmodule;
  185. pivmodule->start();
  186. gpapp = pa;
  187. return 1;
  188. }