System_Manager.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. }
  24. System_Manager::~System_Manager()
  25. {
  26. //第一步, 清除流程,保证不会再调用plc,然后最先析构plc,防止回调崩溃
  27. for(int i=0;i<m_terminal_vector.size();++i)
  28. {
  29. if(m_terminal_vector[i]!=NULL)
  30. {
  31. m_terminal_vector[i]->force_stop_command();
  32. }
  33. }
  34. //析构plc
  35. if(mp_plc!=NULL)
  36. {
  37. delete mp_plc;
  38. mp_plc=NULL;
  39. }
  40. //第二步析构terminor
  41. for(int i=0;i<m_terminal_vector.size();++i)
  42. {
  43. if(m_terminal_vector[i]!=NULL)
  44. {
  45. delete m_terminal_vector[i];
  46. m_terminal_vector[i]=NULL;
  47. }
  48. }
  49. m_terminal_vector.clear();
  50. //析构locater
  51. if(mp_locater!=NULL)
  52. {
  53. delete mp_locater;
  54. mp_locater=NULL;
  55. }
  56. //析构laser
  57. for(int i=0;i<m_laser_vector.size();++i)
  58. {
  59. if(m_laser_vector[i]!=NULL)
  60. {
  61. delete m_laser_vector[i];
  62. m_laser_vector[i]=NULL;
  63. }
  64. }
  65. m_laser_vector.clear();
  66. }
  67. //初始化各个模块,包括雷达,plc,locater,terminor
  68. //1,读取运行环境下setting目录下的laser.prototxt,plc.prototxt,locater.prototxt,terminal.prototxt
  69. //2,根据配置创建雷达,plc,locater,terminal
  70. Error_manager System_Manager::init()
  71. {
  72. MeasureTopicPublisher::GetInstance();
  73. Error_manager code;
  74. //读laser配置
  75. Laser_proto::Laser_parameter_all laser_parameters;
  76. if(!read_proto_param("./setting/laser.prototxt",laser_parameters))
  77. {
  78. return Error_manager(SYSTEM_READ_PARAMETER_ERROR,NORMAL,"read laser parameter failed");
  79. }
  80. //读取plc配置
  81. plc_module::plc_connection_params plc_parameter;
  82. if(!read_proto_param("./setting/plc.prototxt",plc_parameter))
  83. {
  84. return Error_manager(SYSTEM_READ_PARAMETER_ERROR,NORMAL,"read plc parameter failed");
  85. }
  86. //读取locate配置
  87. Measure::LocateParameter locater_parameter;
  88. if(!read_proto_param("./setting/locate.prototxt",locater_parameter))
  89. {
  90. return Error_manager(SYSTEM_READ_PARAMETER_ERROR,NORMAL,"read locate parameter failed");
  91. }
  92. //读取terminor配置
  93. Terminal::Terminor_parameter_all terminor_parameter_all;
  94. if(!read_proto_param("./setting/terminal.prototxt",terminor_parameter_all))
  95. {
  96. return Error_manager(SYSTEM_READ_PARAMETER_ERROR,NORMAL,"read terminal parameter failed");
  97. }
  98. m_terminal_parameter_all=terminor_parameter_all;
  99. //第二步,根据配置,创建各个模块
  100. //创建雷达
  101. int laser_cout=laser_parameters.laser_parameters_size();
  102. m_laser_vector.resize(laser_cout);
  103. for(int i=0;i<laser_parameters.laser_parameters_size();++i)
  104. {
  105. m_laser_vector[i]=LaserRegistory::CreateLaser(laser_parameters.laser_parameters(i).type(),i,
  106. laser_parameters.laser_parameters(i));
  107. if(m_laser_vector[i]!=NULL)
  108. {
  109. if(!m_laser_vector[i]->Connect())
  110. {
  111. char description[255]={0};
  112. sprintf(description,"Laser %d connect failed...",i);
  113. return Error_manager(LASER_CONNECT_FAILED,NORMAL,description);
  114. }
  115. }
  116. }
  117. //查询是否有livox雷达,初始化库
  118. for (int i = 0; i < laser_parameters.laser_parameters_size(); ++i)
  119. {
  120. std::string type = laser_parameters.laser_parameters(i).type();
  121. if (type.find("Livox") != type.npos)
  122. {
  123. if (Start() == false)
  124. {
  125. Uninit();
  126. return Error_manager(LASER_LIVOX_SKD_INIT_FAILED,NORMAL,"Livox laser init failed...");
  127. }
  128. break;
  129. }
  130. }
  131. //创建测量模块
  132. mp_locater=new Locater();
  133. code=mp_locater->init(locater_parameter);
  134. if(code!=SUCCESS)
  135. {
  136. return code;
  137. }
  138. //创建terminor
  139. int terminor_count=terminor_parameter_all.terminor_parameters_size();
  140. if(terminor_count<=0)
  141. {
  142. return Error_manager(SYSTEM_PARAMETER_ERROR,NORMAL,"parameter error terminor num is 0");
  143. }
  144. m_terminal_vector.resize(terminor_count);
  145. for(int i=0;i<terminor_count;++i)
  146. {
  147. m_terminal_vector[i]=new Terminor_Command_Executor(terminor_parameter_all.terminor_parameters(i));
  148. m_terminal_vector[i]->set_save_root_path(terminor_parameter_all.save_root_path());
  149. }
  150. //最后创建plc,连接,设置回调
  151. mp_plc=new Plc_Communicator(plc_parameter);
  152. code=mp_plc->get_error();
  153. if(code!=SUCCESS)
  154. {
  155. code.set_error_description("plc create error");
  156. return code;
  157. }
  158. code=mp_plc->set_plc_callback(plc_command_callback,this);
  159. return code;
  160. }
  161. //plc指令回调函数,当plc收到测量指令后,调用此回调函数
  162. //terminal_id:收到指令的终端编号
  163. //owner:this指针
  164. Error_manager System_Manager::plc_command_callback(int terminal_id_plc,void* owner)
  165. {
  166. int terminal_id=terminal_id_plc-1;
  167. Error_manager code;
  168. LOG(INFO)<<"RECEIVE terminal command , ID : "<<terminal_id;
  169. //第一步判断输入参数是否合法
  170. if(owner==NULL)
  171. {
  172. return Error_manager(PARAMETER_ERROR,NEGLIGIBLE_ERROR,"plc command callback input parameter(owner) NULL");
  173. }
  174. System_Manager* system_manager=(System_Manager*)owner;
  175. if(terminal_id<0||terminal_id>=system_manager->m_terminal_vector.size())
  176. {
  177. char description[255]={0};
  178. sprintf(description,"terminal command id(%d) out of range, terminal size:%d",
  179. terminal_id,system_manager->m_terminal_vector.size());
  180. return Error_manager(PARAMETER_ERROR,NEGLIGIBLE_ERROR,description);
  181. }
  182. //第二步,根据terminal_id,启动对应terminaor执行指令流程
  183. //根据配置筛选雷达,当前测试使用所有雷达
  184. std::vector<Laser_base*> tp_lasers;
  185. for(int i=0;i<system_manager->m_terminal_parameter_all.terminor_parameters(terminal_id).laser_parameter_size();++i)
  186. {
  187. int laser_id=system_manager->m_terminal_parameter_all.terminor_parameters(terminal_id).laser_parameter(i).id();
  188. if(laser_id>=0&&laser_id<system_manager->m_laser_vector.size())
  189. {
  190. //判断该id的雷达是否已经添加进去, 放置配置中出现重复的雷达编号
  191. bool tb_repeat=false;
  192. for(int j=0;j<tp_lasers.size();++j)
  193. {
  194. if(tp_lasers[j]==system_manager->m_laser_vector[laser_id])
  195. {
  196. tb_repeat=true;
  197. break;
  198. }
  199. }
  200. if(tb_repeat==false)
  201. tp_lasers.push_back(system_manager->m_laser_vector[laser_id]);
  202. }
  203. }
  204. if(tp_lasers.size()==0)
  205. {
  206. return Error_manager(SYSTEM_INPUT_TERMINOR_NO_LASERS,NORMAL,"ternimal command has no laser");
  207. }
  208. code=system_manager->m_terminal_vector[terminal_id]->execute_command(tp_lasers,
  209. system_manager->mp_plc,system_manager->mp_locater,10);
  210. switch(code.get_error_code())
  211. {
  212. case SUCCESS:break;
  213. case TERMINOR_CREATE_WORKING_THREAD_FAILED:
  214. {
  215. LOG(ERROR)<<code.to_string();
  216. break;
  217. }
  218. default:
  219. {
  220. LOG(WARNING)<<code.to_string();
  221. break;
  222. }
  223. }
  224. return code;
  225. }
  226. //读取protobuf 配置文件
  227. //file:文件
  228. //parameter:要读取的配置
  229. bool System_Manager::read_proto_param(std::string file, ::google::protobuf::Message& parameter)
  230. {
  231. int fd = open(file.c_str(), O_RDONLY);
  232. if (fd == -1) return false;
  233. FileInputStream* input = new FileInputStream(fd);
  234. bool success = google::protobuf::TextFormat::Parse(input, &parameter);
  235. delete input;
  236. close(fd);
  237. return success;
  238. }