demo_pcap.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*********************************************************************************************************************
  2. Copyright (c) 2020 RoboSense
  3. All rights reserved
  4. By downloading, copying, installing or using the software you agree to this license. If you do not agree to this
  5. license, do not download, install, copy or use the software.
  6. License Agreement
  7. For RoboSense LiDAR SDK Library
  8. (3-clause BSD License)
  9. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
  10. following conditions are met:
  11. 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
  12. disclaimer.
  13. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
  14. disclaimer in the documentation and/or other materials provided with the distribution.
  15. 3. Neither the names of the RoboSense, nor Suteng Innovation Technology, nor the names of other contributors may be used
  16. to endorse or promote products derived from this software without specific prior written permission.
  17. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  18. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  22. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. *********************************************************************************************************************/
  25. #include <rs_driver/api/lidar_driver.hpp>
  26. #ifdef ENABLE_PCL_POINTCLOUD
  27. #include <rs_driver/msg/pcl_point_cloud_msg.hpp>
  28. #else
  29. #include <rs_driver/msg/point_cloud_msg.hpp>
  30. #endif
  31. //#define ORDERLY_EXIT
  32. typedef PointXYZI PointT;
  33. typedef PointCloudT<PointT> PointCloudMsg;
  34. using namespace robosense::lidar;
  35. SyncQueue<std::shared_ptr<PointCloudMsg>> free_cloud_queue;
  36. SyncQueue<std::shared_ptr<PointCloudMsg>> stuffed_cloud_queue;
  37. //
  38. // @brief point cloud callback function. The caller should register it to the lidar driver.
  39. // Via this fucntion, the driver gets an free/unused point cloud message from the caller.
  40. // @param msg The free/unused point cloud message.
  41. //
  42. std::shared_ptr<PointCloudMsg> driverGetPointCloudFromCallerCallback(void)
  43. {
  44. // Note: This callback function runs in the packet-parsing/point-cloud-constructing thread of the driver,
  45. // so please DO NOT do time-consuming task here.
  46. std::shared_ptr<PointCloudMsg> msg = free_cloud_queue.pop();
  47. if (msg.get() != NULL)
  48. {
  49. return msg;
  50. }
  51. return std::make_shared<PointCloudMsg>();
  52. }
  53. //
  54. // @brief point cloud callback function. The caller should register it to the lidar driver.
  55. // Via this function, the driver gets/returns a stuffed point cloud message to the caller.
  56. // @param msg The stuffed point cloud message.
  57. //
  58. void driverReturnPointCloudToCallerCallback(std::shared_ptr<PointCloudMsg> msg)
  59. {
  60. // Note: This callback function runs in the packet-parsing/point-cloud-constructing thread of the driver,
  61. // so please DO NOT do time-consuming task here. Instead, process it in caller's own thread. (see processCloud() below)
  62. stuffed_cloud_queue.push(msg);
  63. }
  64. //
  65. // @brief exception callback function. The caller should register it to the lidar driver.
  66. // Via this function, the driver inform the caller that something happens.
  67. // @param code The error code to represent the error/warning/information
  68. //
  69. void exceptionCallback(const Error& code)
  70. {
  71. // Note: This callback function runs in the packet-receving and packet-parsing/point-cloud_constructing thread of the driver,
  72. // so please DO NOT do time-consuming task here.
  73. RS_WARNING << code.toString() << RS_REND;
  74. }
  75. bool to_exit_process = false;
  76. void processCloud(void)
  77. {
  78. while (!to_exit_process)
  79. {
  80. std::shared_ptr<PointCloudMsg> msg = stuffed_cloud_queue.popWait();
  81. if (msg.get() == NULL)
  82. {
  83. continue;
  84. }
  85. // Well, it is time to process the point cloud msg, even it is time-consuming.
  86. RS_MSG << "msg: " << msg->seq << " point cloud size: " << msg->points.size() << RS_REND;
  87. #if 0
  88. for (auto it = msg->points.begin(); it != msg->points.end(); it++)
  89. {
  90. std::cout << std::fixed << std::setprecision(3)
  91. << "(" << it->x << ", " << it->y << ", " << it->z << ", " << (int)it->intensity << ")"
  92. << std::endl;
  93. }
  94. #endif
  95. free_cloud_queue.push(msg);
  96. }
  97. }
  98. int main(int argc, char* argv[])
  99. {
  100. RS_TITLE << "------------------------------------------------------" << RS_REND;
  101. RS_TITLE << " RS_Driver Core Version: v" << getDriverVersion() << RS_REND;
  102. RS_TITLE << "------------------------------------------------------" << RS_REND;
  103. RSDriverParam param; ///< Create a parameter object
  104. param.input_type = InputType::PCAP_FILE;
  105. param.input_param.pcap_path = "/home/robosense/lidar.pcap"; ///< Set the pcap file directory
  106. param.input_param.msop_port = 6699; ///< Set the lidar msop port number, the default is 6699
  107. param.input_param.difop_port = 7788; ///< Set the lidar difop port number, the default is 7788
  108. param.lidar_type = LidarType::RSM1; ///< Set the lidar type. Make sure this type is correct
  109. param.print();
  110. LidarDriver<PointCloudMsg> driver; ///< Declare the driver object
  111. driver.regPointCloudCallback(driverGetPointCloudFromCallerCallback, driverReturnPointCloudToCallerCallback); ///< Register the point cloud callback functions
  112. driver.regExceptionCallback(exceptionCallback); ///< Register the exception callback function
  113. if (!driver.init(param)) ///< Call the init function
  114. {
  115. RS_ERROR << "Driver Initialize Error..." << RS_REND;
  116. return -1;
  117. }
  118. std::thread cloud_handle_thread = std::thread(processCloud);
  119. driver.start(); ///< The driver thread will start
  120. RS_DEBUG << "RoboSense Lidar-Driver Linux pcap demo start......" << RS_REND;
  121. #ifdef ORDERLY_EXIT
  122. std::this_thread::sleep_for(std::chrono::seconds(10));
  123. driver.stop();
  124. to_exit_process = true;
  125. cloud_handle_thread.join();
  126. #else
  127. while (true)
  128. {
  129. std::this_thread::sleep_for(std::chrono::seconds(1));
  130. }
  131. #endif
  132. return 0;
  133. }