46 lemlib::Pose current_pose =
chassis.getPose();
47 double est_x = current_pose.x;
48 double est_y = current_pose.y;
53 bool use_pos_x_wall = (est_x >= 0);
54 bool use_pos_y_wall = (est_y >= 0);
56 bool using_odom_x =
true;
57 bool using_odom_y =
true;
59 struct SensorData {
SensorConfig cfg;
double dist_in;
int confidence;};
68 auto is_valid = [&](
int i) {
73 double norm_heading = std::fmod(heading_deg, 360.0);
74 if (norm_heading < 0) norm_heading += 360.0;
75 const double TOLERANCE = 40.0;
77 std::vector<double> x_cands;
78 std::vector<double> y_cands;
80 auto get_global_offsets = [&](
const SensorConfig& cfg,
double heading_rad) {
81 double cos_h = std::cos(heading_rad);
82 double sin_h = std::sin(heading_rad);
85 return std::make_pair(global_offset_x, global_offset_y);
89 if (norm_heading <= TOLERANCE || norm_heading >= 360.0 - TOLERANCE) {
90 double angle_off_rad = (norm_heading <= TOLERANCE) ?
91 lemlib::degToRad(norm_heading) : lemlib::degToRad(norm_heading - 360.0);
92 double heading_rad = angle_off_rad;
93 double perp_dist_0 =
sensors[0].dist_in * std::cos(angle_off_rad);
94 double perp_dist_3 =
sensors[3].dist_in * std::cos(angle_off_rad);
95 double perp_dist_1 =
sensors[1].dist_in * std::cos(angle_off_rad);
96 double perp_dist_2 =
sensors[2].dist_in * std::cos(angle_off_rad);
98 auto offset_0 = get_global_offsets(
sensors[0].cfg, heading_rad);
99 auto offset_1 = get_global_offsets(
sensors[1].cfg, heading_rad);
100 auto offset_2 = get_global_offsets(
sensors[2].cfg, heading_rad);
101 auto offset_3 = get_global_offsets(
sensors[3].cfg, heading_rad);
103 if (is_valid(0) && use_pos_y_wall) { y_cands.push_back(
HALF_HEIGHT - perp_dist_0 - offset_0.second); }
104 if (is_valid(3) && !use_pos_y_wall) { y_cands.push_back(-
HALF_HEIGHT + perp_dist_3 - offset_3.second); }
105 if (is_valid(1) && !use_pos_x_wall) { x_cands.push_back(-
HALF_WIDTH + perp_dist_1 - offset_1.first); }
106 if (is_valid(2) && use_pos_x_wall) { x_cands.push_back(
HALF_WIDTH - perp_dist_2 - offset_2.first); }
108 else if (std::fabs(norm_heading - 180.0) <= TOLERANCE) {
109 double angle_off_rad = lemlib::degToRad(norm_heading - 180.0);
110 double heading_rad = lemlib::degToRad(norm_heading);
112 double perp_dist_0 =
sensors[0].dist_in * std::cos(angle_off_rad);
113 double perp_dist_3 =
sensors[3].dist_in * std::cos(angle_off_rad);
114 double perp_dist_1 =
sensors[1].dist_in * std::cos(angle_off_rad);
115 double perp_dist_2 =
sensors[2].dist_in * std::cos(angle_off_rad);
117 auto offset_0 = get_global_offsets(
sensors[0].cfg, heading_rad);
118 auto offset_1 = get_global_offsets(
sensors[1].cfg, heading_rad);
119 auto offset_2 = get_global_offsets(
sensors[2].cfg, heading_rad);
120 auto offset_3 = get_global_offsets(
sensors[3].cfg, heading_rad);
123 if (is_valid(0) && !use_pos_y_wall) { y_cands.push_back(-
HALF_HEIGHT + perp_dist_0 - offset_0.second); }
124 if (is_valid(3) && use_pos_y_wall) { y_cands.push_back(
HALF_HEIGHT - perp_dist_3 - offset_3.second); }
125 if (is_valid(1) && use_pos_x_wall) { x_cands.push_back(
HALF_WIDTH - perp_dist_1 - offset_1.first); }
126 if (is_valid(2) && !use_pos_x_wall) { x_cands.push_back(-
HALF_WIDTH + perp_dist_2 - offset_2.first); }
129 else if (std::fabs(norm_heading - 90.0) <= TOLERANCE) {
130 double angle_off_rad = lemlib::degToRad(norm_heading - 90.0);
131 double heading_rad = lemlib::degToRad(norm_heading);
133 double perp_dist_0 =
sensors[0].dist_in * std::cos(angle_off_rad);
134 double perp_dist_3 =
sensors[3].dist_in * std::cos(angle_off_rad);
135 double perp_dist_1 =
sensors[1].dist_in * std::cos(angle_off_rad);
136 double perp_dist_2 =
sensors[2].dist_in * std::cos(angle_off_rad);
138 auto offset_0 = get_global_offsets(
sensors[0].cfg, heading_rad);
139 auto offset_1 = get_global_offsets(
sensors[1].cfg, heading_rad);
140 auto offset_2 = get_global_offsets(
sensors[2].cfg, heading_rad);
141 auto offset_3 = get_global_offsets(
sensors[3].cfg, heading_rad);
144 if (is_valid(0) && use_pos_x_wall) { x_cands.push_back(
HALF_WIDTH - perp_dist_0 - offset_0.first); }
145 if (is_valid(3) && !use_pos_x_wall) { x_cands.push_back(-
HALF_WIDTH + perp_dist_3 - offset_3.first); }
146 if (is_valid(1) && use_pos_y_wall) { y_cands.push_back(
HALF_HEIGHT - perp_dist_1 - offset_1.second); }
147 if (is_valid(2) && !use_pos_y_wall) { y_cands.push_back(-
HALF_HEIGHT + perp_dist_2 - offset_2.second); }
150 else if (std::fabs(norm_heading - 270.0) <= TOLERANCE) {
151 double angle_off_rad = lemlib::degToRad(norm_heading - 270.0);
152 double heading_rad = lemlib::degToRad(norm_heading);
154 double perp_dist_0 =
sensors[0].dist_in * std::cos(angle_off_rad);
155 double perp_dist_3 =
sensors[3].dist_in * std::cos(angle_off_rad);
156 double perp_dist_1 =
sensors[1].dist_in * std::cos(angle_off_rad);
157 double perp_dist_2 =
sensors[2].dist_in * std::cos(angle_off_rad);
159 auto offset_0 = get_global_offsets(
sensors[0].cfg, heading_rad);
160 auto offset_1 = get_global_offsets(
sensors[1].cfg, heading_rad);
161 auto offset_2 = get_global_offsets(
sensors[2].cfg, heading_rad);
162 auto offset_3 = get_global_offsets(
sensors[3].cfg, heading_rad);
164 if (is_valid(0) && !use_pos_x_wall) { x_cands.push_back(-
HALF_WIDTH + perp_dist_0 - offset_0.first); }
165 if (is_valid(3) && use_pos_x_wall) { x_cands.push_back(
HALF_WIDTH - perp_dist_3 - offset_3.first); }
166 if (is_valid(1) && !use_pos_y_wall) { y_cands.push_back(-
HALF_HEIGHT + perp_dist_1 - offset_1.second); }
167 if (is_valid(2) && use_pos_y_wall) { y_cands.push_back(
HALF_HEIGHT - perp_dist_2 - offset_2.second); }
170 if (!x_cands.empty()) {
171 est_x = std::accumulate(x_cands.begin(), x_cands.end(), 0.0) / x_cands.size();
172 using_odom_x =
false;
175 if (!y_cands.empty()) {
176 est_y = std::accumulate(y_cands.begin(), y_cands.end(), 0.0) / y_cands.size();
177 using_odom_y =
false;