Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 1 | #include <unistd.h> |
| 2 | |
| 3 | #include <cmath> |
| 4 | #include <cstdio> |
| 5 | #include <cstring> |
| 6 | |
| 7 | #include "aos/actions/actions.h" |
| 8 | #include "aos/init.h" |
| 9 | #include "aos/logging/logging.h" |
| 10 | #include "aos/network/team_number.h" |
| 11 | #include "aos/util/log_interval.h" |
| 12 | #include "frc971/autonomous/base_autonomous_actor.h" |
| 13 | #include "frc971/control_loops/drivetrain/localizer_generated.h" |
| 14 | #include "frc971/control_loops/profiled_subsystem_generated.h" |
| 15 | #include "frc971/input/action_joystick_input.h" |
| 16 | #include "frc971/input/driver_station_data.h" |
| 17 | #include "frc971/input/drivetrain_input.h" |
| 18 | #include "frc971/input/joystick_input.h" |
| 19 | #include "frc971/zeroing/wrap.h" |
| 20 | #include "y2023/constants.h" |
| 21 | #include "y2023/control_loops/drivetrain/drivetrain_base.h" |
James Kuszmaul | 4e17143 | 2023-02-26 13:39:37 -0800 | [diff] [blame] | 22 | #include "y2023/control_loops/drivetrain/target_selector_hint_generated.h" |
milind-u | defab71 | 2023-02-20 22:22:02 -0800 | [diff] [blame] | 23 | #include "y2023/control_loops/superstructure/arm/generated_graph.h" |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 24 | #include "y2023/control_loops/superstructure/superstructure_goal_generated.h" |
| 25 | #include "y2023/control_loops/superstructure/superstructure_status_generated.h" |
| 26 | |
| 27 | using frc971::CreateProfileParameters; |
| 28 | using frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal; |
| 29 | using frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal; |
| 30 | using frc971::input::driver_station::ButtonLocation; |
| 31 | using frc971::input::driver_station::ControlBit; |
| 32 | using frc971::input::driver_station::JoystickAxis; |
| 33 | using frc971::input::driver_station::POVLocation; |
James Kuszmaul | 4e17143 | 2023-02-26 13:39:37 -0800 | [diff] [blame] | 34 | using y2023::control_loops::drivetrain::GridSelectionHint; |
James Kuszmaul | 055fe76 | 2023-03-03 21:14:01 -0800 | [diff] [blame] | 35 | using y2023::control_loops::drivetrain::RowSelectionHint; |
James Kuszmaul | 4e17143 | 2023-02-26 13:39:37 -0800 | [diff] [blame] | 36 | using y2023::control_loops::drivetrain::SpotSelectionHint; |
| 37 | using y2023::control_loops::drivetrain::TargetSelectorHint; |
James Kuszmaul | 055fe76 | 2023-03-03 21:14:01 -0800 | [diff] [blame] | 38 | using y2023::control_loops::superstructure::RollerGoal; |
| 39 | using Side = frc971::control_loops::drivetrain::RobotSide; |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 40 | |
| 41 | namespace y2023 { |
| 42 | namespace input { |
| 43 | namespace joysticks { |
| 44 | |
milind-u | defab71 | 2023-02-20 22:22:02 -0800 | [diff] [blame] | 45 | // TODO(milind): add correct locations |
Austin Schuh | 8e6f287 | 2023-03-08 20:55:27 -0800 | [diff] [blame] | 46 | const ButtonLocation kDriverSpit(2, 1); |
Austin Schuh | 23a9002 | 2023-02-24 22:13:39 -0800 | [diff] [blame] | 47 | const ButtonLocation kSpit(4, 13); |
| 48 | |
Austin Schuh | 9b3e41c | 2023-02-26 22:29:53 -0800 | [diff] [blame] | 49 | const ButtonLocation kHighConeScoreLeft(4, 14); |
| 50 | const ButtonLocation kHighConeScoreRight(3, 1); |
| 51 | |
| 52 | const ButtonLocation kMidConeScoreLeft(4, 15); |
| 53 | const ButtonLocation kMidConeScoreRight(3, 2); |
| 54 | |
Austin Schuh | e062be0 | 2023-03-04 21:12:07 -0800 | [diff] [blame] | 55 | const ButtonLocation kLowConeScoreLeft(4, 16); |
| 56 | const ButtonLocation kLowConeScoreRight(3, 3); |
| 57 | |
Austin Schuh | 9b3e41c | 2023-02-26 22:29:53 -0800 | [diff] [blame] | 58 | const ButtonLocation kHighCube(4, 1); |
| 59 | const ButtonLocation kMidCube(4, 2); |
| 60 | const ButtonLocation kLowCube(4, 3); |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 61 | |
| 62 | const ButtonLocation kGroundPickupConeUp(4, 7); |
Austin Schuh | e062be0 | 2023-03-04 21:12:07 -0800 | [diff] [blame] | 63 | const ButtonLocation kGroundPickupConeDownBase(4, 8); |
milind-u | 71da539 | 2023-02-26 12:45:00 -0800 | [diff] [blame] | 64 | const ButtonLocation kGroundPickupCube(4, 10); |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 65 | const ButtonLocation kHPConePickup(4, 6); |
| 66 | |
Austin Schuh | 9b3e41c | 2023-02-26 22:29:53 -0800 | [diff] [blame] | 67 | const ButtonLocation kSuck(4, 11); |
| 68 | const ButtonLocation kBack(4, 12); |
milind-u | defab71 | 2023-02-20 22:22:02 -0800 | [diff] [blame] | 69 | |
Austin Schuh | 6dc925b | 2023-02-24 16:23:32 -0800 | [diff] [blame] | 70 | const ButtonLocation kWrist(4, 10); |
Austin Schuh | 038b545 | 2023-03-08 20:55:45 -0800 | [diff] [blame^] | 71 | const ButtonLocation kStayIn(3, 4); |
Austin Schuh | 6dc925b | 2023-02-24 16:23:32 -0800 | [diff] [blame] | 72 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 73 | namespace superstructure = y2023::control_loops::superstructure; |
milind-u | defab71 | 2023-02-20 22:22:02 -0800 | [diff] [blame] | 74 | namespace arm = superstructure::arm; |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 75 | |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 76 | enum class GamePiece { |
| 77 | CONE_UP = 0, |
| 78 | CONE_DOWN = 1, |
| 79 | CUBE = 2, |
| 80 | }; |
| 81 | |
James Kuszmaul | 4ac4576 | 2023-03-04 19:10:55 -0800 | [diff] [blame] | 82 | struct ButtonData { |
| 83 | ButtonLocation button; |
| 84 | std::optional<SpotSelectionHint> spot = std::nullopt; |
| 85 | }; |
| 86 | |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 87 | struct ArmSetpoint { |
| 88 | uint32_t index; |
| 89 | double wrist_goal; |
| 90 | std::optional<double> score_wrist_goal = std::nullopt; |
| 91 | GamePiece game_piece; |
James Kuszmaul | 4ac4576 | 2023-03-04 19:10:55 -0800 | [diff] [blame] | 92 | std::vector<ButtonData> buttons; |
Austin Schuh | 9b3e41c | 2023-02-26 22:29:53 -0800 | [diff] [blame] | 93 | Side side; |
James Kuszmaul | 4e17143 | 2023-02-26 13:39:37 -0800 | [diff] [blame] | 94 | std::optional<RowSelectionHint> row_hint = std::nullopt; |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | const std::vector<ArmSetpoint> setpoints = { |
| 98 | { |
| 99 | .index = arm::GroundPickupBackConeUpIndex(), |
| 100 | .wrist_goal = 0.0, |
| 101 | .game_piece = GamePiece::CONE_UP, |
James Kuszmaul | 4ac4576 | 2023-03-04 19:10:55 -0800 | [diff] [blame] | 102 | .buttons = {{kGroundPickupConeUp}}, |
Austin Schuh | 9b3e41c | 2023-02-26 22:29:53 -0800 | [diff] [blame] | 103 | .side = Side::BACK, |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 104 | }, |
| 105 | { |
Austin Schuh | e062be0 | 2023-03-04 21:12:07 -0800 | [diff] [blame] | 106 | .index = arm::GroundPickupFrontConeUpIndex(), |
| 107 | .wrist_goal = 0.2, |
| 108 | .game_piece = GamePiece::CONE_UP, |
James Kuszmaul | 4ac4576 | 2023-03-04 19:10:55 -0800 | [diff] [blame] | 109 | .buttons = {{kGroundPickupConeUp}}, |
Austin Schuh | e062be0 | 2023-03-04 21:12:07 -0800 | [diff] [blame] | 110 | .side = Side::FRONT, |
| 111 | }, |
| 112 | { |
| 113 | .index = arm::GroundPickupBackConeDownBaseIndex(), |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 114 | .wrist_goal = 0.0, |
| 115 | .game_piece = GamePiece::CONE_DOWN, |
James Kuszmaul | 4ac4576 | 2023-03-04 19:10:55 -0800 | [diff] [blame] | 116 | .buttons = {{kGroundPickupConeDownBase}}, |
Austin Schuh | 9b3e41c | 2023-02-26 22:29:53 -0800 | [diff] [blame] | 117 | .side = Side::BACK, |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 118 | }, |
| 119 | { |
Austin Schuh | e062be0 | 2023-03-04 21:12:07 -0800 | [diff] [blame] | 120 | .index = arm::GroundPickupFrontConeDownBaseIndex(), |
| 121 | .wrist_goal = 0.2, |
| 122 | .game_piece = GamePiece::CONE_DOWN, |
James Kuszmaul | 4ac4576 | 2023-03-04 19:10:55 -0800 | [diff] [blame] | 123 | .buttons = {{kGroundPickupConeDownBase}}, |
Austin Schuh | e062be0 | 2023-03-04 21:12:07 -0800 | [diff] [blame] | 124 | .side = Side::FRONT, |
| 125 | }, |
| 126 | { |
| 127 | .index = arm::ScoreBackMidConeUpIndex(), |
| 128 | .wrist_goal = 0.0, |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 129 | .game_piece = GamePiece::CONE_UP, |
James Kuszmaul | 4ac4576 | 2023-03-04 19:10:55 -0800 | [diff] [blame] | 130 | .buttons = {{kMidConeScoreRight, SpotSelectionHint::RIGHT}, |
| 131 | {kMidConeScoreLeft, SpotSelectionHint::LEFT}}, |
Austin Schuh | 9b3e41c | 2023-02-26 22:29:53 -0800 | [diff] [blame] | 132 | .side = Side::BACK, |
James Kuszmaul | 4e17143 | 2023-02-26 13:39:37 -0800 | [diff] [blame] | 133 | .row_hint = RowSelectionHint::MIDDLE, |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 134 | }, |
| 135 | { |
Austin Schuh | e062be0 | 2023-03-04 21:12:07 -0800 | [diff] [blame] | 136 | .index = arm::ScoreBackLowConeUpIndex(), |
| 137 | .wrist_goal = 0.0, |
| 138 | .game_piece = GamePiece::CONE_UP, |
James Kuszmaul | 4ac4576 | 2023-03-04 19:10:55 -0800 | [diff] [blame] | 139 | .buttons = {{kLowConeScoreLeft, SpotSelectionHint::LEFT}, |
| 140 | {kLowConeScoreRight, SpotSelectionHint::RIGHT}}, |
Austin Schuh | e062be0 | 2023-03-04 21:12:07 -0800 | [diff] [blame] | 141 | .side = Side::BACK, |
| 142 | .row_hint = RowSelectionHint::BOTTOM, |
Austin Schuh | e062be0 | 2023-03-04 21:12:07 -0800 | [diff] [blame] | 143 | }, |
| 144 | { |
| 145 | .index = arm::ScoreFrontLowConeUpIndex(), |
| 146 | .wrist_goal = 0.0, |
| 147 | .game_piece = GamePiece::CONE_UP, |
James Kuszmaul | 4ac4576 | 2023-03-04 19:10:55 -0800 | [diff] [blame] | 148 | .buttons = {{kLowConeScoreLeft, SpotSelectionHint::LEFT}, |
| 149 | {kLowConeScoreRight, SpotSelectionHint::RIGHT}}, |
Austin Schuh | e062be0 | 2023-03-04 21:12:07 -0800 | [diff] [blame] | 150 | .side = Side::FRONT, |
| 151 | .row_hint = RowSelectionHint::BOTTOM, |
Austin Schuh | e062be0 | 2023-03-04 21:12:07 -0800 | [diff] [blame] | 152 | }, |
| 153 | { |
| 154 | .index = arm::ScoreBackMidConeDownBaseIndex(), |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 155 | .wrist_goal = 2.2, |
| 156 | .score_wrist_goal = 0.0, |
| 157 | .game_piece = GamePiece::CONE_DOWN, |
James Kuszmaul | 4ac4576 | 2023-03-04 19:10:55 -0800 | [diff] [blame] | 158 | .buttons = {{kMidConeScoreLeft, SpotSelectionHint::LEFT}, |
| 159 | {kMidConeScoreRight, SpotSelectionHint::RIGHT}}, |
Austin Schuh | 9b3e41c | 2023-02-26 22:29:53 -0800 | [diff] [blame] | 160 | .side = Side::BACK, |
James Kuszmaul | 4e17143 | 2023-02-26 13:39:37 -0800 | [diff] [blame] | 161 | .row_hint = RowSelectionHint::MIDDLE, |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 162 | }, |
| 163 | { |
Austin Schuh | e062be0 | 2023-03-04 21:12:07 -0800 | [diff] [blame] | 164 | .index = arm::ScoreBackLowConeDownBaseIndex(), |
| 165 | .wrist_goal = 0.0, |
| 166 | .score_wrist_goal = 0.0, |
| 167 | .game_piece = GamePiece::CONE_DOWN, |
James Kuszmaul | 4ac4576 | 2023-03-04 19:10:55 -0800 | [diff] [blame] | 168 | .buttons = {{kLowConeScoreLeft, SpotSelectionHint::LEFT}, |
| 169 | {kLowConeScoreRight, SpotSelectionHint::RIGHT}}, |
Austin Schuh | e062be0 | 2023-03-04 21:12:07 -0800 | [diff] [blame] | 170 | .side = Side::BACK, |
| 171 | .row_hint = RowSelectionHint::BOTTOM, |
Austin Schuh | e062be0 | 2023-03-04 21:12:07 -0800 | [diff] [blame] | 172 | }, |
| 173 | { |
| 174 | .index = arm::ScoreFrontLowConeDownBaseIndex(), |
| 175 | .wrist_goal = 0.0, |
| 176 | .game_piece = GamePiece::CONE_DOWN, |
James Kuszmaul | 4ac4576 | 2023-03-04 19:10:55 -0800 | [diff] [blame] | 177 | .buttons = {{kLowConeScoreLeft, SpotSelectionHint::LEFT}, |
| 178 | {kLowConeScoreRight, SpotSelectionHint::RIGHT}}, |
Austin Schuh | e062be0 | 2023-03-04 21:12:07 -0800 | [diff] [blame] | 179 | .side = Side::FRONT, |
| 180 | .row_hint = RowSelectionHint::BOTTOM, |
Austin Schuh | e062be0 | 2023-03-04 21:12:07 -0800 | [diff] [blame] | 181 | }, |
| 182 | { |
| 183 | .index = arm::ScoreFrontMidConeDownBaseIndex(), |
| 184 | .wrist_goal = 2.0, |
| 185 | .score_wrist_goal = 0.0, |
| 186 | .game_piece = GamePiece::CONE_DOWN, |
James Kuszmaul | 4ac4576 | 2023-03-04 19:10:55 -0800 | [diff] [blame] | 187 | .buttons = {{kMidConeScoreLeft, SpotSelectionHint::LEFT}, |
| 188 | {kMidConeScoreRight, SpotSelectionHint::RIGHT}}, |
Austin Schuh | e062be0 | 2023-03-04 21:12:07 -0800 | [diff] [blame] | 189 | .side = Side::FRONT, |
| 190 | .row_hint = RowSelectionHint::MIDDLE, |
Austin Schuh | e062be0 | 2023-03-04 21:12:07 -0800 | [diff] [blame] | 191 | }, |
| 192 | { |
| 193 | .index = arm::ScoreFrontHighConeDownBaseIndex(), |
| 194 | .wrist_goal = 2.0, |
| 195 | .score_wrist_goal = 0.0, |
| 196 | .game_piece = GamePiece::CONE_DOWN, |
James Kuszmaul | 4ac4576 | 2023-03-04 19:10:55 -0800 | [diff] [blame] | 197 | .buttons = {{kHighConeScoreLeft, SpotSelectionHint::LEFT}, |
| 198 | {kHighConeScoreRight, SpotSelectionHint::RIGHT}}, |
Austin Schuh | e062be0 | 2023-03-04 21:12:07 -0800 | [diff] [blame] | 199 | .side = Side::FRONT, |
| 200 | .row_hint = RowSelectionHint::TOP, |
Austin Schuh | e062be0 | 2023-03-04 21:12:07 -0800 | [diff] [blame] | 201 | }, |
| 202 | { |
| 203 | .index = arm::HPPickupFrontConeUpIndex(), |
| 204 | .wrist_goal = 0.0, |
| 205 | .game_piece = GamePiece::CONE_UP, |
James Kuszmaul | 4ac4576 | 2023-03-04 19:10:55 -0800 | [diff] [blame] | 206 | .buttons = {{kHPConePickup}}, |
Austin Schuh | e062be0 | 2023-03-04 21:12:07 -0800 | [diff] [blame] | 207 | .side = Side::FRONT, |
| 208 | }, |
| 209 | { |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 210 | .index = arm::HPPickupBackConeUpIndex(), |
Austin Schuh | e062be0 | 2023-03-04 21:12:07 -0800 | [diff] [blame] | 211 | .wrist_goal = 0.4, |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 212 | .game_piece = GamePiece::CONE_UP, |
James Kuszmaul | 4ac4576 | 2023-03-04 19:10:55 -0800 | [diff] [blame] | 213 | .buttons = {{kHPConePickup}}, |
Austin Schuh | 9b3e41c | 2023-02-26 22:29:53 -0800 | [diff] [blame] | 214 | .side = Side::BACK, |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 215 | }, |
| 216 | { |
Austin Schuh | e062be0 | 2023-03-04 21:12:07 -0800 | [diff] [blame] | 217 | .index = arm::ScoreFrontHighConeUpIndex(), |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 218 | .wrist_goal = 0.05, |
| 219 | .game_piece = GamePiece::CONE_UP, |
James Kuszmaul | 4ac4576 | 2023-03-04 19:10:55 -0800 | [diff] [blame] | 220 | .buttons = {{kHighConeScoreLeft, SpotSelectionHint::LEFT}, |
| 221 | {kHighConeScoreRight, SpotSelectionHint::RIGHT}}, |
Austin Schuh | 9b3e41c | 2023-02-26 22:29:53 -0800 | [diff] [blame] | 222 | .side = Side::FRONT, |
James Kuszmaul | 4e17143 | 2023-02-26 13:39:37 -0800 | [diff] [blame] | 223 | .row_hint = RowSelectionHint::TOP, |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 224 | }, |
| 225 | { |
Austin Schuh | e062be0 | 2023-03-04 21:12:07 -0800 | [diff] [blame] | 226 | .index = arm::ScoreFrontMidConeUpIndex(), |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 227 | .wrist_goal = 0.05, |
| 228 | .game_piece = GamePiece::CONE_UP, |
James Kuszmaul | 4ac4576 | 2023-03-04 19:10:55 -0800 | [diff] [blame] | 229 | .buttons = {{kMidConeScoreLeft, SpotSelectionHint::LEFT}, |
| 230 | {kMidConeScoreRight, SpotSelectionHint::RIGHT}}, |
Austin Schuh | 9b3e41c | 2023-02-26 22:29:53 -0800 | [diff] [blame] | 231 | .side = Side::FRONT, |
James Kuszmaul | 4e17143 | 2023-02-26 13:39:37 -0800 | [diff] [blame] | 232 | .row_hint = RowSelectionHint::MIDDLE, |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 233 | }, |
milind-u | 68842e1 | 2023-02-26 12:45:40 -0800 | [diff] [blame] | 234 | { |
| 235 | .index = arm::GroundPickupBackCubeIndex(), |
| 236 | .wrist_goal = 0.6, |
| 237 | .game_piece = GamePiece::CUBE, |
James Kuszmaul | 4ac4576 | 2023-03-04 19:10:55 -0800 | [diff] [blame] | 238 | .buttons = {{kGroundPickupCube}}, |
Austin Schuh | 9b3e41c | 2023-02-26 22:29:53 -0800 | [diff] [blame] | 239 | .side = Side::BACK, |
| 240 | }, |
| 241 | { |
| 242 | .index = arm::ScoreFrontMidCubeIndex(), |
| 243 | .wrist_goal = 0.6, |
| 244 | .game_piece = GamePiece::CUBE, |
James Kuszmaul | 4ac4576 | 2023-03-04 19:10:55 -0800 | [diff] [blame] | 245 | .buttons = {{kMidCube, SpotSelectionHint::MIDDLE}}, |
Austin Schuh | 9b3e41c | 2023-02-26 22:29:53 -0800 | [diff] [blame] | 246 | .side = Side::FRONT, |
James Kuszmaul | 4e17143 | 2023-02-26 13:39:37 -0800 | [diff] [blame] | 247 | .row_hint = RowSelectionHint::MIDDLE, |
Austin Schuh | 9b3e41c | 2023-02-26 22:29:53 -0800 | [diff] [blame] | 248 | }, |
| 249 | { |
| 250 | .index = arm::ScoreBackMidCubeIndex(), |
| 251 | .wrist_goal = 0.6, |
| 252 | .score_wrist_goal = 0.0, |
| 253 | .game_piece = GamePiece::CUBE, |
James Kuszmaul | 4ac4576 | 2023-03-04 19:10:55 -0800 | [diff] [blame] | 254 | .buttons = {{kMidCube, SpotSelectionHint::MIDDLE}}, |
Austin Schuh | 9b3e41c | 2023-02-26 22:29:53 -0800 | [diff] [blame] | 255 | .side = Side::BACK, |
James Kuszmaul | 4e17143 | 2023-02-26 13:39:37 -0800 | [diff] [blame] | 256 | .row_hint = RowSelectionHint::MIDDLE, |
Austin Schuh | 9b3e41c | 2023-02-26 22:29:53 -0800 | [diff] [blame] | 257 | }, |
| 258 | { |
| 259 | .index = arm::ScoreFrontLowCubeIndex(), |
| 260 | .wrist_goal = 0.6, |
| 261 | .game_piece = GamePiece::CUBE, |
James Kuszmaul | 4ac4576 | 2023-03-04 19:10:55 -0800 | [diff] [blame] | 262 | .buttons = {{kLowCube, SpotSelectionHint::MIDDLE}}, |
Austin Schuh | 9b3e41c | 2023-02-26 22:29:53 -0800 | [diff] [blame] | 263 | .side = Side::FRONT, |
James Kuszmaul | 4e17143 | 2023-02-26 13:39:37 -0800 | [diff] [blame] | 264 | .row_hint = RowSelectionHint::BOTTOM, |
Austin Schuh | 9b3e41c | 2023-02-26 22:29:53 -0800 | [diff] [blame] | 265 | }, |
| 266 | { |
| 267 | .index = arm::ScoreBackLowCubeIndex(), |
| 268 | .wrist_goal = 0.6, |
| 269 | .game_piece = GamePiece::CUBE, |
James Kuszmaul | 4ac4576 | 2023-03-04 19:10:55 -0800 | [diff] [blame] | 270 | .buttons = {{kLowCube, SpotSelectionHint::MIDDLE}}, |
Austin Schuh | 9b3e41c | 2023-02-26 22:29:53 -0800 | [diff] [blame] | 271 | .side = Side::BACK, |
James Kuszmaul | 4e17143 | 2023-02-26 13:39:37 -0800 | [diff] [blame] | 272 | .row_hint = RowSelectionHint::BOTTOM, |
Austin Schuh | 9b3e41c | 2023-02-26 22:29:53 -0800 | [diff] [blame] | 273 | }, |
| 274 | { |
| 275 | .index = arm::ScoreFrontHighCubeIndex(), |
| 276 | .wrist_goal = 0.6, |
| 277 | .game_piece = GamePiece::CUBE, |
James Kuszmaul | 4ac4576 | 2023-03-04 19:10:55 -0800 | [diff] [blame] | 278 | .buttons = {{kHighCube, SpotSelectionHint::MIDDLE}}, |
Austin Schuh | 9b3e41c | 2023-02-26 22:29:53 -0800 | [diff] [blame] | 279 | .side = Side::FRONT, |
James Kuszmaul | 4e17143 | 2023-02-26 13:39:37 -0800 | [diff] [blame] | 280 | .row_hint = RowSelectionHint::TOP, |
Austin Schuh | 9b3e41c | 2023-02-26 22:29:53 -0800 | [diff] [blame] | 281 | }, |
| 282 | { |
| 283 | .index = arm::ScoreBackHighCubeIndex(), |
| 284 | .wrist_goal = 0.6, |
| 285 | .score_wrist_goal = 0.0, |
| 286 | .game_piece = GamePiece::CUBE, |
James Kuszmaul | 4ac4576 | 2023-03-04 19:10:55 -0800 | [diff] [blame] | 287 | .buttons = {{kHighCube, SpotSelectionHint::MIDDLE}}, |
Austin Schuh | 9b3e41c | 2023-02-26 22:29:53 -0800 | [diff] [blame] | 288 | .side = Side::BACK, |
James Kuszmaul | 4e17143 | 2023-02-26 13:39:37 -0800 | [diff] [blame] | 289 | .row_hint = RowSelectionHint::TOP, |
Austin Schuh | 9b3e41c | 2023-02-26 22:29:53 -0800 | [diff] [blame] | 290 | }, |
| 291 | { |
| 292 | .index = arm::GroundPickupFrontCubeIndex(), |
| 293 | .wrist_goal = 0.6, |
| 294 | .game_piece = GamePiece::CUBE, |
James Kuszmaul | 4ac4576 | 2023-03-04 19:10:55 -0800 | [diff] [blame] | 295 | .buttons = {{kGroundPickupCube}}, |
Austin Schuh | 9b3e41c | 2023-02-26 22:29:53 -0800 | [diff] [blame] | 296 | .side = Side::FRONT, |
milind-u | 68842e1 | 2023-02-26 12:45:40 -0800 | [diff] [blame] | 297 | }, |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 298 | }; |
| 299 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 300 | class Reader : public ::frc971::input::ActionJoystickInput { |
| 301 | public: |
| 302 | Reader(::aos::EventLoop *event_loop) |
| 303 | : ::frc971::input::ActionJoystickInput( |
| 304 | event_loop, |
| 305 | ::y2023::control_loops::drivetrain::GetDrivetrainConfig(), |
| 306 | ::frc971::input::DrivetrainInputReader::InputType::kPistol, {}), |
| 307 | superstructure_goal_sender_( |
| 308 | event_loop->MakeSender<superstructure::Goal>("/superstructure")), |
James Kuszmaul | 4e17143 | 2023-02-26 13:39:37 -0800 | [diff] [blame] | 309 | target_selector_hint_sender_( |
| 310 | event_loop->MakeSender<TargetSelectorHint>("/drivetrain")), |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 311 | superstructure_status_fetcher_( |
| 312 | event_loop->MakeFetcher<superstructure::Status>( |
| 313 | "/superstructure")) {} |
| 314 | |
| 315 | void AutoEnded() override { AOS_LOG(INFO, "Auto ended.\n"); } |
| 316 | |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 317 | GamePiece current_game_piece_ = GamePiece::CONE_UP; |
| 318 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 319 | void HandleTeleop( |
| 320 | const ::frc971::input::driver_station::Data &data) override { |
| 321 | superstructure_status_fetcher_.Fetch(); |
| 322 | if (!superstructure_status_fetcher_.get()) { |
| 323 | AOS_LOG(ERROR, "Got no superstructure status message.\n"); |
| 324 | return; |
| 325 | } |
| 326 | |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 327 | if (!superstructure_status_fetcher_->has_wrist()) { |
| 328 | AOS_LOG(ERROR, "Got no superstructure status message.\n"); |
| 329 | return; |
| 330 | } |
milind-u | defab71 | 2023-02-20 22:22:02 -0800 | [diff] [blame] | 331 | |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 332 | double wrist_goal = 0.0; |
| 333 | RollerGoal roller_goal = RollerGoal::IDLE; |
Austin Schuh | 9a11ebd | 2023-02-26 14:16:31 -0800 | [diff] [blame] | 334 | arm_goal_position_ = arm::NeutralIndex(); |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 335 | std::optional<double> score_wrist_goal = std::nullopt; |
| 336 | |
| 337 | if (data.IsPressed(kGroundPickupConeUp) || data.IsPressed(kHPConePickup)) { |
Maxwell Henderson | bf1bcec | 2023-03-05 18:00:20 -0800 | [diff] [blame] | 338 | roller_goal = RollerGoal::INTAKE_CONE_UP; |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 339 | current_game_piece_ = GamePiece::CONE_UP; |
Austin Schuh | e062be0 | 2023-03-04 21:12:07 -0800 | [diff] [blame] | 340 | } else if (data.IsPressed(kGroundPickupConeDownBase)) { |
Maxwell Henderson | bf1bcec | 2023-03-05 18:00:20 -0800 | [diff] [blame] | 341 | roller_goal = RollerGoal::INTAKE_CONE_DOWN; |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 342 | current_game_piece_ = GamePiece::CONE_DOWN; |
milind-u | 71da539 | 2023-02-26 12:45:00 -0800 | [diff] [blame] | 343 | } else if (data.IsPressed(kGroundPickupCube)) { |
| 344 | roller_goal = RollerGoal::INTAKE_CUBE; |
| 345 | current_game_piece_ = GamePiece::CUBE; |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 346 | } |
| 347 | |
milind-u | 68842e1 | 2023-02-26 12:45:40 -0800 | [diff] [blame] | 348 | if (current_game_piece_ == GamePiece::CUBE) { |
| 349 | wrist_goal = 0.6; |
| 350 | } |
| 351 | |
James Kuszmaul | 4e17143 | 2023-02-26 13:39:37 -0800 | [diff] [blame] | 352 | std::optional<RowSelectionHint> placing_row; |
| 353 | std::optional<SpotSelectionHint> placing_spot; |
Austin Schuh | 9b3e41c | 2023-02-26 22:29:53 -0800 | [diff] [blame] | 354 | |
Austin Schuh | e062be0 | 2023-03-04 21:12:07 -0800 | [diff] [blame] | 355 | // Keep the setpoint if the button is still held. This lets us release the |
| 356 | // back button once a side has been selected. |
| 357 | if (current_setpoint_ != nullptr) { |
| 358 | bool found = false; |
James Kuszmaul | 4ac4576 | 2023-03-04 19:10:55 -0800 | [diff] [blame] | 359 | for (const auto &button : current_setpoint_->buttons) { |
| 360 | if (data.IsPressed(button.button)) { |
Austin Schuh | e062be0 | 2023-03-04 21:12:07 -0800 | [diff] [blame] | 361 | found = true; |
James Kuszmaul | 4ac4576 | 2023-03-04 19:10:55 -0800 | [diff] [blame] | 362 | placing_spot = button.spot; |
Austin Schuh | e062be0 | 2023-03-04 21:12:07 -0800 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | if (!found) { |
| 366 | current_setpoint_ = nullptr; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | // Ok, no active setpoint. Search for the right one. |
| 371 | if (current_setpoint_ == nullptr) { |
| 372 | const Side current_side = |
| 373 | data.IsPressed(kBack) ? Side::BACK : Side::FRONT; |
| 374 | // Search for the active setpoint. |
| 375 | for (const ArmSetpoint &setpoint : setpoints) { |
James Kuszmaul | 4ac4576 | 2023-03-04 19:10:55 -0800 | [diff] [blame] | 376 | for (const auto &button : setpoint.buttons) { |
| 377 | if (data.IsPressed(button.button)) { |
Austin Schuh | e062be0 | 2023-03-04 21:12:07 -0800 | [diff] [blame] | 378 | if (setpoint.game_piece == current_game_piece_ && |
| 379 | setpoint.side == current_side) { |
| 380 | current_setpoint_ = &setpoint; |
James Kuszmaul | 4ac4576 | 2023-03-04 19:10:55 -0800 | [diff] [blame] | 381 | placing_spot = button.spot; |
Austin Schuh | e062be0 | 2023-03-04 21:12:07 -0800 | [diff] [blame] | 382 | } |
Austin Schuh | 9b3e41c | 2023-02-26 22:29:53 -0800 | [diff] [blame] | 383 | } |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 384 | } |
| 385 | } |
Austin Schuh | 6dc925b | 2023-02-24 16:23:32 -0800 | [diff] [blame] | 386 | } |
Austin Schuh | e062be0 | 2023-03-04 21:12:07 -0800 | [diff] [blame] | 387 | |
| 388 | // And, pull the bits out of it. |
| 389 | if (current_setpoint_ != nullptr) { |
Austin Schuh | 038b545 | 2023-03-08 20:55:45 -0800 | [diff] [blame^] | 390 | if (!data.IsPressed(kStayIn)) { |
| 391 | wrist_goal = current_setpoint_->wrist_goal; |
| 392 | arm_goal_position_ = current_setpoint_->index; |
| 393 | score_wrist_goal = current_setpoint_->score_wrist_goal; |
| 394 | } |
| 395 | |
Austin Schuh | e062be0 | 2023-03-04 21:12:07 -0800 | [diff] [blame] | 396 | placing_row = current_setpoint_->row_hint; |
Austin Schuh | e062be0 | 2023-03-04 21:12:07 -0800 | [diff] [blame] | 397 | } |
| 398 | |
James Kuszmaul | 4e17143 | 2023-02-26 13:39:37 -0800 | [diff] [blame] | 399 | CHECK_EQ(placing_row.has_value(), placing_spot.has_value()); |
Austin Schuh | 6dc925b | 2023-02-24 16:23:32 -0800 | [diff] [blame] | 400 | |
Austin Schuh | 23a9002 | 2023-02-24 22:13:39 -0800 | [diff] [blame] | 401 | if (data.IsPressed(kSuck)) { |
milind-u | 71da539 | 2023-02-26 12:45:00 -0800 | [diff] [blame] | 402 | roller_goal = RollerGoal::INTAKE_LAST; |
Austin Schuh | 8e6f287 | 2023-03-08 20:55:27 -0800 | [diff] [blame] | 403 | } else if (data.IsPressed(kSpit) || data.IsPressed(kDriverSpit)) { |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 404 | if (score_wrist_goal.has_value()) { |
| 405 | wrist_goal = score_wrist_goal.value(); |
Austin Schuh | 23a9002 | 2023-02-24 22:13:39 -0800 | [diff] [blame] | 406 | |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 407 | // If we are supposed to dunk it, wait until we are close enough to |
| 408 | // spit. |
| 409 | if (std::abs(score_wrist_goal.value() - |
| 410 | superstructure_status_fetcher_->wrist()->position()) < |
| 411 | 0.1) { |
| 412 | roller_goal = RollerGoal::SPIT; |
| 413 | } |
| 414 | } else { |
| 415 | roller_goal = RollerGoal::SPIT; |
| 416 | } |
milind-u | defab71 | 2023-02-20 22:22:02 -0800 | [diff] [blame] | 417 | } |
| 418 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 419 | { |
| 420 | auto builder = superstructure_goal_sender_.MakeBuilder(); |
| 421 | |
Austin Schuh | 6dc925b | 2023-02-24 16:23:32 -0800 | [diff] [blame] | 422 | flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal> |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 423 | wrist_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal( |
| 424 | *builder.fbb(), wrist_goal, |
| 425 | CreateProfileParameters(*builder.fbb(), 12.0, 90.0)); |
Austin Schuh | 6dc925b | 2023-02-24 16:23:32 -0800 | [diff] [blame] | 426 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 427 | superstructure::Goal::Builder superstructure_goal_builder = |
| 428 | builder.MakeBuilder<superstructure::Goal>(); |
milind-u | defab71 | 2023-02-20 22:22:02 -0800 | [diff] [blame] | 429 | superstructure_goal_builder.add_arm_goal_position(arm_goal_position_); |
Maxwell Henderson | 5938a83 | 2023-02-23 09:33:15 -0800 | [diff] [blame] | 430 | superstructure_goal_builder.add_roller_goal(roller_goal); |
Austin Schuh | 6dc925b | 2023-02-24 16:23:32 -0800 | [diff] [blame] | 431 | superstructure_goal_builder.add_wrist(wrist_offset); |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 432 | if (builder.Send(superstructure_goal_builder.Finish()) != |
| 433 | aos::RawSender::Error::kOk) { |
| 434 | AOS_LOG(ERROR, "Sending superstructure goal failed.\n"); |
| 435 | } |
| 436 | } |
James Kuszmaul | 4e17143 | 2023-02-26 13:39:37 -0800 | [diff] [blame] | 437 | if (placing_row.has_value()) { |
| 438 | auto builder = target_selector_hint_sender_.MakeBuilder(); |
| 439 | auto hint_builder = builder.MakeBuilder<TargetSelectorHint>(); |
| 440 | hint_builder.add_row(placing_row.value()); |
| 441 | hint_builder.add_spot(placing_spot.value()); |
James Kuszmaul | 055fe76 | 2023-03-03 21:14:01 -0800 | [diff] [blame] | 442 | hint_builder.add_robot_side(CHECK_NOTNULL(current_setpoint_)->side); |
James Kuszmaul | 4e17143 | 2023-02-26 13:39:37 -0800 | [diff] [blame] | 443 | if (builder.Send(hint_builder.Finish()) != aos::RawSender::Error::kOk) { |
| 444 | AOS_LOG(ERROR, "Sending target selector hint failed.\n"); |
| 445 | } |
| 446 | } |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | private: |
| 450 | ::aos::Sender<superstructure::Goal> superstructure_goal_sender_; |
James Kuszmaul | 4e17143 | 2023-02-26 13:39:37 -0800 | [diff] [blame] | 451 | ::aos::Sender<TargetSelectorHint> target_selector_hint_sender_; |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 452 | |
| 453 | ::aos::Fetcher<superstructure::Status> superstructure_status_fetcher_; |
milind-u | defab71 | 2023-02-20 22:22:02 -0800 | [diff] [blame] | 454 | |
| 455 | uint32_t arm_goal_position_; |
Austin Schuh | e062be0 | 2023-03-04 21:12:07 -0800 | [diff] [blame] | 456 | |
| 457 | const ArmSetpoint *current_setpoint_ = nullptr; |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 458 | }; |
| 459 | |
| 460 | } // namespace joysticks |
| 461 | } // namespace input |
| 462 | } // namespace y2023 |
| 463 | |
| 464 | int main(int argc, char **argv) { |
| 465 | ::aos::InitGoogle(&argc, &argv); |
| 466 | |
| 467 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 468 | aos::configuration::ReadConfig("aos_config.json"); |
| 469 | |
| 470 | ::aos::ShmEventLoop event_loop(&config.message()); |
| 471 | ::y2023::input::joysticks::Reader reader(&event_loop); |
| 472 | |
| 473 | event_loop.Run(); |
| 474 | |
| 475 | return 0; |
| 476 | } |