terminal_command_executor.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. #include "terminal_command_executor.h"
  2. #include <glog/logging.h>
  3. #include <chrono>
  4. #include <pcl/filters/passthrough.h>
  5. #include "../tool/pathcreator.h"
  6. ///增加获取wj电子围栏模块头文件
  7. #include "terminor_msg.pb.h"
  8. #include "MeasureRequest.h"
  9. //////以下两个函数用于测试,读取指定点云
  10. bool string2point(std::string str,pcl::PointXYZ& point)
  11. {
  12. std::istringstream iss;
  13. iss.str(str);
  14. float value;
  15. float data[3]={0};
  16. for(int i=0;i<3;++i)
  17. {
  18. if(iss>>value)
  19. {
  20. data[i]=value;
  21. }
  22. else
  23. {
  24. return false;
  25. }
  26. }
  27. point.x=data[0];
  28. point.y=data[1];
  29. point.z=data[2];
  30. return true;
  31. }
  32. pcl::PointCloud<pcl::PointXYZ>::Ptr ReadTxtCloud(std::string file)
  33. {
  34. std::ifstream fin(file.c_str());
  35. const int line_length=64;
  36. char str[line_length]={0};
  37. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
  38. while(fin.getline(str,line_length))
  39. {
  40. pcl::PointXYZ point;
  41. if(string2point(str,point))
  42. {
  43. cloud->push_back(point);
  44. }
  45. }
  46. return cloud;
  47. }
  48. //////////
  49. Terminor_Command_Executor::Terminor_Command_Executor(Terminal::Terminor_parameter parameter)
  50. :m_terminor_statu(TERMINOR_READY), mp_command_thread(0),m_timeout_second(15),mb_force_quit(false)
  51. {
  52. m_terminor_parameter=parameter;
  53. }
  54. Error_manager Terminor_Command_Executor::force_stop_command()
  55. {
  56. mb_force_quit=true;
  57. //等待执行线程退出
  58. if(mp_command_thread!=NULL)
  59. {
  60. if(mp_command_thread->joinable())
  61. {
  62. mp_command_thread->join();
  63. delete mp_command_thread;
  64. mp_command_thread=NULL;
  65. }
  66. }
  67. return SUCCESS;
  68. }
  69. Terminor_Command_Executor::~Terminor_Command_Executor()
  70. {
  71. if(mp_command_thread!=0)
  72. {
  73. force_stop_command();
  74. if(mp_command_thread->joinable())
  75. {
  76. mp_command_thread->join();
  77. delete mp_command_thread;
  78. mp_command_thread=0;
  79. }
  80. }
  81. }
  82. TerminorStatu Terminor_Command_Executor::get_terminor_statu()
  83. {
  84. return m_terminor_statu;
  85. }
  86. Error_manager Terminor_Command_Executor::execute_command(std::vector<Laser_base*> lasers,float timeout)
  87. {
  88. Error_manager code;
  89. //第一步检查输入参数合法性
  90. for(int i=0;i<lasers.size();++i)
  91. {
  92. if(lasers[i]==0)
  93. {
  94. char description[255]={0};
  95. sprintf(description,"terminor input laser* is null index:%d, terminor id:%d",i,m_terminor_parameter.id());
  96. return Error_manager(TERMINOR_INPUT_LASER_NULL,NORMAL,description);
  97. }
  98. }
  99. //第二步,雷达,plc以及定位模块是否能正常接收指令;
  100. for(int i=0;i<lasers.size();++i)
  101. {
  102. code=lasers[i]->check_laser();
  103. if(code!=SUCCESS)
  104. {
  105. LOG(ERROR)<<" terminor check laser error:"<<code.to_string()<<" terminor id :"<<m_terminor_parameter.id();
  106. return code;
  107. }
  108. }
  109. // 检查当前终端指令执行器的状态是否空闲;如果空闲,进入测量流程,设置忙碌状态
  110. std::lock_guard<std::mutex> t_lock(m_mutex_lock);
  111. if(get_terminor_statu()!=TERMINOR_READY)
  112. {
  113. char description[255]={0};
  114. sprintf(description,"terminor is not ready, terminor id:%d",m_terminor_parameter.id());
  115. return Error_manager(TERMINOR_NOT_READY,NORMAL,description);
  116. }
  117. //检查上次线程是否退出,并等待线程退出
  118. if(mp_command_thread!=0)
  119. {
  120. if(mp_command_thread->joinable())
  121. {
  122. mp_command_thread->join();
  123. delete mp_command_thread;
  124. mp_command_thread=0;
  125. }
  126. }
  127. //第二步接受指令,保存输入参数,更新指令状态为TERMINOR_BUSY,启动内部工作线程.返回SUCCESS
  128. mp_laser_vector=lasers;
  129. m_terminor_statu=TERMINOR_BUSY;
  130. m_timeout_second=timeout;
  131. mp_command_thread=new std::thread(thread_command_working,this);
  132. if(mp_command_thread==0)
  133. {
  134. return Error_manager(TERMINOR_CREATE_WORKING_THREAD_FAILED,NORMAL,"terminor create thread failed");
  135. }
  136. return SUCCESS;
  137. }
  138. void Terminor_Command_Executor::thread_command_working(Terminor_Command_Executor* terminor)
  139. {
  140. if(terminor==NULL)
  141. {
  142. return ;
  143. }
  144. //执行指令,阻塞直到指令执行完成或者异常结束,此处如果失败,连续三次测量
  145. Error_manager code;
  146. int measure_times=0;
  147. while(terminor->mb_force_quit==false) {
  148. if (measure_times == 3)
  149. break;
  150. code = terminor->scanning_measuring();
  151. switch (code.get_error_code()) {
  152. case SUCCESS:
  153. break;
  154. case TERMINOR_LASER_TIMEOUT: {
  155. //雷达扫描超时,代表雷达故障,此时需要重置雷达模块
  156. }
  157. }
  158. if (code != SUCCESS ) {
  159. if(measure_times==2)
  160. {
  161. LOG(ERROR) << " Measure times("<<measure_times<<") failed : "<< code.to_string();
  162. }
  163. else
  164. {
  165. LOG(WARNING) << " Measure times("<<measure_times<<") failed : "<< code.to_string();
  166. }
  167. measure_times++;
  168. }
  169. else {
  170. LOG(INFO) << " Measure complete!!! times:"<<measure_times;
  171. break;
  172. }
  173. }
  174. //更新终端状态为Ready状态
  175. terminor->m_terminor_statu=TERMINOR_READY;
  176. if (code != SUCCESS) {
  177. LOG(ERROR) << "Command measure execute failed : "<< code.to_string();
  178. }
  179. else {
  180. LOG(INFO) << "Command execute complete!!!";
  181. }
  182. }
  183. void Terminor_Command_Executor::set_save_root_path(std::string root)
  184. {
  185. m_save_root_path=root;
  186. }
  187. Error_manager Terminor_Command_Executor::scanning_measuring()
  188. {
  189. //计时起点
  190. auto t_start_point=std::chrono::system_clock::now();
  191. //准备目录
  192. time_t tt;
  193. time( &tt );
  194. tt = tt + 8*3600; // transform the time zone
  195. tm* t= gmtime( &tt );
  196. char path[255]={0};
  197. sprintf(path,"%s/%4d/%02d/%02d",m_save_root_path.c_str(),
  198. t->tm_year + 1900,
  199. t->tm_mon + 1,
  200. t->tm_mday);
  201. PathCreator path_creator;
  202. path_creator.CreateDatePath(path);
  203. std::string project_path=path_creator.GetCurPath();
  204. Error_manager code;
  205. //第一步,启动雷达扫描点云,切换当前状态为扫描中
  206. //根据配置筛选雷达
  207. pcl::PointCloud<pcl::PointXYZ>::Ptr scan_cloud(new pcl::PointCloud<pcl::PointXYZ>);
  208. std::mutex cloud_lock;
  209. std::vector<Task_Base*> laser_task_vector;
  210. std::vector<Laser_base*> tp_lasers;
  211. for(int i=0;i<m_terminor_parameter.laser_parameter_size();++i)
  212. {
  213. int laser_id=m_terminor_parameter.laser_parameter(i).id();
  214. int frame_count=m_terminor_parameter.laser_parameter(i).frame_count();
  215. if(frame_count<=0)
  216. {
  217. LOG(WARNING)<<"terminal parameter laser["<<laser_id<<"] frame count is <0";
  218. continue;
  219. }
  220. if(laser_id>=0&&laser_id<mp_laser_vector.size())
  221. {
  222. //判断该id的雷达是否已经添加进去, 放置配置中出现重复的雷达编号
  223. bool tb_repeat=false;
  224. for(int j=0;j<tp_lasers.size();++j)
  225. {
  226. if(tp_lasers[j]==mp_laser_vector[laser_id])
  227. {
  228. tb_repeat=true;
  229. break;
  230. }
  231. }
  232. if(tb_repeat==false)
  233. {
  234. tp_lasers.push_back(mp_laser_vector[laser_id]);
  235. //创建扫描任务,
  236. Laser_task* laser_task=new Laser_task();
  237. //
  238. laser_task->task_init(TASK_CREATED,scan_cloud,&cloud_lock);
  239. laser_task->set_task_frame_maxnum(frame_count);
  240. laser_task->set_task_save_path(project_path);
  241. laser_task_vector.push_back(laser_task);
  242. //发送任务单给雷达
  243. code=tp_lasers[i]->execute_task(laser_task);
  244. if(code!=SUCCESS)
  245. {
  246. return code;
  247. }
  248. }
  249. }
  250. }
  251. if(tp_lasers.size()==0)
  252. {
  253. return Error_manager(TERMINOR_NOT_CONTAINS_LASER,NORMAL,"terminal not contains laser");
  254. }
  255. m_terminor_statu=TERMINOR_SCANNING;
  256. //等待雷达完成任务单
  257. while(1)
  258. {
  259. //判断是否强制退出
  260. if(mb_force_quit==true)
  261. {
  262. char description[255]={0};
  263. sprintf(description,"ternimal is force quit terminal id : %d",m_terminor_parameter.id());
  264. return Error_manager(TERMINOR_FORCE_QUIT,NORMAL,description);
  265. }
  266. //判断雷达任务单是否全部完成
  267. bool tb_laser_complete=true;
  268. for(int i=0;i<laser_task_vector.size();++i)
  269. {
  270. tb_laser_complete &=(TASK_OVER==laser_task_vector[i]->get_statu());
  271. }
  272. if(tb_laser_complete)
  273. {
  274. break;
  275. }
  276. //计算扫描时间,若超时,并且没有点,则返回错误.
  277. auto t_end_point=std::chrono::system_clock::now();
  278. auto duration=std::chrono::duration_cast<std::chrono::milliseconds>(t_end_point - t_start_point);
  279. double second=double(duration.count()) *
  280. std::chrono::milliseconds::period::num / std::chrono::milliseconds::period::den;
  281. if(second>m_timeout_second)
  282. {
  283. //扫描超时,点云中没有点,则返回错误
  284. if(scan_cloud->size()==0)
  285. {
  286. return Error_manager(TERMINOR_LASER_TIMEOUT,MAJOR_ERROR,"laser scanning timeout");
  287. }
  288. }
  289. usleep(1000*100);
  290. }
  291. //检查雷达任务完成情况,是否是正常完成
  292. LOG(INFO)<<" laser scanning over cloud size:"<<scan_cloud->size();
  293. //释放扫描任务单
  294. for(int i=0;i<laser_task_vector.size();++i)
  295. {
  296. if(laser_task_vector[i]!=0)
  297. {
  298. delete laser_task_vector[i];
  299. laser_task_vector[i]=NULL;
  300. }
  301. }
  302. //第二步,根据区域筛选点云
  303. pcl::PointCloud<pcl::PointXYZ>::Ptr locate_cloud(new pcl::PointCloud<pcl::PointXYZ>);
  304. pcl::PassThrough<pcl::PointXYZ> passX;
  305. pcl::PassThrough<pcl::PointXYZ> passY;
  306. pcl::PassThrough<pcl::PointXYZ> passZ;
  307. passX.setInputCloud(scan_cloud);
  308. passX.setFilterFieldName("x");
  309. passX.setFilterLimits(m_terminor_parameter.area_3d().min_x(),m_terminor_parameter.area_3d().max_x());
  310. passX.filter(*locate_cloud);
  311. passY.setInputCloud(locate_cloud);
  312. passY.setFilterFieldName("y");
  313. passY.setFilterLimits(m_terminor_parameter.area_3d().min_y(),m_terminor_parameter.area_3d().max_y());
  314. passY.filter(*locate_cloud);
  315. passY.setInputCloud(locate_cloud);
  316. passY.setFilterFieldName("z");
  317. passY.setFilterLimits(m_terminor_parameter.area_3d().min_z(),m_terminor_parameter.area_3d().max_z());
  318. passY.filter(*locate_cloud);
  319. LOG(INFO)<<"筛选点云点数 : "<<locate_cloud->size();
  320. return code;
  321. }
  322. cv::RotatedRect Terminor_Command_Executor::create_rotate_rect(float length,float width,float angle,float cx,float cy)
  323. {
  324. const double C_PI=3.14159265;
  325. float theta=C_PI*(angle/180.0);
  326. float a00=cos(theta);
  327. float a01=-sin(theta);
  328. float a10=sin(theta);
  329. float a11=cos(theta);
  330. cv::Point2f point[4];
  331. point[0].x=-length/2.0;
  332. point[0].y=-width/2.0;
  333. point[1].x=-length/2.0;
  334. point[1].y=width/2.0;
  335. point[2].x=length/2.0;
  336. point[2].y=-width/2.0;
  337. point[3].x=length/2.0;
  338. point[3].y=width/2.0;
  339. std::vector<cv::Point2f> point_vec;
  340. for(int i=0;i<4;++i)
  341. {
  342. float x=point[i].x*a00+point[i].y*a01+cx;
  343. float y=point[i].x*a10+point[i].y*a11+cy;
  344. point_vec.push_back(cv::Point2f(x,y));
  345. }
  346. return cv::minAreaRect(point_vec);
  347. }