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" |
milind-u | defab71 | 2023-02-20 22:22:02 -0800 | [diff] [blame] | 22 | #include "y2023/control_loops/superstructure/arm/generated_graph.h" |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 23 | #include "y2023/control_loops/superstructure/superstructure_goal_generated.h" |
| 24 | #include "y2023/control_loops/superstructure/superstructure_status_generated.h" |
| 25 | |
| 26 | using frc971::CreateProfileParameters; |
| 27 | using frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal; |
| 28 | using frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal; |
| 29 | using frc971::input::driver_station::ButtonLocation; |
| 30 | using frc971::input::driver_station::ControlBit; |
| 31 | using frc971::input::driver_station::JoystickAxis; |
| 32 | using frc971::input::driver_station::POVLocation; |
Maxwell Henderson | 5938a83 | 2023-02-23 09:33:15 -0800 | [diff] [blame] | 33 | using y2023::control_loops::superstructure::RollerGoal; |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 34 | |
| 35 | namespace y2023 { |
| 36 | namespace input { |
| 37 | namespace joysticks { |
| 38 | |
milind-u | defab71 | 2023-02-20 22:22:02 -0800 | [diff] [blame] | 39 | // TODO(milind): add correct locations |
Austin Schuh | 6dc925b | 2023-02-24 16:23:32 -0800 | [diff] [blame] | 40 | const ButtonLocation kScore(4, 4); |
Austin Schuh | 23a9002 | 2023-02-24 22:13:39 -0800 | [diff] [blame] | 41 | const ButtonLocation kSpit(4, 13); |
| 42 | |
Austin Schuh | 9b3e41c | 2023-02-26 22:29:53 -0800 | [diff] [blame] | 43 | const ButtonLocation kHighConeScoreLeft(4, 14); |
| 44 | const ButtonLocation kHighConeScoreRight(3, 1); |
| 45 | |
| 46 | const ButtonLocation kMidConeScoreLeft(4, 15); |
| 47 | const ButtonLocation kMidConeScoreRight(3, 2); |
| 48 | |
| 49 | const ButtonLocation kHighCube(4, 1); |
| 50 | const ButtonLocation kMidCube(4, 2); |
| 51 | const ButtonLocation kLowCube(4, 3); |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 52 | |
| 53 | const ButtonLocation kGroundPickupConeUp(4, 7); |
| 54 | const ButtonLocation kGroundPickupConeDown(4, 8); |
milind-u | 71da539 | 2023-02-26 12:45:00 -0800 | [diff] [blame] | 55 | const ButtonLocation kGroundPickupCube(4, 10); |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 56 | const ButtonLocation kHPConePickup(4, 6); |
| 57 | |
Austin Schuh | 9b3e41c | 2023-02-26 22:29:53 -0800 | [diff] [blame] | 58 | const ButtonLocation kSuck(4, 11); |
| 59 | const ButtonLocation kBack(4, 12); |
milind-u | defab71 | 2023-02-20 22:22:02 -0800 | [diff] [blame] | 60 | |
Austin Schuh | 6dc925b | 2023-02-24 16:23:32 -0800 | [diff] [blame] | 61 | const ButtonLocation kWrist(4, 10); |
| 62 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 63 | namespace superstructure = y2023::control_loops::superstructure; |
milind-u | defab71 | 2023-02-20 22:22:02 -0800 | [diff] [blame] | 64 | namespace arm = superstructure::arm; |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 65 | |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 66 | enum class GamePiece { |
| 67 | CONE_UP = 0, |
| 68 | CONE_DOWN = 1, |
| 69 | CUBE = 2, |
| 70 | }; |
| 71 | |
Austin Schuh | 9b3e41c | 2023-02-26 22:29:53 -0800 | [diff] [blame] | 72 | enum class Side { |
| 73 | FRONT = 0, |
| 74 | BACK = 1, |
| 75 | }; |
| 76 | |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 77 | struct ArmSetpoint { |
| 78 | uint32_t index; |
| 79 | double wrist_goal; |
| 80 | std::optional<double> score_wrist_goal = std::nullopt; |
| 81 | GamePiece game_piece; |
Austin Schuh | 9b3e41c | 2023-02-26 22:29:53 -0800 | [diff] [blame] | 82 | std::vector<ButtonLocation> buttons; |
| 83 | Side side; |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | const std::vector<ArmSetpoint> setpoints = { |
| 87 | { |
| 88 | .index = arm::GroundPickupBackConeUpIndex(), |
| 89 | .wrist_goal = 0.0, |
| 90 | .game_piece = GamePiece::CONE_UP, |
Austin Schuh | 9b3e41c | 2023-02-26 22:29:53 -0800 | [diff] [blame] | 91 | .buttons = {kGroundPickupConeUp}, |
| 92 | .side = Side::BACK, |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 93 | }, |
| 94 | { |
| 95 | .index = arm::GroundPickupBackConeDownIndex(), |
| 96 | .wrist_goal = 0.0, |
| 97 | .game_piece = GamePiece::CONE_DOWN, |
Austin Schuh | 9b3e41c | 2023-02-26 22:29:53 -0800 | [diff] [blame] | 98 | .buttons = {kGroundPickupConeDown}, |
| 99 | .side = Side::BACK, |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 100 | }, |
| 101 | { |
| 102 | .index = arm::ScoreBackMidConeUpPosIndex(), |
| 103 | .wrist_goal = 0.55, |
| 104 | .game_piece = GamePiece::CONE_UP, |
Austin Schuh | 9b3e41c | 2023-02-26 22:29:53 -0800 | [diff] [blame] | 105 | .buttons = {kMidConeScoreRight}, |
| 106 | .side = Side::BACK, |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 107 | }, |
| 108 | { |
| 109 | .index = arm::ScoreBackMidConeDownPosIndex(), |
| 110 | .wrist_goal = 2.2, |
| 111 | .score_wrist_goal = 0.0, |
| 112 | .game_piece = GamePiece::CONE_DOWN, |
Austin Schuh | 9b3e41c | 2023-02-26 22:29:53 -0800 | [diff] [blame] | 113 | .buttons = {kMidConeScoreRight}, |
| 114 | .side = Side::BACK, |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 115 | }, |
| 116 | { |
| 117 | .index = arm::HPPickupBackConeUpIndex(), |
| 118 | .wrist_goal = 0.2, |
| 119 | .game_piece = GamePiece::CONE_UP, |
Austin Schuh | 9b3e41c | 2023-02-26 22:29:53 -0800 | [diff] [blame] | 120 | .buttons = {kHPConePickup}, |
| 121 | .side = Side::BACK, |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 122 | }, |
| 123 | { |
| 124 | .index = arm::ScoreFrontHighConeUpPosIndex(), |
| 125 | .wrist_goal = 0.05, |
| 126 | .game_piece = GamePiece::CONE_UP, |
Austin Schuh | 9b3e41c | 2023-02-26 22:29:53 -0800 | [diff] [blame] | 127 | .buttons = {kHighConeScoreLeft, kHighConeScoreRight}, |
| 128 | .side = Side::FRONT, |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 129 | }, |
| 130 | { |
| 131 | .index = arm::ScoreFrontMidConeUpPosIndex(), |
| 132 | .wrist_goal = 0.05, |
| 133 | .game_piece = GamePiece::CONE_UP, |
Austin Schuh | 9b3e41c | 2023-02-26 22:29:53 -0800 | [diff] [blame] | 134 | .buttons = {kMidConeScoreLeft, kMidConeScoreRight}, |
| 135 | .side = Side::FRONT, |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 136 | }, |
milind-u | 68842e1 | 2023-02-26 12:45:40 -0800 | [diff] [blame] | 137 | { |
| 138 | .index = arm::GroundPickupBackCubeIndex(), |
| 139 | .wrist_goal = 0.6, |
| 140 | .game_piece = GamePiece::CUBE, |
Austin Schuh | 9b3e41c | 2023-02-26 22:29:53 -0800 | [diff] [blame] | 141 | .buttons = {kGroundPickupCube}, |
| 142 | .side = Side::BACK, |
| 143 | }, |
| 144 | { |
| 145 | .index = arm::ScoreFrontMidCubeIndex(), |
| 146 | .wrist_goal = 0.6, |
| 147 | .game_piece = GamePiece::CUBE, |
| 148 | .buttons = {kMidCube}, |
| 149 | .side = Side::FRONT, |
| 150 | }, |
| 151 | { |
| 152 | .index = arm::ScoreBackMidCubeIndex(), |
| 153 | .wrist_goal = 0.6, |
| 154 | .score_wrist_goal = 0.0, |
| 155 | .game_piece = GamePiece::CUBE, |
| 156 | .buttons = {kMidCube}, |
| 157 | .side = Side::BACK, |
| 158 | }, |
| 159 | { |
| 160 | .index = arm::ScoreFrontLowCubeIndex(), |
| 161 | .wrist_goal = 0.6, |
| 162 | .game_piece = GamePiece::CUBE, |
| 163 | .buttons = {kLowCube}, |
| 164 | .side = Side::FRONT, |
| 165 | }, |
| 166 | { |
| 167 | .index = arm::ScoreBackLowCubeIndex(), |
| 168 | .wrist_goal = 0.6, |
| 169 | .game_piece = GamePiece::CUBE, |
| 170 | .buttons = {kLowCube}, |
| 171 | .side = Side::BACK, |
| 172 | }, |
| 173 | { |
| 174 | .index = arm::ScoreFrontHighCubeIndex(), |
| 175 | .wrist_goal = 0.6, |
| 176 | .game_piece = GamePiece::CUBE, |
| 177 | .buttons = {kHighCube}, |
| 178 | .side = Side::FRONT, |
| 179 | }, |
| 180 | { |
| 181 | .index = arm::ScoreBackHighCubeIndex(), |
| 182 | .wrist_goal = 0.6, |
| 183 | .score_wrist_goal = 0.0, |
| 184 | .game_piece = GamePiece::CUBE, |
| 185 | .buttons = {kHighCube}, |
| 186 | .side = Side::BACK, |
| 187 | }, |
| 188 | { |
| 189 | .index = arm::GroundPickupFrontCubeIndex(), |
| 190 | .wrist_goal = 0.6, |
| 191 | .game_piece = GamePiece::CUBE, |
| 192 | .buttons = {kGroundPickupCube}, |
| 193 | .side = Side::FRONT, |
milind-u | 68842e1 | 2023-02-26 12:45:40 -0800 | [diff] [blame] | 194 | }, |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 195 | }; |
| 196 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 197 | class Reader : public ::frc971::input::ActionJoystickInput { |
| 198 | public: |
| 199 | Reader(::aos::EventLoop *event_loop) |
| 200 | : ::frc971::input::ActionJoystickInput( |
| 201 | event_loop, |
| 202 | ::y2023::control_loops::drivetrain::GetDrivetrainConfig(), |
| 203 | ::frc971::input::DrivetrainInputReader::InputType::kPistol, {}), |
| 204 | superstructure_goal_sender_( |
| 205 | event_loop->MakeSender<superstructure::Goal>("/superstructure")), |
| 206 | superstructure_status_fetcher_( |
| 207 | event_loop->MakeFetcher<superstructure::Status>( |
| 208 | "/superstructure")) {} |
| 209 | |
| 210 | void AutoEnded() override { AOS_LOG(INFO, "Auto ended.\n"); } |
| 211 | |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 212 | GamePiece current_game_piece_ = GamePiece::CONE_UP; |
| 213 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 214 | void HandleTeleop( |
| 215 | const ::frc971::input::driver_station::Data &data) override { |
| 216 | superstructure_status_fetcher_.Fetch(); |
| 217 | if (!superstructure_status_fetcher_.get()) { |
| 218 | AOS_LOG(ERROR, "Got no superstructure status message.\n"); |
| 219 | return; |
| 220 | } |
| 221 | |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 222 | if (!superstructure_status_fetcher_->has_wrist()) { |
| 223 | AOS_LOG(ERROR, "Got no superstructure status message.\n"); |
| 224 | return; |
| 225 | } |
milind-u | defab71 | 2023-02-20 22:22:02 -0800 | [diff] [blame] | 226 | |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 227 | double wrist_goal = 0.0; |
| 228 | RollerGoal roller_goal = RollerGoal::IDLE; |
Austin Schuh | 9a11ebd | 2023-02-26 14:16:31 -0800 | [diff] [blame] | 229 | arm_goal_position_ = arm::NeutralIndex(); |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 230 | std::optional<double> score_wrist_goal = std::nullopt; |
| 231 | |
| 232 | if (data.IsPressed(kGroundPickupConeUp) || data.IsPressed(kHPConePickup)) { |
milind-u | 71da539 | 2023-02-26 12:45:00 -0800 | [diff] [blame] | 233 | roller_goal = RollerGoal::INTAKE_CONE; |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 234 | current_game_piece_ = GamePiece::CONE_UP; |
| 235 | } else if (data.IsPressed(kGroundPickupConeDown)) { |
milind-u | 71da539 | 2023-02-26 12:45:00 -0800 | [diff] [blame] | 236 | roller_goal = RollerGoal::INTAKE_CONE; |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 237 | current_game_piece_ = GamePiece::CONE_DOWN; |
milind-u | 71da539 | 2023-02-26 12:45:00 -0800 | [diff] [blame] | 238 | } else if (data.IsPressed(kGroundPickupCube)) { |
| 239 | roller_goal = RollerGoal::INTAKE_CUBE; |
| 240 | current_game_piece_ = GamePiece::CUBE; |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 241 | } |
| 242 | |
milind-u | 68842e1 | 2023-02-26 12:45:40 -0800 | [diff] [blame] | 243 | if (current_game_piece_ == GamePiece::CUBE) { |
| 244 | wrist_goal = 0.6; |
| 245 | } |
| 246 | |
Austin Schuh | 9b3e41c | 2023-02-26 22:29:53 -0800 | [diff] [blame] | 247 | const Side current_side = data.IsPressed(kBack) ? Side::BACK : Side::FRONT; |
| 248 | |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 249 | // Search for the active setpoint. |
| 250 | for (const ArmSetpoint &setpoint : setpoints) { |
Austin Schuh | 9b3e41c | 2023-02-26 22:29:53 -0800 | [diff] [blame] | 251 | for (const ButtonLocation &button : setpoint.buttons) { |
| 252 | if (data.IsPressed(button)) { |
| 253 | if (setpoint.game_piece == current_game_piece_ && |
| 254 | setpoint.side == current_side) { |
| 255 | wrist_goal = setpoint.wrist_goal; |
| 256 | arm_goal_position_ = setpoint.index; |
| 257 | score_wrist_goal = setpoint.score_wrist_goal; |
| 258 | break; |
| 259 | } |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 260 | } |
| 261 | } |
Austin Schuh | 6dc925b | 2023-02-24 16:23:32 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Austin Schuh | 23a9002 | 2023-02-24 22:13:39 -0800 | [diff] [blame] | 264 | if (data.IsPressed(kSuck)) { |
milind-u | 71da539 | 2023-02-26 12:45:00 -0800 | [diff] [blame] | 265 | roller_goal = RollerGoal::INTAKE_LAST; |
Austin Schuh | 23a9002 | 2023-02-24 22:13:39 -0800 | [diff] [blame] | 266 | } else if (data.IsPressed(kSpit)) { |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 267 | if (score_wrist_goal.has_value()) { |
| 268 | wrist_goal = score_wrist_goal.value(); |
Austin Schuh | 23a9002 | 2023-02-24 22:13:39 -0800 | [diff] [blame] | 269 | |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 270 | // If we are supposed to dunk it, wait until we are close enough to |
| 271 | // spit. |
| 272 | if (std::abs(score_wrist_goal.value() - |
| 273 | superstructure_status_fetcher_->wrist()->position()) < |
| 274 | 0.1) { |
| 275 | roller_goal = RollerGoal::SPIT; |
| 276 | } |
| 277 | } else { |
| 278 | roller_goal = RollerGoal::SPIT; |
| 279 | } |
milind-u | defab71 | 2023-02-20 22:22:02 -0800 | [diff] [blame] | 280 | } |
| 281 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 282 | { |
| 283 | auto builder = superstructure_goal_sender_.MakeBuilder(); |
| 284 | |
Austin Schuh | 6dc925b | 2023-02-24 16:23:32 -0800 | [diff] [blame] | 285 | flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal> |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 286 | wrist_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal( |
| 287 | *builder.fbb(), wrist_goal, |
| 288 | CreateProfileParameters(*builder.fbb(), 12.0, 90.0)); |
Austin Schuh | 6dc925b | 2023-02-24 16:23:32 -0800 | [diff] [blame] | 289 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 290 | superstructure::Goal::Builder superstructure_goal_builder = |
| 291 | builder.MakeBuilder<superstructure::Goal>(); |
milind-u | defab71 | 2023-02-20 22:22:02 -0800 | [diff] [blame] | 292 | superstructure_goal_builder.add_arm_goal_position(arm_goal_position_); |
Maxwell Henderson | 5938a83 | 2023-02-23 09:33:15 -0800 | [diff] [blame] | 293 | superstructure_goal_builder.add_roller_goal(roller_goal); |
Austin Schuh | 6dc925b | 2023-02-24 16:23:32 -0800 | [diff] [blame] | 294 | superstructure_goal_builder.add_wrist(wrist_offset); |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 295 | if (builder.Send(superstructure_goal_builder.Finish()) != |
| 296 | aos::RawSender::Error::kOk) { |
| 297 | AOS_LOG(ERROR, "Sending superstructure goal failed.\n"); |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | private: |
| 303 | ::aos::Sender<superstructure::Goal> superstructure_goal_sender_; |
| 304 | |
| 305 | ::aos::Fetcher<superstructure::Status> superstructure_status_fetcher_; |
milind-u | defab71 | 2023-02-20 22:22:02 -0800 | [diff] [blame] | 306 | |
| 307 | uint32_t arm_goal_position_; |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 308 | }; |
| 309 | |
| 310 | } // namespace joysticks |
| 311 | } // namespace input |
| 312 | } // namespace y2023 |
| 313 | |
| 314 | int main(int argc, char **argv) { |
| 315 | ::aos::InitGoogle(&argc, &argv); |
| 316 | |
| 317 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 318 | aos::configuration::ReadConfig("aos_config.json"); |
| 319 | |
| 320 | ::aos::ShmEventLoop event_loop(&config.message()); |
| 321 | ::y2023::input::joysticks::Reader reader(&event_loop); |
| 322 | |
| 323 | event_loop.Run(); |
| 324 | |
| 325 | return 0; |
| 326 | } |