Henry Speiser | 354d278 | 2022-07-22 13:56:48 -0700 | [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/input/action_joystick_input.h" |
| 14 | #include "frc971/input/driver_station_data.h" |
| 15 | #include "frc971/input/drivetrain_input.h" |
| 16 | #include "frc971/input/joystick_input.h" |
| 17 | #include "y2022_bot3/control_loops/drivetrain/drivetrain_base.h" |
| 18 | #include "y2022_bot3/control_loops/superstructure/superstructure_goal_generated.h" |
| 19 | #include "y2022_bot3/control_loops/superstructure/superstructure_status_generated.h" |
| 20 | |
Niko Sohmers | dfeb19b | 2022-09-03 16:33:39 -0700 | [diff] [blame] | 21 | using frc971::CreateProfileParameters; |
| 22 | using frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal; |
| 23 | using frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal; |
Henry Speiser | 354d278 | 2022-07-22 13:56:48 -0700 | [diff] [blame] | 24 | using frc971::input::driver_station::ButtonLocation; |
| 25 | using frc971::input::driver_station::ControlBit; |
| 26 | using frc971::input::driver_station::JoystickAxis; |
| 27 | using frc971::input::driver_station::POVLocation; |
| 28 | |
| 29 | namespace y2022_bot3 { |
| 30 | namespace input { |
| 31 | namespace joysticks { |
| 32 | |
| 33 | namespace superstructure = y2022_bot3::control_loops::superstructure; |
| 34 | |
Niko Sohmers | dfeb19b | 2022-09-03 16:33:39 -0700 | [diff] [blame] | 35 | // TODO(niko): add a climber button |
| 36 | const ButtonLocation kIntake(4, 10); |
| 37 | const ButtonLocation kSpit(4, 9); |
| 38 | |
Henry Speiser | 354d278 | 2022-07-22 13:56:48 -0700 | [diff] [blame] | 39 | class Reader : public ::frc971::input::ActionJoystickInput { |
| 40 | public: |
| 41 | Reader(::aos::EventLoop *event_loop) |
| 42 | : ::frc971::input::ActionJoystickInput( |
| 43 | event_loop, |
| 44 | ::y2022_bot3::control_loops::drivetrain::GetDrivetrainConfig(), |
Austin Schuh | fafdd93 | 2023-04-09 17:03:36 -0700 | [diff] [blame^] | 45 | ::frc971::input::DrivetrainInputReader::InputType::kPistol, {}), |
Henry Speiser | 354d278 | 2022-07-22 13:56:48 -0700 | [diff] [blame] | 46 | superstructure_goal_sender_( |
| 47 | event_loop->MakeSender<superstructure::Goal>("/superstructure")), |
| 48 | superstructure_status_fetcher_( |
| 49 | event_loop->MakeFetcher<superstructure::Status>( |
| 50 | "/superstructure")) {} |
| 51 | |
| 52 | void AutoEnded() override { AOS_LOG(INFO, "Auto ended.\n"); } |
| 53 | |
| 54 | void HandleTeleop( |
Niko Sohmers | dfeb19b | 2022-09-03 16:33:39 -0700 | [diff] [blame] | 55 | const ::frc971::input::driver_station::Data &data) override { |
Henry Speiser | 354d278 | 2022-07-22 13:56:48 -0700 | [diff] [blame] | 56 | superstructure_status_fetcher_.Fetch(); |
| 57 | if (!superstructure_status_fetcher_.get()) { |
| 58 | AOS_LOG(ERROR, "Got no superstructure status message.\n"); |
| 59 | return; |
| 60 | } |
Niko Sohmers | dfeb19b | 2022-09-03 16:33:39 -0700 | [diff] [blame] | 61 | constexpr double kIntakeOutPosition = 0.0; |
| 62 | constexpr double kIntakeInPosition = 1.47; |
| 63 | |
| 64 | double roller_speed = 0.0; |
| 65 | double intake_pos = kIntakeInPosition; |
| 66 | |
| 67 | if (data.IsPressed(kIntake) || data.IsPressed(kSpit)) { |
| 68 | intake_pos = kIntakeOutPosition; |
| 69 | |
| 70 | if (data.IsPressed(kIntake)) { |
| 71 | roller_speed = 12.0; |
| 72 | } else { |
| 73 | roller_speed = -12.0; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | { |
| 78 | auto builder = superstructure_goal_sender_.MakeBuilder(); |
| 79 | |
| 80 | flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal> |
| 81 | intake_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal( |
| 82 | *builder.fbb(), intake_pos, |
| 83 | CreateProfileParameters(*builder.fbb(), 8.0, 40.0)); |
| 84 | |
| 85 | superstructure::Goal::Builder superstructure_goal_builder = |
| 86 | builder.MakeBuilder<superstructure::Goal>(); |
| 87 | |
| 88 | superstructure_goal_builder.add_intake(intake_offset); |
| 89 | superstructure_goal_builder.add_roller_speed(roller_speed); |
| 90 | |
| 91 | if (builder.Send(superstructure_goal_builder.Finish()) != |
| 92 | aos::RawSender::Error::kOk) { |
| 93 | AOS_LOG(ERROR, "Sending superstructure goal failed.\n"); |
| 94 | } |
| 95 | } |
Henry Speiser | 354d278 | 2022-07-22 13:56:48 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | private: |
| 99 | ::aos::Sender<superstructure::Goal> superstructure_goal_sender_; |
| 100 | |
| 101 | ::aos::Fetcher<superstructure::Status> superstructure_status_fetcher_; |
| 102 | }; |
| 103 | |
| 104 | } // namespace joysticks |
| 105 | } // namespace input |
| 106 | } // namespace y2022_bot3 |
| 107 | |
| 108 | int main(int argc, char **argv) { |
| 109 | ::aos::InitGoogle(&argc, &argv); |
| 110 | |
| 111 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 112 | aos::configuration::ReadConfig("aos_config.json"); |
| 113 | |
| 114 | ::aos::ShmEventLoop event_loop(&config.message()); |
| 115 | ::y2022_bot3::input::joysticks::Reader reader(&event_loop); |
| 116 | |
| 117 | event_loop.Run(); |
| 118 | |
| 119 | return 0; |
| 120 | } |