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