System_Manager.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. using google::protobuf::io::FileInputStream;
  10. using google::protobuf::io::FileOutputStream;
  11. using google::protobuf::io::ZeroCopyInputStream;
  12. using google::protobuf::io::CodedInputStream;
  13. using google::protobuf::io::ZeroCopyOutputStream;
  14. using google::protobuf::io::CodedOutputStream;
  15. using google::protobuf::Message;
  16. System_Manager::System_Manager()
  17. {
  18. m_laser_vector.clear();
  19. m_terminal_vector.clear();
  20. mp_plc=NULL;
  21. mp_locater=NULL;
  22. }
  23. System_Manager::~System_Manager()
  24. {
  25. //第一步, 清除流程,保证不会再调用plc,然后最先析构plc,防止回调崩溃
  26. for(int i=0;i<m_terminal_vector.size();++i)
  27. {
  28. if(m_terminal_vector[i]!=NULL)
  29. {
  30. m_terminal_vector[i]->force_stop_command();
  31. }
  32. }
  33. //析构plc
  34. if(mp_plc!=NULL)
  35. {
  36. delete mp_plc;
  37. mp_plc=NULL;
  38. }
  39. //第二步析构terminor
  40. for(int i=0;i<m_terminal_vector.size();++i)
  41. {
  42. if(m_terminal_vector[i]!=NULL)
  43. {
  44. delete m_terminal_vector[i];
  45. m_terminal_vector[i]=NULL;
  46. }
  47. }
  48. m_terminal_vector.clear();
  49. //析构locater
  50. if(mp_locater!=NULL)
  51. {
  52. delete mp_locater;
  53. mp_locater=NULL;
  54. }
  55. //析构laser
  56. for(int i=0;i<m_laser_vector.size();++i)
  57. {
  58. if(m_laser_vector[i]!=NULL)
  59. {
  60. delete m_laser_vector[i];
  61. m_laser_vector[i]=NULL;
  62. }
  63. }
  64. m_laser_vector.clear();
  65. }
  66. //初始化各个模块,包括雷达,plc,locater,terminor
  67. //1,读取运行环境下setting目录下的laser.prototxt,plc.prototxt,locater.prototxt,terminal.prototxt
  68. //2,根据配置创建雷达,plc,locater,terminal
  69. Error_manager System_Manager::init()
  70. {
  71. Error_manager code;
  72. //读取plc配置
  73. plc_module::plc_connection_params plc_parameter;
  74. if(!read_proto_param("./setting/plc.prototxt",plc_parameter))
  75. {
  76. return Error_manager(SYSTEM_READ_PARAMETER_ERROR,NORMAL,"read plc parameter failed");
  77. }
  78. //读取locate配置
  79. Measure::LocateParameter locater_parameter;
  80. if(!read_proto_param("./setting/locate.prototxt",locater_parameter))
  81. {
  82. return Error_manager(SYSTEM_READ_PARAMETER_ERROR,NORMAL,"read locate parameter failed");
  83. }
  84. //读取terminor配置
  85. Terminal::Terminor_parameter_all terminor_parameter_all;
  86. if(!read_proto_param("./setting/terminal.prototxt",terminor_parameter_all))
  87. {
  88. return Error_manager(SYSTEM_READ_PARAMETER_ERROR,NORMAL,"read terminal parameter failed");
  89. }
  90. //第二步,根据配置,创建各个模块
  91. //创建雷达
  92. //
  93. //创建测量模块
  94. Locater* p_locater=new Locater();
  95. code=p_locater->init(locater_parameter);
  96. if(code!=SUCCESS)
  97. {
  98. return code;
  99. }
  100. //创建terminor
  101. int terminor_count=terminor_parameter_all.terminor_parameters_size();
  102. if(terminor_count<=0)
  103. {
  104. return Error_manager(SYSTEM_PARAMETER_ERROR,NORMAL,"parameter error terminor num is 0");
  105. }
  106. m_terminal_vector.resize(terminor_count);
  107. for(int i=0;i<terminor_count;++i)
  108. {
  109. m_terminal_vector[i]=new Terminor_Command_Executor(terminor_parameter_all.terminor_parameters(i));
  110. }
  111. //最后创建plc,连接,设置回调
  112. Plc_Communicator* plc=new Plc_Communicator(plc_parameter);
  113. code=plc->get_error();
  114. if(code!=SUCCESS)
  115. {
  116. code.set_error_description("plc create error");
  117. return code;
  118. }
  119. code=plc->set_plc_callback(plc_command_callback,this);
  120. return code;
  121. }
  122. //plc指令回调函数,当plc收到测量指令后,调用此回调函数
  123. //terminal_id:收到指令的终端编号
  124. //owner:this指针
  125. Error_manager System_Manager::plc_command_callback(int terminal_id,void* owner)
  126. {
  127. Error_manager code;
  128. LOG(INFO)<<"RECEIVE terminal command , ID : "<<terminal_id;
  129. //第一步判断输入参数是否合法
  130. if(owner==NULL)
  131. {
  132. return Error_manager(PARAMETER_ERROR,NEGLIGIBLE_ERROR,"plc command callback input parameter(owner) NULL");
  133. }
  134. System_Manager* system_manager=(System_Manager*)owner;
  135. if(terminal_id<0||terminal_id>=system_manager->m_terminal_vector.size())
  136. {
  137. char description[255]={0};
  138. sprintf(description,"terminal command id(%d) out of range, terminal size:%d",
  139. terminal_id,system_manager->m_terminal_vector.size());
  140. return Error_manager(PARAMETER_ERROR,NEGLIGIBLE_ERROR,description);
  141. }
  142. //第二步,根据terminal_id,启动对应terminaor执行指令流程
  143. //根据配置筛选雷达,当前测试使用所有雷达
  144. code=system_manager->m_terminal_vector[terminal_id]->execute_command(system_manager->m_laser_vector,
  145. system_manager->mp_plc,system_manager->mp_locater,10);
  146. return code;
  147. }
  148. //读取protobuf 配置文件
  149. //file:文件
  150. //parameter:要读取的配置
  151. bool System_Manager::read_proto_param(std::string file, ::google::protobuf::Message& parameter)
  152. {
  153. int fd = open(file.c_str(), O_RDONLY);
  154. if (fd == -1) return false;
  155. FileInputStream* input = new FileInputStream(fd);
  156. bool success = google::protobuf::TextFormat::Parse(input, &parameter);
  157. delete input;
  158. close(fd);
  159. return success;
  160. }