1516X Push Back 1.0
1516X's robot code for the 2025-2026 VEX Robotics Competition
Loading...
Searching...
No Matches
distanceReset.cpp File Reference
#include "globals.h"
#include "lemlib/util.hpp"
#include "pros/distance.hpp"
#include <numeric>

Go to the source code of this file.

Classes

struct  SensorConfig
struct  SensorReadings
struct  distancePose

Functions

distancePose calculateGlobalPosition (const SensorReadings &front_data, const SensorReadings &left_data, const SensorReadings &right_data, const SensorReadings &back_data, double heading_deg)
distancePose distanceReset (bool setPose=false, bool filter=true)
distancePose distanceReset (bool left_use, bool right_use, bool front_use, bool back_use, bool setPose)

Variables

const double MM_TO_IN = 0.0393701
const double FIELD_WIDTH = 3657.6 * MM_TO_IN
const double FIELD_HEIGHT = 3657.6 * MM_TO_IN
const double HALF_WIDTH = FIELD_WIDTH / 2.0
const double HALF_HEIGHT = FIELD_HEIGHT / 2.0
const double MAX_SENSOR_RANGE = 2000 * MM_TO_IN
const double MIN_SENSOR_RANGE = 0 * MM_TO_IN
bool controller_screen_avilable
const SensorConfig front_sensor_cfg = {-0.75, -3, 0}
const SensorConfig left_sensor_cfg = {-0.5, -7.2, 90}
const SensorConfig right_sensor_cfg = {-0.5, 6.3, -90}
const SensorConfig back_sensor_cfg = {-10.5, -3, 180}

Function Documentation

◆ calculateGlobalPosition()

distancePose calculateGlobalPosition ( const SensorReadings & front_data,
const SensorReadings & left_data,
const SensorReadings & right_data,
const SensorReadings & back_data,
double heading_deg )

Definition at line 45 of file distanceReset.cpp.

51{
52 lemlib::Pose current_pose = chassis.getPose();
53 double est_x = current_pose.x;
54 double est_y = current_pose.y;
55
56 // 1. Determine Quadrant based on current Odom
57 // If x >= 0, we are in the Right half (closer to +X wall)
58 // If y >= 0, we are in the Top half (closer to +Y wall)
59 bool use_pos_x_wall = (est_x >= 0);
60 bool use_pos_y_wall = (est_y >= 0);
61
62 bool using_odom_x = true;
63 bool using_odom_y = true;
64
65 struct SensorData { SensorConfig cfg; double dist_in; int confidence;};
66 const SensorData sensors[] = {
67 {front_sensor_cfg, front_data.dist_mm * MM_TO_IN, frontDistance.get_confidence()},
68 {left_sensor_cfg, left_data.dist_mm * MM_TO_IN, leftDistance.get_confidence()},
69 {right_sensor_cfg, right_data.dist_mm * MM_TO_IN, rightDistance.get_confidence()},
70 {back_sensor_cfg, back_data.dist_mm * MM_TO_IN, backDistance.get_confidence()}
71 };
72
73 // Helper to check validity (range and now Quadrant relevance)
74 auto is_valid = [&](int i) {
75 return (sensors[i].dist_in < MAX_SENSOR_RANGE &&
76 sensors[i].dist_in >= MIN_SENSOR_RANGE);
77 };
78
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;
82
83 std::vector<double> x_cands;
84 std::vector<double> y_cands;
85
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);
89 double global_offset_x = (cfg.forward_offset * sin_h) + (cfg.strafe_offset * cos_h);
90 double global_offset_y = (cfg.forward_offset * cos_h) - (cfg.strafe_offset * sin_h);
91 return std::make_pair(global_offset_x, global_offset_y);
92 };
93
94
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);
103
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);
108
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); }
113 }
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);
117
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);
122
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);
127
128
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); }
133 }
134
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);
138
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);
143
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);
148
149
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); }
154 }
155
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);
159
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);
164
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);
169
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); }
174 }
175
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;
179 }
180
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;
184 }
185
186
187 distancePose pose;
188 pose.x = est_x;
189 pose.y = est_y;
190 pose.using_odom_x = using_odom_x;
191 pose.using_odom_y = using_odom_y;
192 return pose;
193}
const double MAX_SENSOR_RANGE
const double MM_TO_IN
const SensorConfig left_sensor_cfg
const SensorConfig back_sensor_cfg
const SensorConfig right_sensor_cfg
const double HALF_WIDTH
const SensorConfig front_sensor_cfg
const double HALF_HEIGHT
const double MIN_SENSOR_RANGE
pros::Distance frontDistance(14)
pros::Distance rightDistance(20)
pros::Distance backDistance(16)
pros::Distance leftDistance(15)
lemlib::OdomSensors sensors
lemlib::Chassis chassis
double forward_offset

