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