rslidar_mqtt_async.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include "rslidar_mqtt_async.h"
  2. void RslidarMqttAsyncClient::init() {
  3. m_message_arrived_callback_ = CloudDataArrived;
  4. LOG(INFO) << "load mqtt etc file from: " << ETC_PATH"/etc/rslidar_mqtt_async.json";
  5. MqttAsyncClient::init_from_proto(ETC_PATH"/etc/rslidar_mqtt_async.json");
  6. }
  7. bool RslidarMqttAsyncClient::setCloudData(const std::string &topic, pcl::PointCloud<pcl::PointALL>::Ptr &cloud) {
  8. if (cloud->empty()) {
  9. printf("---Debug cloud size %ld\n", cloud->size());
  10. return false;
  11. }
  12. auto iter_full_cloud = m_full_cloud_.find(topic);
  13. if (iter_full_cloud == m_full_cloud_.end()) {
  14. m_full_cloud_.insert(std::pair<std::string, pcl::PointCloud<pcl::PointALL>::Ptr>(topic,new pcl::PointCloud<pcl::PointALL>));
  15. iter_full_cloud = m_full_cloud_.find(topic);
  16. }
  17. {
  18. std::lock_guard<std::mutex> lck(m_cloud_mutex);
  19. iter_full_cloud->second->swap(*cloud);
  20. }
  21. return true;
  22. }
  23. bool RslidarMqttAsyncClient::getCloudData(const std::string &topic, pcl::PointCloud<pcl::PointXYZI>::Ptr &cloud) {
  24. std::lock_guard<std::mutex> lck(m_cloud_mutex);
  25. auto iter = m_full_cloud_.find(topic);
  26. if (iter == m_full_cloud_.end()) {
  27. LOG(WARNING) << topic << " can't find data.";
  28. return false;
  29. }
  30. cloud->clear();
  31. pcl::PointXYZI cloud_p;
  32. for (auto &point:*iter->second) {
  33. cloud_p.x = point.x;
  34. cloud_p.y = point.y;
  35. cloud_p.x = point.z;
  36. cloud_p.intensity = point.intensity;
  37. cloud->points.push_back(cloud_p);
  38. }
  39. return true;
  40. }
  41. bool RslidarMqttAsyncClient::getCloudData(const std::string &topic, pcl::PointCloud<pcl::PointXYZ>::Ptr &cloud) {
  42. std::lock_guard<std::mutex> lck(m_cloud_mutex);
  43. auto iter = m_full_cloud_.find(topic);
  44. if (iter == m_full_cloud_.end()) {
  45. return false;
  46. }
  47. cloud->clear();
  48. for (auto &point: *iter->second) {
  49. cloud->points.emplace_back(point.x, point.y, point.z);
  50. }
  51. return true;
  52. }
  53. bool RslidarMqttAsyncClient::getCloudData(const std::string &topic, pcl::PointCloud<pcl::PointXYZRGB>::Ptr &cloud, const uint8_t *color) {
  54. std::lock_guard<std::mutex> lck(m_cloud_mutex);
  55. auto iter = m_full_cloud_.find(topic);
  56. if (iter == m_full_cloud_.end()) {
  57. return false;
  58. }
  59. cloud->clear();
  60. pcl::PointXYZRGB rgb;
  61. for (auto &point: *iter->second) {
  62. rgb.x = point.x;
  63. rgb.y = point.y;
  64. rgb.z = point.z;
  65. rgb.r = color[0];
  66. rgb.g = color[1];
  67. rgb.b = color[2];
  68. cloud->points.emplace_back(rgb);
  69. }
  70. return true;
  71. }
  72. int RslidarMqttAsyncClient::CloudDataArrived(MqttAsyncClient *client, char *topicName, int topicLen,
  73. MQTTAsync_message *message) {
  74. pcl::PointCloud<pcl::PointALL>::Ptr cloud(new pcl::PointCloud<pcl::PointALL>);
  75. CloudPointUnpacket unpacket;
  76. unpacket.unpacket((unsigned char *) message->payload, message->payloadlen, cloud);
  77. RslidarMqttAsyncClient *t_client = (RslidarMqttAsyncClient *) client;
  78. t_client->setCloudData(topicName, cloud);
  79. return 1;
  80. }