References back_sensor_cfg, backDistance(), chassis, SensorReadings::dist_mm, SensorConfig::forward_offset, front_sensor_cfg, frontDistance(), HALF_HEIGHT, HALF_WIDTH, left_sensor_cfg, leftDistance(), MAX_SENSOR_RANGE, MIN_SENSOR_RANGE, MM_TO_IN, right_sensor_cfg, rightDistance(), sensors, SensorConfig::strafe_offset, distancePose::using_odom_x, distancePose::using_odom_y, distancePose::x, and distancePose::y.

Referenced by distanceReset(), and distanceReset().

◆ distanceReset() [1/2]

distancePose distanceReset ( bool left_use,
bool right_use,
bool front_use,
bool back_use,
bool setPose )

Definition at line 228 of file distanceReset.cpp.

228 {
229 double heading_deg = chassis.getPose().theta;
230
231 const int invalid_dist_mm = 10000;
232 const int invalid_confidence = 0;
233
234 // Direct readings based on boolean flags
235 SensorReadings front_data = front_use
236 ? SensorReadings{(double)frontDistance.get_distance(), frontDistance.get_object_size(), frontDistance.get_confidence()}
237 : SensorReadings{invalid_dist_mm, 0, invalid_confidence};
238
239 SensorReadings left_data = left_use
240 ? SensorReadings{(double)leftDistance.get_distance(), leftDistance.get_object_size(), leftDistance.get_confidence()}
241 : SensorReadings{invalid_dist_mm, 0, invalid_confidence};
242
243 SensorReadings right_data = right_use
244 ? SensorReadings{(double)rightDistance.get_distance(), rightDistance.get_object_size(), rightDistance.get_confidence()}
245 : SensorReadings{invalid_dist_mm, 0, invalid_confidence};
246
247 SensorReadings back_data = back_use
248 ? SensorReadings{(double)backDistance.get_distance(), backDistance.get_object_size(), backDistance.get_confidence()}
249 : SensorReadings{invalid_dist_mm, 0, invalid_confidence};
250
251 distancePose pose = calculateGlobalPosition(front_data, left_data, right_data, back_data, heading_deg);
252
253 if(setPose) {
254 chassis.setPose(pose.x, pose.y, chassis.getPose().theta);
255 }
256
257 return pose;
258}
distancePose calculateGlobalPosition(const SensorReadings &front_data, const SensorReadings &left_data, const SensorReadings &right_data, const SensorReadings &back_data, double heading_deg)

References backDistance(), calculateGlobalPosition(), chassis, frontDistance(), leftDistance(), rightDistance(), distancePose::x, and distancePose::y.

◆ distanceReset() [2/2]

distancePose distanceReset ( bool setPose = false,
bool filter = true )

Definition at line 196 of file distanceReset.cpp.

