1516X Push Back 1.0
1516X's robot code for the 2025-2026 VEX Robotics Competition
Loading...
Searching...
No Matches
main.cpp File Reference
#include "main.h"
#include "globals.h"
#include "liblvgl/core/lv_obj_pos.h"
#include "auton/autonRoutines.h"
#include "robodash/views/image.hpp"
#include "robodash/views/selector.hpp"
#include "colorSort.h"
#include "warnings.h"
#include <string>
#include "MCL.h"
#include "distanceReset.h"
#include "auton/autonFunctions.h"
#include "motorDashboard.h"
#include "IntakeAntiJam.h"

Go to the source code of this file.

Functions

 LV_IMG_DECLARE (callogo)
static void update_alliance_btn (lv_obj_t *btn, Color newColor)
static void alliance_btn_event_handler (lv_event_t *e)
void create_alliance_selector ()
rd::Selector selector ({ {"Right 7 Split", right_auton_split}, {"Right 7 Wing", right_7_wing}, {"Right 7 Hood", right_7_hood}, {"Right Rush", right_rush}, {"Left 7 Block", left_auton_split}, {"Left Rush", left_rush}, {"AWP", awp_auton}, {"Skills", skills_auton}, {"Carry", carry_auton} })
void auton_check (std::optional< rd::Selector::routine_t > auton)
void initialize ()
void disabled ()
void competition_initialize ()
void autonomous ()
float dynamic_steer_curve (float throttle, float steer, float min_turn_scale=0.4, float max_turn_scale=1.5)
void opcontrol ()

Variables

rd::Console console
rd::Image image = rd::Image(&callogo, "logo")

Function Documentation

◆ alliance_btn_event_handler()

void alliance_btn_event_handler ( lv_event_t * e)
static

Definition at line 34 of file main.cpp.

34 {
35 lv_obj_t* btn = lv_event_get_target(e);
36
37 if (allianceColor == RED) {
38 allianceColor = Color::BLUE;
39 } else {
40 allianceColor = Color::RED;
41 }
42
43 lv_async_call([](void* user_data){
44 lv_obj_t* btn = (lv_obj_t*)user_data;
46 }, btn);
47}
Color allianceColor
Definition globals.cpp:103
static void update_alliance_btn(lv_obj_t *btn, Color newColor)
Definition main.cpp:18

References allianceColor, and update_alliance_btn().

Referenced by create_alliance_selector().

◆ auton_check()

void auton_check ( std::optional< rd::Selector::routine_t > auton)

Definition at line 79 of file main.cpp.

80{
81 if(selector.get_auton().value().name == "Skills")
82 {
83 intake_lift.extend();
84 }
85 precompute_auton_paths(selector.get_auton().value().name);
86}
void precompute_auton_paths(std::string path_name)
pros::adi::Pneumatics intake_lift('A', false)
rd::Selector selector({ {"Right 7 Split", right_auton_split}, {"Right 7 Wing", right_7_wing}, {"Right 7 Hood", right_7_hood}, {"Right Rush", right_rush}, {"Left 7 Block", left_auton_split}, {"Left Rush", left_rush}, {"AWP", awp_auton}, {"Skills", skills_auton}, {"Carry", carry_auton} })

References intake_lift(), precompute_auton_paths(), and selector().

Referenced by initialize().

◆ autonomous()

void autonomous ( )

Definition at line 144 of file main.cpp.

144 {
145 //awp_auton();
146 //left_auton_split();
147 skills_auton();
148 //test_auton();
149}
void skills_auton()

References skills_auton().

◆ competition_initialize()

void competition_initialize ( )

Definition at line 141 of file main.cpp.

141 {
142}

◆ create_alliance_selector()

void create_alliance_selector ( )

Definition at line 49 of file main.cpp.

49 {
50 lv_obj_t* btn = lv_btn_create(lv_layer_top());
51 lv_obj_align(btn, LV_ALIGN_BOTTOM_RIGHT, -10, -10);
52 lv_obj_set_size(btn, 80, 40);
53
54 lv_obj_set_style_bg_color(btn, lv_palette_main(LV_PALETTE_RED), 0);
55
56 lv_obj_t* label = lv_label_create(btn);
57 lv_label_set_text(label, "RED");
58 lv_obj_center(label);
59
60 lv_obj_add_event_cb(btn, alliance_btn_event_handler, LV_EVENT_CLICKED, nullptr);
61}
static void alliance_btn_event_handler(lv_event_t *e)
Definition main.cpp:34

