terminal_command_executor.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  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. std::mutex Terminor_Command_Executor::ms_mutex_scan_measure;
  10. //////以下两个函数用于测试,读取指定点云
  11. bool string2point(std::string str,pcl::PointXYZ& point)
  12. {
  13. std::istringstream iss;
  14. iss.str(str);
  15. float value;
  16. float data[3]={0};
  17. for(int i=0;i<3;++i)
  18. {
  19. if(iss>>value)
  20. {
  21. data[i]=value;
  22. }
  23. else
  24. {
  25. return false;
  26. }
  27. }
  28. point.x=data[0];
  29. point.y=data[1];
  30. point.z=data[2];
  31. return true;
  32. }
  33. pcl::PointCloud<pcl::PointXYZ>::Ptr ReadTxtCloud(std::string file)
  34. {
  35. std::ifstream fin(file.c_str());
  36. const int line_length=64;
  37. char str[line_length]={0};
  38. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
  39. while(fin.getline(str,line_length))
  40. {
  41. pcl::PointXYZ point;
  42. if(string2point(str,point))
  43. {
  44. cloud->push_back(point);
  45. }
  46. }
  47. return cloud;
  48. }
  49. //////////
  50. Terminor_Command_Executor::Terminor_Command_Executor(Terminal::Terminor_parameter parameter)
  51. :m_terminor_statu(TERMINOR_READY), mp_command_thread(0),m_timeout_second(15),mb_force_quit(false)
  52. {
  53. m_terminor_parameter=parameter;
  54. mp_wj_lidar=NULL;
  55. }
  56. Error_manager Terminor_Command_Executor::force_stop_command()
  57. {
  58. mb_force_quit=true;
  59. //等待执行线程退出
  60. if(mp_command_thread!=NULL)
  61. {
  62. if(mp_command_thread->joinable())
  63. {
  64. mp_command_thread->join();
  65. delete mp_command_thread;
  66. mp_command_thread=NULL;
  67. }
  68. }
  69. return SUCCESS;
  70. }
  71. Terminor_Command_Executor::~Terminor_Command_Executor()
  72. {
  73. if(mp_command_thread!=0)
  74. {
  75. force_stop_command();
  76. if(mp_command_thread->joinable())
  77. {
  78. mp_command_thread->join();
  79. delete mp_command_thread;
  80. mp_command_thread=0;
  81. }
  82. }
  83. }
  84. TerminorStatu Terminor_Command_Executor::get_terminor_statu()
  85. {
  86. return m_terminor_statu;
  87. }
  88. Error_manager Terminor_Command_Executor::execute_command(std::vector<Laser_base*> lasers,Fence_controller* wj_lidar,
  89. Plc_Communicator* plc,Locater* locater,Verify_result* verify_tool,float timeout)
  90. {
  91. Error_manager code;
  92. //第一步检查输入参数合法性
  93. for(int i=0;i<lasers.size();++i)
  94. {
  95. if(lasers[i]==0)
  96. {
  97. char description[255]={0};
  98. sprintf(description,"terminor input laser* is null index:%d, terminor id:%d",i,m_terminor_parameter.id());
  99. return Error_manager(TERMINOR_INPUT_LASER_NULL,NORMAL,description);
  100. }
  101. }
  102. if(plc==0)
  103. {
  104. char description[255]={0};
  105. sprintf(description,"terminor input plc* is null terminor id:%d",m_terminor_parameter.id());
  106. return Error_manager(TERMINOR_INPUT_PLC_NULL,NORMAL,description);
  107. }
  108. if(wj_lidar==NULL)
  109. {
  110. char description[255]={0};
  111. sprintf(description,"terminor input wj_lidar* is null terminor id:%d",m_terminor_parameter.id());
  112. return Error_manager(TERMINOR_INPUT_LOCATER_NULL,NORMAL,description);
  113. }
  114. if(locater==0)
  115. {
  116. char description[255]={0};
  117. sprintf(description,"terminor input locater* is null terminor id:%d",m_terminor_parameter.id());
  118. return Error_manager(TERMINOR_INPUT_LOCATER_NULL,NORMAL,description);
  119. }
  120. mp_verify_tool=verify_tool;
  121. //第二步,雷达,plc以及定位模块是否能正常接收指令;
  122. for(int i=0;i<lasers.size();++i)
  123. {
  124. code=lasers[i]->check_laser();
  125. if(code!=SUCCESS)
  126. {
  127. LOG(ERROR)<<" terminor check laser error:"<<code.to_string()<<" terminor id :"<<m_terminor_parameter.id();
  128. return code;
  129. }
  130. }
  131. //检查plc状态
  132. code=plc->get_error();
  133. if(code!=SUCCESS)
  134. {
  135. LOG(ERROR)<<"terminor check plc error : "<<code.to_string()<<" terminor id:"<<m_terminor_parameter.id();
  136. return code;
  137. }
  138. //检查测量模块状态
  139. code=locater->get_error();
  140. if(code!=SUCCESS)
  141. {
  142. LOG(ERROR)<<"terminor check locater error : "<<code.to_string()<<" terminor id:"<<m_terminor_parameter.id();
  143. return code;
  144. }
  145. // 检查当前终端指令执行器的状态是否空闲;如果空闲,进入测量流程,设置忙碌状态
  146. std::lock_guard<std::mutex> t_lock(m_mutex_lock);
  147. if(get_terminor_statu()!=TERMINOR_READY)
  148. {
  149. char description[255]={0};
  150. sprintf(description,"terminor is not ready, terminor id:%d",m_terminor_parameter.id());
  151. return Error_manager(TERMINOR_NOT_READY,NORMAL,description);
  152. }
  153. //检查上次线程是否退出,并等待线程退出
  154. if(mp_command_thread!=0)
  155. {
  156. if(mp_command_thread->joinable())
  157. {
  158. mp_command_thread->join();
  159. delete mp_command_thread;
  160. mp_command_thread=0;
  161. }
  162. }
  163. //第二步接受指令,保存输入参数,更新指令状态为TERMINOR_BUSY,启动内部工作线程.返回SUCCESS
  164. mp_laser_vector=lasers;
  165. mp_plc=plc;
  166. mp_wj_lidar=wj_lidar;
  167. mp_locater=locater;
  168. m_terminor_statu=TERMINOR_BUSY;
  169. m_timeout_second=timeout;
  170. mp_command_thread=new std::thread(thread_command_working,this);
  171. if(mp_command_thread==0)
  172. {
  173. return Error_manager(TERMINOR_CREATE_WORKING_THREAD_FAILED,NORMAL,"terminor create thread failed");
  174. }
  175. return SUCCESS;
  176. }
  177. void Terminor_Command_Executor::thread_command_working(Terminor_Command_Executor* terminor)
  178. {
  179. if(terminor==NULL)
  180. {
  181. return ;
  182. }
  183. //执行指令,阻塞直到指令执行完成或者异常结束,此处如果失败,连续三次测量
  184. Error_manager code;
  185. int measure_times=0;
  186. while(terminor->mb_force_quit==false) {
  187. if (measure_times == 3)
  188. break;
  189. code = terminor->scanning_measuring();
  190. switch (code.get_error_code()) {
  191. case SUCCESS:
  192. break;
  193. case TERMINOR_LASER_TIMEOUT: {
  194. //雷达扫描超时,代表雷达故障,此时需要重置雷达模块
  195. }
  196. }
  197. if (code != SUCCESS ) {
  198. if(measure_times==2)
  199. {
  200. LOG(ERROR) << " Measure times("<<measure_times<<") failed : "<< code.to_string();
  201. }
  202. else
  203. {
  204. LOG(WARNING) << " Measure times("<<measure_times<<") failed : "<< code.to_string();
  205. }
  206. measure_times++;
  207. }
  208. else {
  209. LOG(INFO) << " Measure complete!!! times:"<<measure_times;
  210. break;
  211. }
  212. }
  213. //根据结果执行上传任务
  214. code=terminor->post_measure_information();
  215. //更新终端状态为Ready状态
  216. terminor->m_terminor_statu=TERMINOR_READY;
  217. if (code != SUCCESS) {
  218. LOG(ERROR) << "Command measure execute failed : "<< code.to_string();
  219. }
  220. else {
  221. LOG(INFO) << "Command execute complete!!!";
  222. }
  223. }
  224. /*
  225. * 执行上传plc任务
  226. */
  227. Error_manager Terminor_Command_Executor::post_measure_information()
  228. {
  229. //计时起点
  230. auto t_start_point=std::chrono::system_clock::now();
  231. Error_manager code;
  232. //执行plc上传任务
  233. m_terminor_statu=TERMINOR_POSTING;
  234. Plc_Task* plc_task=new Plc_Task();
  235. measure_result measure_information;
  236. measure_information.x=m_measure_information.locate_x;
  237. measure_information.y=m_measure_information.locate_y;
  238. measure_information.angle=m_measure_information.locate_theta;
  239. measure_information.length=m_measure_information.locate_length;
  240. measure_information.width=m_measure_information.locate_width;
  241. measure_information.height=m_measure_information.locate_hight;
  242. measure_information.wheel_base=m_measure_information.locate_wheel_base;
  243. //plc终端编号从1开始,因此,上传给plc的终端编号必须+1
  244. measure_information.terminal_id=m_terminor_parameter.id()+1;
  245. measure_information.correctness=m_measure_information.locate_correct;
  246. plc_task->set_result(measure_information);
  247. code=mp_plc->execute_task(plc_task);
  248. while(code!=SUCCESS)
  249. {
  250. //判断是否强制退出
  251. if(mb_force_quit==true)
  252. {
  253. char description[255]={0};
  254. sprintf(description,"ternimal is force quit terminal id : %d",m_terminor_parameter.id());
  255. return Error_manager(TERMINOR_FORCE_QUIT,NORMAL,description);
  256. }
  257. //对于上传不成功,作失败处理,重新传输
  258. code=mp_plc->execute_task(plc_task);
  259. ///不成功,检测本次流程是否超时
  260. if(code!=SUCCESS)
  261. {
  262. auto t_end_point=std::chrono::system_clock::now();
  263. auto duration=std::chrono::duration_cast<std::chrono::milliseconds>(t_end_point - t_start_point);
  264. double second=double(duration.count()) *
  265. std::chrono::milliseconds::period::num / std::chrono::milliseconds::period::den;
  266. if(second>m_timeout_second)
  267. {
  268. //扫描超时处理
  269. return Error_manager(TERMINOR_POST_PLC_TIMEOUT,NORMAL,"post plc timeout");
  270. }
  271. usleep(1000*100);
  272. }
  273. }
  274. return code;
  275. }
  276. void Terminor_Command_Executor::set_save_root_path(std::string root)
  277. {
  278. m_save_root_path=root;
  279. }
  280. // !!!!!!!!!!!! 临时使用,保存点云debug
  281. void save_cloud_txt(std::string txt, pcl::PointCloud<pcl::PointXYZ>::Ptr pCloud)
  282. {
  283. std::ofstream os;
  284. os.open(txt, std::ios::out);
  285. for (int i = 0; i < pCloud->points.size(); i++)
  286. {
  287. pcl::PointXYZ point = pCloud->points[i];
  288. char buf[255];
  289. memset(buf, 0, 255);
  290. sprintf(buf, "%f %f %f\n", point.x, point.y, point.z);
  291. os.write(buf, strlen(buf));
  292. }
  293. os.close();
  294. }
  295. Error_manager Terminor_Command_Executor::scanning_measuring() {
  296. std::lock_guard<std::mutex> lock(ms_mutex_scan_measure);
  297. Error_manager code;
  298. LOG(INFO) << ">>>>>>>>>>>> Enter terminator ID:" << m_terminor_parameter.id();
  299. //计时起点
  300. auto t_start_point = std::chrono::system_clock::now();
  301. //准备目录
  302. time_t tt;
  303. time(&tt);
  304. tt = tt + 8 * 3600; // transform the time zone
  305. tm *t = gmtime(&tt);
  306. char path[255] = {0};
  307. sprintf(path, "%s/%4d/%02d/%02d", m_save_root_path.c_str(),
  308. t->tm_year + 1900,
  309. t->tm_mon + 1,
  310. t->tm_mday);
  311. PathCreator path_creator;
  312. path_creator.CreateDatePath(path);
  313. std::string project_path = path_creator.GetCurPath();
  314. //第一步,启动雷达扫描点云,切换当前状态为扫描中
  315. //根据配置筛选雷达
  316. pcl::PointCloud<pcl::PointXYZ> scan_cloud;
  317. std::mutex cloud_lock;
  318. std::vector<Laser_task *> laser_task_vector;
  319. std::vector<Laser_base *> tp_lasers;
  320. for (int i = 0; i < m_terminor_parameter.laser_parameter_size(); ++i) {
  321. int laser_id = m_terminor_parameter.laser_parameter(i).id();
  322. int frame_count = m_terminor_parameter.laser_parameter(i).frame_count();
  323. if (frame_count <= 0) {
  324. LOG(WARNING) << "terminal parameter laser[" << laser_id << "] frame count is <0";
  325. continue;
  326. }
  327. if (laser_id >= 0 && laser_id < mp_laser_vector.size()) {
  328. //判断该id的雷达是否已经添加进去, 放置配置中出现重复的雷达编号
  329. bool tb_repeat = false;
  330. for (int j = 0; j < tp_lasers.size(); ++j) {
  331. if (tp_lasers[j] == mp_laser_vector[laser_id]) {
  332. tb_repeat = true;
  333. break;
  334. }
  335. }
  336. if (tb_repeat == false) {
  337. tp_lasers.push_back(mp_laser_vector[laser_id]);
  338. //创建扫描任务,
  339. Laser_task *laser_task = new Laser_task();
  340. //
  341. laser_task->task_init(TASK_CREATED, &scan_cloud, &cloud_lock);
  342. laser_task->set_task_frame_maxnum(frame_count);
  343. laser_task->set_save_path(project_path);
  344. laser_task_vector.push_back(laser_task);
  345. //发送任务单给雷达
  346. code = tp_lasers[i]->execute_task(laser_task);
  347. if (code != SUCCESS) {
  348. return code;
  349. }
  350. }
  351. }
  352. }
  353. if (tp_lasers.size() == 0) {
  354. return Error_manager(TERMINOR_NOT_CONTAINS_LASER, NORMAL, "terminal not contains laser");
  355. }
  356. m_terminor_statu = TERMINOR_SCANNING;
  357. //等待雷达完成任务单
  358. while (1) {
  359. //判断是否强制退出
  360. if (mb_force_quit == true) {
  361. char description[255] = {0};
  362. sprintf(description, "ternimal is force quit terminal id : %d", m_terminor_parameter.id());
  363. return Error_manager(TERMINOR_FORCE_QUIT, NORMAL, description);
  364. }
  365. //判断雷达任务单是否全部完成
  366. bool tb_laser_complete = true;
  367. for (int i = 0; i < laser_task_vector.size(); ++i) {
  368. tb_laser_complete &= (TASK_OVER == laser_task_vector[i]->get_statu());
  369. }
  370. if (tb_laser_complete) {
  371. break;
  372. }
  373. //计算扫描时间,若超时,并且没有点,则返回错误.
  374. auto t_end_point = std::chrono::system_clock::now();
  375. auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(t_end_point - t_start_point);
  376. double second = double(duration.count()) *
  377. std::chrono::milliseconds::period::num / std::chrono::milliseconds::period::den;
  378. if (second > m_timeout_second) {
  379. //扫描超时,点云中没有点,则返回错误
  380. if (scan_cloud.size() == 0) {
  381. return Error_manager(TERMINOR_LASER_TIMEOUT, MAJOR_ERROR, "laser scanning timeout");
  382. }
  383. }
  384. usleep(1000 * 100);
  385. }
  386. //检查雷达任务完成情况,是否是正常完成
  387. LOG(INFO) << " laser scanning over cloud size:" << scan_cloud.size();
  388. //释放扫描任务单
  389. for (int i = 0; i < laser_task_vector.size(); ++i) {
  390. if (laser_task_vector[i] != 0) {
  391. delete laser_task_vector[i];
  392. laser_task_vector[i] = NULL;
  393. }
  394. }
  395. //第二步,根据区域筛选点云
  396. pcl::PointCloud<pcl::PointXYZ>::Ptr locate_cloud(new pcl::PointCloud<pcl::PointXYZ>);
  397. pcl::PassThrough<pcl::PointXYZ> passX;
  398. pcl::PassThrough<pcl::PointXYZ> passY;
  399. pcl::PassThrough<pcl::PointXYZ> passZ;
  400. passX.setInputCloud(scan_cloud.makeShared());
  401. passX.setFilterFieldName("x");
  402. passX.setFilterLimits(m_terminor_parameter.area_3d().min_x(), m_terminor_parameter.area_3d().max_x());
  403. passX.filter(*locate_cloud);
  404. passY.setInputCloud(locate_cloud);
  405. passY.setFilterFieldName("y");
  406. passY.setFilterLimits(m_terminor_parameter.area_3d().min_y(), m_terminor_parameter.area_3d().max_y());
  407. passY.filter(*locate_cloud);
  408. passY.setInputCloud(locate_cloud);
  409. passY.setFilterFieldName("z");
  410. passY.setFilterLimits(m_terminor_parameter.area_3d().min_z(), m_terminor_parameter.area_3d().max_z());
  411. passY.filter(*locate_cloud);
  412. LOG(INFO) << "筛选点云点数 : " << locate_cloud->size();
  413. //第三步,创建测量任务, 进入测量中
  414. Locate_task *locate_task = new Locate_task();
  415. locate_task->set_locate_cloud(locate_cloud);
  416. locate_task->set_save_path(project_path);
  417. m_terminor_statu = TERMINOR_MEASURING;
  418. code = mp_locater->execute_task(locate_task);
  419. m_measure_information.locate_correct = false;
  420. //获取测量结果
  421. Locate_information dj_locate_information = locate_task->get_locate_information();
  422. //added by yct, 获取车轮点云
  423. pcl::PointCloud<pcl::PointXYZ>::Ptr t_combined_wheel_cloud(new pcl::PointCloud<pcl::PointXYZ>);
  424. t_combined_wheel_cloud = locate_task->get_locate_cloud();
  425. //释放locate task
  426. if (locate_task != NULL) {
  427. delete locate_task;
  428. locate_task = NULL;
  429. }
  430. if (code != SUCCESS) {
  431. return code;
  432. }
  433. //进入万集雷达流程--------
  434. Wj_lidar_Task *wj_task = new Wj_lidar_Task();
  435. code = wj_task->init();
  436. if (code != SUCCESS) {
  437. delete wj_task;
  438. return code;
  439. }
  440. wj_command t_wj_command;
  441. t_wj_command.terminal_id = m_terminor_parameter.id();
  442. t_wj_command.command_time = std::chrono::steady_clock::now();
  443. t_wj_command.timeout_in_milliseconds = 2000;
  444. wj_task->set_command(t_wj_command);
  445. code = mp_wj_lidar->execute_task(wj_task);
  446. if (code != SUCCESS) {
  447. delete wj_task;
  448. return code;
  449. }
  450. ///万集测量正确,对比两数据
  451. wj_measure_result wj_result;
  452. code = wj_task->get_result(wj_result);
  453. //added by yct, wj wheel cloud
  454. pcl::PointCloud<pcl::PointXYZ> t_wj_wheel = wj_task->get_cloud();
  455. for (int m = 0; m < t_wj_wheel.size(); ++m) {
  456. t_wj_wheel[m].x *= 1000.0;
  457. t_wj_wheel[m].y *= 1000.0;
  458. t_wj_wheel[m].z *= 1000.0;
  459. }
  460. t_combined_wheel_cloud->operator+=(t_wj_wheel);
  461. if (code != SUCCESS) {
  462. delete wj_task;
  463. return code;
  464. }
  465. Locate_information wj_locate_information;
  466. wj_locate_information.locate_x = wj_result.x;
  467. wj_locate_information.locate_y = wj_result.y;
  468. wj_locate_information.locate_theta = wj_result.angle;
  469. wj_locate_information.locate_wheel_base = wj_result.wheel_base;
  470. wj_locate_information.locate_width = wj_result.width;
  471. //对比两个数据
  472. float offset_x = fabs(dj_locate_information.locate_x - wj_locate_information.locate_x);
  473. float offset_y = fabs(dj_locate_information.locate_y - wj_locate_information.locate_y);
  474. float offset_angle = fabs(dj_locate_information.locate_theta - wj_locate_information.locate_theta);
  475. float offset_width = fabs(dj_locate_information.locate_width - wj_locate_information.locate_width);
  476. float offset_wheel_base = fabs(
  477. dj_locate_information.locate_wheel_base - wj_locate_information.locate_wheel_base);
  478. if (offset_x > 100 || offset_y > 150 || offset_angle > 2.5 || offset_wheel_base > 350 || offset_width > 250) {
  479. LOG(WARNING) << "chekc failed. (offset x>100, y>150, angle>2.5, wheel_base>130, width>250): " << offset_x
  480. << " " << offset_y << " " << offset_angle << " " << offset_wheel_base << " " << offset_width;
  481. return Error_manager(TERMINOR_CHECK_RESULTS_ERROR, NORMAL, "check results failed ");
  482. }
  483. ///根据sigmod函数计算角度权重
  484. // double angle_weight=1.0-1.0/(1.0+exp(-offset_angle/1.8));
  485. // added by yct, 角度偏差越大,越倾向于万集,之后校验结果
  486. double angle_weight=1.0/(1.0+exp(-offset_angle/0.5));
  487. ///根据两个结果选择最优结果,中心点选择万集雷达,角度选择两值加权,轴距已以2750为基准作卡尔曼滤波
  488. ///车长以大疆雷达为准,车宽以万集雷达为准,高以大疆雷达为准
  489. m_measure_information.locate_x = wj_locate_information.locate_x;
  490. m_measure_information.locate_y = wj_locate_information.locate_y;
  491. m_measure_information.locate_theta =angle_weight * wj_locate_information.locate_theta +
  492. (1.0-angle_weight) * dj_locate_information.locate_theta;
  493. /* float dj_distance = fabs(dj_locate_information.locate_wheel_base - 2750);
  494. float wj_distance = fabs(wj_locate_information.locate_wheel_base - 2750);
  495. float weight_dj = wj_distance / (dj_distance + wj_distance);
  496. float weight_wj = dj_distance / (dj_distance + wj_distance);*/
  497. m_measure_information.locate_wheel_base = 0.01 * dj_locate_information.locate_wheel_base +
  498. 0.99 * wj_locate_information.locate_wheel_base;
  499. // m_measure_information.locate_wheel_base += 400;
  500. //m_measure_information.locate_wheel_base=m_measure_information.locate_wheel_base>3000?3000:m_measure_information.locate_wheel_base;
  501. m_measure_information.locate_length = dj_locate_information.locate_length;
  502. m_measure_information.locate_width = wj_locate_information.locate_width;
  503. m_measure_information.locate_hight = dj_locate_information.locate_hight;
  504. // 20210527 added by yct
  505. // 6号位标定地面偏低,高度增加21mm
  506. // 20210603 2号位加高10mm, 4号位加高5mm
  507. // 20210925 6号位加高改为13
  508. // 20220621 2号位轴距-10, 4号位y+1mm,轴距+20mm, 6号位y+1mm
  509. if(m_terminor_parameter.id()==5)
  510. {
  511. m_measure_information.locate_hight += 13;
  512. m_measure_information.locate_y += 1;
  513. }
  514. else if(m_terminor_parameter.id()==3)
  515. {
  516. m_measure_information.locate_hight += 5;
  517. m_measure_information.locate_y +=1;
  518. m_measure_information.locate_wheel_base += 20;
  519. }
  520. else if(m_terminor_parameter.id()==1)
  521. {
  522. m_measure_information.locate_hight += 10;
  523. m_measure_information.locate_wheel_base -= 10;
  524. }
  525. m_measure_information.locate_correct = true;
  526. // 20210527 added by yct, 融合大疆轮子与万集点云进行结果校验,机械手内空2150,取2000计算
  527. save_cloud_txt(project_path+"/comb.txt", t_combined_wheel_cloud);
  528. Eigen::Vector3d line_param_left, line_param_right;
  529. double sign = 1.0;
  530. if(m_measure_information.locate_theta==90.0)
  531. {
  532. line_param_left << 1.0, 0, -m_measure_information.locate_x + 1000.0;
  533. line_param_right << 1.0, 0, -m_measure_information.locate_x - 1000.0;
  534. }else{
  535. // 减去2度测试外点个数
  536. double k = (m_measure_information.locate_theta) * M_PI / 180.0;
  537. line_param_left << tan(k), -1, m_measure_information.locate_y - (m_measure_information.locate_x - 1000.0/sin(k))*tan(k);
  538. line_param_right << tan(k), -1, m_measure_information.locate_y - (m_measure_information.locate_x + 1000.0/sin(k))*tan(k);
  539. if(tan(k)<0)
  540. {
  541. sign = -sign;
  542. }
  543. }
  544. int outside_point_count = 0;
  545. pcl::PointCloud<pcl::PointXYZ>::Ptr t_outside_cloud(new pcl::PointCloud<pcl::PointXYZ>);
  546. for (int l = 0; l < t_combined_wheel_cloud->size(); ++l) {
  547. Eigen::Vector3d t_point(t_combined_wheel_cloud->points[l].x,t_combined_wheel_cloud->points[l].y,1.0);
  548. double t_left_value = sign * (line_param_left.transpose() * t_point)[0];
  549. double t_right_value = sign * (line_param_right.transpose() * t_point)[0];
  550. // 点在线右侧为正,左侧为负
  551. if(t_left_value <= 0 || t_right_value >= 0)
  552. {
  553. outside_point_count++;
  554. t_outside_cloud->push_back(pcl::PointXYZ(t_combined_wheel_cloud->points[l].x, t_combined_wheel_cloud->points[l].y, t_combined_wheel_cloud->points[l].z));
  555. }
  556. }
  557. save_cloud_txt(project_path+"/outside.txt", t_outside_cloud);
  558. LOG(INFO) << "outside point count: "<<outside_point_count;
  559. // 安全判断,外点超过15个判断本次测量失败
  560. if((outside_point_count > 15 && m_terminor_parameter.id()!=1) || (m_terminor_parameter.id()==1 && outside_point_count > 25))
  561. {
  562. LOG(WARNING) << "机械手下降判断外点过多,存在风险: "<<outside_point_count;
  563. m_measure_information.locate_correct = false;
  564. code = Error_code::ERROR;
  565. }
  566. ////如果测量正确,检验结果
  567. if (mp_verify_tool != NULL && code==SUCCESS) {
  568. cv::RotatedRect rotated_rect;
  569. rotated_rect.center = cv::Point2f(m_measure_information.locate_x, m_measure_information.locate_y);
  570. rotated_rect.angle = m_measure_information.locate_theta;
  571. float ext_length = 720;
  572. float robot_width = 2650.0;
  573. float new_length = m_measure_information.locate_length + ext_length * 2.0;
  574. rotated_rect = create_rotate_rect(new_length, robot_width, m_measure_information.locate_theta,
  575. m_measure_information.locate_x, m_measure_information.locate_y);
  576. code = mp_verify_tool->verify(rotated_rect, m_measure_information.locate_hight, false);
  577. m_measure_information.locate_correct = (code == SUCCESS);
  578. delete wj_task;
  579. return code;
  580. } else {
  581. delete wj_task;
  582. }
  583. return code;
  584. }
  585. cv::RotatedRect Terminor_Command_Executor::create_rotate_rect(float length,float width,float angle,float cx,float cy)
  586. {
  587. const double C_PI=3.14159265;
  588. float theta=C_PI*(angle/180.0);
  589. float a00=cos(theta);
  590. float a01=-sin(theta);
  591. float a10=sin(theta);
  592. float a11=cos(theta);
  593. cv::Point2f point[4];
  594. point[0].x=-length/2.0;
  595. point[0].y=-width/2.0;
  596. point[1].x=-length/2.0;
  597. point[1].y=width/2.0;
  598. point[2].x=length/2.0;
  599. point[2].y=-width/2.0;
  600. point[3].x=length/2.0;
  601. point[3].y=width/2.0;
  602. std::vector<cv::Point2f> point_vec;
  603. for(int i=0;i<4;++i)
  604. {
  605. float x=point[i].x*a00+point[i].y*a01+cx;
  606. float y=point[i].x*a10+point[i].y*a11+cy;
  607. point_vec.push_back(cv::Point2f(x,y));
  608. }
  609. return cv::minAreaRect(point_vec);
  610. }