test.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #include "test.hpp"
  2. #define DEBUG(...) \
  3. if (0) { \
  4. printf(__VA_ARGS__); \
  5. }
  6. Test::Test(Clamp* clamp):m_clamp(clamp) {
  7. m_snap_wite.m_id = 1;
  8. m_snap_read.m_id = 1;
  9. m_snap_wite.m_start_index = 34;
  10. m_snap_read.m_start_index = 4;
  11. m_snap_wite.m_communication_mode = Snap7_buf::Communication_mode::LOOP_COMMUNICATION;
  12. m_snap_read.m_communication_mode = Snap7_buf::Communication_mode::LOOP_COMMUNICATION;
  13. m_cloud = pcl::PointCloud<pcl::PointXYZ>::Ptr (new pcl::PointCloud<pcl::PointXYZ>);
  14. }
  15. void Test::setWriteSnap(int id, int index) {
  16. m_snap_wite.m_id = id;
  17. m_snap_wite.m_start_index = index;
  18. }
  19. void Test::setReadSnap(int id, int index) {
  20. m_snap_read.m_id = id;
  21. m_snap_read.m_start_index = index;
  22. }
  23. bool Test::GetAlg(float &x) {
  24. float sumx = 0,sumy = 0, w = 0, sumw = 0;
  25. for(int i=0; i < m_cloud->size(); i++) {
  26. w = float(1/pow(cos(atan(fabs(m_cloud->points[i].x/ m_cloud->points[i].y))), 2));
  27. // w = float(sqrt((36.0 - w))/6.0);
  28. sumw += w;
  29. sumx += w * m_cloud->points[i].x;
  30. }
  31. x = sumx/sumw;
  32. // return x > WARNING_ALG_VALUE;
  33. return true;
  34. }
  35. void Test::exportTxtFile() {
  36. // 文件记录算法坐标、实际坐标、记录id、
  37. // 文件夹下存放每一次的雷达点云数据,文件名为记录id、算法坐标、实际坐标
  38. m_record_data.id++;
  39. std::fstream txtFile, lidarFile;
  40. if(NULL == opendir(lidarPath.c_str())) {
  41. mkdir(lidarPath.c_str(), 0777);
  42. }
  43. txtFile.open(filePath + fileName, std::fstream::out | std::fstream::app);
  44. if (GetAlg(m_record_data.alg_x)) {
  45. lidarFile.open(lidarPath + std::to_string(m_record_data.id) + "_" + std::to_string(m_record_data.alg_x) + "_" + std::to_string(m_record_data.real_x - DEFAULT_WHEEL_AXIS) + ".txt", std::fstream::out | std::fstream::app);
  46. for (int i = 0; i < m_cloud->size(); i++) {
  47. lidarFile << m_cloud->points[i].x << " " << m_cloud->points[i].y << std::endl;
  48. }
  49. }
  50. txtFile << float(m_record_data.id) << " " << float(m_record_data.alg_x*1000) << " " << float(m_record_data.real_x - DEFAULT_WHEEL_AXIS ) << std::endl;
  51. lidarFile.close();
  52. txtFile.close();
  53. }
  54. void Test::commnuition() {
  55. float point_x;
  56. bool isMoved = true;
  57. int16_t old_move = 0, now_move = 0, plc_move = 0;
  58. std::fstream pointsFile;
  59. pointsFile.open("../record/big_point_water_measure.txt");
  60. char cloudbuf[256];
  61. pcl::PointXYZ cloud_point;
  62. DEBUG("commnuition\n")
  63. while (true)
  64. {
  65. if(isMoved) {
  66. // 模拟生成随机下一次实际车轮坐标
  67. srand(time(NULL));
  68. point_x = rand()%201 + DEFAULT_WHEEL_AXIS - 100;
  69. // point_x = float(DEFAULT_WHEEL_AXIS);
  70. m_record_data.real_x = point_x;
  71. // 发送给plc目标坐标值
  72. if(!snap7_write(point_x)) {
  73. // continue;
  74. }
  75. isMoved = false;
  76. }
  77. // 读plc数据,等待plc到达设置的车轮坐标
  78. if(snap7_read(plc_move)) {
  79. old_move = now_move;
  80. now_move = plc_move;
  81. } else {
  82. // continue;
  83. }
  84. // srand(time(NULL));
  85. // now_move = rand()%201 > 100;
  86. old_move = now_move = 1;
  87. if (old_move == 1 && now_move == 1) {
  88. m_record_data.real_x = point_x;
  89. #ifdef lidar
  90. update_clould();
  91. #else
  92. pointsFile.getline(cloudbuf, sizeof(cloudbuf));
  93. std::string cloudstr = cloudbuf;
  94. if(std::string::npos == cloudstr.find(" ")) {
  95. DEBUG("%s\n", cloudbuf);
  96. m_cloud->clear();
  97. for (int i = atoi(cloudbuf); i > 0; i--) {
  98. pointsFile.getline(cloudbuf, sizeof(cloudbuf));
  99. DEBUG("%s\n", cloudbuf);
  100. std::istringstream iss;
  101. iss.str(cloudbuf);
  102. float value;
  103. float data[2]={0};
  104. for(int i=0;i<2;++i)
  105. {
  106. if(iss>>value)
  107. {
  108. data[i]=value;
  109. }
  110. }
  111. cloud_point.x=data[0];
  112. cloud_point.y=data[1];
  113. DEBUG("%.4f %.4f\n", cloud_point.x, cloud_point.y);
  114. m_cloud->push_back(cloud_point);
  115. }
  116. }
  117. #endif
  118. exportTxtFile();
  119. usleep(100*1000);
  120. }
  121. if (old_move == 0 && now_move == 1) {
  122. isMoved = true;
  123. } else {
  124. isMoved = false;
  125. }
  126. }
  127. }
  128. void Test::update_clould() {
  129. m_clamp->mutex.lock();
  130. m_cloud->clear();
  131. m_clamp->lidar.get_last_cloud(m_cloud, std::chrono::system_clock::now());
  132. m_clamp->mutex.unlock();
  133. }
  134. float Test::SWAP_float(float x) {
  135. int b;
  136. float value = 0;
  137. memcpy(&b,&x,sizeof(float));
  138. int t = ((((b) & 0xff000000) >> 24) | (((b) & 0x00ff0000) >> 8) |(((b) & 0x0000ff00) << 8) | (((b) & 0x000000ff) << 24));
  139. memcpy(&value,&t,sizeof(float));
  140. return value;
  141. }
  142. bool Test::snap7_write(float &data) {
  143. DEBUG("---Debug write data : %f\n", data);
  144. data = SWAP_float(data);
  145. m_snap_wite.m_size = sizeof(data);
  146. m_snap_wite.mp_buf_reverse = &data;
  147. #ifdef plc
  148. if(m_plc->write_data_buf(m_snap_wite) != SUCCESS) {
  149. // while(m_plc->get_status() == Snap7_communication_base::SNAP7_COMMUNICATION_DISCONNECT) {}
  150. return false;
  151. }
  152. #endif
  153. DEBUG("---Debug write data end : %f\n", data);
  154. return true;
  155. };
  156. bool Test::snap7_read(float &data) {
  157. m_snap_read.m_size = sizeof(data);
  158. m_snap_read.mp_buf_reverse = &data;
  159. #ifdef plc
  160. if(m_plc->read_data_buf(m_snap_read) == SUCCESS) {
  161. // while(m_plc->get_status() == Snap7_communication_base::SNAP7_COMMUNICATION_DISCONNECT) {}
  162. return false;
  163. }
  164. #endif
  165. data = SWAP_float(data);
  166. return true;
  167. }
  168. bool Test::snap7_write(int16_t &data) {
  169. data = bswap_16(data);
  170. m_snap_wite.m_size = sizeof(data);
  171. m_snap_wite.mp_buf_reverse = &data;
  172. #ifdef plc
  173. if(m_plc->write_data_buf(m_snap_wite) != SUCCESS) {
  174. // while(m_plc->get_status() == Snap7_communication_base::SNAP7_COMMUNICATION_DISCONNECT) {}
  175. return false;
  176. }
  177. #endif
  178. return true;
  179. };
  180. bool Test::snap7_read(int16_t &data) {
  181. m_snap_read.m_size = sizeof(data);
  182. m_snap_read.mp_buf_reverse = &data;
  183. #ifdef plc
  184. if(m_plc->read_data_buf(m_snap_read) == SUCCESS) {
  185. // while(m_plc->get_status() == Snap7_communication_base::SNAP7_COMMUNICATION_DISCONNECT) {}
  186. return false;
  187. }
  188. #endif
  189. data = bswap_16(data);
  190. return true;
  191. }