References alliance_btn_event_handler().

Referenced by initialize().

◆ disabled()

void disabled ( )

Definition at line 138 of file main.cpp.

138 {
139}

◆ dynamic_steer_curve()

float dynamic_steer_curve ( float throttle,
float steer,
float min_turn_scale = 0.4,
float max_turn_scale = 1.5 )

Definition at line 153 of file main.cpp.

153 {
154 float t = std::abs(throttle) / 127.0f;
155 float s = steer / 127.0f;
156 float curve = t * t;
157 float scale = min_turn_scale + (max_turn_scale - min_turn_scale) * curve;
158 float s_out = s * scale;
159 return s_out * 127.0f;
160}

Referenced by opcontrol().

◆ initialize()

void initialize ( )

Definition at line 88 of file main.cpp.

88 {
89 chassis.calibrate();
90 //temp_warning();
91 selector.focus();
96 pros::Task jamTask(antiJamTask);
97 selector.on_select(auton_check);
98
99
100 //vertical_tracking_sensor.set_data_rate(5);
101 //horizontal_tracking_sensor.set_data_rate(5);
102
103 /*
104 chassis.setPose(-48, -12, 90);
105 distancePose pose = distanceReset(true);
106 double start_x = pose.x;
107 double start_y = pose.y;
108 double start_theta = 90;
109 chassis.setPose(start_x, start_y, start_theta);
110 */
111 chassis.setPose(27.5, -48, 90);
112
113 //MCL::StartMCL(start_x, start_y);
114
115 //pros::Task mcl_task(MCL::MonteCarlo);
116 console.focus();
117 pros::Task screen_task([&]() {
118 while (true) {
119 console.clear();
120
121 lemlib::Pose odomPose = chassis.getPose();
122 //distancePose pose = distanceReset(false);
123 console.printf("X: %.3f Y: %.3f\n", odomPose.x, odomPose.y);
124 console.printf("Theta: %.3f\n\n", odomPose.theta);
125 //console.printf("Collided? %s\n", chassis.detect_collision() ? "YES" : "NO");
126 //console.printf("Color: %s\n", get_color() == Color::RED ? "RED" : "BLUE");
127 //distancePose pose = distanceReset(false, false);
128 //console.printf("DSR X: %.3f DSR Y: %.3f\n", pose.x, pose.y);
129 //console.printf("DSR using Odom X: %s using Odom Y: %s\n", pose.using_odom_x ? "true" : "false", pose.using_odom_y ? "true" : "false");
130
131 //console.printf("X: %.2f Y: %.2f\n", MCL::global_X, MCL::global_Y);
132 //console.printf("Theta: %.2f\n", MCL::global_Theta);
133 pros::delay(50);
134 }
135 });
136}
void antiJamTask()
Chassis chassis(drivebase, lateral_controller, angular_controller, sensors, &throttle_curve, &steer_curve)
void create_alliance_selector()
Definition main.cpp:49
void auton_check(std::optional< rd::Selector::routine_t > auton)
Definition main.cpp:79
rd::Console console
Definition main.cpp:76
void init_motor_dashboard()
void distance_sensor_disconnect_warning()
Definition warnings.cpp:105
void motor_disconnect_warning()
Definition warnings.cpp:60

References antiJamTask(), auton_check(), chassis(), console, create_alliance_selector(), distance_sensor_disconnect_warning(), init_motor_dashboard(), motor_disconnect_warning(), and selector().

◆ LV_IMG_DECLARE()

LV_IMG_DECLARE ( callogo )

References callogo.

◆ opcontrol()

void opcontrol ( )

Definition at line 162 of file main.cpp.

