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