1516X Push Back 1.0
1516X's robot code for the 2025-2026 VEX Robotics Competition
Loading...
Searching...
No Matches
IntakeAntiJam.cpp
Go to the documentation of this file.
1#include "IntakeAntiJam.h"
2#include "pros/rtos.hpp"
3#include "globals.h"
4
5IntakeAntiJam::IntakeAntiJam(pros::Motor& i_motor, pros::Motor& o_motor, pros::Motor& s_motor, int temp)
6 : intakeMotor(i_motor), outtakeMotor(o_motor), storageMotor(s_motor), maxTemp(temp)
7{
8 cmd_intake = 0;
9 cmd_outtake = 0;
10 cmd_storage = 0;
11
12 jam_trigger_loops = 15;
13 reverse_duration_ms = 250;
14
15 jam_counter = 0;
16 is_unjamming = false;
17 anti_jam_enabled = true;
18}
19
20void IntakeAntiJam::enable_anti_jam(bool enable) {
21 anti_jam_enabled = enable;
22
23 if (!anti_jam_enabled) {
24 jam_counter = 0;
25 }
26}
27
28void IntakeAntiJam::set_velocities(float i_vel, float o_vel, float s_vel) {
29 cmd_intake = i_vel;
30 cmd_outtake = o_vel;
31 cmd_storage = s_vel;
32
33 if (!is_unjamming) {
34 intakeMotor.move_velocity(cmd_intake);
35 outtakeMotor.move_velocity(cmd_outtake);
36 storageMotor.move_velocity(cmd_storage);
37 }
38}
39
40void IntakeAntiJam::stop() {
41 set_velocities(0, 0, 0);
42 if (!is_unjamming) {
43 intakeMotor.brake();
44 outtakeMotor.brake();
45 storageMotor.brake();
46 }
47}
48
49void IntakeAntiJam::update() {
50 double max_t = std::max({intakeMotor.get_temperature(),
51 outtakeMotor.get_temperature(),
52 storageMotor.get_temperature()});
53 if (max_t > maxTemp) {
54 intakeMotor.move_velocity(0);
55 outtakeMotor.move_velocity(0);
56 storageMotor.move_velocity(0);
57 return;
58 }
59
60 if (!anti_jam_enabled || matchloader.is_extended()) {
61 jam_counter = 0;
62 return;
63 }
64
65 bool is_stuck = false;
66
67 if (std::abs(cmd_intake) > 0) {
68 std::int32_t intake_eff = intakeMotor.get_efficiency();
69 if (intake_eff >= 7 && intake_eff <= 20) {
70 is_stuck = true;
71 }
72 }
73
74 if (std::abs(cmd_storage) > 0) {
75 std::int32_t storage_eff = storageMotor.get_efficiency();
76 if (storage_eff >= 7 && storage_eff <= 20) {
77 is_stuck = true;
78 }
79 }
80
81 if (is_stuck) {
82 jam_counter++;
83 } else {
84 jam_counter = 0;
85 }
86
87 if (jam_counter > jam_trigger_loops) {
88 is_unjamming = true;
89
90 intakeMotor.move_velocity(0);
91 outtakeMotor.move_velocity(-cmd_outtake);
92 storageMotor.move_velocity(-cmd_storage);
93
94 pros::delay(reverse_duration_ms);
95
96 intakeMotor.move_velocity(cmd_intake);
97 outtakeMotor.move_velocity(cmd_outtake);
98 storageMotor.move_velocity(cmd_storage);
99
100 jam_counter = 0;
101 is_unjamming = false;
102
103 pros::delay(150);
104 }
105}
pros::adi::Pneumatics matchloader('C', false)
pros::Motor storageMotor(2, pros::v5::MotorGears::blue)
pros::Motor outtakeMotor(3, pros::MotorGears::blue)
pros::Motor intakeMotor(-1, pros::v5::MotorGears::blue)