196 {
197 double heading_deg = chassis.getPose().theta;
198
199 const SensorReadings front_data = {(double)frontDistance.get_distance(), frontDistance.get_object_size(), frontDistance.get_confidence()};
200 const SensorReadings left_data = {(double)leftDistance.get_distance(), leftDistance.get_object_size(), leftDistance.get_confidence()};
201 const SensorReadings right_data = {(double)rightDistance.get_distance(), rightDistance.get_object_size(), rightDistance.get_confidence()};
202 const SensorReadings back_data = {(double)backDistance.get_distance(), backDistance.get_object_size(), backDistance.get_confidence()};
203
204 distancePose pose = calculateGlobalPosition(front_data, left_data, right_data, back_data, heading_deg);
205 if(filter)
206 {
207 if(std::abs(pose.x - chassis.getPose().x) > 3.5)
208 {
209 pose.x = chassis.getPose().x;
210 std::cout << "Filtered X" << std::endl;
211 }
212 if(std::abs(pose.y - chassis.getPose().y) > 3.5)
213 {
214 pose.x = chassis.getPose().y;
215 std::cout << "Filtered Y" << std::endl;
216 }
217
218 }
219 if(setPose) {
220 chassis.setPose(pose.x, pose.y, chassis.getPose().theta);
221 pros::delay(2);
222 }
223
224 std::cout << "Distance Reset Pose: " << pose.x << ", " << pose.y << ", using_odom_x: " << pose.using_odom_x << ", using_odom_y: " << pose.using_odom_y << std::endl;
225 return pose;
226}

References backDistance(), calculateGlobalPosition(), chassis, frontDistance(), leftDistance(), rightDistance(), distancePose::using_odom_x, distancePose::using_odom_y, distancePose::x, and distancePose::y.

Referenced by awp_auton(), elim_auton(), left_auton(), opcontrol(), right_auton(), skills_auton(), and test_auton().

Variable Documentation

◆ back_sensor_cfg

const SensorConfig back_sensor_cfg = {-10.5, -3, 180}

Definition at line 30 of file distanceReset.cpp.

30{-10.5, -3, 180};

Referenced by calculateGlobalPosition().

◆ controller_screen_avilable

bool controller_screen_avilable

Definition at line 14 of file distanceReset.cpp.

◆ FIELD_HEIGHT

const double FIELD_HEIGHT = 3657.6 * MM_TO_IN

Definition at line 8 of file distanceReset.cpp.

◆ FIELD_WIDTH

const double FIELD_WIDTH = 3657.6 * MM_TO_IN

Definition at line 7 of file distanceReset.cpp.

◆ front_sensor_cfg

const SensorConfig front_sensor_cfg = {-0.75, -3, 0}

Definition at line 27 of file distanceReset.cpp.

27{-0.75, -3, 0};

Referenced by calculateGlobalPosition().

◆ HALF_HEIGHT

const double HALF_HEIGHT = FIELD_HEIGHT / 2.0

Definition at line 10 of file distanceReset.cpp.

Referenced by calculateGlobalPosition().

◆ HALF_WIDTH

const double HALF_WIDTH = FIELD_WIDTH / 2.0

Definition at line 9 of file distanceReset.cpp.

Referenced by calculateGlobalPosition().

◆ left_sensor_cfg

const SensorConfig left_sensor_cfg = {-0.5, -7.2, 90}

Definition at line 28 of file distanceReset.cpp.

28{-0.5, -7.2, 90};

Referenced by calculateGlobalPosition().

◆ MAX_SENSOR_RANGE

const double MAX_SENSOR_RANGE = 2000 * MM_TO_IN

Definition at line 11 of file distanceReset.cpp.

Referenced by calculateGlobalPosition().

◆ MIN_SENSOR_RANGE

const double MIN_SENSOR_RANGE = 0 * MM_TO_IN

Definition at line 12 of file distanceReset.cpp.

Referenced by calculateGlobalPosition().

◆ MM_TO_IN

const double MM_TO_IN = 0.0393701

Definition at line 6 of file distanceReset.cpp.

Referenced by calculateGlobalPosition().

◆ right_sensor_cfg

const SensorConfig right_sensor_cfg = {-0.5, 6.3, -90}

Definition at line 29 of file distanceReset.cpp.

29{-0.5, 6.3, -90};

Referenced by calculateGlobalPosition().