rslidar_driver.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #include "rslidar_driver.h"
  2. // 万集雷达封装类构造函数
  3. RslidarDevice::RslidarDevice() {
  4. m_rs_lidar_device_status = E_UNKNOWN;
  5. m_decoder_ptr_ = nullptr;
  6. }
  7. // 万集雷达封装类析构函数
  8. RslidarDevice::~RslidarDevice() {
  9. uninit();
  10. }
  11. Error_manager RslidarDevice::init() {
  12. return SUCCESS;
  13. }
  14. Error_manager RslidarDevice::init(RSDriverParam &param) {
  15. m_lidar_params = param;
  16. m_lidar_params.print();
  17. m_lidar_driver.regPointCloudCallback(std::bind(&RslidarDevice::driverGetPointCloudFromCallerCallback, this),
  18. std::bind(&RslidarDevice::driverReturnPointCloudToCallerCallback, this,
  19. std::placeholders::_1));
  20. m_lidar_driver.regExceptionCallback(std::bind(&RslidarDevice::exceptionCallback, this, std::placeholders::_1));
  21. m_lidar_driver.regPacketCallback(std::bind(&RslidarDevice::packetCallback, this, std::placeholders::_1));
  22. if (m_lidar_driver.init(param)) {
  23. m_decoder_ptr_ = DecoderFactory<PointCloudMsg>::createDecoder(param.lidar_type, param.decoder_param);
  24. m_decoder_ptr_->regCallback(&decoderexceptionCallback, &decoderCallback);
  25. m_decoder_ptr_->point_cloud_ = m_point_cloud_;
  26. m_lidar_driver.start();
  27. } else {
  28. LOG(INFO) << param.input_param.msop_port << " init falture";
  29. return FAILED;
  30. }
  31. return SUCCESS;
  32. }
  33. Error_manager RslidarDevice::uninit() {
  34. m_lidar_driver.stop();
  35. return Error_code::SUCCESS;
  36. }
  37. Error_manager RslidarDevice::get_last_cloud(std::vector<unsigned char> &data) {
  38. if (m_lidar_data.empty()) {
  39. return FAILED;
  40. }
  41. {
  42. std::lock_guard<std::mutex> lck(m_cloud_mutex);
  43. data.swap(m_lidar_data.front());
  44. m_lidar_data.pop_front();
  45. }
  46. DLOG(INFO) << "M.size " << data.size() / 7 << " " << m_lidar_data.size();
  47. return SUCCESS;
  48. }
  49. bool RslidarDevice::updateRSTransformParam(RSTransformParam &transform_param) {
  50. m_lidar_params.decoder_param.transform_param = transform_param;
  51. return true;
  52. }
  53. bool RslidarDevice::is_ready() {
  54. update_status();
  55. return m_rs_lidar_device_status == E_READY;
  56. }
  57. RslidarDevice::RSLIDAR_STATUS RslidarDevice::status() {
  58. update_status();
  59. return m_rs_lidar_device_status;
  60. }
  61. void RslidarDevice::update_status() {
  62. if (m_rs_protocol_status == E_FAULT) {
  63. m_rs_lidar_device_status = E_FAULT;
  64. } else if (m_rs_protocol_status == E_BUSY) {
  65. m_rs_lidar_device_status = E_BUSY;
  66. } else if (m_rs_protocol_status == E_READY) {
  67. m_rs_lidar_device_status = E_READY;
  68. } else {
  69. m_rs_lidar_device_status = E_UNKNOWN;
  70. }
  71. }
  72. std::shared_ptr<PointCloudMsg> RslidarDevice::driverGetPointCloudFromCallerCallback(void) {
  73. std::shared_ptr<PointCloudMsg> msg = free_cloud_queue.pop();
  74. if (msg.get() != nullptr) {
  75. return msg;
  76. }
  77. return std::make_shared<PointCloudMsg>();
  78. }
  79. int RslidarDevice::id() {
  80. return m_lidar_params.input_param.msop_port;
  81. }
  82. void RslidarDevice::driverReturnPointCloudToCallerCallback(std::shared_ptr<PointCloudMsg> msg) {
  83. // 先将之前队列的首个给释放回调函数
  84. free_cloud_queue.push(stuffed_cloud_queue.pop());
  85. // 再存入一个
  86. stuffed_cloud_queue.push(msg);
  87. DLOG(INFO) << "Get points form driver, point size: " << msg->points.size();
  88. // 转换
  89. if (!msg->points.empty()) {
  90. std::chrono::steady_clock::time_point ttt = std::chrono::steady_clock::now();
  91. DLOG(INFO) << m_decoder_ptr_->point_cloud_->points.begin()->x << " " << m_decoder_ptr_->point_cloud_->points.begin()->y;
  92. Packet packet;
  93. float x, y, z, x_y, r, roll, yaw;
  94. // 提前计算部分数据,可以在循环中少计算一点这些
  95. float trans_roll = (m_lidar_params.decoder_param.transform_param.roll * float(M_PI)) / 180.0f;
  96. float trans_pitch = (m_lidar_params.decoder_param.transform_param.pitch * float(M_PI)) / 180.0f;
  97. float trans_yaw = (m_lidar_params.decoder_param.transform_param.yaw * float(M_PI)) / 180.0f;
  98. float cosYaw = cos(trans_yaw);
  99. float sinYaw = sin(trans_yaw);
  100. float cosPitch = cos(trans_pitch);
  101. float sinPitch = sin(trans_pitch);
  102. float cosRoll = cos(trans_roll);
  103. float sinRoll = sin(trans_roll);
  104. float rotationMatrix[3][3] = {
  105. {cosYaw * cosPitch, cosYaw * sinPitch * sinRoll - sinYaw * cosRoll, cosYaw * sinPitch * cosRoll + sinYaw * sinRoll},
  106. {sinYaw * cosPitch, sinYaw * sinPitch * sinRoll + cosYaw * cosRoll, sinYaw * sinPitch * cosRoll - cosYaw * sinRoll},
  107. {-sinPitch, cosPitch * sinRoll, cosPitch * cosRoll}
  108. };
  109. float trans_x = m_lidar_params.decoder_param.transform_param.x * 0.01f;
  110. float trans_y = m_lidar_params.decoder_param.transform_param.y * 0.01f;
  111. float trans_z = m_lidar_params.decoder_param.transform_param.z * 0.01f;
  112. for (auto &point: msg->points) {
  113. if (point.intensity > 90) {
  114. continue;
  115. }
  116. x = rotationMatrix[0][0] * point.x + rotationMatrix[0][1] * point.y + rotationMatrix[0][2] * point.z + trans_x;
  117. y = rotationMatrix[1][0] * point.x + rotationMatrix[1][1] * point.y + rotationMatrix[1][2] * point.z + trans_y;
  118. z = rotationMatrix[2][0] * point.x + rotationMatrix[2][1] * point.y + rotationMatrix[2][2] * point.z + trans_z;
  119. r = sqrt(x * x + y * y + z * z) * 400.0f;
  120. roll = (atan2(z, sqrt(x * x + y * y)) * 18000.0f) / float(M_PI);
  121. yaw = (atan2(x, y) * 18000.0f) / float(M_PI);
  122. if (roll < 0) {
  123. roll += 36000;
  124. }
  125. if (roll > 36000) {
  126. roll -= 36000;
  127. }
  128. if (yaw < 0) {
  129. yaw += 36000;
  130. }
  131. if (yaw > 36000) {
  132. yaw -= 36000;
  133. }
  134. m_tmp_data.push_back((unsigned short) r >> 8);
  135. m_tmp_data.push_back((unsigned short) r);
  136. m_tmp_data.push_back((unsigned short) roll >> 8);
  137. m_tmp_data.push_back((unsigned short) roll);
  138. m_tmp_data.push_back((unsigned short) yaw >> 8);
  139. m_tmp_data.push_back((unsigned short) yaw);
  140. m_tmp_data.push_back(point.intensity);
  141. }
  142. std::chrono::duration<double> diff = std::chrono::steady_clock::now() - m_capture_time;
  143. if (diff.count() * 1000 > 10) {
  144. int tmp_size = m_tmp_data.size() / 7;
  145. for (int i = tmp_size; i < 2880 * 10; i++) {
  146. m_tmp_data.emplace_back(0);
  147. m_tmp_data.emplace_back(0);
  148. m_tmp_data.emplace_back(0);
  149. m_tmp_data.emplace_back(0);
  150. m_tmp_data.emplace_back(0);
  151. m_tmp_data.emplace_back(0);
  152. m_tmp_data.emplace_back(0);
  153. }
  154. m_capture_time = std::chrono::steady_clock::now();
  155. std::lock_guard<std::mutex> lck(m_cloud_mutex);
  156. DLOG(INFO) << "save cloud data size: " << m_tmp_data.size() / 7;
  157. m_lidar_data.push_back(m_tmp_data);
  158. m_tmp_data.clear();
  159. if (m_lidar_data.size() > 100) {
  160. m_lidar_data.pop_front();
  161. }
  162. }
  163. }
  164. }
  165. void RslidarDevice::exceptionCallback(const Error &code) {
  166. }
  167. void RslidarDevice::packetCallback(const Packet &pkt) {
  168. return;
  169. }