main.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //
  2. // Created by zx on 2019/12/28.
  3. //
  4. #include <iostream>
  5. #include "./laser/Laser.h"
  6. #include "./laser/LivoxLaser.h"
  7. #include "./error_code/error_code.h"
  8. #include "LogFiles.h"
  9. #include <string.h>
  10. #include <fcntl.h>
  11. #include <google/protobuf/io/zero_copy_stream_impl.h>
  12. #include <google/protobuf/text_format.h>
  13. //#include </usr/local/include/google/protobuf/io/zero_copy_stream_impl.h>
  14. using google::protobuf::io::FileInputStream;
  15. using google::protobuf::io::FileOutputStream;
  16. using google::protobuf::io::ZeroCopyInputStream;
  17. using google::protobuf::io::CodedInputStream;
  18. using google::protobuf::io::ZeroCopyOutputStream;
  19. using google::protobuf::io::CodedOutputStream;
  20. using google::protobuf::Message;
  21. #define O_RDONLY 00
  22. #define LIVOX_NUMBER 2
  23. //读取protobuf 配置文件
  24. //file:文件
  25. //parameter:要读取的配置
  26. bool read_proto_param(std::string file, ::google::protobuf::Message& parameter)
  27. {
  28. int fd = open(file.c_str(), O_RDONLY);
  29. if (fd == -1) return false;
  30. FileInputStream* input = new FileInputStream(fd);
  31. bool success = google::protobuf::TextFormat::Parse(input, &parameter);
  32. delete input;
  33. close(fd);
  34. return success;
  35. }
  36. int main(int argc,char* argv[])
  37. {
  38. std::cout << "huli 101 main start!" << std::endl;
  39. Laser_base * m_laser_vector_array[LIVOX_NUMBER];
  40. Error_manager err;
  41. for (int i = 0; i < LIVOX_NUMBER; ++i)
  42. {
  43. Laser_base *m_laser_vector;
  44. Laser_proto::laser_parameter laser_param;
  45. laser_param.set_frame_num(1000);
  46. laser_param.set_type("Livox");
  47. if ( i==0 )
  48. {
  49. laser_param.set_sn("0TFDFG700601881");
  50. }
  51. else if ( i==1 )
  52. {
  53. laser_param.set_sn("0TFDFCE00502001");
  54. }
  55. // int i = 0;
  56. m_laser_vector_array[i] = m_laser_vector = LaserRegistory::CreateLaser(laser_param.type(), i,
  57. laser_param);
  58. if (m_laser_vector != NULL)
  59. {
  60. err = m_laser_vector->connect_laser();
  61. if ( err != Error_code::SUCCESS)
  62. {
  63. char description[255] = {0};
  64. sprintf(description, "Laser %d connect failed...", i);
  65. err.error_manager_reset(LASER_CONNECT_FAILED, NORMAL, description);
  66. }
  67. else
  68. {
  69. std::cout << "huli 301 connect_laser Error_code::SUCCESS" << std::endl;
  70. }
  71. cout << "huli 311 " << err.to_string() << endl;
  72. }
  73. else
  74. {
  75. cout << "huli: 601 :" << err.to_string() << endl;
  76. }
  77. }
  78. if (!Start()) {
  79. Uninit();
  80. printf("Livox-SDK init fail!\n");
  81. }
  82. else
  83. {
  84. std::cout << "huli 201 Livox-SDK start" << std::endl;
  85. }
  86. while(1)
  87. {
  88. pcl::PointCloud<pcl::PointXYZ>::Ptr scan_cloud(new pcl::PointCloud<pcl::PointXYZ>);
  89. std::mutex cloud_lock;
  90. Laser_task *laser_task = new Laser_task();
  91. //
  92. laser_task->task_init(TASK_CREATED, scan_cloud, &cloud_lock);
  93. laser_task->set_task_frame_maxnum(1000);
  94. // //保存文件初始化
  95. // char time_string[256] = { 0 };
  96. // sprintf(time_string, "../data/%d/", 0);
  97. //
  98. // laser_task->set_task_save_path(time_string);
  99. std::cout << "huli 401 connect_laser Error_code::SUCCESS" << std::endl;
  100. std::cout << " press to start" << std::endl;
  101. char ch ;
  102. // = getchar();
  103. std::cin >> ch ;
  104. if ( ch == 'b' )
  105. {
  106. std::cout << " end scan ------------" << std::endl;
  107. break;
  108. }
  109. std::cout << " start scan ------------" << std::endl;
  110. // err = m_laser_vector_array[0]->execute_task(laser_task);
  111. // usleep(3000 * 1000);
  112. // cout << "huli: 501 :" << err.to_string() << endl;
  113. int n = 5;
  114. while(n)
  115. {
  116. n--;
  117. for (int i = 0; i < LIVOX_NUMBER; ++i)
  118. {
  119. char time_string[256] = { 0 };
  120. sprintf(time_string, "../data/%d/", i);
  121. laser_task->set_task_save_path(time_string);
  122. laser_task->set_task_save_flag(true);
  123. err = m_laser_vector_array[i]->execute_task(laser_task);
  124. // usleep(3000 * 1000);
  125. }
  126. usleep(3000 * 1000);
  127. cout << "huli: 501 :" << err.to_string() << endl;
  128. cout << "huli: 501 :" << err.to_string() << endl;
  129. }
  130. cout << "huli: 601 :" << err.to_string() << endl;
  131. //发送任务单给雷达
  132. std::this_thread::sleep_for(std::chrono::seconds(5));
  133. }
  134. for (int i = 0; i < LIVOX_NUMBER; ++i)
  135. {
  136. std::this_thread::sleep_for(std::chrono::seconds(5));
  137. cout << "huli: 701 :" << err.to_string() << endl;
  138. m_laser_vector_array[i]->disconnect_laser();
  139. cout << "huli: 801 :" << err.to_string() << endl;
  140. m_laser_vector_array[i]->close_save_path();
  141. cout << "huli: 901 :" << err.to_string() << endl;
  142. std::this_thread::sleep_for(std::chrono::seconds(2));
  143. }
  144. Uninit();
  145. cout << "huli 1234 main end" << endl;
  146. }