region_status_checker.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * @Description: 区域状态检查类,用于判断当前各区域车辆状态(已停好、运动中),以及周围是否有障碍物
  3. * @Author: yct
  4. * @Date: 2021-12-21 19:54:34
  5. * @LastEditTime: 2021-12-28 11:53:11
  6. * @LastEditors: yct
  7. */
  8. #ifndef REGION_STATUS_CHECKER_HH
  9. #define REGION_STATUS_CHECKER_HH
  10. #include "tool/singleton.h"
  11. #include "tool/common_data.h"
  12. #include "error_code/error_code.hpp"
  13. #include <map>
  14. #include <mutex>
  15. #include <deque>
  16. #include <chrono>
  17. #include <vector>
  18. #include <algorithm>
  19. #include <thread>
  20. #include "glog/logging.h"
  21. #include <pcl/point_types.h>
  22. #include <pcl/point_cloud.h>
  23. #include <pcl/filters/voxel_grid.h>
  24. #include "icp_svd_registration.hpp"
  25. #include "../tool/point_tool.h"
  26. #define DEFAULT_REGION_STATUS_TIMEOUT_MILLI 600000
  27. #define DEFAULT_REGION_STATUS_MAX_QUEUE_SIZE 600
  28. // 检测到静止后稳定时间
  29. #define MIN_STABLE_TIME_MILLI 1000
  30. // 外点差异量
  31. #define OUTSIDE_POINT_LIMIT 30
  32. class Region_status_checker : public Singleton<Region_status_checker>
  33. {
  34. // 子类必须把父类设定为友元函数,这样父类才能使用子类的私有构造函数。
  35. friend class Singleton<Region_status_checker>;
  36. struct Region_measure_info_stamped
  37. {
  38. Common_data::Car_wheel_information_stamped measure_result;
  39. pcl::PointCloud<pcl::PointXYZ>::Ptr p_cloud;
  40. Region_measure_info_stamped()
  41. {
  42. p_cloud = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>);
  43. }
  44. };
  45. public:
  46. // 必须关闭拷贝构造和赋值构造,只能通过 get_instance 函数来进行操作唯一的实例。
  47. Region_status_checker(const Region_status_checker &) = delete;
  48. Region_status_checker &operator=(const Region_status_checker &) = delete;
  49. ~Region_status_checker() = default;
  50. void Region_status_checker_init()
  51. {
  52. mb_exit = false;
  53. m_manager_thread = new std::thread(&Region_status_checker::thread_func, this);
  54. }
  55. void Region_status_checker_uninit()
  56. {
  57. mb_exit = true;
  58. if(m_manager_thread != nullptr)
  59. {
  60. if(m_manager_thread->joinable())
  61. {
  62. m_manager_thread->join();
  63. }
  64. delete m_manager_thread;
  65. m_manager_thread = nullptr;
  66. }
  67. for (auto iter = m_icp_svd_map.begin(); iter != m_icp_svd_map.end(); iter++)
  68. {
  69. if(iter->second!= nullptr)
  70. {
  71. delete iter->second;
  72. iter->second = nullptr;
  73. }
  74. }
  75. }
  76. // 更新测量数据回调
  77. void add_measure_data(int terminal_id, Common_data::Car_wheel_information_stamped& measure_result, pcl::PointCloud<pcl::PointXYZ>& cloud)
  78. {
  79. Region_measure_info_stamped data;
  80. data.measure_result = measure_result;
  81. std::vector<int> index;
  82. data.p_cloud->operator+=(cloud);
  83. if (!data.measure_result.wheel_data.correctness)
  84. return;
  85. // 未创建队列
  86. std::lock_guard<std::mutex> lck(m_mutex);
  87. if (m_measure_results_map.find(terminal_id) == m_measure_results_map.end())
  88. {
  89. std::deque<Region_measure_info_stamped> t_deque;
  90. t_deque.push_back(data);
  91. m_measure_results_map.insert(std::pair<int, std::deque<Region_measure_info_stamped>>(terminal_id, t_deque));
  92. m_icp_svd_map.insert(std::pair<int, ICPSVDRegistration*>(terminal_id, new ICPSVDRegistration(0.1, 0.0018, 0.036, 20)));
  93. }
  94. else//已创建
  95. {
  96. m_measure_results_map[terminal_id].push_back(data);
  97. }
  98. }
  99. // 根据当前帧判断区域停车状态
  100. // 当前帧不需要存在正确结果,利用点云判断是否车辆未移动即可
  101. Error_manager get_region_parking_status(int terminal_id, Common_data::Car_wheel_information_stamped& measure_result, pcl::PointCloud<pcl::PointXYZ>& cloud)
  102. {
  103. std::lock_guard<std::mutex> lck(m_mutex);
  104. if(cloud.size() <= 0)
  105. {
  106. return Error_manager(WJ_FILTER_LACK_OF_RESULT, MINOR_ERROR, (std::string("当前空点云 终端") + std::to_string(terminal_id)).c_str());
  107. }
  108. if(m_icp_svd_map.find(terminal_id) == m_icp_svd_map.end())
  109. {
  110. return Error_manager(WJ_FILTER_LACK_OF_RESULT, MINOR_ERROR, (std::string("当前icp空指针 终端") + std::to_string(terminal_id)).c_str());
  111. }
  112. ICPSVDRegistration* tp_icp_svd = m_icp_svd_map[terminal_id];
  113. if(m_measure_results_map.find(terminal_id) == m_measure_results_map.end() || m_measure_results_map[terminal_id].size() == 0)
  114. {
  115. return Error_manager(WJ_FILTER_LACK_OF_RESULT, MINOR_ERROR, (std::string("暂无最近历史数据用于推测当前状态 终端") + std::to_string(terminal_id)).c_str());
  116. }
  117. // 时间检查, 当前队列中数据全为正确
  118. // 计算最近一帧外框,x一边外扩4cm, y一边外扩100cm
  119. auto last_data_ref = m_measure_results_map[terminal_id].back();
  120. if(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now()-last_data_ref.measure_result.measure_time).count() > DEFAULT_REGION_STATUS_TIMEOUT_MILLI)
  121. {
  122. return Error_manager(WJ_FILTER_LACK_OF_RESULT, MINOR_ERROR, (std::string("最近历史数据超时 终端") + std::to_string(terminal_id)).c_str());
  123. }
  124. // 截取当前帧框内与框外点云
  125. Eigen::Vector2d t_last_center(
  126. last_data_ref.measure_result.wheel_data.car_center_x,
  127. last_data_ref.measure_result.wheel_data.car_center_y);
  128. Eigen::Vector2d t_width_height(
  129. last_data_ref.measure_result.wheel_data.car_wheel_width + 0.08,
  130. last_data_ref.measure_result.wheel_data.car_wheel_base + 2.0);
  131. // 历史正确数据整车旋转
  132. Eigen::Rotation2Dd t_rot((90 - last_data_ref.measure_result.wheel_data.car_angle) * M_PI / 180.0);
  133. Eigen::Matrix2d t_rot_mat = t_rot.toRotationMatrix();
  134. // 计算历史(模板)框内点云与框外点云
  135. pcl::PointCloud<pcl::PointXYZ>::Ptr t_prev_cloud_inside = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>);
  136. pcl::PointCloud<pcl::PointXYZ>::Ptr t_prev_cloud_outside = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>);
  137. for (size_t i = 0; i < last_data_ref.p_cloud->size(); i++)
  138. {
  139. Eigen::Vector2d t_point(last_data_ref.p_cloud->points[i].x - t_last_center.x(), last_data_ref.p_cloud->points[i].y - t_last_center.y());
  140. Eigen::Vector2d t_rot_point = t_rot_mat * t_point;
  141. if (t_rot_point.x() > -t_width_height.x() / 2.0 && t_rot_point.x() < t_width_height.x() / 2.0 && t_rot_point.y() > -t_width_height.y() / 2.0 && t_rot_point.y() < t_width_height.y() / 2.0)
  142. {
  143. t_prev_cloud_inside->push_back(last_data_ref.p_cloud->points[i]);
  144. }
  145. else
  146. {
  147. t_prev_cloud_outside->push_back(last_data_ref.p_cloud->points[i]);
  148. }
  149. }
  150. // 计算当前帧在框内与框外点云
  151. pcl::PointCloud<pcl::PointXYZ>::Ptr t_curr_cloud_inside = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>);
  152. pcl::PointCloud<pcl::PointXYZ>::Ptr t_curr_cloud_outside = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>);
  153. std::vector<int> index;
  154. pcl::PointCloud<pcl::PointXYZ> t_cloud_valid;
  155. t_cloud_valid = cloud;
  156. // t_cloud_valid.is_dense = false;
  157. // pcl::removeNaNFromPointCloud(t_cloud_valid, t_cloud_valid, index);
  158. for (size_t i = 0; i < t_cloud_valid.size(); i++)
  159. {
  160. Eigen::Vector2d t_point(t_cloud_valid[i].x - t_last_center.x(), t_cloud_valid[i].y - t_last_center.y());
  161. Eigen::Vector2d t_rot_point = t_rot_mat * t_point;
  162. if (t_rot_point.x() > -t_width_height.x() / 2.0 && t_rot_point.x() < t_width_height.x() / 2.0 && t_rot_point.y() > -t_width_height.y() / 2.0 && t_rot_point.y() < t_width_height.y() / 2.0)
  163. {
  164. t_curr_cloud_inside->push_back(t_cloud_valid[i]);
  165. }
  166. else
  167. {
  168. t_curr_cloud_outside->push_back(t_cloud_valid[i]);
  169. }
  170. }
  171. if(t_prev_cloud_inside->size() <= 30 || t_curr_cloud_inside->size() <= 30)
  172. {
  173. char buf[255];
  174. memset(buf, 0, 255);
  175. sprintf(buf, ",, prev %ld, curr %ld;; last center(%.3f, %.3f)", t_prev_cloud_inside->size(), t_curr_cloud_inside->size(), t_last_center.x(), t_last_center.y());
  176. return Error_manager(WJ_FILTER_LACK_OF_RESULT, MINOR_ERROR, (std::string("区域剪切后无点可用于匹配 终端") + std::to_string(terminal_id)+std::string(buf)).c_str());
  177. }
  178. // std::cout << "000" << std::endl;
  179. // 直接使用当前时间,测量时间传入存在变为0的情况!!!!!
  180. measure_result.measure_time = std::chrono::system_clock::now();
  181. double dtime = std::chrono::duration_cast<std::chrono::milliseconds>(measure_result.measure_time - last_data_ref.measure_result.measure_time).count();
  182. // icp计算前后内点匹配情况
  183. //下采样
  184. pcl::PointCloud<pcl::PointXYZ>::Ptr t_prev_cloud_inside_filtered = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>);
  185. pcl::PointCloud<pcl::PointXYZ>::Ptr t_curr_cloud_inside_filtered = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>);
  186. pcl::VoxelGrid<pcl::PointXYZ> vox; //创建滤波对象
  187. vox.setInputCloud(t_prev_cloud_inside); //设置需要过滤的点云给滤波对象
  188. vox.setLeafSize(0.03f, 0.03f, 0.03f); //设置滤波时创建的体素体积为1cm的立方体
  189. vox.filter(*t_prev_cloud_inside_filtered); //执行滤波处理,存储输出
  190. vox.setInputCloud(t_curr_cloud_inside); //设置需要过滤的点云给滤波对象
  191. vox.setLeafSize(0.03f, 0.03f, 0.03f); //设置滤波时创建的体素体积为1cm的立方体
  192. vox.filter(*t_curr_cloud_inside_filtered); //执行滤波处理,存储输出
  193. tp_icp_svd->SetInputTarget(t_prev_cloud_inside_filtered);
  194. // std::cout << "111" << std::endl;
  195. pcl::PointCloud<pcl::PointXYZ>::Ptr t_result_cloud = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>);
  196. Eigen::Matrix4f t_pred_pose, t_res_pose;
  197. t_pred_pose = Eigen::Matrix4f::Identity();
  198. // std::cout << "222" << std::endl;
  199. if (!tp_icp_svd->ScanMatch(t_curr_cloud_inside_filtered, t_pred_pose, t_result_cloud, t_res_pose))
  200. {
  201. return Error_manager(WJ_FILTER_FLUCTUATING, MINOR_ERROR, (std::string("icp匹配失败,终端") + std::to_string(terminal_id)).c_str());
  202. }
  203. // std::cout << "2222" << std::endl;
  204. if(is_significant(t_res_pose, dtime / 1000.0))
  205. {
  206. mb_is_stable = false;
  207. return Error_manager(WJ_FILTER_FLUCTUATING, MINOR_ERROR, (std::string("检测到车辆移动,终端") + std::to_string(terminal_id)).c_str());
  208. }else{
  209. if (!mb_is_stable)
  210. {
  211. mb_is_stable = true;
  212. m_stable_time = std::chrono::system_clock::now();
  213. return Error_manager(WJ_FILTER_FLUCTUATING, MINOR_ERROR, (std::string("开始等待稳定,终端") + std::to_string(terminal_id)).c_str());
  214. }else
  215. {
  216. auto current_time = std::chrono::system_clock::now();
  217. double duration = std::chrono::duration_cast<std::chrono::milliseconds>(current_time - m_stable_time).count();
  218. if(duration > MIN_STABLE_TIME_MILLI)
  219. {
  220. // 判断外点数量
  221. // LOG_IF(WARNING, terminal_id == 4) << "ouside point prev curr: " << t_prev_cloud_outside->size() << ", " << t_curr_cloud_outside->size();
  222. if (t_curr_cloud_outside->size() > (t_prev_cloud_outside->size() + OUTSIDE_POINT_LIMIT))
  223. {
  224. // debug提示用
  225. // LOG_IF(WARNING, terminal_id == 4) << "检测到车身外侧噪点存在!!!";
  226. }
  227. return SUCCESS;
  228. }else
  229. {
  230. return Error_manager(WJ_FILTER_FLUCTUATING, MINOR_ERROR, (std::string("等待稳定中,终端") + std::to_string(terminal_id)).c_str());
  231. }
  232. }
  233. }
  234. return ERROR;
  235. }
  236. // 显著性判断,平移x方向速度0.02m/s,y方向速度0.06m/s误差,角度3deg/s误差
  237. bool is_significant(Eigen::Matrix4f trans, double dtime, double vx_eps = 0.06, double vy_eps = 0.06, double omega_eps = 0.051)
  238. {
  239. // 间隔时间为负数,或间隔超过0.5s则手动赋值
  240. if(dtime<0 || dtime>0.5)
  241. {
  242. dtime = 0.1;
  243. }
  244. // return false;
  245. Eigen::Vector3f translation = trans.block<3, 1>(0, 3);
  246. float rot_angle = fabs(acos((std::min(trans.block<3, 3>(0, 0).trace(), 2.999999f) - 1.0f) / 2.0f));
  247. // 值足够小,可当做车辆未移动
  248. Eigen::Vector3d velocity(fabs(translation.x() / dtime), fabs(translation.y() / dtime), fabs(rot_angle / dtime));
  249. if (velocity.x() <= vx_eps && velocity.y() <= vy_eps && velocity.z() <= omega_eps)
  250. {
  251. return false;
  252. }
  253. return true;
  254. }
  255. void thread_func()
  256. {
  257. while(!mb_exit)
  258. {
  259. {
  260. std::lock_guard<std::mutex> lck(m_mutex);
  261. for (auto iter = m_measure_results_map.begin(); iter != m_measure_results_map.end(); iter++)
  262. {
  263. std::deque<Region_measure_info_stamped> *tp_data_queue = &(iter->second);
  264. for (size_t i = 0; i < tp_data_queue->size(); i++)
  265. {
  266. // 判断队列头部(最老历史数据)是否超时,超时则丢出
  267. double dtime = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now() - tp_data_queue->front().measure_result.measure_time).count();
  268. if(dtime > DEFAULT_REGION_STATUS_TIMEOUT_MILLI)
  269. {
  270. tp_data_queue->pop_front();
  271. }
  272. }
  273. // 维持队列长度
  274. while (tp_data_queue->size() > DEFAULT_REGION_STATUS_MAX_QUEUE_SIZE)
  275. {
  276. tp_data_queue->pop_front();
  277. }
  278. }
  279. }
  280. usleep(1000 * 100);
  281. }
  282. }
  283. private:
  284. // 父类的构造函数必须保护,子类的构造函数必须私有。
  285. Region_status_checker() = default;
  286. // map访问锁
  287. std::mutex m_mutex;
  288. // 各终端测量数据队列
  289. std::map<int, std::deque<Region_measure_info_stamped> > m_measure_results_map;
  290. bool mb_exit;
  291. // 内部管理线程
  292. std::thread *m_manager_thread;
  293. // icp匹配
  294. // ICPSVDRegistration *mp_icp_svd;
  295. std::map<int, ICPSVDRegistration*> m_icp_svd_map;
  296. // 是否已经稳定
  297. bool mb_is_stable;
  298. // 最初稳定时刻
  299. std::chrono::system_clock::time_point m_stable_time;
  300. };
  301. #endif // !REGION_STATUS_CHECKER_HH