main.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //
  2. // Created by zx on 2019/12/28.
  3. //
  4. #include <fcntl.h>
  5. #include <iostream>
  6. #include "tool/pathcreator.h"
  7. #include <glog/logging.h>
  8. #include "verifier.h"
  9. #include <google/protobuf/io/zero_copy_stream_impl.h>
  10. #include <google/protobuf/text_format.h>
  11. #include <livox_sdk.h>
  12. #include "LivoxMid70.h"
  13. #include "snap7_communication_base.h"
  14. #include <thread>
  15. using google::protobuf::io::FileInputStream;
  16. using google::protobuf::io::FileOutputStream;
  17. using google::protobuf::io::ZeroCopyInputStream;
  18. using google::protobuf::io::CodedInputStream;
  19. using google::protobuf::io::ZeroCopyOutputStream;
  20. using google::protobuf::io::CodedOutputStream;
  21. using google::protobuf::Message;
  22. #include "setting.pb.h"
  23. #include "safety_excutor.h"
  24. #include <byteswap.h>
  25. GOOGLE_GLOG_DLL_DECL void shut_down_logging(const char* data, int size)
  26. {
  27. time_t tt;
  28. time( &tt );
  29. tt = tt + 8*3600; // transform the time zone
  30. tm* t= gmtime( &tt );
  31. char buf[255]={0};
  32. sprintf(buf,"./%d%02d%02d-%02d%02d%02d-dump.txt",
  33. t->tm_year + 1900,
  34. t->tm_mon + 1,
  35. t->tm_mday,
  36. t->tm_hour,
  37. t->tm_min,
  38. t->tm_sec);
  39. FILE* tp_file=fopen(buf,"w");
  40. fprintf(tp_file,data,strlen(data));
  41. fclose(tp_file);
  42. }
  43. void init_glog()
  44. {
  45. time_t tt = time(0);//时间cuo
  46. struct tm* t = localtime(&tt);
  47. char strYear[255]={0};
  48. char strMonth[255]={0};
  49. char strDay[255]={0};
  50. sprintf(strYear,"%04d", t->tm_year+1900);
  51. sprintf(strMonth,"%02d", t->tm_mon+1);
  52. sprintf(strDay,"%02d", t->tm_mday);
  53. char buf[255]={0};
  54. getcwd(buf,255);
  55. char strdir[255]={0};
  56. sprintf(strdir,"%s/log/%s/%s/%s", buf,strYear,strMonth,strDay);
  57. PathCreator creator;
  58. creator.Mkdir(strdir);
  59. char logPath[255] = { 0 };
  60. sprintf(logPath, "%s/", strdir);
  61. FLAGS_max_log_size = 100;
  62. FLAGS_logbufsecs = 0;
  63. google::InitGoogleLogging("shutter_verify");
  64. google::SetStderrLogging(google::INFO);
  65. google::SetLogDestination(0, logPath);
  66. google::SetLogFilenameExtension("zxlog");
  67. google::InstallFailureSignalHandler();
  68. google::InstallFailureWriter(&shut_down_logging);
  69. FLAGS_colorlogtostderr = true; // Set log color
  70. FLAGS_logbufsecs = 0; // Set log output speed(s)
  71. FLAGS_max_log_size = 1024; // Set max log file size(GB)
  72. FLAGS_stop_logging_if_full_disk = true;
  73. }
  74. //读取protobuf 配置文件
  75. //file:文件
  76. //parameter:要读取的配置
  77. bool read_proto_param(std::string file, ::google::protobuf::Message& parameter)
  78. {
  79. int fd = open(file.c_str(), O_RDONLY);
  80. if (fd == -1) return false;
  81. FileInputStream* input = new FileInputStream(fd);
  82. bool success = google::protobuf::TextFormat::Parse(input, &parameter);
  83. delete input;
  84. close(fd);
  85. return success;
  86. }
  87. #if VIEW
  88. extern pcl::visualization::PCLVisualizer viewer;
  89. #endif
  90. int Working()
  91. {
  92. while(1)
  93. {
  94. break;
  95. }
  96. return 0;
  97. }
  98. int main(int argc,char* argv[])
  99. {
  100. //初始化日志
  101. init_glog();
  102. #if VIEW
  103. viewer.addCoordinateSystem(2.0,0,0,0,"car_center");
  104. #endif
  105. int ret = livox::LdsLidar::InitLdsLidar();
  106. shutter::setting_parameter setting_parameter;
  107. if(read_proto_param("./setting/setting.prototxt",setting_parameter)==false)
  108. {
  109. printf(" read proto failed \n");
  110. return -1;
  111. }
  112. safety_excutor* excutor_entrance=safety_excutor::CreateExcutor(setting_parameter.entrance_parameter());
  113. safety_excutor* excutor_export=safety_excutor::CreateExcutor(setting_parameter.export_parameter());
  114. Snap7_communication_base snap7_client; //通信的客户端
  115. Error_manager code=snap7_client.communication_init(setting_parameter.plc_setting().ip());
  116. if(code!=SUCCESS)
  117. {
  118. std::cout<<code<<std::endl;
  119. return -1;
  120. }
  121. usleep(3000*1000);
  122. //std::thread* thread=new std::thread(Working);
  123. tagMeasureData measure_data_entrance;
  124. tagMeasureData measure_data_export;
  125. #pragma pack(push,1)
  126. struct PLCData
  127. {
  128. unsigned short pingpong;
  129. unsigned short wheel_path_safety;
  130. unsigned short parkspace_safety;
  131. float left_space;
  132. float right_space;
  133. float theta;
  134. float offset_x;
  135. unsigned short wheel_path_safety_export;
  136. unsigned short parkspace_safety_export;
  137. float left_space_export;
  138. float right_space_export;
  139. float theta_export;
  140. float offset_x_export;
  141. };
  142. #pragma pack(pop)
  143. int PING=0;
  144. Snap7_buf buf;
  145. buf.m_communication_mode = Snap7_buf::Communication_mode::LOOP_COMMUNICATION;
  146. while(1)
  147. {
  148. memset(&measure_data_entrance,0,sizeof(measure_data_entrance));
  149. memset(&measure_data_export,0,sizeof(measure_data_export));
  150. if(excutor_entrance!= nullptr)
  151. {
  152. printf(" entrance <<<<<< ");
  153. excutor_entrance->verify(measure_data_entrance);
  154. }
  155. if(excutor_export!= nullptr)
  156. {
  157. printf(" export >>>>>> ");
  158. excutor_export->verify(measure_data_export);
  159. }
  160. struct PLCData plc_data;
  161. plc_data.pingpong=bswap_16((PING++)%100000);
  162. plc_data.offset_x=bswap_32(measure_data_entrance.offset_x);
  163. plc_data.left_space=bswap_32(measure_data_entrance.left_space);
  164. plc_data.right_space=bswap_32(measure_data_entrance.right_space);
  165. plc_data.theta=bswap_32(measure_data_entrance.theta);
  166. plc_data.wheel_path_safety=bswap_16(measure_data_entrance.wheel_path_safety);
  167. plc_data.parkspace_safety=bswap_16(measure_data_entrance.parkspcae_safety);
  168. plc_data.offset_x_export=bswap_32(measure_data_export.offset_x);
  169. plc_data.left_space_export=bswap_32(measure_data_export.left_space);
  170. plc_data.right_space_export=bswap_32(measure_data_export.right_space);
  171. plc_data.theta_export=bswap_32(measure_data_export.theta);
  172. plc_data.wheel_path_safety_export=bswap_16(measure_data_export.wheel_path_safety);
  173. plc_data.parkspace_safety_export=bswap_16(measure_data_export.parkspcae_safety);
  174. buf.m_id=setting_parameter.plc_setting().dbnumber();
  175. buf.m_size=sizeof(plc_data);
  176. buf.m_start_index=setting_parameter.plc_setting().start_id();
  177. buf.mp_buf_reverse=&plc_data;
  178. code=snap7_client.write_data_buf(buf);
  179. if(code!=SUCCESS)
  180. {
  181. snap7_client.communication_uninit();
  182. usleep(500*1000);
  183. snap7_client.communication_init(setting_parameter.plc_setting().ip());
  184. usleep(500*1000);
  185. std::cout<<code<<std::endl;
  186. }
  187. usleep(50*1000);
  188. }
  189. return 0;
  190. }