1516X High Stakes 2.0
Codebase for 1516X High Stakes season
Loading...
Searching...
No Matches
ladybrown.cpp
Go to the documentation of this file.
1#include "robot/ladybrown.h"
2#include "globals.h"
3#include "lemlib/pid.hpp"
4#include "lemlib/timer.hpp"
5#include "pros/misc.h"
6#include "pros/motors.h"
7#include "pros/rtos.hpp"
8#include <cstdlib>
9
10using namespace Robot;
11using namespace Robot::Globals;
12
14
15LadyBrown::LadyBrown() : MoveToPointPID(2, 0, 0, 2, false), ActiveBrakePID(0.8, 0, 1, 6, true) {
16 LadyBrownRotation.set_position(0);
17 bool manualControl = false;
18 LadyBrownMotor.set_brake_mode(pros::E_MOTOR_BRAKE_HOLD);
19}
20
21
22void LadyBrown::run(bool async, int timeout) {
23 LADYBROWN_STATE move_to;
24
25 if (!isPIDRunning) {
26
27 if (controller.get_digital(pros::E_CONTROLLER_DIGITAL_R2)) {
29 LadyBrownMotor.set_brake_mode(pros::E_MOTOR_BRAKE_COAST);
30
31 //delay to exit the previous PID inside this control flow
32 pros::delay(50);
33 manualControl = true;
34 LadyBrownMotor.move_velocity(-200);
35
36 if (LadyBrownRotation.get_position() > -10000) {
37 LadyBrown::brake_target = LadyBrownRotation.get_position() - 4500;
38 } else {
39 LadyBrown::brake_target = LadyBrownRotation.get_position() - 2300;
40 }
41
42 }
43 else {
44 if (!manualControl) {
45 LadyBrownMotor.move_velocity(0);
46 } else {
47 LadyBrownMotor.move_voltage(ActiveBrakePID.update(LadyBrown::brake_target - LadyBrownRotation.get_position()));
48 }
49 }
50 }
51
52 if (controller.get_digital_new_press(pros::E_CONTROLLER_DIGITAL_L2)) {
54 LadyBrownMotor.set_brake_mode(pros::E_MOTOR_BRAKE_HOLD);
55 manualControl = false;
56 //delay to exit the previous PID inside this control flow
57 pros::delay(50);
58
60 move_to = LOAD_STATE;
61 } else {
62 move_to = BASE_STATE;
63 }
64
65 std::cout << "Moving to: " << move_to << std::endl;
66 std::cout << "current state: " << current_state << std::endl;
67
68
69 if (!async) {
70 MoveToPoint(move_to);
71 } else {
72 pros::Task move([move_to, this]() { MoveToPoint(move_to); }, "LadyBrownMove");
73 }
74
75 if (!isPIDRunning) {
76 current_state = move_to;
77 }
78 }
79}
80
82
83void LadyBrown::MoveToPoint(LADYBROWN_STATE state, int max_error, int timeout) {
84
85 std::cout << "state: " << current_state << std::endl;
86 constexpr double base_location = 30;
87 constexpr double load_location = -1900;
88 constexpr double attack_location = -15500;
89
90 int target;
91
92 std::cout << "state: " << state << std::endl;
93 std::cout << "pid: " << isPIDRunning << std::endl;
94
95 if (!isPIDRunning) {
96
97 std::cout << "inner pid: " << isPIDRunning << std::endl;
99
100 switch (state) {
102 target = base_location;
103 break;
105 target = load_location;
106 break;
108 target = attack_location;
109 break;
111 target = -12550;
112 break;
113 }
114
115 std::cout << "target: " << target << std::endl;
116
117 MoveToPointPID.reset();
118
119 lemlib::Timer timer(timeout);
120
121 while (LadyBrown::isPIDRunning == true) {
122 double error = target - LadyBrownRotation.get_position();
123 double motor_voltage = MoveToPointPID.update(error);
124
125 // motor_voltage = lemlib::slew(motor_voltage, LadyBrownMotor.get_voltage(), 1500);
126
127 if (std::abs(error) < max_error || timer.isDone()) {
128 LadyBrownMotor.brake();
131 break;
132 }
133
134 LadyBrownMotor.move_voltage(motor_voltage);
136
138 return;
139 }
140
141 pros::delay(20);
142 }
143 }
144}
lemlib::PID ActiveBrakePID
Definition ladybrown.h:25
void run(bool async=true, int timeout=1000)
Definition ladybrown.cpp:22
lemlib::PID MoveToPointPID
Definition ladybrown.h:23
static LADYBROWN_STATE current_state
Definition ladybrown.h:29
void MoveToPoint(LadyBrown::LADYBROWN_STATE state, int max_error=150, int timeout=1000)
Definition ladybrown.cpp:83
pros::Motor LadyBrownMotor(-10, pros::v5::MotorGears::green, pros::v5::MotorUnits::degrees)
pros::Controller controller(pros::E_CONTROLLER_MASTER)
pros::Rotation LadyBrownRotation(9)
Definition auton.h:10