main.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #include <QCoreApplication>
  2. #include "leishen32.h"
  3. #include <getopt.h>
  4. #include <yaml-cpp/yaml.h> //if no this,sudo apt-get install libyaml-cpp-dev
  5. #include "ivfault.h"
  6. #include "ivlog.h"
  7. static char gstr_memname[256];
  8. static char gstr_rollang[256];
  9. static char gstr_inclinationang_yaxis[256]; //from y axis
  10. static char gstr_inclinationang_xaxis[256]; //from x axis
  11. //char gstr_hostip[256];
  12. static char gstr_port[256];
  13. static char gstr_devport[256];
  14. static char gstr_yaml[256];
  15. static char gstr_modulename[256];
  16. static char gstr_devmode[256];
  17. /**
  18. * @brief print_useage
  19. */
  20. void print_useage()
  21. {
  22. std::cout<<" -n --modulename $modulename : set module name. eq. -n lidarleft"<<std::endl;
  23. std::cout<<" -m --memname $memname : share memory name. eq. -m lidar_pc"<<std::endl;
  24. std::cout<<" -r --rollang $rollang : roll angle. eq. -r 10.0"<<std::endl;
  25. std::cout<<" -x --inclinationang_xaxis $inclinationang_xaxis : inclination angle from x axis. eq. -x 0.0"<<std::endl;
  26. std::cout<<" -y --inclinationang_yaxis $inclinationang_yaxis : inclination angle from y axis. eq. -y 0.0"<<std::endl;
  27. // std::cout<<" -o --hostip $hostip : host ip. eq. -o 192.168.1.111"<<std::endl;
  28. std::cout<<" -p --port $port : port . eq. -p 2368"<<std::endl;
  29. std::cout<<" -d --devport $port : port . eq. -d 2369"<<std::endl;
  30. std::cout<<" -f --devport $port : port . eq. -f 0"<<std::endl;
  31. std::cout<<" -s --setyaml $yaml : port . eq. -s rs1.yaml"<<std::endl;
  32. std::cout<<" -h --help print help"<<std::endl;
  33. }
  34. int GetOptLong(int argc, char *argv[]) {
  35. int nRtn = 0;
  36. int opt; // getopt_long() 的返回值
  37. int digit_optind = 0; // 设置短参数类型及是否需要参数
  38. (void)digit_optind;
  39. // 如果option_index非空,它指向的变量将记录当前找到参数符合long_opts里的
  40. // 第几个元素的描述,即是long_opts的下标值
  41. int option_index = 0;
  42. // 设置短参数类型及是否需要参数
  43. const char *optstring = "n:m:r:x:y:p:d:f:s:h";
  44. // 设置长参数类型及其简写,比如 --reqarg <==>-r
  45. /*
  46. struct option {
  47. const char * name; // 参数的名称
  48. int has_arg; // 是否带参数值,有三种:no_argument, required_argument,optional_argument
  49. int * flag; // 为空时,函数直接将 val 的数值从getopt_long的返回值返回出去,
  50. // 当非空时,val的值会被赋到 flag 指向的整型数中,而函数返回值为0
  51. int val; // 用于指定函数找到该选项时的返回值,或者当flag非空时指定flag指向的数据的值
  52. };
  53. 其中:
  54. no_argument(即0),表明这个长参数不带参数(即不带数值,如:--name)
  55. required_argument(即1),表明这个长参数必须带参数(即必须带数值,如:--name Bob)
  56. optional_argument(即2),表明这个长参数后面带的参数是可选的,(即--name和--name Bob均可)
  57. */
  58. static struct option long_options[] = {
  59. {"modulename", required_argument, NULL, 'n'},
  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. {"port", required_argument, NULL, 'p'},
  65. {"devport", required_argument, NULL, 'd'},
  66. {"devmode", required_argument, NULL, 'f'},
  67. {"setyaml", required_argument, NULL, 's'},
  68. {"help", no_argument, NULL, 'h'},
  69. // {"optarg", optional_argument, NULL, 'o'},
  70. {0, 0, 0, 0} // 添加 {0, 0, 0, 0} 是为了防止输入空值
  71. };
  72. while ( (opt = getopt_long(argc,
  73. argv,
  74. optstring,
  75. long_options,
  76. &option_index)) != -1) {
  77. // printf("opt = %c\n", opt); // 命令参数,亦即 -a -b -n -r
  78. // printf("optarg = %s\n", optarg); // 参数内容
  79. // printf("optind = %d\n", optind); // 下一个被处理的下标值
  80. // printf("argv[optind - 1] = %s\n", argv[optind - 1]); // 参数内容
  81. // printf("option_index = %d\n", option_index); // 当前打印参数的下标值
  82. // printf("\n");
  83. switch(opt)
  84. {
  85. case 'n':
  86. strncpy(gstr_modulename,optarg,255);
  87. break;
  88. case 'm':
  89. strncpy(gstr_memname,optarg,255);
  90. break;
  91. case 'r':
  92. strncpy(gstr_rollang,optarg,255);
  93. break;
  94. case 'x':
  95. strncpy(gstr_inclinationang_xaxis,optarg,255);
  96. break;
  97. case 'y':
  98. strncpy(gstr_inclinationang_yaxis,optarg,255);
  99. break;
  100. // case 'o':
  101. // strncpy(gstr_hostip,optarg,255);
  102. // break;
  103. case 'p':
  104. strncpy(gstr_port,optarg,255);
  105. break;
  106. case 'd':
  107. strncpy(gstr_devport,optarg,255);
  108. break;
  109. case 'f':
  110. strncpy(gstr_devmode,optarg,255);
  111. break;
  112. case 's':
  113. strncpy(gstr_yaml,optarg,255);
  114. break;
  115. case 'h':
  116. print_useage();
  117. nRtn = 1; //because use -h
  118. break;
  119. default:
  120. break;
  121. }
  122. }
  123. return nRtn;
  124. }
  125. /**
  126. * @brief decodeyaml
  127. * @param stryaml yaml path
  128. */
  129. void decodeyaml(const char * stryaml)
  130. {
  131. YAML::Node config;
  132. try
  133. {
  134. config = YAML::LoadFile(stryaml);
  135. }
  136. catch(YAML::BadFile e)
  137. {
  138. qDebug("load yaml error.");
  139. return;
  140. }
  141. if(config["memname"])
  142. {
  143. strncpy(gstr_memname,config["memname"].as<std::string>().data(),255);
  144. }
  145. if(config["rollang"])
  146. {
  147. strncpy(gstr_rollang,config["rollang"].as<std::string>().data(),255);
  148. }
  149. if(config["inclinationang_xaxis"])
  150. {
  151. strncpy(gstr_inclinationang_xaxis,config["inclinationang_xaxis"].as<std::string>().data(),255);
  152. }
  153. if(config["inclinationang_yaxis"])
  154. {
  155. strncpy(gstr_inclinationang_yaxis,config["inclinationang_yaxis"].as<std::string>().data(),255);
  156. }
  157. // if(config["hostip"])
  158. // {
  159. // strncpy(gstr_hostip,config["hostip"].as<std::string>().data(),255);
  160. // }
  161. if(config["port"])
  162. {
  163. strncpy(gstr_port,config["port"].as<std::string>().data(),255);
  164. }
  165. if(config["devport"])
  166. {
  167. strncpy(gstr_devport,config["devport"].as<std::string>().data(),255);
  168. }
  169. if(config["devmode"])
  170. {
  171. strncpy(gstr_devmode,config["port"].as<std::string>().data(),255);
  172. }
  173. // std::cout<<gstr_memname<<std::endl;
  174. // std::cout<<gstr_rollang<<std::endl;
  175. // std::cout<<gstr_inclinationang_xaxis<<std::endl;
  176. // std::cout<<gstr_inclinationang_yaxis<<std::endl;
  177. // std::cout<<gstr_hostip<<std::endl;
  178. // std::cout<<gstr_port<<std::endl;
  179. }
  180. int main(int argc, char *argv[])
  181. {
  182. QCoreApplication a(argc, argv);
  183. snprintf(gstr_memname,255,"lidar_pc");
  184. snprintf(gstr_rollang,255,"270.0");//这个角度要根据安装情况进行校正!!!!!!!!
  185. snprintf(gstr_inclinationang_xaxis,255,"0.0");
  186. snprintf(gstr_inclinationang_yaxis,255,"0");
  187. // snprintf(gstr_hostip,255,"192.168.1.102");
  188. snprintf(gstr_port,255,"2368");//默认端口号
  189. snprintf(gstr_devport,255,"2369");//默认端口号
  190. snprintf(gstr_devmode,255,"0");//默认端口号
  191. snprintf(gstr_yaml,255,"p");
  192. snprintf(gstr_modulename,255,"driver_lidar_leishen32");
  193. int nRtn = GetOptLong(argc,argv);
  194. if(nRtn == 1) //show help,so exit.
  195. {
  196. return 0;
  197. }
  198. if(strnlen(gstr_yaml,255)>1)
  199. {
  200. decodeyaml(gstr_yaml);
  201. }
  202. leishen32 * pleishen32 = new leishen32(gstr_memname,atof(gstr_rollang),atof(gstr_inclinationang_xaxis),
  203. atof(gstr_inclinationang_yaxis),atoi(gstr_devmode),static_cast<unsigned short>(atoi(gstr_port)),
  204. static_cast<unsigned short>(atoi(gstr_devport)));
  205. (void)pleishen32;
  206. return a.exec();
  207. }