measure_filter.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * @Description: 测量结果滤波器
  3. * @Author: yct
  4. * @Date: 2021-03-15 14:41:46
  5. * @LastEditTime: 2022-01-18 13:38:27
  6. * @LastEditors: yct
  7. */
  8. #ifndef MEASURE_FILTER_HH
  9. #define MEASURE_FILTER_HH
  10. #include "tool/singleton.h"
  11. #include "tool/common_data.h"
  12. #include "error_code/error_code.hpp"
  13. #include <map>
  14. #include <deque>
  15. #include <thread>
  16. #include <mutex>
  17. #include <chrono>
  18. #include <vector>
  19. #include <algorithm>
  20. #include "glog/logging.h"
  21. #define FILTER_SIZE 10
  22. #define MAX_QUEUE_SIZE 20
  23. #define MAX_TIME_INTERVAL_MILLI 5000
  24. class Measure_filter : public Singleton<Measure_filter>
  25. {
  26. // 子类必须把父类设定为友元函数,这样父类才能使用子类的私有构造函数。
  27. friend class Singleton<Measure_filter>;
  28. public:
  29. // 必须关闭拷贝构造和赋值构造,只能通过 get_instance 函数来进行操作唯一的实例。
  30. Measure_filter(const Measure_filter &) = delete;
  31. Measure_filter &operator=(const Measure_filter &) = delete;
  32. ~Measure_filter()
  33. {
  34. mb_exit = true;
  35. if (mp_work_thread != nullptr)
  36. {
  37. if(mp_work_thread->joinable())
  38. {
  39. mp_work_thread->join();
  40. }
  41. delete mp_work_thread;
  42. mp_work_thread = nullptr;
  43. }
  44. }
  45. void work_thread_func()
  46. {
  47. while(!mb_exit)
  48. {
  49. {
  50. std::lock_guard<std::mutex> lck(m_mutex);
  51. for (auto iter = m_measure_results_map.begin(); iter != m_measure_results_map.end(); iter++)
  52. {
  53. std::deque<Common_data::Car_wheel_information_stamped> *t_queue = &(iter->second);
  54. // 剔除超时数据
  55. for (size_t i = 0; i < t_queue->size(); i++)
  56. {
  57. double dtime = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now() - t_queue->front().measure_time).count();
  58. if(dtime > MAX_TIME_INTERVAL_MILLI)
  59. {
  60. t_queue->pop_front();
  61. }
  62. }
  63. // 维持队列长度
  64. while(t_queue->size() > MAX_QUEUE_SIZE)
  65. {
  66. t_queue->pop_front();
  67. }
  68. }
  69. }
  70. usleep(1000 * 10);
  71. }
  72. }
  73. // 更新测量数据回调
  74. void update_data(int terminal_id, Common_data::Car_wheel_information_stamped data)
  75. {
  76. if(!data.wheel_data.correctness)
  77. return;
  78. // LOG(INFO) << data.wheel_data.to_string();
  79. // 未创建队列
  80. {
  81. std::lock_guard<std::mutex> lck(m_mutex);
  82. if (m_measure_results_map.find(terminal_id) == m_measure_results_map.end())
  83. {
  84. std::deque<Common_data::Car_wheel_information_stamped> t_deque;
  85. t_deque.push_back(data);
  86. m_measure_results_map.insert(std::pair<int, std::deque<Common_data::Car_wheel_information_stamped>>(terminal_id, t_deque));
  87. }
  88. else //已创建
  89. {
  90. m_measure_results_map[terminal_id].push_back(data);
  91. // 队列管理放在线程中自动控制
  92. }
  93. }
  94. }
  95. // 获取滤波后最新测量数据
  96. Error_manager get_filtered_wheel_information(int terminal_id, Common_data::Car_wheel_information& result)
  97. {
  98. std::lock_guard<std::mutex> lck(m_mutex);
  99. // 检查数据量
  100. if(m_measure_results_map.find(terminal_id) == m_measure_results_map.end() || m_measure_results_map.find(terminal_id)->second.size() < FILTER_SIZE)
  101. {
  102. return Error_manager(WJ_FILTER_LACK_OF_RESULT, MINOR_ERROR,
  103. (std::string("缺少足够用于滤波的结果")+
  104. std::to_string(terminal_id)+
  105. std::string(m_measure_results_map.find(terminal_id) == m_measure_results_map.end()?
  106. "end":
  107. std::to_string(m_measure_results_map.find(terminal_id)->second.size()))).c_str());
  108. }
  109. // 填充待滤波结果到数组
  110. std::deque<Common_data::Car_wheel_information_stamped>* tp_deque = &m_measure_results_map.find(terminal_id)->second;
  111. std::vector<Common_data::Car_wheel_information_stamped> t_result_vec;
  112. Common_data::Car_wheel_information_stamped t_avg_result;
  113. for (std::deque<Common_data::Car_wheel_information_stamped>::reverse_iterator t_iter = tp_deque->rbegin(); t_iter != tp_deque->rend(); t_iter++)
  114. {
  115. t_result_vec.push_back(*t_iter);
  116. t_avg_result += *t_iter;
  117. if (t_result_vec.size() >= FILTER_SIZE)
  118. break;
  119. }
  120. if(t_result_vec.size()<FILTER_SIZE)
  121. return Error_manager(WJ_FILTER_LACK_OF_RESULT, MINOR_ERROR, "结果缺失");
  122. // 各结果计算平均,并打分
  123. t_avg_result /= t_result_vec.size();
  124. std::vector<float> t_score_vec, t_sorted_score_vec;
  125. for (size_t i = 0; i < t_result_vec.size(); i++)
  126. {
  127. t_score_vec.push_back((t_result_vec[i] - t_avg_result).calc_score());
  128. t_sorted_score_vec.push_back((t_result_vec[i] - t_avg_result).calc_score());
  129. }
  130. // 排序,丢掉最高两个(即与均值偏差最大两个,剩下平均获得最终值)
  131. std::sort(t_sorted_score_vec.begin(), t_sorted_score_vec.end());
  132. Common_data::Car_wheel_information_stamped t_final_result;
  133. int t_result_count = 0;
  134. for (size_t i = 0; i < t_score_vec.size(); i++)
  135. {
  136. if(t_score_vec[i] > t_sorted_score_vec[t_sorted_score_vec.size()-3])
  137. {
  138. continue;
  139. }else{
  140. t_final_result += t_result_vec[i];
  141. t_result_count++;
  142. }
  143. }
  144. if(t_result_count<=0)
  145. return Error_manager(WJ_FILTER_FLUCTUATING, MINOR_ERROR, "结果波动");
  146. t_final_result /= t_result_count;
  147. result = t_final_result.wheel_data;
  148. // LOG(INFO) << "\navg: \n\t"<<t_avg_result.wheel_data.to_string()<<"final: \t"<<t_final_result.wheel_data.to_string();
  149. return SUCCESS;
  150. }
  151. private:
  152. // 父类的构造函数必须保护,子类的构造函数必须私有。
  153. Measure_filter()
  154. {
  155. mb_exit = false;
  156. mp_work_thread = new std::thread(&Measure_filter::work_thread_func, this);
  157. }
  158. // 各终端测量数据队列
  159. std::map<int, std::deque<Common_data::Car_wheel_information_stamped> > m_measure_results_map;
  160. std::mutex m_mutex;
  161. std::thread *mp_work_thread;
  162. bool mb_exit;
  163. };
  164. #endif // !MEASURE_FILTER_HH