LivoxHorizon.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include "LivoxHorizon.h"
  2. #include "livox_driver.h"
  3. RegisterLaser(Horizon)
  4. CHorizonLaser::CHorizonLaser(int id, Laser_proto::laser_parameter laser_param)
  5. :CLivoxLaser(id, laser_param)
  6. {
  7. Error_manager t_error;
  8. m_frame_count = 0;
  9. m_frame_maxnum = laser_param.frame_num();//初始化从外部参数导入,实际运行前从任务单导入.
  10. //默认handle为-1,切记不能初始化为0.因为0表示0号雷达,是有效的.....
  11. m_livox_device.handle = 255;
  12. //判断参数类型,
  13. if(laser_param.type()=="Horizon")
  14. {
  15. //填充雷达设备的广播码
  16. t_error = Livox_driver::get_instance_references().Livox_insert_sn_laser(laser_param.sn(), this);
  17. //此时不进行驱动层的初始化,在所有雷达创建完成之后,在laser_manager统一初始化.
  18. if ( t_error != SUCCESS)
  19. {
  20. //如果sn码不规范,或者sn码重复都会引发错误,
  21. //故障处理,
  22. // 方案一:回退.取消创建雷达的操作.释放雷达内存.
  23. //方案二:允许创建雷达,但是状态改为故障.禁用后续的功能.
  24. //以后再写.
  25. ;
  26. }
  27. }
  28. }
  29. CHorizonLaser::~CHorizonLaser()
  30. {
  31. //清空队列
  32. m_queue_livox_data.clear_and_delete();
  33. }
  34. //将二进制消息转化为三维点云的功能函数,每次只转化一个CBinaryData,
  35. // 纯虚函数,必须由子类重载,
  36. Error_manager CHorizonLaser::transform_buf_to_points()
  37. {
  38. Error_manager t_error;
  39. Binary_buf* tp_binaty_buf=NULL;
  40. //从队列中取出缓存。 tp_binaty_buf的内存 从链表取出时,就自带内存了。需要手动释放。
  41. if(m_queue_laser_data.try_pop(tp_binaty_buf))
  42. {
  43. //livox的二进制数据格式就是三维点坐标。直接强行转化指针类型即可
  44. LivoxExtendRawPoint *tp_Livox_data = (LivoxExtendRawPoint *)tp_binaty_buf->get_buf();
  45. //计算这一帧数据有多少个三维点。
  46. int t_count = tp_binaty_buf->get_length() / (sizeof(LivoxExtendRawPoint));
  47. //转变三维点格式,并存入vector。
  48. for (int i = 0; i < t_count; ++i)
  49. {
  50. //三维点的坐标变换,从雷达自己的坐标系,转化到公共坐标系,(目前以plc为公共坐标系)
  51. CPoint3D point_source;
  52. point_source.x = tp_Livox_data[i].x;
  53. point_source.y = tp_Livox_data[i].y;
  54. point_source.z = tp_Livox_data[i].z;
  55. CPoint3D point_destination = transform_by_matrix(point_source);
  56. //保存雷达扫描 三维点云的最终结果。
  57. if(m_save_flag)
  58. {
  59. char buf[64] = {0};
  60. sprintf(buf, "%f %f %f\n", point_destination.x, point_destination.y, point_destination.z);
  61. m_points_log_tool.write(buf, strlen(buf));
  62. }
  63. //LivoxExtendRawPoint 转为 pcl::PointXYZ
  64. //int32_t 转 double。不要信号强度
  65. pcl::PointXYZ pointXYZ;
  66. pointXYZ.x = point_destination.x/1000.0;
  67. pointXYZ.y = point_destination.y/1000.0;
  68. pointXYZ.z = point_destination.z/1000.0;
  69. //注:单位毫米转为米, 最终数据单位统一为米.....
  70. t_error = mp_laser_task->task_push_point(&pointXYZ);
  71. if ( t_error != Error_code::SUCCESS )
  72. {
  73. delete tp_binaty_buf;
  74. return t_error;
  75. }
  76. }
  77. //统计扫描点个数。
  78. m_points_count += t_count;
  79. delete tp_binaty_buf;
  80. }
  81. else
  82. {
  83. return NODATA;// m_queue_laser_data 没有数据并不意味着程序就出错了,
  84. }
  85. return Error_code::SUCCESS;
  86. }