1516X Push Back 1.0
1516X's robot code for the 2025-2026 VEX Robotics Competition
Loading...
Searching...
No Matches
visonAlignment.cpp
Go to the documentation of this file.
1#include "globals.h"
2#include "pros/colors.h"
3#include "globals.h"
4#include "velocityController.h"
5#include <algorithm>
6#include <cmath>
7
9 pros::Task calibrate_vision([]() {
10 pros::vision_signature_s_t SIG_1 = pros::Vision::signature_from_utility(1, 2283, 6317, 4300, -5329, -4553, -4941, 5.3, 0);
11 vision_sensor.set_signature(1, &SIG_1);
12 vision_sensor.set_auto_white_balance(true);
13 pros::delay(1500);
14 int wb_value = vision_sensor.get_white_balance();
15 vision_sensor.set_auto_white_balance(false);
16 vision_sensor.set_white_balance(wb_value);
17 pros::Task::current().remove();
18 });
19}
20
21static constexpr float INCH_TO_METER = 0.0254f;
22static constexpr float TRACK_WIDTH_M = 12.8f * INCH_TO_METER;
23static constexpr float wheel_circumference = (float)3.25 * M_PI * INCH_TO_METER;
24static constexpr float gear_ratio = 4.0f / 3.0f;
25static constexpr float rpm_to_mps_factor = (wheel_circumference / gear_ratio) / 60.0f;
26
27VoltageController driveController(
28 5.8432642308, 0.213526937516, 1.14429410811, 0.906356177095,
29 0.347072436421, 11.4953431776, 54.5797495382, 0.05, TRACK_WIDTH_M
30);
31
32bool alignToGoal(int SIG_NUM) {
33 const float VISION_kP = 0.012f;
34 const float MAX_W = 3.0f;
35 const int CENTER_X = 158;
36 const int ERROR_THRESHOLD = 5;
37 const int FRAMES_TO_SETTLE = 12;
38
39 int aligned_counter = 0;
40 int lost_frame_counter = 0;
41 float time_elapsed = 0;
42 bool is_aligned = false;
43
44 vision_sensor.clear_led();
45
46 while (time_elapsed < 3000 && !controller.get_digital_new_press(pros::E_CONTROLLER_DIGITAL_A)) {
47
48 pros::vision_object_s_t goal = vision_sensor.get_by_sig(0, SIG_NUM);
49 double mLeft = leftMotors.get_actual_velocity() * rpm_to_mps_factor;
50 double mRight = rightMotors.get_actual_velocity() * rpm_to_mps_factor;
51
52 double targetW = 0;
53
54 if (goal.width > 15) {
55 lost_frame_counter = 0;
56
57 int error = -CENTER_X + goal.x_middle_coord;
58
59 if (std::abs(error) <= ERROR_THRESHOLD) {
60 aligned_counter++;
61 vision_sensor.set_led(pros::c::COLOR_GREEN);
62 targetW = 0;
63
64 if (aligned_counter > FRAMES_TO_SETTLE) {
65 is_aligned = true;
66 break;
67 }
68 } else {
69 aligned_counter = 0;
70 vision_sensor.set_led(pros::c::COLOR_BLUE);
71 targetW = error * VISION_kP;
72 targetW = std::clamp((float)targetW, -MAX_W, MAX_W);
73 }
74 } else {
75 lost_frame_counter++;
76 if (lost_frame_counter > 3) {
77 vision_sensor.set_led(pros::c::COLOR_RED);
78 targetW = 0;
79 aligned_counter = 0;
80 }
81 }
82
83 DrivetrainVoltages volts = driveController.update(0.0, targetW, mLeft, mRight);
84
85 leftMotors.move_voltage(volts.leftVoltage);
86 rightMotors.move_voltage(volts.rightVoltage);
87
88 pros::delay(10);
89 time_elapsed += 10;
90 }
91
92 leftMotors.brake();
93 rightMotors.brake();
94 return is_aligned;
95}
pros::Vision vision_sensor(16)
pros::MotorGroup rightMotors
pros::MotorGroup leftMotors
Definition globals.cpp:20
pros::Controller controller
void calibrate_vision()
static constexpr float TRACK_WIDTH_M
static constexpr float INCH_TO_METER
VoltageController driveController(5.8432642308, 0.213526937516, 1.14429410811, 0.906356177095, 0.347072436421, 11.4953431776, 54.5797495382, 0.05, TRACK_WIDTH_M)
static constexpr float wheel_circumference
static constexpr float gear_ratio
bool alignToGoal(int SIG_NUM)
static constexpr float rpm_to_mps_factor