162 {
163 float time = 0;
164 midgoal_first = false;
165 bool trapDoor_commanded = false;
166 bool matchload_on = true;
167 float maxDeltaThrottle = 4;
168 float prevThrottle = 0;
169 float starting_pitch = imu.get_roll();
170 float roll_forward_threshold = 1;
171 float roll_backward_threshold = -1;
172 bool slewOn = false;
173
174 int radio_cooldown = 50;
175 bool needs_clear = false;
176 bool needs_print = false;
177 bool needs_rumble = false;
178
179 leftMotors.set_brake_mode(pros::E_MOTOR_BRAKE_COAST);
180 rightMotors.set_brake_mode(pros::E_MOTOR_BRAKE_COAST);
181
182 enum class ScoringMode {
183 NONE, INTAKE, OUTTAKE, MIDGOAL, LONGGOAL, MANUAL_UP, BRAKE
184 };
185
186 jamManager.enable_anti_jam(antiJamEnabled);
187
188 while (true) {
189
190 float throttle = controller.get_analog(pros::E_CONTROLLER_ANALOG_LEFT_Y);
191 float steer = controller.get_analog(pros::E_CONTROLLER_ANALOG_RIGHT_X);
192
193 if (std::abs(throttle) < 5) throttle = 0;
194 if (std::abs(steer) < 5) steer = 0;
195
196 if(slewOn) {
197 bool isTipping = (imu.get_roll() - starting_pitch > roll_forward_threshold || imu.get_roll() - starting_pitch < roll_backward_threshold);
198 bool isSlowingDown = (std::abs(throttle) < std::abs(prevThrottle));
199 bool isReversing = (prevThrottle > 0 && throttle < 0) || (prevThrottle < 0 && throttle > 0);
200
201 if (isTipping && (isSlowingDown || isReversing)) {
202 throttle = lemlib::slew(throttle, prevThrottle, maxDeltaThrottle);
203 }
204 }
205
206 if (controller.get_digital_new_press(pros::E_CONTROLLER_DIGITAL_X)) {
208 jamManager.enable_anti_jam(antiJamEnabled);
209
210 needs_clear = true;
211 needs_print = true;
212 needs_rumble = true;
213 }
214
215 if (radio_cooldown >= 50) {
216 if (needs_clear) {
217 controller.clear_line(0);
218 needs_clear = false;
219 radio_cooldown = 0;
220 }
221 else if (needs_print) {
222 controller.print(0, 0, "ANTIJAM %s", (antiJamEnabled) ? "ON" : "OFF");
223 needs_print = false;
224 radio_cooldown = 0;
225 }
226 else if (needs_rumble) {
227 controller.rumble(".");
228 needs_rumble = false;
229 radio_cooldown = 0;
230 }
231 }
232
233 if (controller.get_digital_new_press(pros::E_CONTROLLER_DIGITAL_B)) {
234 if(matchloader.is_extended()) {
235 matchloader.retract();
236 } else {
237 matchloader.extend();
238 }
239 }
240
241 ScoringMode scoringMode = ScoringMode::NONE;
242
243 if (controller.get_digital(pros::E_CONTROLLER_DIGITAL_RIGHT)) {
244 mid_descore.extend();
245 scoringMode = ScoringMode::BRAKE;
246 }
247
248 if (controller.get_digital(pros::E_CONTROLLER_DIGITAL_A)) {
249 outtakeMotor.move_velocity(600);
250 scoringMode = ScoringMode::BRAKE;
251 }
252
253 if (controller.get_digital(pros::E_CONTROLLER_DIGITAL_LEFT)) {
254 storageMotor.move_velocity(600);
255 scoringMode = ScoringMode::BRAKE;
256 }
257
258 if (controller.get_digital(pros::E_CONTROLLER_DIGITAL_DOWN)) {
259 descore.retract();
260 scoringMode = ScoringMode::BRAKE;
261 }
262
263 if (controller.get_digital(pros::E_CONTROLLER_DIGITAL_L1)) {
264 scoringMode = ScoringMode::LONGGOAL;
265 } else if (controller.get_digital(pros::E_CONTROLLER_DIGITAL_L2)) {
266 scoringMode = ScoringMode::MIDGOAL;
267 } else if (controller.get_digital(pros::E_CONTROLLER_DIGITAL_UP)) {
268 scoringMode = ScoringMode::MANUAL_UP;
269 } else if (controller.get_digital(pros::E_CONTROLLER_DIGITAL_R2)) {
270 scoringMode = ScoringMode::OUTTAKE;
271 } else if (controller.get_digital(pros::E_CONTROLLER_DIGITAL_R1)) {
272 scoringMode = ScoringMode::INTAKE;
273 }
274
275 switch (scoringMode) {
276 case ScoringMode::INTAKE:
277 intake();
278 break;
279 case ScoringMode::OUTTAKE:
280 outtake();
281 break;
282 case ScoringMode::MIDGOAL:
284 break;
285 case ScoringMode::LONGGOAL:
287 break;
288 case ScoringMode::MANUAL_UP:
289 break;
290 case ScoringMode::NONE:
291 intake_stop();
292 descore.extend();
293 mid_descore.retract();
294 break;
295 case ScoringMode::BRAKE:
296 break;
297 }
298
299 chassis.curvature(throttle,dynamic_steer_curve(throttle, steer, 0.4, 1.5), false);
300 if(liveReplay)
301 {
302 float leftVel = leftMotors.get_actual_velocity() * 0.00324173f;
303 float rightVel = rightMotors.get_actual_velocity() * 0.00324173f;
304 std::cout << time << ", " << (leftVel + rightVel) / 2 << std::endl;
305 }
306 prevThrottle = throttle;
307 radio_cooldown += 10;
308
309 if(liveReplay)
310 {
311 pros::delay(20);
312 time += 0.02;
313 }
314 else {
315 pros::delay(10);
316 }
317 }
318}
void score_longgoal(int power, Color allianceColor)
void intake(int power)
void score_midgoal(int power)
void intake_stop(bool hood_state)
void outtake(int power)
pros::adi::Pneumatics matchloader('C', false)
IntakeAntiJam jamManager(intakeMotor, outtakeMotor, storageMotor, 55)
pros::adi::Pneumatics mid_descore('E', false)
pros::Motor storageMotor(2, pros::v5::MotorGears::blue)
pros::Motor outtakeMotor(3, pros::MotorGears::blue)
pros::Imu imu(16)
pros::adi::Pneumatics descore('D', true)
bool antiJamEnabled
Definition globals.cpp:108
lemlib::Drivetrain drivebase & leftMotors
Definition globals.cpp:17
bool midgoal_first
Definition globals.cpp:105
bool liveReplay
Definition globals.cpp:109
pros::Controller controller(pros::E_CONTROLLER_MASTER)
pros::MotorGroup rightMotors({17, 19, -18}, pros::MotorGears::blue)
float dynamic_steer_curve(float throttle, float steer, float min_turn_scale=0.4, float max_turn_scale=1.5)
Definition main.cpp:153

