|
|
@@ -1,205 +1,72 @@
|
|
|
+
|
|
|
+
|
|
|
#include "Laser.h"
|
|
|
|
|
|
-CBinaryData::CBinaryData()
|
|
|
-:m_buf(0)
|
|
|
-,m_length(0)
|
|
|
-{
|
|
|
-}
|
|
|
-CBinaryData::CBinaryData(const char* buf, int len, DATA_type type)
|
|
|
-:m_buf(0),m_length(0)
|
|
|
-{
|
|
|
- if (len > 0)
|
|
|
- {
|
|
|
- m_buf = (char*)malloc(len);
|
|
|
- memcpy(m_buf, buf, len);
|
|
|
- m_length = len;
|
|
|
- }
|
|
|
-}
|
|
|
-CBinaryData::CBinaryData(const CBinaryData& data)
|
|
|
-:m_buf(0),m_length(0)
|
|
|
-{
|
|
|
- if (m_buf)
|
|
|
- {
|
|
|
- free(m_buf);
|
|
|
- m_buf = 0;
|
|
|
- m_length = 0;
|
|
|
- }
|
|
|
- if (data.Length() > 0)
|
|
|
- {
|
|
|
- m_buf = (char*)malloc(data.Length());
|
|
|
- memcpy(m_buf, data.Data(), data.Length());
|
|
|
- m_length = data.Length();
|
|
|
- }
|
|
|
-}
|
|
|
-CBinaryData::~CBinaryData()
|
|
|
-{
|
|
|
- if (m_buf && m_length)
|
|
|
- {
|
|
|
- free(m_buf);
|
|
|
- m_length = 0;
|
|
|
- }
|
|
|
-}
|
|
|
-char* CBinaryData::Data()const {
|
|
|
- return m_buf;
|
|
|
-}
|
|
|
-int CBinaryData::Length()const {
|
|
|
- return m_length;
|
|
|
-}
|
|
|
-CBinaryData& CBinaryData::operator=(const CBinaryData& data)
|
|
|
-{
|
|
|
- if (m_buf)
|
|
|
- {
|
|
|
- free(m_buf);
|
|
|
- m_buf = 0;
|
|
|
- m_length = 0;
|
|
|
- }
|
|
|
- if (data.Length() > 0)
|
|
|
- {
|
|
|
- m_buf = (char*)malloc(data.Length());
|
|
|
- memcpy(m_buf, data.Data(), data.Length());
|
|
|
- m_length = data.Length();
|
|
|
- }
|
|
|
- return *this;
|
|
|
-}
|
|
|
-bool CBinaryData::operator==(const char* str)
|
|
|
-{
|
|
|
- int length = strlen(str);
|
|
|
- return (strncmp((const char*)m_buf, str, length) == 0);
|
|
|
-}
|
|
|
-const char* CBinaryData::operator+(int n)
|
|
|
-{
|
|
|
- if (n < m_length)
|
|
|
- return m_buf + n;
|
|
|
- else
|
|
|
- return NULL;
|
|
|
-}
|
|
|
-CBinaryData& CBinaryData::operator+(CBinaryData& data)
|
|
|
-{
|
|
|
- if (data.Length() > 0)
|
|
|
- {
|
|
|
- int length = m_length + data.Length();
|
|
|
- char* buf = (char*)malloc(length);
|
|
|
- memcpy(buf, m_buf, m_length);
|
|
|
- memcpy(buf + m_length, data.Data(), data.Length());
|
|
|
- free(m_buf);
|
|
|
- m_buf = buf;
|
|
|
- m_length = length;
|
|
|
- }
|
|
|
- return *this;
|
|
|
-}
|
|
|
-char& CBinaryData::operator[](int n)
|
|
|
-{
|
|
|
- if (n >= 0 && n < m_length)
|
|
|
- return m_buf[n];
|
|
|
-}
|
|
|
-//////////////
|
|
|
|
|
|
-Laser_base::Laser_base(int id, Laser_proto::laser_parameter laser_param)
|
|
|
- :m_ThreadRcv(0)
|
|
|
- ,m_ThreadPro(0)
|
|
|
-,m_ThreadPub(0)
|
|
|
-,m_id(id)
|
|
|
-,m_points_count(0)
|
|
|
-,m_statu(eLaser_disconnect)
|
|
|
-,m_dMatrix(0)
|
|
|
-, m_point_callback_fnc(0)
|
|
|
-, m_point_callback_pointer(0)
|
|
|
+Laser_base::Laser_base(int laser_id, Laser_proto::laser_parameter laser_param)
|
|
|
+:m_laser_id(laser_id)
|
|
|
,m_laser_param(laser_param)
|
|
|
-,m_bSave_file(false)
|
|
|
-,m_bscan_start(false)
|
|
|
+,m_laser_scan_flag(false)
|
|
|
+,m_laser_statu(LASER_DISCONNECT)
|
|
|
+,m_points_count(0)
|
|
|
+,m_save_flag(false)
|
|
|
{
|
|
|
- mp_laser_task = NULL;
|
|
|
+ mp_thread_receive = NULL;
|
|
|
+ mp_thread_transform = NULL;
|
|
|
+ mp_thread_publish = NULL;
|
|
|
+ init_laser_matrix();
|
|
|
+ mp_laser_task = NULL;
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
Laser_base::~Laser_base()
|
|
|
{
|
|
|
- if (m_dMatrix)
|
|
|
- {
|
|
|
- free(m_dMatrix);
|
|
|
- m_dMatrix = 0;
|
|
|
- }
|
|
|
+ m_queue_laser_data.termination_queue();
|
|
|
+ close_save_path();
|
|
|
+ m_condition_receive.kill_all();
|
|
|
+ m_condition_transform.kill_all();
|
|
|
+ m_condition_publish.kill_all();
|
|
|
}
|
|
|
|
|
|
-bool Laser_base::Start()
|
|
|
+//雷达链接设备,为3个线程添加线程执行函数。
|
|
|
+Error_manager Laser_base::connect_laser()
|
|
|
{
|
|
|
- m_bscan_start=true;
|
|
|
- m_statu = eLaser_busy;
|
|
|
- m_bStart_capture.Notify(true);
|
|
|
- m_points_count=0;
|
|
|
- return true;
|
|
|
-}
|
|
|
+ m_bThreadRcvRun.notify_all(true);
|
|
|
+ if(m_ThreadRcv==0)
|
|
|
+ m_ThreadRcv = new std::thread(&Laser_base::thread_recv, this);
|
|
|
+ m_ThreadRcv->detach();
|
|
|
|
|
|
-bool Laser_base::Stop()
|
|
|
-{
|
|
|
- m_bscan_start=false;
|
|
|
- m_bStart_capture.Notify(false);
|
|
|
- m_binary_log_tool.close();
|
|
|
- m_pts_log_tool.close();
|
|
|
- m_statu=eLaser_ready;
|
|
|
-
|
|
|
- //在停止雷达时,将雷达任务状态改为 TASK_OVER 已结束
|
|
|
- //强制改为TASK_OVER,不管它当前在做什么。
|
|
|
- if(mp_laser_task !=NULL)
|
|
|
- {
|
|
|
- mp_laser_task->set_task_statu(TASK_OVER);
|
|
|
- }
|
|
|
+ m_bThreadProRun.notify_all(true);
|
|
|
+ if(m_ThreadPro==0)
|
|
|
+ m_ThreadPro = new std::thread(&Laser_base::thread_toXYZ, this);
|
|
|
+ m_ThreadPro->detach();
|
|
|
|
|
|
- return true;
|
|
|
-}
|
|
|
+ if(m_ThreadPub==0)
|
|
|
+ m_ThreadPub = new std::thread(threadPublish, this);
|
|
|
+ m_ThreadPub->detach();
|
|
|
|
|
|
-void Laser_base::SetMetrix(double* data)
|
|
|
-{
|
|
|
- if (m_dMatrix == 0)
|
|
|
- {
|
|
|
- m_dMatrix = (double*)malloc(12 * sizeof(double));
|
|
|
- }
|
|
|
- memcpy(m_dMatrix, data, 12 * sizeof(double));
|
|
|
+ return Error_code::SUCCESS;
|
|
|
}
|
|
|
-void Laser_base::SetPointCallBack(PointCallBack fnc, void* pointer)
|
|
|
+//雷达断开链接,释放3个线程
|
|
|
+Error_manager Laser_base::disconnect_laser()
|
|
|
{
|
|
|
- m_point_callback_fnc = fnc;
|
|
|
- m_point_callback_pointer = pointer;
|
|
|
-}
|
|
|
-
|
|
|
-bool Laser_base::Connect()
|
|
|
-{
|
|
|
- m_bThreadRcvRun.Notify(true);
|
|
|
- if(m_ThreadRcv==0)
|
|
|
- m_ThreadRcv = new std::thread(&Laser_base::thread_recv, this);
|
|
|
- m_ThreadRcv->detach();
|
|
|
-
|
|
|
- m_bThreadProRun.Notify(true);
|
|
|
- if(m_ThreadPro==0)
|
|
|
- m_ThreadPro = new std::thread(&Laser_base::thread_toXYZ, this);
|
|
|
- m_ThreadPro->detach();
|
|
|
-
|
|
|
- if(m_ThreadPub==0)
|
|
|
- m_ThreadPub = new std::thread(threadPublish, this);
|
|
|
- m_ThreadPub->detach();
|
|
|
-
|
|
|
- return true;
|
|
|
-}
|
|
|
-void Laser_base::Disconnect()
|
|
|
-{
|
|
|
- if (m_ThreadRcv)
|
|
|
- {
|
|
|
- delete m_ThreadRcv;
|
|
|
- m_ThreadRcv = 0;
|
|
|
- }
|
|
|
-
|
|
|
- if (m_ThreadPub)
|
|
|
- {
|
|
|
- delete m_ThreadPub;
|
|
|
- m_ThreadPub = 0;
|
|
|
- }
|
|
|
-
|
|
|
- if (m_ThreadPro)
|
|
|
- {
|
|
|
- delete m_ThreadPro;
|
|
|
- m_ThreadPro = NULL;
|
|
|
- }
|
|
|
- m_statu = eLaser_disconnect;
|
|
|
+ if (m_ThreadRcv)
|
|
|
+ {
|
|
|
+ delete m_ThreadRcv;
|
|
|
+ m_ThreadRcv = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (m_ThreadPub)
|
|
|
+ {
|
|
|
+ delete m_ThreadPub;
|
|
|
+ m_ThreadPub = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (m_ThreadPro)
|
|
|
+ {
|
|
|
+ delete m_ThreadPro;
|
|
|
+ m_ThreadPro = NULL;
|
|
|
+ }
|
|
|
+ m_laser_statu = LASER_DISCONNECT;
|
|
|
+ return Error_code::SUCCESS;
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -208,24 +75,56 @@ void Laser_base::Disconnect()
|
|
|
//注:这个函数为虚函数,实际的处理任务的代码由子类重载并实现。
|
|
|
Error_manager Laser_base::execute_task(Task_Base* p_laser_task)
|
|
|
{
|
|
|
- //检查指针
|
|
|
- if(p_laser_task == NULL)
|
|
|
- {
|
|
|
- return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
|
|
|
- "Laser_base::execute_task failed, POINTER_IS_NULL");
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- return Error_manager(Error_code::CLASS_BASE_FUNCTION_CANNOT_USE, Error_level::NEGLIGIBLE_ERROR,
|
|
|
- "Laser_base::execute_task cannot use");
|
|
|
- }
|
|
|
+ //检查指针
|
|
|
+ if(p_laser_task == NULL)
|
|
|
+ {
|
|
|
+ return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
|
|
|
+ "Laser_base::execute_task failed, POINTER_IS_NULL");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return Error_manager(Error_code::CLASS_BASE_FUNCTION_CANNOT_USE, Error_level::NEGLIGIBLE_ERROR,
|
|
|
+ "Laser_base::execute_task cannot use");
|
|
|
+ }
|
|
|
}
|
|
|
//检查雷达状态,是否正常运行
|
|
|
-Error_manager Laser_base::check_error()
|
|
|
+Error_manager Laser_base::check_laser()
|
|
|
+{
|
|
|
+ return Error_code::SUCCESS;
|
|
|
+}
|
|
|
+//雷达的启动接口函数, 让雷达进行扫描,一般需要子类重载,不同的雷达开始方式不同。
|
|
|
+Error_manager Laser_base::start_scan()
|
|
|
{
|
|
|
- return SUCCESS;
|
|
|
+ m_laser_scan_flag=true;
|
|
|
+ m_laser_statu = LASER_BUSY;
|
|
|
+ m_bStart_capture.notify_all(true);
|
|
|
+ m_points_count=0;
|
|
|
+ return Error_code::SUCCESS;
|
|
|
+}
|
|
|
+//雷达的停止接口函数, 让雷达停止扫描,一般需要子类重载,不同的雷达结束方式不同。
|
|
|
+Error_manager Laser_base::stop_scan()
|
|
|
+{
|
|
|
+ m_laser_scan_flag=false;
|
|
|
+ m_bStart_capture.notify_all(false);
|
|
|
+ m_binary_log_tool.close();
|
|
|
+ m_points_log_tool.close();
|
|
|
+ m_laser_statu=LASER_READY;
|
|
|
+
|
|
|
+ //在停止雷达时,将雷达任务状态改为 TASK_OVER 已结束
|
|
|
+ //强制改为TASK_OVER,不管它当前在做什么。
|
|
|
+ if(mp_laser_task !=NULL)
|
|
|
+ {
|
|
|
+ mp_laser_task->set_task_statu(TASK_OVER);
|
|
|
+ }
|
|
|
+
|
|
|
+ return Error_code::SUCCESS;
|
|
|
}
|
|
|
|
|
|
+//结束任务单,stop之后,要检查线程状态和数据结果,然后才能 end_task
|
|
|
+Error_manager Laser_base::end_task()
|
|
|
+{
|
|
|
+ return Error_code::SUCCESS;
|
|
|
+}
|
|
|
/*
|
|
|
//对外的接口函数,负责接受并处理任务单,
|
|
|
//input:laser_task 雷达任务单,必须是子类,并且任务正确。(传引用)
|
|
|
@@ -278,184 +177,264 @@ Error_manager Laser_base::porform_task(Laser_task* p_laser_task)
|
|
|
}
|
|
|
*/
|
|
|
|
|
|
-void Laser_base::SetSaveDir(std::string strDir,bool bSaveFile)
|
|
|
-{
|
|
|
- m_bSave_file = bSaveFile;
|
|
|
- m_pts_log_tool.close();
|
|
|
- m_binary_log_tool.close();
|
|
|
-
|
|
|
- if (bSaveFile == false)
|
|
|
- return;
|
|
|
-
|
|
|
- m_pts_save_path = strDir;
|
|
|
- char pts_file[255] = { 0 };
|
|
|
- sprintf(pts_file, "%s/pts%d.txt", strDir.c_str(), m_id+1);
|
|
|
- m_pts_log_tool.open(pts_file);
|
|
|
|
|
|
- char bin_file[255] = { 0 };
|
|
|
- sprintf(bin_file, "%s/laser%d.data", strDir.c_str(), m_id+1);
|
|
|
- m_binary_log_tool.open(bin_file,true);
|
|
|
+//设置保存文件的路径,并打开文件,
|
|
|
+Error_manager Laser_base::set_open_save_path(std::string save_path,bool is_save)
|
|
|
+{
|
|
|
+ m_save_flag = is_save;
|
|
|
+ m_points_log_tool.close();
|
|
|
+ m_binary_log_tool.close();
|
|
|
+
|
|
|
+ if (is_save == false)
|
|
|
+ {
|
|
|
+ return Error_code::SUCCESS;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ m_save_path = save_path;
|
|
|
+ char pts_file[255] = { 0 };
|
|
|
+ sprintf(pts_file, "%s/pts%d.txt", save_path.c_str(), m_laser_id+1);
|
|
|
+ m_points_log_tool.open(pts_file);
|
|
|
+
|
|
|
+ char bin_file[255] = { 0 };
|
|
|
+ sprintf(bin_file, "%s/laser%d.data", save_path.c_str(), m_laser_id+1);
|
|
|
+ m_binary_log_tool.open(bin_file,true);
|
|
|
+ return Error_code::SUCCESS;
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
-void Laser_base::thread_recv()
|
|
|
+//关闭保存文件,推出前一定要执行
|
|
|
+Error_manager Laser_base::close_save_path()
|
|
|
{
|
|
|
- while (m_bThreadRcvRun.WaitFor(1))
|
|
|
- {
|
|
|
- if (m_bStart_capture.WaitFor(1) == false)
|
|
|
- continue;
|
|
|
- CBinaryData* data=new CBinaryData();
|
|
|
- if (this->RecvData(*data))
|
|
|
- {
|
|
|
- m_queue_laser_data.push(data);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- delete data;
|
|
|
- data=0;
|
|
|
- }
|
|
|
+ m_points_log_tool.close();
|
|
|
+ m_binary_log_tool.close();
|
|
|
+ return Error_code::SUCCESS;
|
|
|
+}
|
|
|
|
|
|
- }
|
|
|
+//判断雷达状态是否为待机,如果已经准备好,则可以执行任务。
|
|
|
+bool Laser_base::is_ready()
|
|
|
+{
|
|
|
+ return ( m_laser_statu == LASER_READY );
|
|
|
+}
|
|
|
+//获取雷达状态
|
|
|
+Laser_statu Laser_base::get_laser_statu()
|
|
|
+{
|
|
|
+ return m_laser_statu;
|
|
|
+}
|
|
|
+//获取雷达id
|
|
|
+int Laser_base::get_laser_id()
|
|
|
+{
|
|
|
+ return m_laser_id;
|
|
|
}
|
|
|
|
|
|
-CPoint3D Laser_base::transfor(CPoint3D point)
|
|
|
+
|
|
|
+void Laser_base::thread_recv()
|
|
|
{
|
|
|
- if (m_dMatrix)
|
|
|
- {
|
|
|
- CPoint3D result;
|
|
|
- double x = point.x;
|
|
|
- double y = point.y;
|
|
|
- double z = point.z;
|
|
|
- result.x = x * m_dMatrix[0] + y*m_dMatrix[1] + z*m_dMatrix[2] + m_dMatrix[3];
|
|
|
- result.y = x * m_dMatrix[4] + y*m_dMatrix[5] + z*m_dMatrix[6] + m_dMatrix[7];
|
|
|
- result.z = x * m_dMatrix[8] + y*m_dMatrix[9] + z*m_dMatrix[10] + m_dMatrix[11];
|
|
|
- return result;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- return point;
|
|
|
- }
|
|
|
+ while (m_bThreadRcvRun.wait_for_millisecond(1))
|
|
|
+ {
|
|
|
+ if (m_bStart_capture.wait_for_millisecond(1) == false)
|
|
|
+ continue;
|
|
|
+ Binary_buf* data=new Binary_buf();
|
|
|
+ if (this->RecvData(*data))
|
|
|
+ {
|
|
|
+ m_queue_laser_data.push(data);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ delete data;
|
|
|
+ data=0;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
void Laser_base::thread_toXYZ()
|
|
|
{
|
|
|
- while (m_bThreadProRun.WaitFor(1))
|
|
|
- {
|
|
|
- if (m_bStart_capture.WaitFor(1) == false)
|
|
|
- {
|
|
|
- m_queue_laser_data.clear();
|
|
|
- continue;
|
|
|
- }
|
|
|
- CBinaryData* pData=NULL;
|
|
|
- bool bPop = m_queue_laser_data.try_pop(pData);
|
|
|
- DATA_type type = eUnknow;
|
|
|
- if (bPop || m_last_data.Length() != 0)
|
|
|
- {
|
|
|
- std::vector<CPoint3D> cloud;
|
|
|
- if (bPop)
|
|
|
- {
|
|
|
- if (pData == NULL)
|
|
|
- continue;
|
|
|
- if(m_bSave_file)
|
|
|
- {
|
|
|
- m_binary_log_tool.write(pData->Data(), pData->Length());
|
|
|
- }
|
|
|
- type= this->Data2PointXYZ(pData, cloud);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- type = this->Data2PointXYZ(NULL, cloud);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- if (type == eUnknow)
|
|
|
- {
|
|
|
- delete pData;
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- if (type == eData || type == eStart || type == eStop)
|
|
|
- {
|
|
|
- for (int i = 0; i < cloud.size(); ++i)
|
|
|
- {
|
|
|
- CPoint3D point = transfor(cloud[i]);
|
|
|
- if(m_bSave_file) {
|
|
|
- char buf[64] = {0};
|
|
|
- sprintf(buf, "%f %f %f\n", point.x, point.y, point.z);
|
|
|
- m_pts_log_tool.write(buf, strlen(buf));
|
|
|
- }
|
|
|
-
|
|
|
- //此时cloud为雷达采集数据的结果,转换后的三维点云。
|
|
|
- //将每个点存入任务单里面的点云容器。
|
|
|
-
|
|
|
- if(mp_laser_task!=NULL)
|
|
|
- {
|
|
|
- Error_manager code= mp_laser_task->task_push_point(pcl::PointXYZ(point.x,point.y,point.z));
|
|
|
- }
|
|
|
- }
|
|
|
- m_points_count+=cloud.size();
|
|
|
- if (type == eStop)
|
|
|
- {
|
|
|
- if(m_bSave_file) {
|
|
|
- m_pts_log_tool.close();
|
|
|
- m_binary_log_tool.close();
|
|
|
- }
|
|
|
- m_statu = eLaser_ready;
|
|
|
-
|
|
|
- //在停止雷达时,将雷达任务状态改为 TASK_OVER 已结束
|
|
|
- if(mp_laser_task !=NULL)
|
|
|
- {
|
|
|
- if(mp_laser_task->get_statu() == TASK_WORKING)
|
|
|
- {
|
|
|
- mp_laser_task->set_task_statu(TASK_OVER);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- else if (type == eReady)
|
|
|
- {
|
|
|
- if(m_bSave_file) {
|
|
|
- m_pts_log_tool.close();
|
|
|
- m_binary_log_tool.close();
|
|
|
- }
|
|
|
- m_statu = eLaser_ready;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- delete pData;
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
+ while (m_bThreadProRun.wait_for_millisecond(1))
|
|
|
+ {
|
|
|
+ if (m_bStart_capture.wait_for_millisecond(1) == false)
|
|
|
+ {
|
|
|
+ m_queue_laser_data.clear();
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ Binary_buf* pData=NULL;
|
|
|
+ bool bPop = m_queue_laser_data.try_pop(pData);
|
|
|
+ Buf_type type = BUF_UNKNOW;
|
|
|
+ if (bPop || m_last_data.get_length() != 0)
|
|
|
+ {
|
|
|
+ std::vector<CPoint3D> cloud;
|
|
|
+ if (bPop)
|
|
|
+ {
|
|
|
+ if (pData == NULL)
|
|
|
+ continue;
|
|
|
+ if(m_save_flag)
|
|
|
+ {
|
|
|
+ m_binary_log_tool.write(pData->get_buf(), pData->get_length());
|
|
|
+ }
|
|
|
+ type= this->Data2PointXYZ(pData, cloud);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ type = this->Data2PointXYZ(NULL, cloud);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if (type == BUF_UNKNOW)
|
|
|
+ {
|
|
|
+ delete pData;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (type == BUF_DATA || type == BUF_START || type == BUF_STOP)
|
|
|
+ {
|
|
|
+ for (int i = 0; i < cloud.size(); ++i)
|
|
|
+ {
|
|
|
+ CPoint3D point = transform_by_matrix(cloud[i]);
|
|
|
+ if(m_save_flag) {
|
|
|
+ char buf[64] = {0};
|
|
|
+ sprintf(buf, "%f %f %f\n", point.x, point.y, point.z);
|
|
|
+ m_points_log_tool.write(buf, strlen(buf));
|
|
|
+ }
|
|
|
+
|
|
|
+ //此时cloud为雷达采集数据的结果,转换后的三维点云。
|
|
|
+ //将每个点存入任务单里面的点云容器。
|
|
|
+
|
|
|
+ if(mp_laser_task!=NULL)
|
|
|
+ {
|
|
|
+ Error_manager code= mp_laser_task->task_push_point(pcl::PointXYZ(point.x,point.y,point.z));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ m_points_count+=cloud.size();
|
|
|
+ if (type == BUF_STOP)
|
|
|
+ {
|
|
|
+ if(m_save_flag) {
|
|
|
+ m_points_log_tool.close();
|
|
|
+ m_binary_log_tool.close();
|
|
|
+ }
|
|
|
+ m_laser_statu = LASER_READY;
|
|
|
+
|
|
|
+ //在停止雷达时,将雷达任务状态改为 TASK_OVER 已结束
|
|
|
+ if(mp_laser_task !=NULL)
|
|
|
+ {
|
|
|
+ if(mp_laser_task->get_statu() == TASK_WORKING)
|
|
|
+ {
|
|
|
+ mp_laser_task->set_task_statu(TASK_OVER);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ else if (type == BUF_READY)
|
|
|
+ {
|
|
|
+ if(m_save_flag) {
|
|
|
+ m_points_log_tool.close();
|
|
|
+ m_binary_log_tool.close();
|
|
|
+ }
|
|
|
+ m_laser_statu = LASER_READY;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ delete pData;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|
|
|
#include "unistd.h"
|
|
|
void Laser_base::threadPublish(Laser_base *laser)
|
|
|
{
|
|
|
- if(laser==0) return ;
|
|
|
- while(laser->m_bThreadRcvRun.WaitFor(1))
|
|
|
- {
|
|
|
- laser->PublishMsg();
|
|
|
- usleep(1000*300);
|
|
|
- }
|
|
|
+ if(laser==0) return ;
|
|
|
+ while(laser->m_bThreadRcvRun.wait_for_millisecond(1))
|
|
|
+ {
|
|
|
+ laser->PublishMsg();
|
|
|
+ usleep(1000*300);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
void Laser_base::PublishMsg()
|
|
|
{
|
|
|
- /*
|
|
|
- globalmsg::msg msg;
|
|
|
- msg.set_msg_type(globalmsg::eLaser);
|
|
|
-
|
|
|
- globalmsg::laserStatus status;
|
|
|
- if(GetStatu()==eLaser_ready) status=globalmsg::eLaserConnected;
|
|
|
- else if(GetStatu()==eLaser_disconnect) status=globalmsg::eLaserDisconnected;
|
|
|
- else if(GetStatu()==eLaser_busy) status=globalmsg::eLaserBusy;
|
|
|
- else status=globalmsg::eLaserUnknown;
|
|
|
-
|
|
|
- msg.mutable_laser_msg()->set_id(m_id);
|
|
|
- msg.mutable_laser_msg()->set_laser_status(status);
|
|
|
- msg.mutable_laser_msg()->set_queue_data_count(m_queue_laser_data.size());
|
|
|
- msg.mutable_laser_msg()->set_cloud_count(m_points_count);
|
|
|
- MeasureTopicPublisher::GetInstance()->Publish(msg.SerializeAsString());
|
|
|
- */
|
|
|
-}
|
|
|
+ /*
|
|
|
+ globalmsg::msg msg;
|
|
|
+ msg.set_msg_type(globalmsg::eLaser);
|
|
|
+
|
|
|
+ globalmsg::laserStatus status;
|
|
|
+ if(GetStatu()==eLaser_ready) status=globalmsg::eLaserConnected;
|
|
|
+ else if(GetStatu()==eLaser_disconnect) status=globalmsg::eLaserDisconnected;
|
|
|
+ else if(GetStatu()==eLaser_busy) status=globalmsg::eLaserBusy;
|
|
|
+ else status=globalmsg::eLaserUnknown;
|
|
|
+
|
|
|
+ msg.mutable_laser_msg()->set_id(m_laser_id);
|
|
|
+ msg.mutable_laser_msg()->set_laser_status(status);
|
|
|
+ msg.mutable_laser_msg()->set_queue_data_count(m_queue_laser_data.size());
|
|
|
+ msg.mutable_laser_msg()->set_cloud_count(m_points_count);
|
|
|
+ MeasureTopicPublisher::GetInstance()->Publish(msg.SerializeAsString());
|
|
|
+ */
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+//初始化变换矩阵,设置默认值
|
|
|
+Error_manager Laser_base::init_laser_matrix()
|
|
|
+{
|
|
|
+ if ( LASER_MATRIX_ARRAY_SIZE == 12 )
|
|
|
+ {
|
|
|
+ //详见转化的算法transfor。保证转化前后坐标一致。
|
|
|
+ mp_laser_matrix[0] = 1;
|
|
|
+ mp_laser_matrix[1] = 0;
|
|
|
+ mp_laser_matrix[2] = 0;
|
|
|
+ mp_laser_matrix[3] = 0;
|
|
|
+ mp_laser_matrix[4] = 0;
|
|
|
+ mp_laser_matrix[5] = 1;
|
|
|
+ mp_laser_matrix[6] = 0;
|
|
|
+ mp_laser_matrix[7] = 0;
|
|
|
+ mp_laser_matrix[8] = 0;
|
|
|
+ mp_laser_matrix[9] = 0;
|
|
|
+ mp_laser_matrix[10] = 1;
|
|
|
+ mp_laser_matrix[11] = 0;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ for (int i = 0; i < LASER_MATRIX_ARRAY_SIZE; ++i)
|
|
|
+ {
|
|
|
+ //设为0之后,变换之后,新的点坐标全部为0
|
|
|
+ mp_laser_matrix[i] = 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return Error_code::SUCCESS;
|
|
|
+}
|
|
|
+//设置变换矩阵,用作三维点的坐标变换,
|
|
|
+Error_manager Laser_base::set_laser_matrix(double* p_matrix, int size)
|
|
|
+{
|
|
|
+ if ( p_matrix == NULL )
|
|
|
+ {
|
|
|
+ return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
|
|
|
+ " set_laser_matrix p_matrix IS_NULL ");
|
|
|
+ }
|
|
|
+ else if ( size != LASER_MATRIX_ARRAY_SIZE )
|
|
|
+ {
|
|
|
+ return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
|
|
|
+ " set_laser_matrix size is not Match");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ memcpy(mp_laser_matrix, p_matrix, LASER_MATRIX_ARRAY_SIZE * sizeof(double));
|
|
|
+ return Error_code::SUCCESS;
|
|
|
+ }
|
|
|
+}
|
|
|
+//三维点的坐标变换的功能函数,从雷达自己的坐标系,转化到公共坐标系,(目前以plc为公共坐标系)
|
|
|
+CPoint3D Laser_base::transform_by_matrix(CPoint3D point)
|
|
|
+{
|
|
|
+ CPoint3D result;
|
|
|
+ double x = point.x;
|
|
|
+ double y = point.y;
|
|
|
+ double z = point.z;
|
|
|
+ result.x = x * mp_laser_matrix[0] + y*mp_laser_matrix[1] + z*mp_laser_matrix[2] + mp_laser_matrix[3];
|
|
|
+ result.y = x * mp_laser_matrix[4] + y*mp_laser_matrix[5] + z*mp_laser_matrix[6] + mp_laser_matrix[7];
|
|
|
+ result.z = x * mp_laser_matrix[8] + y*mp_laser_matrix[9] + z*mp_laser_matrix[10] + mp_laser_matrix[11];
|
|
|
+ return result;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|