ソースを参照

20210925, 剥离地面雷达 123

huli 3 年 前
コミット
c6cc069fbf
2 ファイル変更130 行追加49 行削除
  1. 119 48
      dispatch/dispatch_ground_lidar.cpp
  2. 11 1
      dispatch/dispatch_ground_lidar.h

+ 119 - 48
dispatch/dispatch_ground_lidar.cpp

@@ -9,6 +9,8 @@ Dispatch_ground_lidar::Dispatch_ground_lidar()
 	m_dispatch_ground_lidar_status = DISPATCH_GROUND_LIDAR_UNKNOW;
 	m_plc_id = 0;
 	m_ground_lidar_id = 0;
+	m_ground_status_msg_updata_flag = false;
+
 }
 
 Dispatch_ground_lidar::~Dispatch_ground_lidar()
@@ -22,65 +24,44 @@ Error_manager Dispatch_ground_lidar::dispatch_plc_init(int plc_id, int ground_li
 {
 	m_plc_id =	plc_id;
 	m_ground_lidar_id = ground_lidar_id;
+	mp_execute_thread = nullptr ;
+	m_ground_status_msg_updata_time = std::chrono::system_clock::now();
+	m_ground_status_msg_updata_flag = false;
+
+	//	线程默认开启
+	m_execute_condition.reset(false, true, false);
+	mp_execute_thread = new std::thread(&Dispatch_ground_lidar::execute_thread_fun, this);
+
+	m_dispatch_ground_lidar_status = DISPATCH_GROUND_LIDAR_READY;
 
 	return Error_code::SUCCESS;
 }
 //调度地面雷达 反初始化
 Error_manager Dispatch_ground_lidar::dispatch_plc_uninit()
 {
-	return Error_code::SUCCESS;
-}
-//调度地面雷达 执行状态消息
-Error_manager Dispatch_ground_lidar::execute_for_ground_status_msg(message::Ground_status_msg &ground_status_msg)
-{
-	//将nnxx的protobuf 转化为 snap7的DB块,  把店面雷达数据转发给plc
-	int t_inlet_id = ground_status_msg.terminal_id() %2;
-	std::unique_lock<std::mutex> t_lock(Dispatch_communication::get_instance_references().m_data_lock);
-	Dispatch_communication::Ground_lidar_response_from_manager_to_plc_for_data * p_response_data = & Dispatch_communication::get_instance_references().m_ground_lidar_response_from_manager_to_plc_for_data[t_inlet_id];
-	Dispatch_communication::Ground_lidar_response_from_manager_to_plc_for_key * p_response_key = & Dispatch_communication::get_instance_references().m_ground_lidar_response_from_manager_to_plc_for_key[t_inlet_id];
-	Dispatch_communication::Ground_lidar_request_from_plc_to_manager * p_request = & Dispatch_communication::get_instance_references().m_ground_lidar_request_from_plc_to_manager[t_inlet_id];
-
-	if ( p_request->m_request_communication_mode == 0 )
-	{
-		p_response_key->m_response_heartbeat++;
-		p_response_key->m_response_communication_mode = p_request->m_request_communication_mode;
-		p_response_key->m_response_refresh_command = p_request->m_request_refresh_command;
-
-		p_response_data->m_response_car_center_x = ground_status_msg.locate_information_realtime().locate_x();
-		p_response_data->m_response_car_center_y = ground_status_msg.locate_information_realtime().locate_y();
-		p_response_data->m_response_car_angle = ground_status_msg.locate_information_realtime().locate_angle();
-		p_response_data->m_response_car_front_theta = ground_status_msg.locate_information_realtime().locate_front_theta();
-		p_response_data->m_response_car_length = ground_status_msg.locate_information_realtime().locate_length();
-		p_response_data->m_response_car_width = ground_status_msg.locate_information_realtime().locate_width();
-		p_response_data->m_response_car_height = ground_status_msg.locate_information_realtime().locate_height();
-		p_response_data->m_response_car_wheel_base = ground_status_msg.locate_information_realtime().locate_wheel_base();
-		p_response_data->m_response_car_wheel_width = ground_status_msg.locate_information_realtime().locate_wheel_width();
-		p_response_data->m_response_locate_correct = ground_status_msg.locate_information_realtime().locate_correct();
-	}
-	else if ( p_response_key->m_response_refresh_command == p_request->m_request_refresh_command )
+	if (mp_execute_thread)
 	{
-		p_response_key->m_response_heartbeat++;
-		p_response_key->m_response_communication_mode = p_request->m_request_communication_mode;
-		p_response_key->m_response_refresh_command = p_request->m_request_refresh_command;
+		m_execute_condition.kill_all();
 	}
-	else
+	if (mp_execute_thread)
 	{
-		p_response_key->m_response_heartbeat++;
-		p_response_key->m_response_communication_mode = p_request->m_request_communication_mode;
-		p_response_key->m_response_refresh_command = p_request->m_request_refresh_command;
-
-		p_response_data->m_response_car_center_x = ground_status_msg.locate_information_realtime().locate_x();
-		p_response_data->m_response_car_center_y = ground_status_msg.locate_information_realtime().locate_y();
-		p_response_data->m_response_car_angle = ground_status_msg.locate_information_realtime().locate_angle();
-		p_response_data->m_response_car_front_theta = ground_status_msg.locate_information_realtime().locate_front_theta();
-		p_response_data->m_response_car_length = ground_status_msg.locate_information_realtime().locate_length();
-		p_response_data->m_response_car_width = ground_status_msg.locate_information_realtime().locate_width();
-		p_response_data->m_response_car_height = ground_status_msg.locate_information_realtime().locate_height();
-		p_response_data->m_response_car_wheel_base = ground_status_msg.locate_information_realtime().locate_wheel_base();
-		p_response_data->m_response_car_wheel_width = ground_status_msg.locate_information_realtime().locate_wheel_width();
-		p_response_data->m_response_locate_correct = ground_status_msg.locate_information_realtime().locate_correct();
+		mp_execute_thread->join();
+		delete mp_execute_thread;
+		mp_execute_thread = NULL;
 	}
 
+	m_dispatch_ground_lidar_status = Dispatch_ground_lidar::DISPATCH_GROUND_LIDAR_UNKNOW;
+
+	return Error_code::SUCCESS;
+}
+//调度地面雷达 执行状态消息
+Error_manager Dispatch_ground_lidar::execute_for_ground_status_msg(message::Ground_status_msg &ground_status_msg)
+{
+	std::unique_lock<std::mutex> t_lock2(m_lock);
+	m_ground_status_msg = ground_status_msg;
+	m_ground_status_msg_updata_time = std::chrono::system_clock::now();
+	m_ground_status_msg_updata_flag = true;
+	return Error_code::SUCCESS;
 }
 
 Dispatch_ground_lidar::Dispatch_ground_lidar_status Dispatch_ground_lidar::get_dispatch_ground_lidar_status()
@@ -92,6 +73,96 @@ Dispatch_ground_lidar::Dispatch_ground_lidar_status Dispatch_ground_lidar::get_d
 
 
 
+//执行外界任务的执行函数
+void Dispatch_ground_lidar::execute_thread_fun()
+{
+	LOG(INFO) << " Dispatch_ground_lidar::execute_thread_fun() start " << this;
+	Error_manager t_error;
+
+	while (m_execute_condition.is_alive())
+	{
+		m_execute_condition.wait();
+		if (m_execute_condition.is_alive())
+		{
+			std::this_thread::sleep_for(std::chrono::milliseconds(1));
+//			std::this_thread::sleep_for(std::chrono::seconds(1));
+
+			std::this_thread::yield();
+
+
+			if (m_ground_status_msg_updata_flag)
+			{
+				std::unique_lock<std::mutex> t_lock(Dispatch_communication::get_instance_references().m_data_lock);
+				std::unique_lock<std::mutex> t_lock2(m_lock);
+
+				//将nnxx的protobuf 转化为 snap7的DB块,  把店面雷达数据转发给plc
+				int t_inlet_id = m_ground_status_msg.terminal_id() % 2;
+				Dispatch_communication::Ground_lidar_response_from_manager_to_plc_for_data *p_response_data = &Dispatch_communication::get_instance_references().m_ground_lidar_response_from_manager_to_plc_for_data[t_inlet_id];
+				Dispatch_communication::Ground_lidar_response_from_manager_to_plc_for_key *p_response_key = &Dispatch_communication::get_instance_references().m_ground_lidar_response_from_manager_to_plc_for_key[t_inlet_id];
+				Dispatch_communication::Ground_lidar_request_from_plc_to_manager *p_request = &Dispatch_communication::get_instance_references().m_ground_lidar_request_from_plc_to_manager[t_inlet_id];
+
+				if (p_request->m_request_communication_mode == 0)
+				{
+					p_response_key->m_response_heartbeat++;
+					p_response_key->m_response_communication_mode = p_request->m_request_communication_mode;
+					p_response_key->m_response_refresh_command = p_request->m_request_refresh_command;
+
+					p_response_data->m_response_car_center_x = m_ground_status_msg.locate_information_realtime().locate_x();
+					p_response_data->m_response_car_center_y = m_ground_status_msg.locate_information_realtime().locate_y();
+					p_response_data->m_response_car_angle = m_ground_status_msg.locate_information_realtime().locate_angle();
+					p_response_data->m_response_car_front_theta = m_ground_status_msg.locate_information_realtime().locate_front_theta();
+					p_response_data->m_response_car_length = m_ground_status_msg.locate_information_realtime().locate_length();
+					p_response_data->m_response_car_width = m_ground_status_msg.locate_information_realtime().locate_width();
+					p_response_data->m_response_car_height = m_ground_status_msg.locate_information_realtime().locate_height();
+					p_response_data->m_response_car_wheel_base = m_ground_status_msg.locate_information_realtime().locate_wheel_base();
+					p_response_data->m_response_car_wheel_width = m_ground_status_msg.locate_information_realtime().locate_wheel_width();
+					p_response_data->m_response_locate_correct = m_ground_status_msg.locate_information_realtime().locate_correct();
+				}
+				else if (p_response_key->m_response_refresh_command == p_request->m_request_refresh_command)
+				{
+					p_response_key->m_response_heartbeat++;
+					p_response_key->m_response_communication_mode = p_request->m_request_communication_mode;
+					p_response_key->m_response_refresh_command = p_request->m_request_refresh_command;
+				}
+				else
+				{
+					p_response_key->m_response_heartbeat++;
+					p_response_key->m_response_communication_mode = p_request->m_request_communication_mode;
+					p_response_key->m_response_refresh_command = p_request->m_request_refresh_command;
+
+					p_response_data->m_response_car_center_x = m_ground_status_msg.locate_information_realtime().locate_x();
+					p_response_data->m_response_car_center_y = m_ground_status_msg.locate_information_realtime().locate_y();
+					p_response_data->m_response_car_angle = m_ground_status_msg.locate_information_realtime().locate_angle();
+					p_response_data->m_response_car_front_theta = m_ground_status_msg.locate_information_realtime().locate_front_theta();
+					p_response_data->m_response_car_length = m_ground_status_msg.locate_information_realtime().locate_length();
+					p_response_data->m_response_car_width = m_ground_status_msg.locate_information_realtime().locate_width();
+					p_response_data->m_response_car_height = m_ground_status_msg.locate_information_realtime().locate_height();
+					p_response_data->m_response_car_wheel_base = m_ground_status_msg.locate_information_realtime().locate_wheel_base();
+					p_response_data->m_response_car_wheel_width = m_ground_status_msg.locate_information_realtime().locate_wheel_width();
+					p_response_data->m_response_locate_correct = m_ground_status_msg.locate_information_realtime().locate_correct();
+				}
+
+				m_ground_status_msg_updata_flag = false;
+			}
+
+
+			//判断超时
+			{
+				std::unique_lock<std::mutex> t_lock3(m_lock);
+				if (std::chrono::system_clock::now() - m_ground_status_msg_updata_time > std::chrono::seconds(5))
+				{
+					m_dispatch_ground_lidar_status = DISPATCH_GROUND_LIDAR_DISCONNECT;
+				}
+				else
+				{
+					m_dispatch_ground_lidar_status = DISPATCH_GROUND_LIDAR_READY;
+				}
+			}
+		}
+
+	}
+}
+
 
 
 

+ 11 - 1
dispatch/dispatch_ground_lidar.h

@@ -47,7 +47,8 @@ public://API functions
 public://get or set member variable
 	Dispatch_ground_lidar_status get_dispatch_ground_lidar_status();
 protected://member functions
-
+	//执行外界任务的执行函数
+	void execute_thread_fun();
 protected://member variable
 	std::atomic<Dispatch_ground_lidar_status>				m_dispatch_ground_lidar_status;//调度地面雷达的状态
 
@@ -55,6 +56,15 @@ protected://member variable
 	int 										m_plc_id;				//设备id, 索引, 就是楚天车库的单元号.
 	int 										m_ground_lidar_id;		//地面雷达的id
 	std::mutex									m_lock;	//锁
+
+	//任务执行线程
+	std::thread*        						mp_execute_thread;   			//执行的线程指针,内存由本类管理
+	Thread_condition							m_execute_condition;			//执行的条件变量
+
+	//数据缓存
+	message::Ground_status_msg 						m_ground_status_msg;
+	std::chrono::system_clock::time_point			m_ground_status_msg_updata_time;	//状态更新时间点
+	std::atomic<bool>								m_ground_status_msg_updata_flag;
 private:
 
 };