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 32 of file visonAlignment.cpp.

32 {
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
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, rpm_to_mps_factor, and vision_sensor().

Referenced by opcontrol().

◆ calibrate_vision()

void calibrate_vision ( )

Definition at line 8 of file visonAlignment.cpp.

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

References calibrate_vision(), and vision_sensor().

Referenced by calibrate_vision(), and initialize().

◆ 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 24 of file visonAlignment.cpp.

◆ INCH_TO_METER

float INCH_TO_METER = 0.0254f
staticconstexpr

Definition at line 21 of file visonAlignment.cpp.

◆ rpm_to_mps_factor

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

Definition at line 25 of file visonAlignment.cpp.

Referenced by alignToGoal().

◆ TRACK_WIDTH_M

float TRACK_WIDTH_M = 12.8f * INCH_TO_METER
staticconstexpr

Definition at line 22 of file visonAlignment.cpp.

Referenced by driveController().

◆ wheel_circumference

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

Definition at line 23 of file visonAlignment.cpp.