Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 1 | #include <math.h> |
| 2 | #include <stdio.h> |
| 3 | #include <string.h> |
| 4 | #include <unistd.h> |
| 5 | |
| 6 | #include "aos/actions/actions.h" |
| 7 | #include "aos/init.h" |
| 8 | #include "aos/input/action_joystick_input.h" |
| 9 | #include "aos/input/driver_station_data.h" |
| 10 | #include "aos/input/drivetrain_input.h" |
| 11 | #include "aos/input/joystick_input.h" |
| 12 | #include "aos/logging/logging.h" |
| 13 | #include "aos/network/team_number.h" |
| 14 | #include "aos/util/log_interval.h" |
| 15 | #include "frc971/autonomous/base_autonomous_actor.h" |
| 16 | #include "y2020/control_loops/drivetrain/drivetrain_base.h" |
Sabina Davis | a8fed3d | 2020-02-22 21:44:57 -0800 | [diff] [blame] | 17 | #include "y2020/constants.h" |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 18 | #include "y2020/control_loops/superstructure/superstructure_goal_generated.h" |
| 19 | #include "y2020/control_loops/superstructure/superstructure_status_generated.h" |
| 20 | |
| 21 | using aos::input::driver_station::ButtonLocation; |
| 22 | using aos::input::driver_station::ControlBit; |
| 23 | using aos::input::driver_station::JoystickAxis; |
| 24 | using aos::input::driver_station::POVLocation; |
| 25 | |
Austin Schuh | f0a637c | 2020-02-25 23:44:12 -0800 | [diff] [blame] | 26 | using frc971::CreateProfileParameters; |
Sabina Davis | a8fed3d | 2020-02-22 21:44:57 -0800 | [diff] [blame] | 27 | using frc971::control_loops::CreateStaticZeroingSingleDOFProfiledSubsystemGoal; |
| 28 | using frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal; |
| 29 | |
| 30 | |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 31 | namespace y2020 { |
| 32 | namespace input { |
| 33 | namespace joysticks { |
| 34 | |
| 35 | namespace superstructure = y2020::control_loops::superstructure; |
| 36 | |
Sabina Davis | a8fed3d | 2020-02-22 21:44:57 -0800 | [diff] [blame] | 37 | // TODO(sabina): fix button locations. |
| 38 | |
Austin Schuh | f0a637c | 2020-02-25 23:44:12 -0800 | [diff] [blame] | 39 | const ButtonLocation kShootFast(3, 16); |
| 40 | const ButtonLocation kTurret(3, 15); |
| 41 | const ButtonLocation kHood(3, 3); |
Sabina Davis | a8fed3d | 2020-02-22 21:44:57 -0800 | [diff] [blame] | 42 | const ButtonLocation kShootSlow(4, 2); |
Austin Schuh | 43a220f | 2020-02-26 22:02:34 -0800 | [diff] [blame] | 43 | const ButtonLocation kFeed(4, 1); |
Austin Schuh | f0a637c | 2020-02-25 23:44:12 -0800 | [diff] [blame] | 44 | const ButtonLocation kIntakeExtend(3, 9); |
Sabina Davis | a8fed3d | 2020-02-22 21:44:57 -0800 | [diff] [blame] | 45 | const ButtonLocation kIntakeIn(4, 4); |
Tyler Chatow | 1039e43 | 2020-02-28 21:37:50 -0800 | [diff] [blame] | 46 | const ButtonLocation kSpit(4, 3); |
| 47 | |
| 48 | const ButtonLocation kWinch(3, 14); |
Sabina Davis | a8fed3d | 2020-02-22 21:44:57 -0800 | [diff] [blame] | 49 | |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 50 | class Reader : public ::aos::input::ActionJoystickInput { |
| 51 | public: |
| 52 | Reader(::aos::EventLoop *event_loop) |
| 53 | : ::aos::input::ActionJoystickInput( |
| 54 | event_loop, |
| 55 | ::y2020::control_loops::drivetrain::GetDrivetrainConfig(), |
| 56 | ::aos::input::DrivetrainInputReader::InputType::kPistol, {}), |
| 57 | superstructure_goal_sender_( |
| 58 | event_loop->MakeSender<superstructure::Goal>("/superstructure")), |
| 59 | superstructure_status_fetcher_( |
| 60 | event_loop->MakeFetcher<superstructure::Status>( |
| 61 | "/superstructure")) {} |
| 62 | |
| 63 | void AutoEnded() override { |
| 64 | AOS_LOG(INFO, "Auto ended, assuming disc and have piece\n"); |
| 65 | } |
| 66 | |
Sabina Davis | a8fed3d | 2020-02-22 21:44:57 -0800 | [diff] [blame] | 67 | void HandleTeleop(const ::aos::input::driver_station::Data &data) override { |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 68 | superstructure_status_fetcher_.Fetch(); |
| 69 | if (!superstructure_status_fetcher_.get()) { |
| 70 | AOS_LOG(ERROR, "Got no superstructure status message.\n"); |
| 71 | return; |
| 72 | } |
Sabina Davis | a8fed3d | 2020-02-22 21:44:57 -0800 | [diff] [blame] | 73 | |
Austin Schuh | f0a637c | 2020-02-25 23:44:12 -0800 | [diff] [blame] | 74 | double hood_pos = constants::Values::kHoodRange().middle(); |
| 75 | double intake_pos = -0.89; |
Sabina Davis | a8fed3d | 2020-02-22 21:44:57 -0800 | [diff] [blame] | 76 | double turret_pos = 0.0; |
| 77 | float roller_speed = 0.0f; |
| 78 | double accelerator_speed = 0.0; |
| 79 | double finisher_speed = 0.0; |
Tyler Chatow | 1039e43 | 2020-02-28 21:37:50 -0800 | [diff] [blame] | 80 | double climber_speed = 0.0; |
Sabina Davis | a8fed3d | 2020-02-22 21:44:57 -0800 | [diff] [blame] | 81 | |
Austin Schuh | f0a637c | 2020-02-25 23:44:12 -0800 | [diff] [blame] | 82 | if (data.IsPressed(kTurret)) { |
| 83 | turret_pos = 3.5; |
Sabina Davis | a8fed3d | 2020-02-22 21:44:57 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Austin Schuh | f0a637c | 2020-02-25 23:44:12 -0800 | [diff] [blame] | 86 | if (data.IsPressed(kHood)) { |
| 87 | hood_pos = 0.05; |
| 88 | } |
| 89 | |
| 90 | if (data.IsPressed(kShootFast)) { |
Austin Schuh | 43a220f | 2020-02-26 22:02:34 -0800 | [diff] [blame] | 91 | accelerator_speed = 500.0; |
| 92 | finisher_speed = 500.0; |
Austin Schuh | f0a637c | 2020-02-25 23:44:12 -0800 | [diff] [blame] | 93 | } else if (data.IsPressed(kShootSlow)) { |
Sabina Davis | a8fed3d | 2020-02-22 21:44:57 -0800 | [diff] [blame] | 94 | accelerator_speed = 30.0; |
| 95 | finisher_speed = 30.0; |
| 96 | } |
| 97 | |
| 98 | if (data.IsPressed(kIntakeExtend)) { |
Austin Schuh | 13e5552 | 2020-02-29 23:11:17 -0800 | [diff] [blame] | 99 | intake_pos = 1.2; |
| 100 | roller_speed = 9.0f; |
| 101 | } |
| 102 | |
| 103 | if (superstructure_status_fetcher_.get() && |
| 104 | superstructure_status_fetcher_->intake()->position() > -0.5) { |
| 105 | roller_speed = std::max(roller_speed, 6.0f); |
Sabina Davis | a8fed3d | 2020-02-22 21:44:57 -0800 | [diff] [blame] | 106 | } |
| 107 | |
Austin Schuh | 43a220f | 2020-02-26 22:02:34 -0800 | [diff] [blame] | 108 | if (data.IsPressed(kFeed)) { |
| 109 | } |
| 110 | |
Sabina Davis | a8fed3d | 2020-02-22 21:44:57 -0800 | [diff] [blame] | 111 | if (data.IsPressed(kIntakeIn)) { |
| 112 | roller_speed = 6.0f; |
| 113 | } else if (data.IsPressed(kSpit)) { |
| 114 | roller_speed = -6.0f; |
| 115 | } |
| 116 | |
Tyler Chatow | 1039e43 | 2020-02-28 21:37:50 -0800 | [diff] [blame] | 117 | if (data.IsPressed(kWinch)) { |
| 118 | climber_speed = 12.0f; |
| 119 | } |
| 120 | |
Sabina Davis | a8fed3d | 2020-02-22 21:44:57 -0800 | [diff] [blame] | 121 | auto builder = superstructure_goal_sender_.MakeBuilder(); |
| 122 | |
| 123 | flatbuffers::Offset<superstructure::Goal> superstructure_goal_offset; |
| 124 | { |
| 125 | flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal> |
| 126 | hood_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal( |
Austin Schuh | f0a637c | 2020-02-25 23:44:12 -0800 | [diff] [blame] | 127 | *builder.fbb(), hood_pos, |
Austin Schuh | 13e5552 | 2020-02-29 23:11:17 -0800 | [diff] [blame] | 128 | CreateProfileParameters(*builder.fbb(), 0.7, 3.0)); |
Sabina Davis | a8fed3d | 2020-02-22 21:44:57 -0800 | [diff] [blame] | 129 | |
| 130 | flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal> |
| 131 | intake_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal( |
Austin Schuh | f0a637c | 2020-02-25 23:44:12 -0800 | [diff] [blame] | 132 | *builder.fbb(), intake_pos, |
| 133 | CreateProfileParameters(*builder.fbb(), 10.0, 30.0)); |
Sabina Davis | a8fed3d | 2020-02-22 21:44:57 -0800 | [diff] [blame] | 134 | |
| 135 | flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal> |
| 136 | turret_offset = CreateStaticZeroingSingleDOFProfiledSubsystemGoal( |
Austin Schuh | f0a637c | 2020-02-25 23:44:12 -0800 | [diff] [blame] | 137 | *builder.fbb(), turret_pos, |
| 138 | CreateProfileParameters(*builder.fbb(), 6.0, 20.0)); |
Sabina Davis | a8fed3d | 2020-02-22 21:44:57 -0800 | [diff] [blame] | 139 | |
| 140 | flatbuffers::Offset<superstructure::ShooterGoal> shooter_offset = |
Austin Schuh | f0a637c | 2020-02-25 23:44:12 -0800 | [diff] [blame] | 141 | superstructure::CreateShooterGoal(*builder.fbb(), accelerator_speed, |
| 142 | finisher_speed); |
Sabina Davis | a8fed3d | 2020-02-22 21:44:57 -0800 | [diff] [blame] | 143 | |
| 144 | superstructure::Goal::Builder superstructure_goal_builder = |
| 145 | builder.MakeBuilder<superstructure::Goal>(); |
| 146 | |
| 147 | superstructure_goal_builder.add_hood(hood_offset); |
| 148 | superstructure_goal_builder.add_intake(intake_offset); |
| 149 | superstructure_goal_builder.add_turret(turret_offset); |
| 150 | superstructure_goal_builder.add_roller_voltage(roller_speed); |
| 151 | superstructure_goal_builder.add_shooter(shooter_offset); |
Austin Schuh | 43a220f | 2020-02-26 22:02:34 -0800 | [diff] [blame] | 152 | superstructure_goal_builder.add_shooting(data.IsPressed(kFeed)); |
Tyler Chatow | 1039e43 | 2020-02-28 21:37:50 -0800 | [diff] [blame] | 153 | superstructure_goal_builder.add_climber_voltage(climber_speed); |
Sabina Davis | a8fed3d | 2020-02-22 21:44:57 -0800 | [diff] [blame] | 154 | |
| 155 | if (!builder.Send(superstructure_goal_builder.Finish())) { |
| 156 | AOS_LOG(ERROR, "Sending superstructure goal failed.\n"); |
| 157 | } |
| 158 | } |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | private: |
| 162 | ::aos::Sender<superstructure::Goal> superstructure_goal_sender_; |
| 163 | |
| 164 | ::aos::Fetcher<superstructure::Status> superstructure_status_fetcher_; |
| 165 | }; |
| 166 | |
| 167 | } // namespace joysticks |
| 168 | } // namespace input |
| 169 | } // namespace y2020 |
| 170 | |
| 171 | int main() { |
James Kuszmaul | ad8a808 | 2020-02-14 21:21:58 -0800 | [diff] [blame] | 172 | ::aos::InitNRT(); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 173 | |
| 174 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 175 | aos::configuration::ReadConfig("config.json"); |
| 176 | |
| 177 | ::aos::ShmEventLoop event_loop(&config.message()); |
| 178 | ::y2020::input::joysticks::Reader reader(&event_loop); |
| 179 | |
| 180 | event_loop.Run(); |
| 181 | |
| 182 | ::aos::Cleanup(); |
| 183 | } |