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; |
| 33 | |
| 34 | namespace y2023 { |
| 35 | namespace input { |
| 36 | namespace joysticks { |
| 37 | |
milind-u | defab71 | 2023-02-20 22:22:02 -0800 | [diff] [blame^] | 38 | // TODO(milind): add correct locations |
| 39 | const ButtonLocation kIntake(3, 3); |
| 40 | const ButtonLocation kScore(3, 3); |
| 41 | const ButtonLocation kSpit(3, 3); |
| 42 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 43 | namespace superstructure = y2023::control_loops::superstructure; |
milind-u | defab71 | 2023-02-20 22:22:02 -0800 | [diff] [blame^] | 44 | namespace arm = superstructure::arm; |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 45 | |
| 46 | class Reader : public ::frc971::input::ActionJoystickInput { |
| 47 | public: |
| 48 | Reader(::aos::EventLoop *event_loop) |
| 49 | : ::frc971::input::ActionJoystickInput( |
| 50 | event_loop, |
| 51 | ::y2023::control_loops::drivetrain::GetDrivetrainConfig(), |
| 52 | ::frc971::input::DrivetrainInputReader::InputType::kPistol, {}), |
| 53 | superstructure_goal_sender_( |
| 54 | event_loop->MakeSender<superstructure::Goal>("/superstructure")), |
| 55 | superstructure_status_fetcher_( |
| 56 | event_loop->MakeFetcher<superstructure::Status>( |
| 57 | "/superstructure")) {} |
| 58 | |
| 59 | void AutoEnded() override { AOS_LOG(INFO, "Auto ended.\n"); } |
| 60 | |
| 61 | void HandleTeleop( |
| 62 | const ::frc971::input::driver_station::Data &data) override { |
| 63 | superstructure_status_fetcher_.Fetch(); |
| 64 | if (!superstructure_status_fetcher_.get()) { |
| 65 | AOS_LOG(ERROR, "Got no superstructure status message.\n"); |
| 66 | return; |
| 67 | } |
| 68 | |
milind-u | defab71 | 2023-02-20 22:22:02 -0800 | [diff] [blame^] | 69 | bool intake = false; |
| 70 | bool spit = false; |
| 71 | |
| 72 | // TODO(milind): add more actions and paths |
| 73 | if (data.IsPressed(kIntake)) { |
| 74 | intake = true; |
| 75 | arm_goal_position_ = arm::ConePosIndex(); |
| 76 | } else if (data.IsPressed(kSpit)) { |
| 77 | spit = true; |
| 78 | arm_goal_position_ = arm::ConePosIndex(); |
| 79 | } else if (data.IsPressed(kScore)) { |
| 80 | arm_goal_position_ = arm::ConePerchPosIndex(); |
| 81 | } |
| 82 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 83 | { |
| 84 | auto builder = superstructure_goal_sender_.MakeBuilder(); |
| 85 | |
| 86 | superstructure::Goal::Builder superstructure_goal_builder = |
| 87 | builder.MakeBuilder<superstructure::Goal>(); |
milind-u | defab71 | 2023-02-20 22:22:02 -0800 | [diff] [blame^] | 88 | superstructure_goal_builder.add_arm_goal_position(arm_goal_position_); |
| 89 | superstructure_goal_builder.add_intake(intake); |
| 90 | superstructure_goal_builder.add_spit(spit); |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 91 | if (builder.Send(superstructure_goal_builder.Finish()) != |
| 92 | aos::RawSender::Error::kOk) { |
| 93 | AOS_LOG(ERROR, "Sending superstructure goal failed.\n"); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | private: |
| 99 | ::aos::Sender<superstructure::Goal> superstructure_goal_sender_; |
| 100 | |
| 101 | ::aos::Fetcher<superstructure::Status> superstructure_status_fetcher_; |
milind-u | defab71 | 2023-02-20 22:22:02 -0800 | [diff] [blame^] | 102 | |
| 103 | uint32_t arm_goal_position_; |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | } // namespace joysticks |
| 107 | } // namespace input |
| 108 | } // namespace y2023 |
| 109 | |
| 110 | int main(int argc, char **argv) { |
| 111 | ::aos::InitGoogle(&argc, &argv); |
| 112 | |
| 113 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 114 | aos::configuration::ReadConfig("aos_config.json"); |
| 115 | |
| 116 | ::aos::ShmEventLoop event_loop(&config.message()); |
| 117 | ::y2023::input::joysticks::Reader reader(&event_loop); |
| 118 | |
| 119 | event_loop.Run(); |
| 120 | |
| 121 | return 0; |
| 122 | } |