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

Go to the source code of this file.

Functions

void crossBarrier (int times=2, bool reverse=false, bool fullyDrop=true)

Variables

double PITCH_CLIMB_THRESHOLD = 8
double PITCH_LEVEL_THRESHOLD = 1
const double CROSSING_TIMEOUT = 1200
const double DRIVE_SPEED = 127
const double HEADING_KP = 6.0
const int POST_LANDING_TIME = 200

Function Documentation

◆ crossBarrier()

void crossBarrier ( int times = 2,
bool reverse = false,
bool fullyDrop = true )

Definition at line 15 of file crossBarrierDetection.cpp.

15 {
16 double targetHeading = imu.get_heading();
17 int dir = reverse ? -1 : 1;
18 if(reverse)
19 {
22 }
23
24 for(int i = 0; i < times; i++) {
25 bool hasClimbed = false;
26 bool hasDropped = false;
27
28 uint32_t startTime = pros::millis();
29 uint32_t previousTime = pros::millis();
30
31 while (true) {
32
33 if (pros::millis() - startTime > CROSSING_TIMEOUT) {
34 chassis.tank(0,0);
35 leftMotors.brake();
36 rightMotors.brake();
37 return;
38 }
39
40 double currentPitch = imu.get_roll() * dir;
41 double currentHeading = imu.get_heading();
42
43 double error = targetHeading - currentHeading;
44 while (error > 180) error -= 360;
45 while (error < -180) error += 360;
46
47 double turnOffset = error * HEADING_KP;
48
49 chassis.tank((DRIVE_SPEED * dir) + turnOffset,
50 (DRIVE_SPEED * dir) - turnOffset);
51
52 // Detect climb
53 if (!hasClimbed && currentPitch > PITCH_CLIMB_THRESHOLD) {
54 hasClimbed = true;
55 }
56
57 // Detect crest
58 if (hasClimbed && !hasDropped && currentPitch < PITCH_LEVEL_THRESHOLD) {
59 hasDropped = true;
60 }
61
62 // --- LANDING LOGIC ---
63 if (hasClimbed && hasDropped) {
64 if(i == 1 && times > 1)
65 matchloader.retract();
66
67 // FULLY DROP MODE
68 if (fullyDrop && std::abs(currentPitch) < PITCH_LEVEL_THRESHOLD) {
69
70 uint32_t clearStartTime = pros::millis();
71 while (pros::millis() - clearStartTime < POST_LANDING_TIME) {
72
73 currentHeading = imu.get_heading();
74 error = targetHeading - currentHeading;
75 while (error > 180) error -= 360;
76 while (error < -180) error += 360;
77
78 turnOffset = error * HEADING_KP;
79
80 chassis.tank((DRIVE_SPEED * dir) + turnOffset,
81 (DRIVE_SPEED * dir) - turnOffset);
82
83 pros::delay(10);
84 }
85
86 chassis.tank(0, 0);
87 leftMotors.brake();
88 rightMotors.brake();
89 break;
90 }
91
92 // ANGLED PARK MODE
93 if (!fullyDrop && currentPitch < -PITCH_LEVEL_THRESHOLD) {
94
95 chassis.tank(0, 0);
96
97 // use hold instead of brake to prevent rocking forward
98 leftMotors.set_brake_mode(pros::E_MOTOR_BRAKE_HOLD);
99 rightMotors.set_brake_mode(pros::E_MOTOR_BRAKE_HOLD);
100
101 break;
102 }
103 }
104
105 pros::Task::delay_until(&previousTime, 10);
106 }
107
108 if (i < times - 1)
109 pros::delay(200);
110 }
111}
double PITCH_LEVEL_THRESHOLD
const double CROSSING_TIMEOUT
const double DRIVE_SPEED
const double HEADING_KP
const int POST_LANDING_TIME
double PITCH_CLIMB_THRESHOLD
Chassis chassis(drivebase, lateral_controller, angular_controller, sensors, &throttle_curve, &steer_curve)
pros::adi::Pneumatics matchloader('C', false)
pros::Imu imu(16)
lemlib::Drivetrain drivebase & leftMotors
Definition globals.cpp:21
pros::MotorGroup rightMotors({17, 19, -18}, pros::MotorGears::blue)

References chassis(), CROSSING_TIMEOUT, DRIVE_SPEED, HEADING_KP, imu(), leftMotors, matchloader(), PITCH_CLIMB_THRESHOLD, PITCH_LEVEL_THRESHOLD, POST_LANDING_TIME, and rightMotors().

Variable Documentation

◆ CROSSING_TIMEOUT

const double CROSSING_TIMEOUT = 1200

Definition at line 10 of file crossBarrierDetection.cpp.

Referenced by crossBarrier().

◆ DRIVE_SPEED

const double DRIVE_SPEED = 127

Definition at line 11 of file crossBarrierDetection.cpp.

Referenced by crossBarrier().

◆ HEADING_KP

const double HEADING_KP = 6.0

Definition at line 12 of file crossBarrierDetection.cpp.

Referenced by crossBarrier().

◆ PITCH_CLIMB_THRESHOLD

double PITCH_CLIMB_THRESHOLD = 8

Definition at line 8 of file crossBarrierDetection.cpp.

Referenced by crossBarrier().

◆ PITCH_LEVEL_THRESHOLD

double PITCH_LEVEL_THRESHOLD = 1

Definition at line 9 of file crossBarrierDetection.cpp.

Referenced by crossBarrier().

◆ POST_LANDING_TIME

const int POST_LANDING_TIME = 200

Definition at line 13 of file crossBarrierDetection.cpp.

Referenced by crossBarrier().