1516X Push Back 1.0
1516X's robot code for the 2025-2026 VEX Robotics Competition
Loading...
Searching...
No Matches
distanceReset.cpp
Go to the documentation of this file.
1#include "main.h"
2#include "lemlib/api.hpp"
3#include "pros/misc.h"
4#include "lemlib/api.hpp"
5#include "lemlib/util.hpp"
6#include "globals.h"
7
8
9const double FIELD_WIDTH_MM = 3657.6;
10const double FIELD_HEIGHT_MM = 3657.6;
11
12
13const double OFFSET_TO_LEFT_SENSOR = 150.0;
14const double OFFSET_TO_RIGHT_SENSOR = 155.0;
15const double OFFSET_TO_BACK_SENSOR = 170.0;
16const double OFFSET_TO_FRONT_SENSOR = 165.0;
17
18
19std::pair<double, double> distanceReset()
20{
21 double dist_left = left.get_distance();
22 double dist_right = right.get();
23 double dist_back = back.get();
24 double dist_front = front.get();
25 double heading_rad = chassis.getPose(true).theta;
26 double cos_h = std::cos(heading_rad);
27 double sin_h = std::sin(heading_rad);
28
29 // Determine nearest walls
30 bool near_left, near_back;
31 if ((dist_left + OFFSET_TO_LEFT_SENSOR) < (dist_right + OFFSET_TO_RIGHT_SENSOR)) {
32 near_left = true;
33 } else {
34 near_left = false;
35 }
36
37 if ((dist_back + OFFSET_TO_BACK_SENSOR) < (dist_front + OFFSET_TO_FRONT_SENSOR)) {
38 near_back = true;
39 } else {
40 near_back = false;
41 }
42
43 // Reference corner position in global frame
44 double ref_x, ref_y;
45 if (near_left) {
46 ref_x = 0.0;
47 } else {
48 ref_x = FIELD_WIDTH_MM;
49 }
50
51 if (near_back) {
52 ref_y = 0.0;
53 } else {
54 ref_y = FIELD_HEIGHT_MM;
55 }
56
57 // Local distances from nearest walls
58 double x_local, y_local;
59 if (near_left) {
60 x_local = dist_left + OFFSET_TO_LEFT_SENSOR;
61 } else {
62 x_local = dist_right + OFFSET_TO_RIGHT_SENSOR;
63 }
64
65 if (near_back) {
66 y_local = dist_back + OFFSET_TO_BACK_SENSOR;
67 } else {
68 y_local = dist_front + OFFSET_TO_FRONT_SENSOR;
69 }
70
71 // Rotate robot-local offset vector into global frame
72 double global_x = ref_x + x_local * cos_h - y_local * sin_h;
73 double global_y = ref_y + x_local * sin_h + y_local * cos_h;
74
75 return {global_x, global_y};
76}
const double FIELD_HEIGHT_MM
const double OFFSET_TO_FRONT_SENSOR
const double OFFSET_TO_BACK_SENSOR
std::pair< double, double > distanceReset()
const double OFFSET_TO_RIGHT_SENSOR
const double OFFSET_TO_LEFT_SENSOR
const double FIELD_WIDTH_MM
pros::Distance front(7)
pros::Distance left(5)
pros::Distance back(8)
pros::Distance right(4)
lemlib::Chassis chassis