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 kIntake(4, 5); |
| 41 | const ButtonLocation kScore(4, 4); |
Austin Schuh | 23a9002 | 2023-02-24 22:13:39 -0800 | [diff] [blame] | 42 | const ButtonLocation kSpit(4, 13); |
| 43 | |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 44 | const ButtonLocation kMidBackTipConeScoreLeft(4, 15); |
| 45 | const ButtonLocation kHighBackTipConeScoreLeft(4, 14); |
| 46 | const ButtonLocation kMidBackTipConeScoreRight(3, 2); |
| 47 | |
| 48 | const ButtonLocation kGroundPickupConeUp(4, 7); |
| 49 | const ButtonLocation kGroundPickupConeDown(4, 8); |
milind-u | 71da539 | 2023-02-26 12:45:00 -0800 | [diff] [blame] | 50 | const ButtonLocation kGroundPickupCube(4, 10); |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 51 | const ButtonLocation kHPConePickup(4, 6); |
| 52 | |
Austin Schuh | 23a9002 | 2023-02-24 22:13:39 -0800 | [diff] [blame] | 53 | const ButtonLocation kSuck(4, 12); |
milind-u | defab71 | 2023-02-20 22:22:02 -0800 | [diff] [blame] | 54 | |
Austin Schuh | 6dc925b | 2023-02-24 16:23:32 -0800 | [diff] [blame] | 55 | const ButtonLocation kWrist(4, 10); |
| 56 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 57 | namespace superstructure = y2023::control_loops::superstructure; |
milind-u | defab71 | 2023-02-20 22:22:02 -0800 | [diff] [blame] | 58 | namespace arm = superstructure::arm; |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 59 | |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 60 | enum class GamePiece { |
| 61 | CONE_UP = 0, |
| 62 | CONE_DOWN = 1, |
| 63 | CUBE = 2, |
| 64 | }; |
| 65 | |
| 66 | struct ArmSetpoint { |
| 67 | uint32_t index; |
| 68 | double wrist_goal; |
| 69 | std::optional<double> score_wrist_goal = std::nullopt; |
| 70 | GamePiece game_piece; |
| 71 | ButtonLocation button; |
| 72 | }; |
| 73 | |
| 74 | const std::vector<ArmSetpoint> setpoints = { |
| 75 | { |
| 76 | .index = arm::GroundPickupBackConeUpIndex(), |
| 77 | .wrist_goal = 0.0, |
| 78 | .game_piece = GamePiece::CONE_UP, |
| 79 | .button = kGroundPickupConeUp, |
| 80 | }, |
| 81 | { |
| 82 | .index = arm::GroundPickupBackConeDownIndex(), |
| 83 | .wrist_goal = 0.0, |
| 84 | .game_piece = GamePiece::CONE_DOWN, |
| 85 | .button = kGroundPickupConeDown, |
| 86 | }, |
| 87 | { |
| 88 | .index = arm::ScoreBackMidConeUpPosIndex(), |
| 89 | .wrist_goal = 0.55, |
| 90 | .game_piece = GamePiece::CONE_UP, |
| 91 | .button = kMidBackTipConeScoreRight, |
| 92 | }, |
| 93 | { |
| 94 | .index = arm::ScoreBackMidConeDownPosIndex(), |
| 95 | .wrist_goal = 2.2, |
| 96 | .score_wrist_goal = 0.0, |
| 97 | .game_piece = GamePiece::CONE_DOWN, |
| 98 | .button = kMidBackTipConeScoreRight, |
| 99 | }, |
| 100 | { |
| 101 | .index = arm::HPPickupBackConeUpIndex(), |
| 102 | .wrist_goal = 0.2, |
| 103 | .game_piece = GamePiece::CONE_UP, |
| 104 | .button = kHPConePickup, |
| 105 | }, |
| 106 | { |
| 107 | .index = arm::ScoreFrontHighConeUpPosIndex(), |
| 108 | .wrist_goal = 0.05, |
| 109 | .game_piece = GamePiece::CONE_UP, |
| 110 | .button = kHighBackTipConeScoreLeft, |
| 111 | }, |
| 112 | { |
| 113 | .index = arm::ScoreFrontMidConeUpPosIndex(), |
| 114 | .wrist_goal = 0.05, |
| 115 | .game_piece = GamePiece::CONE_UP, |
| 116 | .button = kMidBackTipConeScoreLeft, |
| 117 | }, |
| 118 | }; |
| 119 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 120 | class Reader : public ::frc971::input::ActionJoystickInput { |
| 121 | public: |
| 122 | Reader(::aos::EventLoop *event_loop) |
| 123 | : ::frc971::input::ActionJoystickInput( |
| 124 | event_loop, |
| 125 | ::y2023::control_loops::drivetrain::GetDrivetrainConfig(), |
| 126 | ::frc971::input::DrivetrainInputReader::InputType::kPistol, {}), |
| 127 | superstructure_goal_sender_( |
| 128 | event_loop->MakeSender<superstructure::Goal>("/superstructure")), |
| 129 | superstructure_status_fetcher_( |
| 130 | event_loop->MakeFetcher<superstructure::Status>( |
| 131 | "/superstructure")) {} |
| 132 | |
| 133 | void AutoEnded() override { AOS_LOG(INFO, "Auto ended.\n"); } |
| 134 | |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 135 | GamePiece current_game_piece_ = GamePiece::CONE_UP; |
| 136 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 137 | void HandleTeleop( |
| 138 | const ::frc971::input::driver_station::Data &data) override { |
| 139 | superstructure_status_fetcher_.Fetch(); |
| 140 | if (!superstructure_status_fetcher_.get()) { |
| 141 | AOS_LOG(ERROR, "Got no superstructure status message.\n"); |
| 142 | return; |
| 143 | } |
| 144 | |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 145 | if (!superstructure_status_fetcher_->has_wrist()) { |
| 146 | AOS_LOG(ERROR, "Got no superstructure status message.\n"); |
| 147 | return; |
| 148 | } |
milind-u | defab71 | 2023-02-20 22:22:02 -0800 | [diff] [blame] | 149 | |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 150 | double wrist_goal = 0.0; |
| 151 | RollerGoal roller_goal = RollerGoal::IDLE; |
| 152 | arm_goal_position_ = arm::NeutralPosIndex(); |
| 153 | std::optional<double> score_wrist_goal = std::nullopt; |
| 154 | |
| 155 | if (data.IsPressed(kGroundPickupConeUp) || data.IsPressed(kHPConePickup)) { |
milind-u | 71da539 | 2023-02-26 12:45:00 -0800 | [diff] [blame] | 156 | roller_goal = RollerGoal::INTAKE_CONE; |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 157 | current_game_piece_ = GamePiece::CONE_UP; |
| 158 | } else if (data.IsPressed(kGroundPickupConeDown)) { |
milind-u | 71da539 | 2023-02-26 12:45:00 -0800 | [diff] [blame] | 159 | roller_goal = RollerGoal::INTAKE_CONE; |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 160 | current_game_piece_ = GamePiece::CONE_DOWN; |
milind-u | 71da539 | 2023-02-26 12:45:00 -0800 | [diff] [blame] | 161 | } else if (data.IsPressed(kGroundPickupCube)) { |
| 162 | roller_goal = RollerGoal::INTAKE_CUBE; |
| 163 | current_game_piece_ = GamePiece::CUBE; |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | // Search for the active setpoint. |
| 167 | for (const ArmSetpoint &setpoint : setpoints) { |
| 168 | if (data.IsPressed(setpoint.button)) { |
| 169 | if (setpoint.game_piece == current_game_piece_) { |
| 170 | wrist_goal = setpoint.wrist_goal; |
| 171 | arm_goal_position_ = setpoint.index; |
| 172 | score_wrist_goal = setpoint.score_wrist_goal; |
| 173 | break; |
| 174 | } |
| 175 | } |
Austin Schuh | 6dc925b | 2023-02-24 16:23:32 -0800 | [diff] [blame] | 176 | } |
| 177 | |
Austin Schuh | 23a9002 | 2023-02-24 22:13:39 -0800 | [diff] [blame] | 178 | if (data.IsPressed(kSuck)) { |
milind-u | 71da539 | 2023-02-26 12:45:00 -0800 | [diff] [blame] | 179 | roller_goal = RollerGoal::INTAKE_LAST; |
Austin Schuh | 23a9002 | 2023-02-24 22:13:39 -0800 | [diff] [blame] | 180 | } else if (data.IsPressed(kSpit)) { |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 181 | if (score_wrist_goal.has_value()) { |
| 182 | wrist_goal = score_wrist_goal.value(); |
Austin Schuh | 23a9002 | 2023-02-24 22:13:39 -0800 | [diff] [blame] | 183 | |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 184 | // If we are supposed to dunk it, wait until we are close enough to |
| 185 | // spit. |
| 186 | if (std::abs(score_wrist_goal.value() - |
| 187 | superstructure_status_fetcher_->wrist()->position()) < |
| 188 | 0.1) { |
| 189 | roller_goal = RollerGoal::SPIT; |
| 190 | } |
| 191 | } else { |
| 192 | roller_goal = RollerGoal::SPIT; |
| 193 | } |
milind-u | defab71 | 2023-02-20 22:22:02 -0800 | [diff] [blame] | 194 | } |
| 195 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 196 | { |
| 197 | auto builder = superstructure_goal_sender_.MakeBuilder(); |
| 198 | |
Austin Schuh | 6dc925b | 2023-02-24 16:23:32 -0800 | [diff] [blame] | 199 | flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal> |
Maxwell Henderson | 1ac7aac | 2023-02-23 17:35:32 -0800 | [diff] [blame] | 200 | wrist_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal( |
| 201 | *builder.fbb(), wrist_goal, |
| 202 | CreateProfileParameters(*builder.fbb(), 12.0, 90.0)); |
Austin Schuh | 6dc925b | 2023-02-24 16:23:32 -0800 | [diff] [blame] | 203 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 204 | superstructure::Goal::Builder superstructure_goal_builder = |
| 205 | builder.MakeBuilder<superstructure::Goal>(); |
milind-u | defab71 | 2023-02-20 22:22:02 -0800 | [diff] [blame] | 206 | superstructure_goal_builder.add_arm_goal_position(arm_goal_position_); |
Maxwell Henderson | 5938a83 | 2023-02-23 09:33:15 -0800 | [diff] [blame] | 207 | superstructure_goal_builder.add_roller_goal(roller_goal); |
Austin Schuh | 6dc925b | 2023-02-24 16:23:32 -0800 | [diff] [blame] | 208 | superstructure_goal_builder.add_wrist(wrist_offset); |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 209 | if (builder.Send(superstructure_goal_builder.Finish()) != |
| 210 | aos::RawSender::Error::kOk) { |
| 211 | AOS_LOG(ERROR, "Sending superstructure goal failed.\n"); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | private: |
| 217 | ::aos::Sender<superstructure::Goal> superstructure_goal_sender_; |
| 218 | |
| 219 | ::aos::Fetcher<superstructure::Status> superstructure_status_fetcher_; |
milind-u | defab71 | 2023-02-20 22:22:02 -0800 | [diff] [blame] | 220 | |
| 221 | uint32_t arm_goal_position_; |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 222 | }; |
| 223 | |
| 224 | } // namespace joysticks |
| 225 | } // namespace input |
| 226 | } // namespace y2023 |
| 227 | |
| 228 | int main(int argc, char **argv) { |
| 229 | ::aos::InitGoogle(&argc, &argv); |
| 230 | |
| 231 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 232 | aos::configuration::ReadConfig("aos_config.json"); |
| 233 | |
| 234 | ::aos::ShmEventLoop event_loop(&config.message()); |
| 235 | ::y2023::input::joysticks::Reader reader(&event_loop); |
| 236 | |
| 237 | event_loop.Run(); |
| 238 | |
| 239 | return 0; |
| 240 | } |