monitor.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. //
  2. // Created by zx on 22-8-30.
  3. //
  4. #include <pcl/point_types.h>
  5. #include <pcl/point_cloud.h>
  6. #include <pcl/io/pcd_io.h>
  7. #include <pcl/segmentation/sac_segmentation.h>
  8. #include <pcl/ModelCoefficients.h>
  9. #include <pcl/filters/extract_indices.h>
  10. #include <pcl/segmentation/extract_clusters.h>
  11. #include <fstream>
  12. #include "lidar/wanji_716N_device.h"
  13. #include <byteswap.h>
  14. #include "tool/pathcreator.h"
  15. #include <glog/logging.h>
  16. #include <google/protobuf/io/zero_copy_stream_impl.h>
  17. #include <google/protobuf/text_format.h>
  18. #include "clamp_parameter.pb.h"
  19. #include "tool/proto_tool.h"
  20. #include<stdio.h>
  21. #include<stdlib.h>
  22. #include<unistd.h>
  23. #include<string.h>
  24. #include<sys/types.h>
  25. #include<arpa/inet.h>
  26. #include<net/if.h>
  27. #include<string.h>
  28. #include<signal.h>
  29. #include<sys/wait.h>
  30. #include<sys/ioctl.h>
  31. #include<regex.h>
  32. #include "test.hpp"
  33. #include <fstream>
  34. using google::protobuf::io::FileInputStream;
  35. using google::protobuf::io::FileOutputStream;
  36. using google::protobuf::io::ZeroCopyInputStream;
  37. using google::protobuf::io::CodedInputStream;
  38. using google::protobuf::io::ZeroCopyOutputStream;
  39. using google::protobuf::io::CodedOutputStream;
  40. using google::protobuf::Message;
  41. GOOGLE_GLOG_DLL_DECL void shut_down_logging(const char* data, int size)
  42. {
  43. time_t tt;
  44. time( &tt );
  45. tt = tt + 8*3600; // transform the time zone
  46. tm* t= gmtime( &tt );
  47. char buf[255]={0};
  48. sprintf(buf,"./%d%02d%02d-%02d%02d%02d-dump.txt",
  49. t->tm_year + 1900,
  50. t->tm_mon + 1,
  51. t->tm_mday,
  52. t->tm_hour,
  53. t->tm_min,
  54. t->tm_sec);
  55. FILE* tp_file=fopen(buf,"w");
  56. fprintf(tp_file,data,strlen(data));
  57. fclose(tp_file);
  58. }
  59. void init_glog()
  60. {
  61. time_t tt = time(0);//时间cuo
  62. struct tm* t = localtime(&tt);
  63. char strYear[255]={0};
  64. char strMonth[255]={0};
  65. char strDay[255]={0};
  66. sprintf(strYear,"%04d", t->tm_year+1900);
  67. sprintf(strMonth,"%02d", t->tm_mon+1);
  68. sprintf(strDay,"%02d", t->tm_mday);
  69. char buf[255]={0};
  70. getcwd(buf,255);
  71. char strdir[255]={0};
  72. sprintf(strdir,"%s/log/%s/%s/%s", buf,strYear,strMonth,strDay);
  73. PathCreator creator;
  74. creator.Mkdir(strdir);
  75. char logPath[255] = { 0 };
  76. sprintf(logPath, "%s/", strdir);
  77. FLAGS_max_log_size = 100;
  78. FLAGS_logbufsecs = 0;
  79. google::InitGoogleLogging("clamp_safety");
  80. google::SetStderrLogging(google::INFO);
  81. google::SetLogDestination(0, logPath);
  82. google::SetLogFilenameExtension("zxlog");
  83. google::InstallFailureSignalHandler();
  84. google::InstallFailureWriter(&shut_down_logging);
  85. FLAGS_colorlogtostderr = true; // Set log color
  86. FLAGS_logbufsecs = 0; // Set log output speed(s)
  87. FLAGS_max_log_size = 1024; // Set max log file size(GB)
  88. FLAGS_stop_logging_if_full_disk = true;
  89. }
  90. bool string2point(std::string str,pcl::PointXYZ& point)
  91. {
  92. std::istringstream iss;
  93. iss.str(str);
  94. float value;
  95. float data[2]={0};
  96. for(int i=0;i<2;++i)
  97. {
  98. if(iss>>value)
  99. {
  100. data[i]=value;
  101. }
  102. else
  103. {
  104. return false;
  105. }
  106. }
  107. point.x=data[0];
  108. point.y=data[1];
  109. return true;
  110. }
  111. pcl::PointCloud<pcl::PointXYZ>::Ptr ReadTxtCloud(std::string file)
  112. {
  113. std::ifstream fin(file.c_str());
  114. const int line_length=64;
  115. char str[line_length]={0};
  116. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
  117. while(fin.getline(str,line_length))
  118. {
  119. pcl::PointXYZ point;
  120. if(string2point(str,point))
  121. {
  122. printf("%f, %f, %f.\n", point.x, point.y, point.z);
  123. cloud->push_back(point);
  124. }
  125. }
  126. return cloud;
  127. }
  128. // bool run(Wanji_716N_lidar_device* device,clamp_safety_detect* pdetector,Safety_status* safety)
  129. // {
  130. // pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
  131. // device->get_last_cloud(cloud, std::chrono::system_clock::now());
  132. // if (cloud->size() < 100)
  133. // {
  134. // return false;
  135. // }
  136. // pdetector->detect(cloud,*safety);
  137. // return true;
  138. // }
  139. // void thread_func(Clamp* clamp)
  140. // {
  141. // while(1)
  142. // {
  143. // clamp->mutex.lock();
  144. // Safety_status safety_t;
  145. // if(run(&(clamp->lidar),&(clamp->detector),&safety_t))
  146. // clamp->safety_statu=safety_t;
  147. // clamp->mutex.unlock();
  148. // usleep(100*1000);
  149. // }
  150. // }
  151. #if 1
  152. int main(int argc, char* argv[]) {
  153. init_glog();
  154. clamp_safety::clamp_parameter parameter;
  155. std::string cfg_file = "../setting/parameter.prototxt";
  156. if(proto_tool::read_proto_param(cfg_file,parameter)==false) {
  157. LOG(ERROR) << " read prototxt failed";
  158. return -1;
  159. }
  160. // int CLAMP_SIZE = 1;
  161. int CLAMP_SIZE = parameter.lidars_size();
  162. Clamp clamps[CLAMP_SIZE];
  163. for(int i=0;i < CLAMP_SIZE;++i) {
  164. clamp_safety::LidarParameter lidar = parameter.lidars(i);
  165. Point2D_tool::Polar_coordinates_box t_polar_coordinates_box;
  166. Point2D_tool::Point2D_box t_point2D_box;
  167. Point2D_tool::Point2D_transform t_point2D_transform;
  168. t_polar_coordinates_box.angle_min = lidar.angle_min();
  169. t_polar_coordinates_box.angle_max = lidar.angle_max();
  170. t_polar_coordinates_box.distance_min = lidar.range_min();
  171. t_polar_coordinates_box.distance_max = lidar.range_max();
  172. t_point2D_box.x_min = lidar.scan_box_limit().minx();
  173. t_point2D_box.x_max = lidar.scan_box_limit().maxx();
  174. t_point2D_box.y_min = lidar.scan_box_limit().miny();
  175. t_point2D_box.y_max = lidar.scan_box_limit().maxy();
  176. Error_manager code = clamps[i].lidar.init(lidar.net_config().ip_address(),lidar.net_config().port(),
  177. t_polar_coordinates_box,t_point2D_box,t_point2D_transform);
  178. if(code != SUCCESS) {
  179. LOG(ERROR) << code.get_error_description();
  180. return -3;
  181. }
  182. clamps[i].safety_statu.set_timeout(0.3);
  183. }
  184. int Y = 0;
  185. Test test(&clamps[Y]);
  186. test.initPlc(parameter);
  187. test.thread_fun();
  188. return 0;
  189. }
  190. #endif
  191. #if 0
  192. bool GetAlg(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud, float &x) {
  193. float sumx = 0,sumy = 0, w = 0, sumw = 0;
  194. for(int i=0; i < cloud->size(); i++) {
  195. w = float(1/pow(cos(atan(fabs(cloud->points[i].x/ cloud->points[i].y))), 2));
  196. // w = float(sqrt((36.0 - w))/6.0);
  197. sumw += w;
  198. sumx += w * cloud->points[i].x;
  199. }
  200. x = sumx/sumw;
  201. // printf("---Debug alg result %.3f.\n", x*1000);
  202. // return (fabs(x*1000) > 30 && fabs(x*1000) < 150);
  203. return (fabs(x*1000) < 150);
  204. }
  205. int main (int argc, char** argv) {
  206. std::fstream txtFile, pointFile;
  207. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
  208. std::string name = argv[1];
  209. txtFile.open(name + ".txt");
  210. name = "warning/" + name + "/";
  211. if(NULL == opendir(name.c_str())) {
  212. mkdir(name.c_str(), 0777);
  213. }
  214. char buf[256];
  215. std::string str;
  216. pcl::PointXYZ point;
  217. float x;
  218. int warning = 0;
  219. while (txtFile.getline(buf, 256)) {
  220. str = buf;
  221. if(std::string::npos == str.find(" ")) {
  222. // printf("%s\n", buf);
  223. for (int i = atoi(buf); i > 0; i--) {
  224. txtFile.getline(buf, 256);
  225. str = buf;
  226. string2point(str, point);
  227. cloud->push_back(point);
  228. }
  229. if( GetAlg(cloud, x)) {
  230. // printf("find warning point.\n");
  231. warning++;
  232. pointFile.open(name + std::to_string(warning) + "_" + std::to_string(x) + ".txt", std::ios_base::out | std::ios::app);
  233. for(int i = 0; i < cloud->size(); i++) {
  234. pointFile << cloud->points[i].x << " " << cloud->points[i].y << std::endl;
  235. }
  236. usleep(10*1000);
  237. pointFile.close();
  238. }
  239. cloud->clear();
  240. }
  241. }
  242. pointFile.close();
  243. txtFile.close();
  244. }
  245. #endif