System_Manager.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. //
  2. // Created by zx on 2020/1/3.
  3. //
  4. #include "System_Manager.h"
  5. #include <glog/logging.h>
  6. #include <google/protobuf/io/zero_copy_stream_impl.h>
  7. #include <google/protobuf/text_format.h>
  8. #include <fcntl.h>
  9. #include <livox_sdk.h>
  10. using google::protobuf::io::FileInputStream;
  11. using google::protobuf::io::FileOutputStream;
  12. using google::protobuf::io::ZeroCopyInputStream;
  13. using google::protobuf::io::CodedInputStream;
  14. using google::protobuf::io::ZeroCopyOutputStream;
  15. using google::protobuf::io::CodedOutputStream;
  16. using google::protobuf::Message;
  17. System_Manager::System_Manager()
  18. {
  19. m_laser_vector.clear();
  20. m_terminal_vector.clear();
  21. mp_plc=NULL;
  22. mp_locater=NULL;
  23. mp_verify_result_tool=NULL;
  24. mp_wj_controller=NULL;
  25. }
  26. System_Manager::~System_Manager()
  27. {
  28. //第一步, 清除流程,保证不会再调用plc,然后最先析构plc,防止回调崩溃
  29. for(int i=0;i<m_terminal_vector.size();++i)
  30. {
  31. if(m_terminal_vector[i]!=NULL)
  32. {
  33. m_terminal_vector[i]->force_stop_command();
  34. }
  35. }
  36. //析构plc
  37. if(mp_plc!=NULL)
  38. {
  39. delete mp_plc;
  40. mp_plc=NULL;
  41. }
  42. //第二步析构terminor
  43. for(int i=0;i<m_terminal_vector.size();++i)
  44. {
  45. if(m_terminal_vector[i]!=NULL)
  46. {
  47. delete m_terminal_vector[i];
  48. m_terminal_vector[i]=NULL;
  49. }
  50. }
  51. m_terminal_vector.clear();
  52. //析构locater
  53. if(mp_locater!=NULL)
  54. {
  55. delete mp_locater;
  56. mp_locater=NULL;
  57. }
  58. //析构laser
  59. for(int i=0;i<m_laser_vector.size();++i)
  60. {
  61. if(m_laser_vector[i]!=NULL)
  62. {
  63. delete m_laser_vector[i];
  64. m_laser_vector[i]=NULL;
  65. }
  66. }
  67. m_laser_vector.clear();
  68. if(mp_verify_result_tool!=NULL)
  69. {
  70. delete mp_verify_result_tool;
  71. mp_verify_result_tool=NULL;
  72. }
  73. if(mp_wj_controller!=NULL)
  74. {
  75. delete mp_wj_controller;
  76. mp_wj_controller=NULL;
  77. }
  78. }
  79. //初始化各个模块,包括雷达,plc,locater,terminor
  80. //1,读取运行环境下setting目录下的laser.prototxt,plc.prototxt,locater.prototxt,terminal.prototxt
  81. //2,根据配置创建雷达,plc,locater,terminal
  82. Error_manager System_Manager::init()
  83. {
  84. MeasureTopicPublisher::GetInstance();
  85. Error_manager code;
  86. //读laser配置
  87. Laser_proto::Laser_parameter_all laser_parameters;
  88. if(!read_proto_param("./setting/laser.prototxt",laser_parameters))
  89. {
  90. return Error_manager(SYSTEM_READ_PARAMETER_ERROR,NORMAL,"read laser parameter failed");
  91. }
  92. //读取万集雷达配置
  93. wj::wjManagerParams wj_parameter;
  94. if(!read_proto_param("./setting/wj_manager_settings.prototxt",wj_parameter))
  95. {
  96. return Error_manager(SYSTEM_READ_PARAMETER_ERROR,NORMAL,"read wl_lidar parameter failed");
  97. }
  98. //读取plc配置
  99. plc_module::plc_connection_params plc_parameter;
  100. if(!read_proto_param("./setting/plc.prototxt",plc_parameter))
  101. {
  102. return Error_manager(SYSTEM_READ_PARAMETER_ERROR,NORMAL,"read plc parameter failed");
  103. }
  104. //读取locate配置
  105. Measure::LocateParameter locater_parameter;
  106. if(!read_proto_param("./setting/locate.prototxt",locater_parameter))
  107. {
  108. return Error_manager(SYSTEM_READ_PARAMETER_ERROR,NORMAL,"read locate parameter failed");
  109. }
  110. //读取结果检验器配置
  111. Hardware_limit::Hardware_parameter hardware_parameter;
  112. if(!read_proto_param("./setting/limit.prototxt",hardware_parameter))
  113. {
  114. return Error_manager(SYSTEM_READ_PARAMETER_ERROR,NORMAL,"read limit parameter failed");
  115. }
  116. //读取terminor配置
  117. Terminal::Terminor_parameter_all terminor_parameter_all;
  118. if(!read_proto_param("./setting/terminal.prototxt",terminor_parameter_all))
  119. {
  120. return Error_manager(SYSTEM_READ_PARAMETER_ERROR,NORMAL,"read terminal parameter failed");
  121. }
  122. m_terminal_parameter_all=terminor_parameter_all;
  123. //第二步,根据配置,创建各个模块
  124. //创建大疆雷达
  125. int laser_cout=laser_parameters.laser_parameters_size();
  126. m_laser_vector.resize(laser_cout);
  127. for(int i=0;i<laser_parameters.laser_parameters_size();++i)
  128. {
  129. m_laser_vector[i]=LaserRegistory::CreateLaser(laser_parameters.laser_parameters(i).type(),i,
  130. laser_parameters.laser_parameters(i));
  131. if(m_laser_vector[i]!=NULL)
  132. {
  133. if(m_laser_vector[i]->connect_laser()!=SUCCESS)
  134. {
  135. char description[255]={0};
  136. sprintf(description,"Laser %d connect failed...",i);
  137. return Error_manager(LASER_CONNECT_FAILED,NORMAL,description);
  138. }
  139. }
  140. }
  141. //查询是否有livox雷达,初始化库
  142. for (int i = 0; i < laser_parameters.laser_parameters_size(); ++i)
  143. {
  144. std::string type = laser_parameters.laser_parameters(i).type();
  145. if (type.find("Livox") != type.npos)
  146. {
  147. if (Start() == false)
  148. {
  149. Uninit();
  150. return Error_manager(LASER_LIVOX_SKD_INIT_FAILED,NORMAL,"Livox laser init failed...");
  151. }
  152. break;
  153. }
  154. }
  155. //创建limit模块
  156. mp_verify_result_tool=new Verify_result(hardware_parameter);
  157. LOG(WARNING)<<"0 verify addr : "<<mp_verify_result_tool;
  158. //创建万集雷达
  159. /*mp_wj_controller=new Fence_controller(mp_verify_result_tool);
  160. code=mp_wj_controller->initialize(wj_parameter);
  161. if(code!=SUCCESS)
  162. {
  163. return code;
  164. }*/
  165. //创建测量模块
  166. mp_locater=new Locater();
  167. code=mp_locater->init(locater_parameter);
  168. if(code!=SUCCESS)
  169. {
  170. return code;
  171. }
  172. //创建terminor
  173. int terminor_count=terminor_parameter_all.terminor_parameters_size();
  174. if(terminor_count<=0)
  175. {
  176. return Error_manager(SYSTEM_PARAMETER_ERROR,NORMAL,"parameter error terminor num is 0");
  177. }
  178. m_terminal_vector.resize(terminor_count);
  179. for(int i=0;i<terminor_count;++i)
  180. {
  181. m_terminal_vector[i]=new Terminor_Command_Executor(terminor_parameter_all.terminor_parameters(i));
  182. m_terminal_vector[i]->set_save_root_path(terminor_parameter_all.save_root_path());
  183. }
  184. //最后创建plc,连接,设置回调
  185. /*mp_plc=new Plc_Communicator(plc_parameter);
  186. code=mp_plc->get_error();
  187. if(code!=SUCCESS)
  188. {
  189. code.set_error_description("plc create error");
  190. return code;
  191. }
  192. code=mp_plc->set_plc_callback(plc_command_callback,this);*/
  193. plc_command_callback(1,this);
  194. return code;
  195. }
  196. //plc指令回调函数,当plc收到测量指令后,调用此回调函数
  197. //terminal_id:收到指令的终端编号
  198. //owner:this指针
  199. Error_manager System_Manager::plc_command_callback(int terminal_id_plc,void* owner)
  200. {
  201. int terminal_id=terminal_id_plc-1;
  202. Error_manager code;
  203. LOG(INFO)<<"RECEIVE terminal command , ID : "<<terminal_id;
  204. //第一步判断输入参数是否合法
  205. if(owner==NULL)
  206. {
  207. return Error_manager(PARAMETER_ERROR,NEGLIGIBLE_ERROR,"plc command callback input parameter(owner) NULL");
  208. }
  209. System_Manager* system_manager=(System_Manager*)owner;
  210. if(terminal_id<0||terminal_id>=system_manager->m_terminal_vector.size())
  211. {
  212. char description[255]={0};
  213. sprintf(description,"terminal command id(%d) out of range, terminal size:%d",
  214. terminal_id,system_manager->m_terminal_vector.size());
  215. return Error_manager(PARAMETER_ERROR,NEGLIGIBLE_ERROR,description);
  216. }
  217. //第二步,根据terminal_id,启动对应terminaor执行指令流程,
  218. if(system_manager->m_laser_vector.size()==0)
  219. {
  220. return Error_manager(SYSTEM_INPUT_TERMINOR_NO_LASERS,NORMAL,"laser parameter has no laser");
  221. }
  222. if(system_manager->mp_wj_controller==NULL)
  223. {
  224. Error_manager(PARAMETER_ERROR,NEGLIGIBLE_ERROR,"wj lidar is null");
  225. }
  226. code=system_manager->m_terminal_vector[terminal_id]->execute_command(system_manager->m_laser_vector,
  227. system_manager->mp_wj_controller,
  228. system_manager->mp_plc,system_manager->mp_locater,system_manager->mp_verify_result_tool,10);
  229. switch(code.get_error_code())
  230. {
  231. case SUCCESS:break;
  232. case TERMINOR_CREATE_WORKING_THREAD_FAILED:
  233. {
  234. LOG(ERROR)<<code.to_string();
  235. break;
  236. }
  237. default:
  238. {
  239. LOG(WARNING)<<code.to_string();
  240. break;
  241. }
  242. }
  243. return code;
  244. }
  245. //读取protobuf 配置文件
  246. //file:文件
  247. //parameter:要读取的配置
  248. bool System_Manager::read_proto_param(std::string file, ::google::protobuf::Message& parameter)
  249. {
  250. int fd = open(file.c_str(), O_RDONLY);
  251. if (fd == -1) return false;
  252. FileInputStream* input = new FileInputStream(fd);
  253. bool success = google::protobuf::TextFormat::Parse(input, &parameter);
  254. delete input;
  255. close(fd);
  256. return success;
  257. }