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 12 of file crossBarrierDetection.cpp.

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

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

Referenced by skills_auton().

Variable Documentation

◆ CROSSING_TIMEOUT

const double CROSSING_TIMEOUT = 1200

Definition at line 7 of file crossBarrierDetection.cpp.

Referenced by crossBarrier().

◆ DRIVE_SPEED

const double DRIVE_SPEED = 127

Definition at line 8 of file crossBarrierDetection.cpp.

Referenced by crossBarrier().

◆ HEADING_KP

const double HEADING_KP = 6.0

Definition at line 9 of file crossBarrierDetection.cpp.

Referenced by crossBarrier().

◆ PITCH_CLIMB_THRESHOLD

double PITCH_CLIMB_THRESHOLD = 8

Definition at line 5 of file crossBarrierDetection.cpp.

Referenced by crossBarrier().

◆ PITCH_LEVEL_THRESHOLD

double PITCH_LEVEL_THRESHOLD = 1

Definition at line 6 of file crossBarrierDetection.cpp.

Referenced by crossBarrier().

◆ POST_LANDING_TIME

const int POST_LANDING_TIME = 200

Definition at line 10 of file crossBarrierDetection.cpp.

Referenced by crossBarrier().