52 lemlib::Pose current_pose =
chassis.getPose();
53 double est_x = current_pose.x;
54 double est_y = current_pose.y;
59 bool use_pos_x_wall = (est_x >= 0);
60 bool use_pos_y_wall = (est_y >= 0);
62 bool using_odom_x =
true;
63 bool using_odom_y =
true;
65 struct SensorData {
SensorConfig cfg;
double dist_in;
int confidence;};
74 auto is_valid = [&](
int i) {
79 double norm_heading = std::fmod(heading_deg, 360.0);
80 if (norm_heading < 0) norm_heading += 360.0;
81 const double TOLERANCE = 40.0;
83 std::vector<double> x_cands;
84 std::vector<double> y_cands;
86 auto get_global_offsets = [&](
const SensorConfig& cfg,
double heading_rad) {
87 double cos_h = std::cos(heading_rad);
88 double sin_h = std::sin(heading_rad);
91 return std::make_pair(global_offset_x, global_offset_y);
95 if (norm_heading <= TOLERANCE || norm_heading >= 360.0 - TOLERANCE) {
96 double angle_off_rad = (norm_heading <= TOLERANCE) ?
97 lemlib::degToRad(norm_heading) : lemlib::degToRad(norm_heading - 360.0);
98 double heading_rad = angle_off_rad;
99 double perp_dist_0 =
sensors[0].dist_in * std::cos(angle_off_rad);
100 double perp_dist_3 =
sensors[3].dist_in * std::cos(angle_off_rad);
101 double perp_dist_1 =
sensors[1].dist_in * std::cos(angle_off_rad);
102 double perp_dist_2 =
sensors[2].dist_in * std::cos(angle_off_rad);
104 auto offset_0 = get_global_offsets(
sensors[0].cfg, heading_rad);
105 auto offset_1 = get_global_offsets(
sensors[1].cfg, heading_rad);
106 auto offset_2 = get_global_offsets(
sensors[2].cfg, heading_rad);
107 auto offset_3 = get_global_offsets(
sensors[3].cfg, heading_rad);
109 if (is_valid(0) && use_pos_y_wall) { y_cands.push_back(
HALF_HEIGHT - perp_dist_0 - offset_0.second); }
110 if (is_valid(3) && !use_pos_y_wall) { y_cands.push_back(-
HALF_HEIGHT + perp_dist_3 - offset_3.second); }
111 if (is_valid(1) && !use_pos_x_wall) { x_cands.push_back(-
HALF_WIDTH + perp_dist_1 - offset_1.first); }
112 if (is_valid(2) && use_pos_x_wall) { x_cands.push_back(
HALF_WIDTH - perp_dist_2 - offset_2.first); }
114 else if (std::fabs(norm_heading - 180.0) <= TOLERANCE) {
115 double angle_off_rad = lemlib::degToRad(norm_heading - 180.0);
116 double heading_rad = lemlib::degToRad(norm_heading);
118 double perp_dist_0 =
sensors[0].dist_in * std::cos(angle_off_rad);
119 double perp_dist_3 =
sensors[3].dist_in * std::cos(angle_off_rad);
120 double perp_dist_1 =
sensors[1].dist_in * std::cos(angle_off_rad);
121 double perp_dist_2 =
sensors[2].dist_in * std::cos(angle_off_rad);
123 auto offset_0 = get_global_offsets(
sensors[0].cfg, heading_rad);
124 auto offset_1 = get_global_offsets(
sensors[1].cfg, heading_rad);
125 auto offset_2 = get_global_offsets(
sensors[2].cfg, heading_rad);
126 auto offset_3 = get_global_offsets(
sensors[3].cfg, heading_rad);
129 if (is_valid(0) && !use_pos_y_wall) { y_cands.push_back(-
HALF_HEIGHT + perp_dist_0 - offset_0.second); }
130 if (is_valid(3) && use_pos_y_wall) { y_cands.push_back(
HALF_HEIGHT - perp_dist_3 - offset_3.second); }
131 if (is_valid(1) && use_pos_x_wall) { x_cands.push_back(
HALF_WIDTH - perp_dist_1 - offset_1.first); }
132 if (is_valid(2) && !use_pos_x_wall) { x_cands.push_back(-
HALF_WIDTH + perp_dist_2 - offset_2.first); }
135 else if (std::fabs(norm_heading - 90.0) <= TOLERANCE) {
136 double angle_off_rad = lemlib::degToRad(norm_heading - 90.0);
137 double heading_rad = lemlib::degToRad(norm_heading);
139 double perp_dist_0 =
sensors[0].dist_in * std::cos(angle_off_rad);
140 double perp_dist_3 =
sensors[3].dist_in * std::cos(angle_off_rad);
141 double perp_dist_1 =
sensors[1].dist_in * std::cos(angle_off_rad);
142 double perp_dist_2 =
sensors[2].dist_in * std::cos(angle_off_rad);
144 auto offset_0 = get_global_offsets(
sensors[0].cfg, heading_rad);
145 auto offset_1 = get_global_offsets(
sensors[1].cfg, heading_rad);
146 auto offset_2 = get_global_offsets(
sensors[2].cfg, heading_rad);
147 auto offset_3 = get_global_offsets(
sensors[3].cfg, heading_rad);
150 if (is_valid(0) && use_pos_x_wall) { x_cands.push_back(
HALF_WIDTH - perp_dist_0 - offset_0.first); }
151 if (is_valid(3) && !use_pos_x_wall) { x_cands.push_back(-
HALF_WIDTH + perp_dist_3 - offset_3.first); }
152 if (is_valid(1) && use_pos_y_wall) { y_cands.push_back(
HALF_HEIGHT - perp_dist_1 - offset_1.second); }
153 if (is_valid(2) && !use_pos_y_wall) { y_cands.push_back(-
HALF_HEIGHT + perp_dist_2 - offset_2.second); }
156 else if (std::fabs(norm_heading - 270.0) <= TOLERANCE) {
157 double angle_off_rad = lemlib::degToRad(norm_heading - 270.0);
158 double heading_rad = lemlib::degToRad(norm_heading);
160 double perp_dist_0 =
sensors[0].dist_in * std::cos(angle_off_rad);
161 double perp_dist_3 =
sensors[3].dist_in * std::cos(angle_off_rad);
162 double perp_dist_1 =
sensors[1].dist_in * std::cos(angle_off_rad);
163 double perp_dist_2 =
sensors[2].dist_in * std::cos(angle_off_rad);
165 auto offset_0 = get_global_offsets(
sensors[0].cfg, heading_rad);
166 auto offset_1 = get_global_offsets(
sensors[1].cfg, heading_rad);
167 auto offset_2 = get_global_offsets(
sensors[2].cfg, heading_rad);
168 auto offset_3 = get_global_offsets(
sensors[3].cfg, heading_rad);
170 if (is_valid(0) && !use_pos_x_wall) { x_cands.push_back(-
HALF_WIDTH + perp_dist_0 - offset_0.first); }
171 if (is_valid(3) && use_pos_x_wall) { x_cands.push_back(
HALF_WIDTH - perp_dist_3 - offset_3.first); }
172 if (is_valid(1) && !use_pos_y_wall) { y_cands.push_back(-
HALF_HEIGHT + perp_dist_1 - offset_1.second); }
173 if (is_valid(2) && use_pos_y_wall) { y_cands.push_back(
HALF_HEIGHT - perp_dist_2 - offset_2.second); }
176 if (!x_cands.empty()) {
177 est_x = std::accumulate(x_cands.begin(), x_cands.end(), 0.0) / x_cands.size();
178 using_odom_x =
false;
181 if (!y_cands.empty()) {
182 est_y = std::accumulate(y_cands.begin(), y_cands.end(), 0.0) / y_cands.size();
183 using_odom_y =
false;