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) {
16 LadyBrownRotation.set_position(0);
17 LadyBrownMotor.set_brake_mode(pros::E_MOTOR_BRAKE_HOLD);
18}
19
20void LadyBrown::run(bool async, int timeout) {
21 LADYBROWN_STATE move_to;
22
23 if (controller.get_digital_new_press(pros::E_CONTROLLER_DIGITAL_R2)) {
25 move_to = LOAD_STATE;
26 } else if (current_state == LOAD_STATE) {
27 move_to = ATTACK_STATE;
28 } else {
29 move_to = BASE_STATE;
30 }
31 std::cout << "Moving to: " << move_to << std::endl;
32 std::cout << "current state: " << current_state << std::endl;
33
34 if (!async) {
35 MoveToPoint(move_to);
36 } else {
37
38 pros::Task move([move_to, this]() { MoveToPoint(move_to); });
39 }
40
41 if (!isPIDRunning) {
42 current_state = move_to;
43 }
44
45 } else if (controller.get_digital_new_press(pros::E_CONTROLLER_DIGITAL_L2)) {
46
48 move_to = LOAD_STATE;
49 } else if (current_state == LOAD_STATE) {
50 move_to = BASE_STATE;
51 } else {
52 return;
53 }
54
55 std::cout << "Moving to: " << move_to << std::endl;
56 std::cout << "current state: " << current_state << std::endl;
57
58 if (!async) {
59 MoveToPoint(move_to);
60 } else {
61 pros::Task move([move_to, this]() { MoveToPoint(move_to); }, "LadyBrownMove");
62 }
63
64 if (!isPIDRunning) {
65 current_state = move_to;
66 }
67 }
68}
69
71
72void LadyBrown::MoveToPoint(LADYBROWN_STATE state, int max_error, int timeout) {
73
74 std::cout << "state: " << current_state << std::endl;
75 constexpr double base_location = 30;
76 constexpr double load_location = -2550;
77 constexpr double attack_location = -15500;
78
79 int target;
80
81 std::cout << "state: " << state << std::endl;
82 std::cout << "pid: " << isPIDRunning << std::endl;
83
84 if (!isPIDRunning) {
85
86 std::cout << "inner pid: " << isPIDRunning << std::endl;
88
89 switch (state) {
91 target = base_location;
92 break;
94 target = load_location;
95 break;
97 target = attack_location;
98 break;
99 }
100
101 std::cout << "target: " << target << std::endl;
102
103 MoveToPointPID.reset();
104
105 lemlib::Timer timer(timeout);
106
107 while (true) {
108 double error = target - LadyBrownRotation.get_position();
109 double motor_voltage = MoveToPointPID.update(error);
110
111 // motor_voltage = lemlib::slew(motor_voltage, LadyBrownMotor.get_voltage(), 1500);
112
113 if (std::abs(error) < max_error || timer.isDone()) {
114 LadyBrownMotor.brake();
117 break;
118 }
119
120 LadyBrownMotor.move_voltage(motor_voltage);
122 pros::delay(20);
123 }
124 }
125}
void run(bool async=true, int timeout=1000)
Definition ladybrown.cpp:20
lemlib::PID MoveToPointPID
Definition ladybrown.h:23
static LADYBROWN_STATE current_state
Definition ladybrown.h:25
void MoveToPoint(LadyBrown::LADYBROWN_STATE state, int max_error=150, int timeout=1000)
Definition ladybrown.cpp:72
pros::Rotation LadyBrownRotation(8)
pros::Controller controller(pros::E_CONTROLLER_MASTER)
pros::Motor LadyBrownMotor(-3, pros::v5::MotorGears::green, pros::v5::MotorUnits::degrees)
Definition auton.h:10