vlp16_driver_test.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * @Description: vlp16驱动测试
  3. * @Author: yct
  4. * @Date: 2021-07-26 14:11:26
  5. * @LastEditTime: 2022-01-20 13:57:25
  6. * @LastEditors: yct
  7. */
  8. #include <iostream>
  9. #include <fstream>
  10. // velodyne driver seperation test
  11. #include "../velodyne_lidar/velodyne_driver/input.h"
  12. #include "../velodyne_lidar/velodyne_driver/rawdata.h"
  13. GOOGLE_GLOG_DLL_DECL void shut_down_logging(const char* data, int size)
  14. {
  15. time_t tt;
  16. time( &tt );
  17. tt = tt + 8*3600; // transform the time zone
  18. tm* t= gmtime( &tt );
  19. char buf[255]={0};
  20. sprintf(buf,"./%d%02d%02d-%02d%02d%02d-dump.txt",
  21. t->tm_year + 1900,
  22. t->tm_mon + 1,
  23. t->tm_mday,
  24. t->tm_hour,
  25. t->tm_min,
  26. t->tm_sec);
  27. FILE* tp_file=fopen(buf,"w");
  28. fprintf(tp_file,data,strlen(data));
  29. fclose(tp_file);
  30. }
  31. bool velo_driver_test()
  32. {
  33. velodyne_driver::InputSocket sock;
  34. if (!sock.init("", 2368))
  35. {
  36. std::cout << "sock init failed" << std::endl;
  37. return false;
  38. }
  39. velodyne_rawdata::RawData data;
  40. data.setup("VLP16", "/home/youchen/extra_space/chutian/measure/puai_wj_2021/setting/VLP16db.yaml", 1.2, 0.1, 0, 360);
  41. int count = 0;
  42. unsigned char pkt[1206];
  43. std::vector<Lidar_point> t_point;
  44. while (count++ < 50000)
  45. {
  46. int ret = sock.getPacket(pkt);
  47. if (ret != 0)
  48. std::cout << "get pack: " << ret << std::endl;
  49. else
  50. {
  51. // t_point.clear();
  52. // memset(&pkt[1205], 0, 1);
  53. // printf("%s\n", pkt);
  54. data.unpack(pkt, t_point);
  55. std::cout << "point size: " << t_point.size() << std::endl;
  56. if(t_point.size() > 30000)
  57. {
  58. std::ofstream out("/home/youchen/extra_space/chutian/measure/puai_wj_2021/build/point.txt");
  59. if(out.is_open())
  60. {
  61. for (size_t i = 0; i < t_point.size(); i++)
  62. {
  63. char buf[255] = {0};
  64. sprintf(buf, "%.3f %.3f %.3f %.6f\n", t_point[i].x, t_point[i].y, t_point[i].z, t_point[i].timestamp);
  65. out << buf;
  66. }
  67. out.close();
  68. return true;
  69. }
  70. else
  71. {
  72. std::cout << "cannot open file to write cloud." << std::endl;
  73. }
  74. }
  75. }
  76. usleep(500);
  77. }
  78. sock.uninit();
  79. return true;
  80. }
  81. // velodyne driver combination test
  82. #include "../velodyne_lidar/velodyne_driver/velodyne_lidar_device.h"
  83. void device_test()
  84. {
  85. velodyne::velodyneLidarParams params;
  86. params.set_ip("");
  87. params.set_port(2368);
  88. params.set_model("VLP16");
  89. params.set_calibrationfile("/home/youchen/extra_space/chutian/measure/puai_wj_2021/setting/VLP16db.yaml");
  90. params.mutable_calib()->set_y(1.57);
  91. Velodyne_lidar_device t_device;
  92. t_device.init(params);
  93. usleep(1000 * 1000);
  94. std::mutex p_mutex;
  95. pcl::PointCloud<pcl::PointXYZ>::Ptr p_cloud(new pcl::PointCloud<pcl::PointXYZ>);
  96. Error_manager ec = t_device.get_new_cloud_and_wait(&p_mutex, p_cloud, std::chrono::steady_clock::now(), 6000);
  97. if (ec == SUCCESS)
  98. {
  99. std::ofstream out("/home/youchen/extra_space/chutian/measure/puai_wj_2021/build/device_point.txt");
  100. if (out.is_open())
  101. {
  102. for (size_t i = 0; i < p_cloud->size(); i++)
  103. {
  104. char buf[255] = {0};
  105. sprintf(buf, "%.3f %.3f %.3f\n", p_cloud->points[i].x, p_cloud->points[i].y, p_cloud->points[i].z);
  106. out << buf;
  107. }
  108. std::cout << "cloud written" << std::endl;
  109. out.close();
  110. }
  111. else
  112. {
  113. std::cout << "cannot open file to write cloud." << std::endl;
  114. }
  115. }else{
  116. std::cout << "get cloud failed" << std::endl;
  117. }
  118. t_device.uninit();
  119. }
  120. // ground region class function test
  121. #include "../velodyne_lidar/ground_region.h"
  122. #include "../tool/measure_filter.h"
  123. #include "../tool/point_tool.h"
  124. #include "../tool/proto_tool.h"
  125. void region_detect()
  126. {
  127. std::string exce = "../tests/region_4_26x_g.txt", envi = "../tests/region_4_env_g.txt";
  128. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_exce(new pcl::PointCloud<pcl::PointXYZ>);
  129. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_envi(new pcl::PointCloud<pcl::PointXYZ>);
  130. bool res = read_pointcloud(exce, cloud_exce);
  131. res &= read_pointcloud(envi, cloud_envi);
  132. if (!res)
  133. {
  134. std::cout << "cannot open file to read cloud." << std::endl;
  135. return;
  136. }
  137. else
  138. {
  139. velodyne::velodyneManagerParams t_velo_params;
  140. std::string prototxt_path = "../setting/velodyne_manager.prototxt";
  141. if (!proto_tool::read_proto_param(prototxt_path, t_velo_params) || t_velo_params.region_size()<2)
  142. {
  143. return;
  144. }
  145. // 初始化region
  146. velodyne::Region param;
  147. param.CopyFrom(t_velo_params.region(1));
  148. pcl::PointCloud<pcl::PointXYZ>::Ptr left = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>);
  149. pcl::PointCloud<pcl::PointXYZ>::Ptr right = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>);
  150. if (!read_pointcloud("../setting/left_model.txt", left) || !read_pointcloud("../setting/right_model.txt", right))
  151. {
  152. std::cout << "cannot read left/right model " << std::endl;
  153. return;
  154. }
  155. Ground_region t_region;
  156. std::cout << "before init" << std::endl;
  157. t_region.init(param, left, right);
  158. std::cout << "after init" << std::endl;
  159. // 测试region功能
  160. // 手动检测,测试计算耗时
  161. for (size_t i = 0; i < 600; i++)
  162. {
  163. detect_wheel_ceres3d::Detect_result t_result;
  164. Error_manager ec = t_region.detect(cloud_exce, t_result);
  165. std::cout << ec.to_string() << std::endl;
  166. usleep(1);
  167. ec = t_region.detect(cloud_envi, t_result);
  168. std::cout << ec.to_string() << std::endl;
  169. usleep(1);
  170. }
  171. // 检测自动识别功能
  172. // std::cout << "---------------" << std::endl;
  173. // for (size_t i = 0; i < 12; i++)
  174. // {
  175. // t_region.update_cloud(t_cloud);
  176. // usleep(1000*100);
  177. // }
  178. // Common_data::Car_wheel_information t_wheel_info;
  179. // ec = Measure_filter::get_instance_references().get_filtered_wheel_information(0, t_wheel_info);
  180. // if (ec == SUCCESS)
  181. // {
  182. // std::cout << t_wheel_info.to_string() << std::endl;
  183. // }
  184. // else { std::cout << ec.to_string() << std::endl; }
  185. }
  186. }
  187. // velodyne manager test
  188. #include "../velodyne_lidar/velodyne_manager.h"
  189. void velo_manager_test()
  190. {
  191. Velodyne_manager_task t_velodyne_manager_task;
  192. Common_data::Car_wheel_information t_car_information_by_velodyne;
  193. t_velodyne_manager_task.task_init(TASK_CREATED,"",NULL,std::chrono::milliseconds(3000),
  194. 0, std::chrono::system_clock::now());
  195. Error_manager ec = Velodyne_manager::get_instance_references().velodyne_manager_init(0);
  196. std::cout << "veodyne_manager = " << Velodyne_manager::get_instance_references().check_status().to_string() << std::endl;
  197. if(ec != SUCCESS)
  198. {
  199. std::cout << "manager init failed: " << ec.to_string() << std::endl;
  200. return;
  201. }
  202. usleep(1000 * 500);
  203. Velodyne_manager::get_instance_references().execute_task(&t_velodyne_manager_task);
  204. ec = Velodyne_manager::get_instance_references().execute_task(&t_velodyne_manager_task);
  205. std::cout << "manager execute base check result: " << ec.to_string() << std::endl;
  206. // 等待处理完成,来自于system_executor
  207. while (t_velodyne_manager_task.is_task_end() == false)
  208. {
  209. if (t_velodyne_manager_task.is_over_time())
  210. {
  211. //超时处理。取消任务。
  212. Velodyne_manager::get_instance_pointer()->cancel_task(&t_velodyne_manager_task);
  213. ec.error_manager_reset(Error_code::VELODYNE_MANAGER_TASK_OVER_TIME, Error_level::MINOR_ERROR,
  214. " t_velodyne_manager_task is_over_time ");
  215. t_velodyne_manager_task.set_task_error_manager(ec);
  216. }
  217. else
  218. {
  219. //继续等待
  220. std::this_thread::sleep_for(std::chrono::microseconds(1));
  221. std::this_thread::yield();
  222. }
  223. }
  224. //提取任务单 的错误码
  225. ec = t_velodyne_manager_task.get_task_error_manager();
  226. if (ec == Error_code::SUCCESS)
  227. {
  228. t_car_information_by_velodyne = t_velodyne_manager_task.get_car_wheel_information();
  229. }
  230. else
  231. {
  232. LOG(INFO) << " velodyne_manager_task error :::::::" << t_velodyne_manager_task.get_task_error_manager().to_string();
  233. }
  234. std::cout << "--------------------------"<< std::endl;
  235. Velodyne_manager::get_instance_references().Velodyne_manager_uninit();
  236. }
  237. // 测试通信功能
  238. #include "../system/system_communication.h"
  239. #include "../system/system_executor.h"
  240. void message_test()
  241. {
  242. nnxx::socket socket{nnxx::SP, nnxx::BUS};
  243. // socket.bind("tcp://127.0.0.1:7000");
  244. socket.connect("tcp://192.168.1.233:30010");
  245. int n = 0;
  246. message::Base_msg base_msg;
  247. message::Ground_detect_request_msg t_ground_detect_request;
  248. message::Base_info t_base_info;
  249. t_base_info.set_msg_type(message::Message_type::eGround_detect_request_msg);
  250. t_base_info.set_timeout_ms(4000);
  251. t_base_info.set_sender(message::Communicator::eEmpty);
  252. t_base_info.set_receiver(message::Communicator::eGround_measurer);
  253. t_ground_detect_request.mutable_base_info()->CopyFrom(t_base_info);
  254. t_ground_detect_request.set_command_key("key for test only");
  255. t_ground_detect_request.mutable_id_struct()->set_terminal_id(0);
  256. usleep(1000 * 1000);
  257. // std::cout << "try to send" << std::endl;
  258. // socket.send(t_ground_detect_request.SerializeAsString());
  259. // usleep(1000 * 100);
  260. while (true)
  261. {
  262. // for (size_t i = 0; i < 10; i++)
  263. // {
  264. // 接收消息
  265. std::string t_receive_string = socket.recv<std::string>(1);
  266. if (t_receive_string.length() > 0)
  267. {
  268. bool result = base_msg.ParseFromString(t_receive_string);
  269. // 接收并打印车位状态信息
  270. std::cout << "====check result====================" << result << "============================" << std::endl;
  271. std::cout << "cycle " << n << std::endl;
  272. if (base_msg.base_info().msg_type() == message::Message_type::eGround_detect_response_msg)
  273. {
  274. message::Ground_detect_response_msg t_ground_detect_response;
  275. std::cout << "----------------- check result -----------------" << std::endl;
  276. t_ground_detect_response.ParseFromString(t_receive_string);
  277. std::cout << "result: "<<t_ground_detect_response.DebugString()<< std::endl;
  278. // std::cout << sift_response.DebugString() << std::endl;
  279. // continue;
  280. }else if(base_msg.base_info().msg_type() == message::Message_type::eGround_status_msg)
  281. {
  282. message::Ground_status_msg t_status_msg;
  283. std::cout << "----------------- heartbeat -----------------" << std::endl;
  284. t_status_msg.ParseFromString(t_receive_string);
  285. std::cout << "heartbeat: "<<t_status_msg.DebugString()<< std::endl;
  286. }
  287. }
  288. // }
  289. // std::this_thread::yield();
  290. n++;
  291. usleep(1000 * 1);
  292. }
  293. }
  294. int main()
  295. {
  296. const char* logPath = "./log/";
  297. google::InitGoogleLogging("LidarMeasurement");
  298. google::SetStderrLogging(google::INFO);
  299. google::SetLogDestination(0, logPath);
  300. google::SetLogFilenameExtension("zxlog");
  301. google::InstallFailureSignalHandler();
  302. google::InstallFailureWriter(&shut_down_logging);
  303. FLAGS_colorlogtostderr = true; // Set log color
  304. FLAGS_logbufsecs = 0; // Set log output speed(s)
  305. FLAGS_max_log_size = 1024; // Set max log file size(GB)
  306. FLAGS_stop_logging_if_full_disk = true;
  307. // velo_driver_test();
  308. // device_test();
  309. // region_detect();
  310. // velo_manager_test();
  311. message_test();
  312. return 0;
  313. }