main.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. float SWAP_float(float x)
  99. {
  100. int b;
  101. memcpy(&b,&x,sizeof(float));
  102. int t= ((((b) & 0xff000000) >> 24) | (((b) & 0x00ff0000) >> 8) |(((b) & 0x0000ff00) << 8) | (((b) & 0x000000ff) << 24));
  103. float value=0;
  104. memcpy(&value,&t,sizeof(float));
  105. return value;
  106. }
  107. float swap(float value)
  108. {
  109. char* p=(char*)&value;
  110. float b=0;
  111. char* pb=(char*)&b;
  112. for(int i=0;i<sizeof(float);++i)
  113. {
  114. memcpy(pb+i,p+sizeof(float)-1-i,1);
  115. }
  116. return b;
  117. }
  118. int main(int argc,char* argv[])
  119. {
  120. //初始化日志
  121. init_glog();
  122. #if VIEW
  123. viewer.addCoordinateSystem(2.0,0,0,0,"car_center");
  124. #endif
  125. int ret = livox::LdsLidar::InitLdsLidar();
  126. shutter::setting_parameter setting_parameter;
  127. if(read_proto_param("./setting/setting.prototxt",setting_parameter)==false)
  128. {
  129. printf(" read proto failed \n");
  130. return -1;
  131. }
  132. safety_excutor* excutor_entrance=safety_excutor::CreateExcutor(setting_parameter.entrance_parameter());
  133. safety_excutor* excutor_export=safety_excutor::CreateExcutor(setting_parameter.export_parameter());
  134. Snap7_communication_base snap7_client; //通信的客户端
  135. Error_manager code=snap7_client.communication_init(setting_parameter.plc_setting().ip());
  136. if(code!=SUCCESS)
  137. {
  138. std::cout<<code<<std::endl;
  139. return -1;
  140. }
  141. usleep(3000*1000);
  142. //std::thread* thread=new std::thread(Working);
  143. tagMeasureData measure_data_entrance;
  144. tagMeasureData measure_data_export;
  145. #pragma pack(push,1)
  146. struct PLCData
  147. {
  148. unsigned short pingpong;
  149. unsigned short wheel_path_safety;
  150. unsigned short parkspace_safety;
  151. float left_space;
  152. float right_space;
  153. float theta;
  154. float offset_x;
  155. unsigned short wheel_path_safety_export;
  156. unsigned short parkspace_safety_export;
  157. float left_space_export;
  158. float right_space_export;
  159. float theta_export;
  160. float offset_x_export;
  161. };
  162. #pragma pack(pop)
  163. int PING=0;
  164. Snap7_buf buf;
  165. buf.m_communication_mode = Snap7_buf::Communication_mode::LOOP_COMMUNICATION;
  166. while(1)
  167. {
  168. memset(&measure_data_entrance,0,sizeof(measure_data_entrance));
  169. memset(&measure_data_export,0,sizeof(measure_data_export));
  170. if(excutor_entrance!= nullptr)
  171. {
  172. printf(" entrance <<<<<< ");
  173. excutor_entrance->verify(measure_data_entrance);
  174. }
  175. if(excutor_export!= nullptr)
  176. {
  177. printf(" export >>>>>> ");
  178. excutor_export->verify(measure_data_export);
  179. }
  180. struct PLCData plc_data;
  181. plc_data.pingpong=bswap_16((PING++)%100000);
  182. plc_data.offset_x=SWAP_float(measure_data_entrance.offset_x);
  183. plc_data.left_space=SWAP_float(measure_data_entrance.left_space);
  184. plc_data.right_space=SWAP_float(measure_data_entrance.right_space);
  185. plc_data.theta=SWAP_float(measure_data_entrance.theta);
  186. plc_data.wheel_path_safety=bswap_16(measure_data_entrance.wheel_path_safety);
  187. plc_data.parkspace_safety=bswap_16(measure_data_entrance.parkspcae_safety);
  188. plc_data.offset_x_export=SWAP_float(measure_data_export.offset_x);
  189. plc_data.left_space_export=SWAP_float(measure_data_export.left_space);
  190. plc_data.right_space_export=SWAP_float(measure_data_export.right_space);
  191. plc_data.theta_export=SWAP_float(measure_data_export.theta);
  192. plc_data.wheel_path_safety_export=bswap_16(measure_data_export.wheel_path_safety);
  193. plc_data.parkspace_safety_export=bswap_16(measure_data_export.parkspcae_safety);
  194. printf("\n ------sizeof float:%d-------------------l_space:%.3f r_space:%.3f wheel_safety: %d parkspace_safety: %d\n",
  195. sizeof(float),
  196. measure_data_entrance.left_space,measure_data_entrance.right_space,
  197. measure_data_entrance.wheel_path_safety,measure_data_entrance.parkspcae_safety);
  198. buf.m_id=setting_parameter.plc_setting().dbnumber();
  199. buf.m_size=sizeof(plc_data);
  200. buf.m_start_index=setting_parameter.plc_setting().start_id();
  201. buf.mp_buf_reverse=&plc_data;
  202. code=snap7_client.write_data_buf(buf);
  203. if(code!=SUCCESS)
  204. {
  205. snap7_client.communication_uninit();
  206. usleep(500*1000);
  207. snap7_client.communication_init(setting_parameter.plc_setting().ip());
  208. usleep(500*1000);
  209. std::cout<<code<<std::endl;
  210. }
  211. usleep(50*1000);
  212. }
  213. return 0;
  214. }