1516X Push Back 1.0
1516X's robot code for the 2025-2026 VEX Robotics Competition
Loading...
Searching...
No Matches
visonAlignment.cpp File Reference
#include "globals.h"
#include "pros/colors.h"
#include "velocityController.h"
#include <algorithm>
#include <cmath>

Go to the source code of this file.

Functions

void calibrate_vision ()
VoltageController driveController (5.8432642308, 0.213526937516, 1.14429410811, 0.906356177095, 0.347072436421, 11.4953431776, 54.5797495382, 0.05, TRACK_WIDTH_M)
bool alignToGoal (int SIG_NUM)

Variables

static constexpr float INCH_TO_METER = 0.0254f
static constexpr float TRACK_WIDTH_M = 12.8f * INCH_TO_METER
static constexpr float wheel_circumference = (float)3.25 * M_PI * INCH_TO_METER
static constexpr float gear_ratio = 4.0f / 3.0f
static constexpr float rpm_to_mps_factor = (wheel_circumference / gear_ratio) / 60.0f

Function Documentation

◆ alignToGoal()

bool alignToGoal ( int SIG_NUM)

Definition at line 35 of file visonAlignment.cpp.

35 {
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)
VoltageController driveController(5.8432642308, 0.213526937516, 1.14429410811, 0.906356177095, 0.347072436421, 11.4953431776, 54.5797495382, 0.05, TRACK_WIDTH_M)
static constexpr float rpm_to_mps_factor

References controller(), driveController(), leftMotors, rightMotors(), and rpm_to_mps_factor.

◆ calibrate_vision()

void calibrate_vision ( )

Definition at line 11 of file visonAlignment.cpp.

11 {
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}
void calibrate_vision()

References calibrate_vision().

Referenced by calibrate_vision().

◆ driveController()

VoltageController driveController ( 5. 8432642308,
0. 213526937516,
1. 14429410811,
0. 906356177095,
0. 347072436421,
11. 4953431776,
54. 5797495382,
0. 05,
TRACK_WIDTH_M  )

References TRACK_WIDTH_M.

Referenced by alignToGoal().

Variable Documentation

◆ gear_ratio

float gear_ratio = 4.0f / 3.0f
staticconstexpr

Definition at line 27 of file visonAlignment.cpp.

◆ INCH_TO_METER

float INCH_TO_METER = 0.0254f
staticconstexpr

Definition at line 24 of file visonAlignment.cpp.

◆ rpm_to_mps_factor

float rpm_to_mps_factor = (wheel_circumference / gear_ratio) / 60.0f
staticconstexpr

Definition at line 28 of file visonAlignment.cpp.

Referenced by alignToGoal().

◆ TRACK_WIDTH_M

float TRACK_WIDTH_M = 12.8f * INCH_TO_METER
staticconstexpr

Definition at line 25 of file visonAlignment.cpp.

Referenced by driveController().

◆ wheel_circumference

float wheel_circumference = (float)3.25 * M_PI * INCH_TO_METER
staticconstexpr

Definition at line 26 of file visonAlignment.cpp.