wj_lidar_test.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include "../wj_lidar/wj_lidar_encapsulation.h"
  2. // #include <pcl/visualization/cloud_viewer.h>
  3. #include <fstream>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <stdint.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. void save_cloud_txt(std::string txt, pcl::PointCloud<pcl::PointXYZ>::Ptr pCloud)
  11. {
  12. std::ofstream os;
  13. os.open(txt, std::ios::out);
  14. for (int i = 0; i < pCloud->points.size(); i++)
  15. {
  16. pcl::PointXYZ point = pCloud->points[i];
  17. char buf[255];
  18. memset(buf, 0, 255);
  19. sprintf(buf, "%f %f %f\n", point.x, point.y, point.z);
  20. os.write(buf, strlen(buf));
  21. }
  22. os.close();
  23. }
  24. bool ReadProtoParam(std::string path, wj::wjLidarParams &params)
  25. {
  26. int fd = open(path.c_str(), O_RDONLY);
  27. if (fd == -1)
  28. return false;
  29. FileInputStream *input = new FileInputStream(fd);
  30. bool success = google::protobuf::TextFormat::Parse(input, &params);
  31. // std::cout<<m_global_param.data_path()<<std::endl;
  32. delete input;
  33. close(fd);
  34. return success;
  35. }
  36. bool ReadProtoParam(std::string path, wj::wjManagerParams &params)
  37. {
  38. int fd = open(path.c_str(), O_RDONLY);
  39. if (fd == -1)
  40. return false;
  41. FileInputStream *input = new FileInputStream(fd);
  42. bool success = google::protobuf::TextFormat::Parse(input, &params);
  43. // std::cout<<m_global_param.data_path()<<std::endl;
  44. delete input;
  45. close(fd);
  46. return success;
  47. }
  48. void lidar_test()
  49. {
  50. Wj_lidar_encapsulation *lidar = new Wj_lidar_encapsulation();
  51. wj::wjLidarParams params;
  52. bool result = ReadProtoParam("../src/test/wj_lidar_settings.prototxt", params);
  53. LOG(INFO) << "读配置结果" << result;
  54. // params.mutable_net_config()->set_ip_address("192.168.0.2");
  55. // params.mutable_net_config()->set_port(2110);
  56. if (lidar != 0)
  57. {
  58. Error_manager code = lidar->initialize(params);
  59. LOG(INFO) << code.to_string();
  60. // usleep(1000 * 1);
  61. if (code == SUCCESS)
  62. {
  63. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>);
  64. for (int i = 0; i < 10; i++)
  65. {
  66. LOG(INFO) << "connect status: " << lidar->get_connection_status();
  67. code = lidar->get_cloud(cloud, std::chrono::steady_clock::now(), 3000);
  68. if (code == SUCCESS)
  69. {
  70. char temp[100];
  71. memset(temp, 0, 100);
  72. sprintf(temp, "cloud_%d.txt", i);
  73. if (cloud->size() > 0)
  74. {
  75. save_cloud_txt(temp, cloud);
  76. LOG(INFO) << "cloud saved to " << std::string(temp);
  77. }
  78. }
  79. else
  80. {
  81. LOG(WARNING) << code.to_string();
  82. }
  83. usleep(1000 * 30);
  84. LOG(INFO) << "end of cycle";
  85. }
  86. }
  87. }
  88. delete lidar;
  89. }
  90. int main(int argc, char *argv[])
  91. {
  92. lidar_test();
  93. usleep(1000 * 1000);
  94. // lidar_test();
  95. // usleep(1000 * 1000);
  96. // lidar_test();
  97. // usleep(1000 * 1000);
  98. LOG(INFO) << "end";
  99. getchar();
  100. return 0;
  101. }