|
@@ -3,16 +3,17 @@
|
|
|
/**
|
|
|
* 初始化连接
|
|
|
* */
|
|
|
-void Wj_lidar_encapsulation::init_tcp_connection(const char *addr, int port, Async_Client **client_, fundata_t fundata_)
|
|
|
+void Wj_lidar_encapsulation::init_tcp_connection(const char *addr, int port, Async_Client **client_, fundata_t fundata_, Wj_lidar_encapsulation *p_handle)
|
|
|
{
|
|
|
|
|
|
io_service iosev;
|
|
|
|
|
|
ip::tcp::endpoint ep(ip::address_v4::from_string(addr), port);
|
|
|
- *client_ = new Async_Client(iosev, ep, fundata_, this);
|
|
|
+ *client_ = new Async_Client(iosev, ep, fundata_, p_handle);
|
|
|
+ (*client_)->initialize();
|
|
|
|
|
|
iosev.run();
|
|
|
-
|
|
|
+ LOG(WARNING) << "ip: " << addr << " 万集异步读写线程退出";
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -23,55 +24,56 @@ Error_manager Wj_lidar_encapsulation::boost_tcp_init_connection(const char *addr
|
|
|
int timecnt = 0;
|
|
|
*client_ = NULL;
|
|
|
|
|
|
- boost::thread tmp(&init_tcp_connection, addr, port, client_, fundata_);
|
|
|
+ boost::thread tmp(&init_tcp_connection, addr, port, client_, fundata_, this);
|
|
|
tmp.detach();
|
|
|
|
|
|
while (timecnt < 50)
|
|
|
{
|
|
|
timecnt++;
|
|
|
usleep(20000); //20 ms
|
|
|
- if ((*client_)->client_connection_status())
|
|
|
+ if ((*client_)->client_initialize_status() && (*client_)->client_connection_status())
|
|
|
{
|
|
|
return Error_manager(Error_code::SUCCESS);
|
|
|
}
|
|
|
}
|
|
|
- *client_ = NULL;
|
|
|
+ // delete *client_;
|
|
|
+ // *client_ = NULL;
|
|
|
return Error_manager(Error_code::WJ_LIDAR_CONNECT_FAILED);
|
|
|
}
|
|
|
|
|
|
-/**
|
|
|
- * 异步写指令
|
|
|
- * */
|
|
|
-Error_manager Wj_lidar_encapsulation::boost_tcp_async_send(Async_Client *client_, const char *msg, const int len)
|
|
|
-{
|
|
|
- if (client_ == NULL || client_->client_connection_status() == 0)
|
|
|
- {
|
|
|
- LOG(WARNING) << "not connected , need to connect first \n";
|
|
|
- return Error_manager(Error_code::WJ_LIDAR_WRITE_FAILED);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- client_->client_async_write((char *)msg, len);
|
|
|
- return Error_manager(Error_code::SUCCESS);
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * 异步读数据
|
|
|
- * */
|
|
|
-Error_manager Wj_lidar_encapsulation::boost_tcp_async_read(Async_Client *client_)
|
|
|
-{
|
|
|
- if (client_ == NULL || client_->client_connection_status() == 0)
|
|
|
- {
|
|
|
- LOG(WARNING) << "not connected , need to connect first \n";
|
|
|
- return Error_manager(Error_code::WJ_LIDAR_READ_FAILED);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- client_->client_async_read();
|
|
|
- return Error_manager(Error_code::SUCCESS);;
|
|
|
- }
|
|
|
-}
|
|
|
+// /**
|
|
|
+// * 异步写指令
|
|
|
+// * */
|
|
|
+// Error_manager Wj_lidar_encapsulation::boost_tcp_async_send(Async_Client *client_, const char *msg, const int len)
|
|
|
+// {
|
|
|
+// if (client_ == NULL || client_->client_connection_status() == 0)
|
|
|
+// {
|
|
|
+// LOG(WARNING) << "not connected , need to connect first \n";
|
|
|
+// return Error_manager(Error_code::WJ_LIDAR_WRITE_FAILED);
|
|
|
+// }
|
|
|
+// else
|
|
|
+// {
|
|
|
+// client_->client_async_write((char *)msg, len);
|
|
|
+// return Error_manager(Error_code::SUCCESS);
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+// /**
|
|
|
+// * 异步读数据
|
|
|
+// * */
|
|
|
+// Error_manager Wj_lidar_encapsulation::boost_tcp_async_read(Async_Client *client_)
|
|
|
+// {
|
|
|
+// if (client_ == NULL || client_->client_connection_status() == 0)
|
|
|
+// {
|
|
|
+// LOG(WARNING) << "not connected , need to connect first \n";
|
|
|
+// return Error_manager(Error_code::WJ_LIDAR_READ_FAILED);
|
|
|
+// }
|
|
|
+// else
|
|
|
+// {
|
|
|
+// client_->client_async_read();
|
|
|
+// return Error_manager(Error_code::SUCCESS);;
|
|
|
+// }
|
|
|
+// }
|
|
|
|
|
|
/**
|
|
|
* 万集读数据回调
|
|
@@ -99,39 +101,32 @@ void Wj_lidar_encapsulation::read_callback(const char *addr, int port, const cha
|
|
|
Error_manager Wj_lidar_encapsulation::get_cloud(pcl::PointCloud<pcl::PointXYZ>::Ptr &cloud,
|
|
|
std::chrono::steady_clock::time_point command_time, int timeout_milli)
|
|
|
{
|
|
|
- if (!mb_initialized || mp_protocol == 0)
|
|
|
+ // 1. 检查参数合法性
|
|
|
+ if (!mb_initialized || mp_protocol == 0 || !mp_protocol->get_initialize_status())
|
|
|
{
|
|
|
return Error_manager(Error_code::WJ_LIDAR_UNINITIALIZED);
|
|
|
}
|
|
|
- std::chrono::steady_clock::time_point scan_time_temp = mp_protocol->get_scan_time();
|
|
|
- while (std::chrono::duration_cast<std::chrono::milliseconds>(scan_time_temp - command_time).count() < timeout_milli)
|
|
|
+
|
|
|
+ // 2. 获取扫描时间,时间点位于指令时刻之后,则获得的点云合法
|
|
|
+ // 若当前时间超过(指令时刻+超时时间段),则获取点云函数超时退出
|
|
|
+ int current_duration = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - command_time).count();
|
|
|
+ while (current_duration < timeout_milli)
|
|
|
{
|
|
|
+ current_duration = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - command_time).count();
|
|
|
|
|
|
- }
|
|
|
+ std::chrono::steady_clock::time_point scan_time_temp = mp_protocol->get_scan_time();
|
|
|
+ int cloud_duration = std::chrono::duration_cast<std::chrono::milliseconds>(scan_time_temp - command_time).count();
|
|
|
|
|
|
- if (!mp_protocol->get_scan_points(cloud))
|
|
|
- {
|
|
|
- return Error_manager(Error_code::WJ_LIDAR_PROTOCOL_FAILED);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- cloud->clear();
|
|
|
- // m_mutex.lock();
|
|
|
- cloud->operator+=(*m_cloud);
|
|
|
- // std::cout<<cloud->size()<<std::endl;
|
|
|
- // return true;
|
|
|
- ros::Duration duration = ros::Time::now() - m_cloud_time;
|
|
|
- if (print)
|
|
|
- {
|
|
|
- // if (m_lidar_param.topic() == "/scan3" || m_lidar_param.topic() == "/scan4" || m_lidar_param.topic() == "/scan5")
|
|
|
- // {
|
|
|
- std::cout << m_lidar_param.topic() << " duration: " << duration.toSec() * 1000 << ", cloud size: " << cloud->size() << std::endl;
|
|
|
- // }
|
|
|
+ // 扫描时刻位于指令时刻之后
|
|
|
+ if (cloud_duration > 0)
|
|
|
+ {
|
|
|
+ Error_manager code = mp_protocol->get_scan_points(cloud);
|
|
|
+ // 内部已打印错误信息
|
|
|
+ return code;
|
|
|
+ }
|
|
|
+ usleep(1000 * 50);
|
|
|
}
|
|
|
- if (duration.toSec() * 1000 > timeout_milli)
|
|
|
- return false;
|
|
|
- else
|
|
|
- return true;
|
|
|
+ return Error_manager(Error_code::WJ_LIDAR_GET_CLOUD_TIMEOUT);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -144,29 +139,27 @@ Error_manager Wj_lidar_encapsulation::initialize(wj::wjLidarParams params)
|
|
|
{
|
|
|
m_ip = params.net_config().ip_address();
|
|
|
m_port = params.net_config().port();
|
|
|
- if (boost_tcp_init_connection(m_ip.c_str(), m_port, &mp_client, &read_callback) == SUCCESS)
|
|
|
+ if (boost_tcp_init_connection(m_ip.c_str(), m_port, &mp_client, &read_callback) != SUCCESS)
|
|
|
{
|
|
|
- if (boost_tcp_async_read(mp_client) != SUCCESS)
|
|
|
- {
|
|
|
- LOG(WARNING) << m_ip << " null or disconnected.";
|
|
|
- return Error_manager(Error_code::WJ_LIDAR_CONNECT_FAILED);
|
|
|
- }
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- LOG(WARNING) << m_ip << " 1 sec timeout, connection failed.";
|
|
|
+ LOG(WARNING) << m_ip << " 1 sec timeout, connection failed, will try to reconnect";
|
|
|
return Error_manager(Error_code::WJ_LIDAR_CONNECT_FAILED);
|
|
|
}
|
|
|
- }else{
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
return Error_manager(Error_code::PARAMETER_ERROR);
|
|
|
}
|
|
|
// 2. 创建协议栈对象
|
|
|
mp_protocol = new Wj_716_lidar_protocol();
|
|
|
Error_manager code = mp_protocol->initialize(params);
|
|
|
- if(code != SUCCESS){
|
|
|
- LOG(ERROR) << code.to_string();
|
|
|
- }else{
|
|
|
+ if (code != SUCCESS)
|
|
|
+ {
|
|
|
+ LOG(ERROR) << "wj_lidar " << m_ip << ":" << m_port << " initialize failed. " << code.to_string();
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
mb_initialized = true;
|
|
|
+ LOG(INFO) << "wj_lidar " << m_ip << ":" << m_port << " successfully initialized.";
|
|
|
}
|
|
|
return code;
|
|
|
}
|
|
@@ -187,6 +180,7 @@ Wj_lidar_encapsulation::~Wj_lidar_encapsulation()
|
|
|
delete mp_client;
|
|
|
mp_client = 0;
|
|
|
}
|
|
|
+
|
|
|
if (mp_protocol != 0)
|
|
|
{
|
|
|
delete mp_protocol;
|