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