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 | |
| 44 | const ButtonLocation kSuck(4, 12); |
milind-u | defab71 | 2023-02-20 22:22:02 -0800 | [diff] [blame] | 45 | |
Austin Schuh | 6dc925b | 2023-02-24 16:23:32 -0800 | [diff] [blame] | 46 | const ButtonLocation kWrist(4, 10); |
| 47 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 48 | namespace superstructure = y2023::control_loops::superstructure; |
milind-u | defab71 | 2023-02-20 22:22:02 -0800 | [diff] [blame] | 49 | namespace arm = superstructure::arm; |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 50 | |
| 51 | class Reader : public ::frc971::input::ActionJoystickInput { |
| 52 | public: |
| 53 | Reader(::aos::EventLoop *event_loop) |
| 54 | : ::frc971::input::ActionJoystickInput( |
| 55 | event_loop, |
| 56 | ::y2023::control_loops::drivetrain::GetDrivetrainConfig(), |
| 57 | ::frc971::input::DrivetrainInputReader::InputType::kPistol, {}), |
| 58 | superstructure_goal_sender_( |
| 59 | event_loop->MakeSender<superstructure::Goal>("/superstructure")), |
| 60 | superstructure_status_fetcher_( |
| 61 | event_loop->MakeFetcher<superstructure::Status>( |
| 62 | "/superstructure")) {} |
| 63 | |
| 64 | void AutoEnded() override { AOS_LOG(INFO, "Auto ended.\n"); } |
| 65 | |
| 66 | void HandleTeleop( |
| 67 | const ::frc971::input::driver_station::Data &data) override { |
| 68 | superstructure_status_fetcher_.Fetch(); |
| 69 | if (!superstructure_status_fetcher_.get()) { |
| 70 | AOS_LOG(ERROR, "Got no superstructure status message.\n"); |
| 71 | return; |
| 72 | } |
| 73 | |
Maxwell Henderson | 5938a83 | 2023-02-23 09:33:15 -0800 | [diff] [blame] | 74 | RollerGoal roller_goal = RollerGoal::IDLE; |
milind-u | defab71 | 2023-02-20 22:22:02 -0800 | [diff] [blame] | 75 | |
| 76 | // TODO(milind): add more actions and paths |
| 77 | if (data.IsPressed(kIntake)) { |
Maxwell Henderson | 5938a83 | 2023-02-23 09:33:15 -0800 | [diff] [blame] | 78 | arm_goal_position_ = arm::ScorePosIndex(); |
milind-u | defab71 | 2023-02-20 22:22:02 -0800 | [diff] [blame] | 79 | } else if (data.IsPressed(kScore)) { |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 80 | arm_goal_position_ = arm::ScorePosIndex(); |
Austin Schuh | 6dc925b | 2023-02-24 16:23:32 -0800 | [diff] [blame] | 81 | } else { |
| 82 | arm_goal_position_ = arm::NeutralPosIndex(); |
| 83 | } |
| 84 | |
Austin Schuh | 23a9002 | 2023-02-24 22:13:39 -0800 | [diff] [blame] | 85 | if (data.IsPressed(kSuck)) { |
| 86 | roller_goal = RollerGoal::INTAKE; |
| 87 | } else if (data.IsPressed(kSpit)) { |
| 88 | roller_goal = RollerGoal::SPIT; |
| 89 | } |
| 90 | |
Austin Schuh | 6dc925b | 2023-02-24 16:23:32 -0800 | [diff] [blame] | 91 | double wrist_goal = 0.1; |
| 92 | |
| 93 | if (data.IsPressed(kWrist)) { |
| 94 | wrist_goal = 1.5; |
| 95 | } else { |
| 96 | wrist_goal = 0.1; |
milind-u | defab71 | 2023-02-20 22:22:02 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 99 | { |
| 100 | auto builder = superstructure_goal_sender_.MakeBuilder(); |
| 101 | |
Austin Schuh | 6dc925b | 2023-02-24 16:23:32 -0800 | [diff] [blame] | 102 | flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal> |
| 103 | wrist_offset = |
| 104 | CreateStaticZeroingSingleDOFProfiledSubsystemGoal( |
| 105 | *builder.fbb(), wrist_goal, |
| 106 | CreateProfileParameters(*builder.fbb(), 12.0, 90.0)); |
| 107 | |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 108 | superstructure::Goal::Builder superstructure_goal_builder = |
| 109 | builder.MakeBuilder<superstructure::Goal>(); |
milind-u | defab71 | 2023-02-20 22:22:02 -0800 | [diff] [blame] | 110 | superstructure_goal_builder.add_arm_goal_position(arm_goal_position_); |
Maxwell Henderson | 5938a83 | 2023-02-23 09:33:15 -0800 | [diff] [blame] | 111 | superstructure_goal_builder.add_roller_goal(roller_goal); |
Austin Schuh | 6dc925b | 2023-02-24 16:23:32 -0800 | [diff] [blame] | 112 | superstructure_goal_builder.add_wrist(wrist_offset); |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 113 | if (builder.Send(superstructure_goal_builder.Finish()) != |
| 114 | aos::RawSender::Error::kOk) { |
| 115 | AOS_LOG(ERROR, "Sending superstructure goal failed.\n"); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | private: |
| 121 | ::aos::Sender<superstructure::Goal> superstructure_goal_sender_; |
| 122 | |
| 123 | ::aos::Fetcher<superstructure::Status> superstructure_status_fetcher_; |
milind-u | defab71 | 2023-02-20 22:22:02 -0800 | [diff] [blame] | 124 | |
| 125 | uint32_t arm_goal_position_; |
Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -0800 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | } // namespace joysticks |
| 129 | } // namespace input |
| 130 | } // namespace y2023 |
| 131 | |
| 132 | int main(int argc, char **argv) { |
| 133 | ::aos::InitGoogle(&argc, &argv); |
| 134 | |
| 135 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 136 | aos::configuration::ReadConfig("aos_config.json"); |
| 137 | |
| 138 | ::aos::ShmEventLoop event_loop(&config.message()); |
| 139 | ::y2023::input::joysticks::Reader reader(&event_loop); |
| 140 | |
| 141 | event_loop.Run(); |
| 142 | |
| 143 | return 0; |
| 144 | } |