milind-u | 086d726 | 2022-01-19 20:44:18 -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" |
Austin Schuh | 39f26f6 | 2022-02-24 21:34:46 -0800 | [diff] [blame^] | 13 | #include "frc971/control_loops/profiled_subsystem_generated.h" |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 14 | #include "frc971/input/action_joystick_input.h" |
| 15 | #include "frc971/input/driver_station_data.h" |
| 16 | #include "frc971/input/drivetrain_input.h" |
| 17 | #include "frc971/input/joystick_input.h" |
| 18 | #include "y2022/control_loops/drivetrain/drivetrain_base.h" |
| 19 | #include "y2022/control_loops/superstructure/superstructure_goal_generated.h" |
| 20 | #include "y2022/control_loops/superstructure/superstructure_status_generated.h" |
| 21 | |
Austin Schuh | 39f26f6 | 2022-02-24 21:34:46 -0800 | [diff] [blame^] | 22 | using frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal; |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 23 | using frc971::input::driver_station::ButtonLocation; |
| 24 | using frc971::input::driver_station::ControlBit; |
| 25 | using frc971::input::driver_station::JoystickAxis; |
| 26 | using frc971::input::driver_station::POVLocation; |
| 27 | |
| 28 | namespace y2022 { |
| 29 | namespace input { |
| 30 | namespace joysticks { |
| 31 | |
Austin Schuh | 39f26f6 | 2022-02-24 21:34:46 -0800 | [diff] [blame^] | 32 | const ButtonLocation kCatapultPos(3, 3); |
| 33 | const ButtonLocation kFire(3, 1); |
| 34 | |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 35 | namespace superstructure = y2022::control_loops::superstructure; |
| 36 | |
| 37 | class Reader : public ::frc971::input::ActionJoystickInput { |
| 38 | public: |
| 39 | Reader(::aos::EventLoop *event_loop) |
| 40 | : ::frc971::input::ActionJoystickInput( |
| 41 | event_loop, |
| 42 | ::y2022::control_loops::drivetrain::GetDrivetrainConfig(), |
| 43 | ::frc971::input::DrivetrainInputReader::InputType::kPistol, {}), |
| 44 | superstructure_goal_sender_( |
| 45 | event_loop->MakeSender<superstructure::Goal>("/superstructure")), |
| 46 | superstructure_status_fetcher_( |
| 47 | event_loop->MakeFetcher<superstructure::Status>( |
| 48 | "/superstructure")) {} |
| 49 | |
| 50 | void AutoEnded() override { AOS_LOG(INFO, "Auto ended.\n"); } |
| 51 | |
| 52 | void HandleTeleop( |
Austin Schuh | 39f26f6 | 2022-02-24 21:34:46 -0800 | [diff] [blame^] | 53 | const ::frc971::input::driver_station::Data &data) override { |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 54 | superstructure_status_fetcher_.Fetch(); |
| 55 | if (!superstructure_status_fetcher_.get()) { |
| 56 | AOS_LOG(ERROR, "Got no superstructure status message.\n"); |
| 57 | return; |
| 58 | } |
Austin Schuh | 39f26f6 | 2022-02-24 21:34:46 -0800 | [diff] [blame^] | 59 | |
| 60 | aos::Sender<superstructure::Goal>::Builder builder = |
| 61 | superstructure_goal_sender_.MakeBuilder(); |
| 62 | |
| 63 | flatbuffers::Offset<frc971::ProfileParameters> catapult_profile = |
| 64 | frc971::CreateProfileParameters(*builder.fbb(), 5.0, 30.0); |
| 65 | |
| 66 | StaticZeroingSingleDOFProfiledSubsystemGoal::Builder |
| 67 | catapult_return_builder = |
| 68 | builder.MakeBuilder<StaticZeroingSingleDOFProfiledSubsystemGoal>(); |
| 69 | catapult_return_builder.add_unsafe_goal( |
| 70 | data.IsPressed(kCatapultPos) ? 0.3 : -0.85); |
| 71 | catapult_return_builder.add_profile_params(catapult_profile); |
| 72 | flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal> |
| 73 | catapult_return_offset = catapult_return_builder.Finish(); |
| 74 | |
| 75 | superstructure::CatapultGoal::Builder catapult_builder = |
| 76 | builder.MakeBuilder<superstructure::CatapultGoal>(); |
| 77 | catapult_builder.add_return_position(catapult_return_offset); |
| 78 | catapult_builder.add_fire(data.IsPressed(kFire)); |
| 79 | catapult_builder.add_shot_position(0.3); |
| 80 | catapult_builder.add_shot_velocity(15.0); |
| 81 | flatbuffers::Offset<superstructure::CatapultGoal> catapult_offset = |
| 82 | catapult_builder.Finish(); |
| 83 | |
| 84 | superstructure::Goal::Builder goal_builder = |
| 85 | builder.MakeBuilder<superstructure::Goal>(); |
| 86 | goal_builder.add_catapult(catapult_offset); |
| 87 | |
| 88 | if (builder.Send(goal_builder.Finish()) != aos::RawSender::Error::kOk) { |
| 89 | AOS_LOG(ERROR, "Sending superstructure goal failed.\n"); |
| 90 | } |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | private: |
| 94 | ::aos::Sender<superstructure::Goal> superstructure_goal_sender_; |
| 95 | |
| 96 | ::aos::Fetcher<superstructure::Status> superstructure_status_fetcher_; |
| 97 | }; |
| 98 | |
| 99 | } // namespace joysticks |
| 100 | } // namespace input |
| 101 | } // namespace y2022 |
| 102 | |
| 103 | int main(int argc, char **argv) { |
| 104 | ::aos::InitGoogle(&argc, &argv); |
| 105 | |
| 106 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
Austin Schuh | c5fa6d9 | 2022-02-25 14:36:28 -0800 | [diff] [blame] | 107 | aos::configuration::ReadConfig("aos_config.json"); |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 108 | |
| 109 | ::aos::ShmEventLoop event_loop(&config.message()); |
| 110 | ::y2022::input::joysticks::Reader reader(&event_loop); |
| 111 | |
| 112 | event_loop.Run(); |
| 113 | |
| 114 | return 0; |
| 115 | } |