References allianceColor, antiJamEnabled, chassis(), controller(), descore(), dynamic_steer_curve(), imu(), intake(), intake_stop(), jamManager(), leftMotors, liveReplay, matchloader(), mid_descore(), midgoal_first, outtake(), outtakeMotor(), rightMotors(), score_longgoal(), score_midgoal(), and storageMotor().

◆ selector()

rd::Selector selector ( { {"Right 7 Split", right_auton_split}, {"Right 7 Wing", right_7_wing}, {"Right 7 Hood", right_7_hood}, {"Right Rush", right_rush}, {"Left 7 Block", left_auton_split}, {"Left Rush", left_rush}, {"AWP", awp_auton}, {"Skills", skills_auton}, {"Carry", carry_auton} } )

◆ update_alliance_btn()

void update_alliance_btn ( lv_obj_t * btn,
Color newColor )
static

Definition at line 18 of file main.cpp.

18 {
19 lv_obj_t* label = lv_obj_get_child(btn, 0);
20
21 if (newColor == Color::RED) {
22 lv_obj_set_style_bg_color(btn, lv_palette_main(LV_PALETTE_RED), 0);
23 lv_label_set_text(label, "RED");
24 } else {
25 lv_obj_set_style_bg_color(btn, lv_palette_main(LV_PALETTE_BLUE), 0);
26 lv_label_set_text(label, "BLUE");
27 }
28
29 std::cout << "Alliance switched to: "
30 << (newColor == Color::RED ? "Red" : "Blue")
31 << std::endl;
32}

Referenced by alliance_btn_event_handler().

Variable Documentation

◆ console

rd::Console console

Definition at line 76 of file main.cpp.

Referenced by initialize().

◆ image

rd::Image image = rd::Image(&callogo, "logo")

Definition at line 77 of file main.cpp.