Boundary.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //
  2. // Created by zx on 23-11-27.
  3. //
  4. #include "Boundary.h"
  5. #include <pcl/point_types.h>
  6. #include <fcntl.h>
  7. #include <google/protobuf/io/zero_copy_stream_impl.h>
  8. #include <google/protobuf/text_format.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. bool Boundary::read_proto_param(std::string prototxt_path, ::google::protobuf::Message &protobuf_parameter) {
  17. int fd = open(prototxt_path.c_str(), O_RDONLY);
  18. if (fd == -1) return false;
  19. FileInputStream *input = new FileInputStream(fd);
  20. bool success = google::protobuf::TextFormat::Parse(input, &protobuf_parameter);
  21. delete input;
  22. close(fd);
  23. return success;
  24. }
  25. bool Boundary::init(const std::string &file) {
  26. return read_proto_param(file, limit_);
  27. }
  28. bool Boundary::init(JetStream::Limit limit) {
  29. limit_ = limit;
  30. return true;
  31. }
  32. Boundary::~Boundary() {}
  33. BoundRet Boundary::limit_boundary(JetStream::MeasureInfo info, JetStream::Boundary bound) {
  34. //判断角度
  35. if (info.theta() <= bound.minangle() / 180.0 * M_PI) return BoundRetRightAngle;
  36. if (info.theta() >= bound.maxangle() / 180.0 * M_PI) return BoundRetLeftAngle;
  37. //计算四轮位置
  38. pcl::PointXYZ pt[4];
  39. pt[0] = pcl::PointXYZ(-info.width() / 2.0, info.wheelbase() / 2.0, 0);
  40. pt[1] = pcl::PointXYZ(info.width() / 2.0, info.wheelbase() / 2.0, 0);
  41. pt[2] = pcl::PointXYZ(-info.width() / 2.0, -info.wheelbase() / 2.0, 0);
  42. pt[3] = pcl::PointXYZ(info.width() / 2.0, -info.wheelbase() / 2.0, 0);
  43. for (int i = 0; i < 4; ++i) {
  44. //计算四轮位置
  45. float x = pt[i].x * cos(info.theta()) - pt[i].y * sin(info.theta()) + info.x();
  46. float y = pt[i].x * sin(info.theta()) + pt[i].y * cos(info.theta()) + info.y();
  47. //判断是否在盖板上
  48. // printf(" %f %f\n", x * x + y * y, bound.radius() * bound.radius());
  49. if (x * x + y * y > bound.radius() * bound.radius()) {
  50. if (fabs(y) > fabs(x)) {
  51. if (y > 0) return BoundRetFront;
  52. else return BoundRetBack;
  53. } else {
  54. if (x > 0) return BoundRetRight;
  55. else return BoundRetLeft;
  56. }
  57. }
  58. //判断左右
  59. if (x < bound.left()) return BoundRetLeft;
  60. if (x > bound.right()) return BoundRetRight;
  61. }
  62. //判断摆正后,后轮是否能够的着
  63. if (info.trans_y() - 0.5 * info.wheelbase() <= bound.buttom()) return BoundRetBack;
  64. if (fabs(info.ftheta()) > bound.fwheelangle()) return BoundRetWheelAngle;
  65. return BoundRetNormal;
  66. }
  67. void Boundary::limit(JetStream::MeasureInfo &info) {
  68. float trans_x = info.x() * cos(info.theta()) - info.y() * sin(info.theta());
  69. float trans_y = info.x() * sin(info.theta()) + info.y() * cos(info.theta());
  70. info.set_trans_x(trans_x);
  71. info.set_trans_y(trans_y);
  72. //判断轴距和宽度是否超
  73. if (info.width() >= limit_.maxwidth()) {
  74. info.set_border_plc(int(BoundRetWidth));
  75. info.set_border_display(int(BoundRetWidth));
  76. return;
  77. }
  78. if (info.wheelbase() >= limit_.maxwheelbase()) {
  79. info.set_border_plc(int(BoundRetWheelBase));
  80. info.set_border_display(int(BoundRetWheelBase));
  81. return;
  82. }
  83. info.set_border_plc(int(limit_boundary(info, limit_.plc_limit())));
  84. info.set_border_display(int(limit_boundary(info, limit_.display_limit())));
  85. }
  86. void Boundary::transMeasureInfo(JetStream::MeasureInfo &in, measure_buffer &out) {
  87. measure_info info;
  88. info.CopyFrom(out.measure_info_to_plc_forward());
  89. // 如果地面雷达状态已经被修改过,直接返回
  90. if (info.ground_status() != Measure_OK) {
  91. out.mutable_measure_info_to_plc_reverse()->CopyFrom(info);
  92. out.mutable_measure_info_to_terminal()->CopyFrom(info);
  93. return;
  94. }
  95. // 设备状态
  96. if (in.border_display() != Range_status::Range_correct || in.border_plc() != Range_status::Range_correct) {
  97. info.set_ground_status((MeasureStatu::Measure_Border));
  98. }
  99. info.set_cx(in.trans_x()); // 车辆中心坐标x
  100. info.set_cy(in.trans_y());
  101. info.set_theta(in.theta() * 180.0 / M_PI);
  102. info.set_length(0);
  103. info.set_width(in.width());
  104. info.set_wheelbase(in.wheelbase());
  105. info.set_front_theta(in.ftheta() * 180.0 / M_PI);
  106. info.set_border_statu(in.border_plc());
  107. info.set_is_stop(0);
  108. if (DetectManager::iter()->carMoveStatus() == FAILED) {
  109. info.set_motion_statu(1); // 静止3秒
  110. } else {
  111. info.set_motion_statu(0); // 运动
  112. }
  113. info.set_move_distance(0);
  114. out.mutable_measure_info_to_plc_forward()->CopyFrom(info);
  115. out.mutable_measure_info_to_plc_reverse()->CopyFrom(info);
  116. out.mutable_measure_info_to_terminal()->CopyFrom(info);
  117. out.mutable_measure_info_to_terminal()->set_border_statu(in.border_display());
  118. }
  119. void Boundary::transMeasureInfo(JetStream::MeasureInfo &in, TransitData::MeasureInfo &out) {
  120. out.x = in.x();
  121. out.y = in.y();
  122. out.transx = in.trans_x();
  123. out.transy = in.trans_y();
  124. out.theta = in.theta();
  125. out.width = in.width();
  126. out.wheelbase = in.wheelbase();
  127. out.ftheta = in.ftheta();
  128. out.border_plc = in.border_plc();
  129. out.border_display = in.border_display();
  130. out.error = in.error();
  131. }
  132. void Boundary::transMeasureInfo(DetectManager::DetectResult &in, JetStream::MeasureInfo &out) {
  133. out.set_x(in.car.wheels_center_x);
  134. out.set_y(in.car.wheels_center_y);
  135. out.set_theta(in.car.theta);
  136. out.set_width(in.car.wheel_width);
  137. out.set_wheelbase(in.car.wheel_base);
  138. out.set_ftheta(in.car.front_wheels_theta);
  139. }