Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | #include <unistd.h> |
| 4 | #include <math.h> |
| 5 | |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 6 | #include "aos/actions/actions.h" |
John Park | 398c74a | 2018-10-20 21:17:39 -0700 | [diff] [blame] | 7 | #include "aos/init.h" |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 8 | #include "aos/input/action_joystick_input.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 9 | #include "aos/input/driver_station_data.h" |
| 10 | #include "aos/logging/logging.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 11 | #include "aos/time/time.h" |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 12 | #include "aos/util/log_interval.h" |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 13 | |
Austin Schuh | 3028b1d | 2017-03-11 22:12:13 -0800 | [diff] [blame] | 14 | #include "frc971/autonomous/auto.q.h" |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 15 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
Austin Schuh | 3028b1d | 2017-03-11 22:12:13 -0800 | [diff] [blame] | 16 | #include "frc971/queues/gyro.q.h" |
| 17 | #include "y2016/actors/autonomous_actor.h" |
| 18 | #include "y2016/actors/superstructure_actor.h" |
| 19 | #include "y2016/actors/vision_align_actor.h" |
| 20 | #include "y2016/constants.h" |
| 21 | #include "y2016/control_loops/drivetrain/drivetrain_base.h" |
Comran Morshed | 200dd4b | 2016-02-16 17:54:58 +0000 | [diff] [blame] | 22 | #include "y2016/control_loops/shooter/shooter.q.h" |
Austin Schuh | 45d07f6 | 2016-03-13 15:33:31 -0700 | [diff] [blame] | 23 | #include "y2016/control_loops/superstructure/superstructure.h" |
Austin Schuh | 3028b1d | 2017-03-11 22:12:13 -0800 | [diff] [blame] | 24 | #include "y2016/control_loops/superstructure/superstructure.q.h" |
Comran Morshed | aa0573c | 2016-03-05 19:05:54 +0000 | [diff] [blame] | 25 | #include "y2016/queues/ball_detector.q.h" |
Austin Schuh | add6d79 | 2016-03-19 01:20:01 -0700 | [diff] [blame] | 26 | #include "y2016/vision/vision.q.h" |
Comran Morshed | 200dd4b | 2016-02-16 17:54:58 +0000 | [diff] [blame] | 27 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 28 | using ::aos::input::driver_station::ButtonLocation; |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 29 | using ::aos::input::driver_station::ControlBit; |
Austin Schuh | 4ea06c1 | 2016-03-12 17:54:31 -0800 | [diff] [blame] | 30 | using ::aos::input::driver_station::JoystickAxis; |
| 31 | using ::aos::input::driver_station::POVLocation; |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 32 | |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 33 | namespace y2016 { |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 34 | namespace input { |
| 35 | namespace joysticks { |
| 36 | |
Austin Schuh | 45d07f6 | 2016-03-13 15:33:31 -0700 | [diff] [blame] | 37 | namespace { |
| 38 | |
| 39 | constexpr double kMaxIntakeAngleBeforeArmInterference = control_loops:: |
| 40 | superstructure::CollisionAvoidance::kMaxIntakeAngleBeforeArmInterference; |
| 41 | |
| 42 | } // namespace |
| 43 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 44 | const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2); |
Campbell Crowley | 5b27f02 | 2016-02-20 16:55:35 -0800 | [diff] [blame] | 45 | const ButtonLocation kShiftHigh(2, 3), kShiftHigh2(2, 2), kShiftLow(2, 1); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 46 | const ButtonLocation kQuickTurn(1, 5); |
| 47 | |
Austin Schuh | 781cdcc | 2016-03-12 13:03:12 -0800 | [diff] [blame] | 48 | const ButtonLocation kTurn1(1, 7); |
| 49 | const ButtonLocation kTurn2(1, 11); |
| 50 | |
Comran Morshed | 200dd4b | 2016-02-16 17:54:58 +0000 | [diff] [blame] | 51 | // Buttons on the lexan driver station to get things running on bring-up day. |
Austin Schuh | 4ea06c1 | 2016-03-12 17:54:31 -0800 | [diff] [blame] | 52 | const ButtonLocation kIntakeDown(3, 11); |
| 53 | const POVLocation kFrontLong(3, 180); |
| 54 | const POVLocation kBackLong(3, 0); |
Austin Schuh | 45d07f6 | 2016-03-13 15:33:31 -0700 | [diff] [blame] | 55 | const POVLocation kBackFender(3, 90); |
| 56 | const POVLocation kFrontFender(3, 270); |
Austin Schuh | 4ea06c1 | 2016-03-12 17:54:31 -0800 | [diff] [blame] | 57 | const ButtonLocation kIntakeIn(3, 12); |
Austin Schuh | 4ea06c1 | 2016-03-12 17:54:31 -0800 | [diff] [blame] | 58 | const ButtonLocation kFire(3, 3); |
Austin Schuh | 4ea06c1 | 2016-03-12 17:54:31 -0800 | [diff] [blame] | 59 | const ButtonLocation kIntakeOut(3, 9); |
Austin Schuh | 843412b | 2016-03-20 16:48:46 -0700 | [diff] [blame] | 60 | const ButtonLocation kPortcullis(3, 7); |
| 61 | const ButtonLocation kChevalDeFrise(3, 8); |
Comran Morshed | 200dd4b | 2016-02-16 17:54:58 +0000 | [diff] [blame] | 62 | |
Austin Schuh | add6d79 | 2016-03-19 01:20:01 -0700 | [diff] [blame] | 63 | const ButtonLocation kVisionAlign(3, 4); |
Austin Schuh | 1879911 | 2016-03-16 22:09:54 -0700 | [diff] [blame] | 64 | |
Diana Vandenberg | 9cc9ab6 | 2016-04-20 21:27:47 -0700 | [diff] [blame] | 65 | const ButtonLocation kExpand(3, 6); |
| 66 | const ButtonLocation kWinch(3, 5); |
| 67 | |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 68 | class Reader : public ::aos::input::ActionJoystickInput { |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 69 | public: |
Austin Schuh | 3e45c75 | 2019-02-02 12:19:11 -0800 | [diff] [blame] | 70 | Reader(::aos::EventLoop *event_loop) |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 71 | : ::aos::input::ActionJoystickInput( |
| 72 | event_loop, control_loops::drivetrain::GetDrivetrainConfig(), |
| 73 | ::aos::input::DrivetrainInputReader::InputType::kSteeringWheel, {}), |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 74 | vision_status_fetcher_( |
| 75 | event_loop->MakeFetcher<::y2016::vision::VisionStatus>( |
| 76 | ".y2016.vision.vision_status")), |
Austin Schuh | 4b652c9 | 2019-05-27 13:22:27 -0700 | [diff] [blame] | 77 | ball_detector_fetcher_( |
| 78 | event_loop->MakeFetcher<::y2016::sensors::BallDetector>( |
| 79 | ".y2016.sensors.ball_detector")), |
Austin Schuh | ae023fb | 2019-06-29 17:11:45 -0700 | [diff] [blame] | 80 | shooter_goal_sender_( |
| 81 | event_loop->MakeSender< |
| 82 | ::y2016::control_loops::shooter::ShooterQueue::Goal>( |
| 83 | ".y2016.control_loops.shooter.shooter_queue.goal")), |
Austin Schuh | 9481d0d | 2019-06-29 21:56:17 -0700 | [diff] [blame] | 84 | superstructure_status_fetcher_( |
| 85 | event_loop->MakeFetcher< |
| 86 | ::y2016::control_loops::SuperstructureQueue::Status>( |
| 87 | ".y2016.control_loops.superstructure_queue.status")), |
| 88 | superstructure_goal_sender_( |
| 89 | event_loop |
| 90 | ->MakeSender<::y2016::control_loops::SuperstructureQueue::Goal>( |
| 91 | ".y2016.control_loops.superstructure_queue.goal")), |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 92 | drivetrain_goal_fetcher_( |
| 93 | event_loop |
| 94 | ->MakeFetcher<::frc971::control_loops::DrivetrainQueue::Goal>( |
| 95 | ".frc971.control_loops.drivetrain_queue.goal")), |
| 96 | drivetrain_status_fetcher_( |
| 97 | event_loop |
| 98 | ->MakeFetcher<::frc971::control_loops::DrivetrainQueue::Status>( |
| 99 | ".frc971.control_loops.drivetrain_queue.status")), |
Comran Morshed | 200dd4b | 2016-02-16 17:54:58 +0000 | [diff] [blame] | 100 | intake_goal_(0.0), |
| 101 | shoulder_goal_(M_PI / 2.0), |
Austin Schuh | d52df77 | 2016-03-19 15:38:41 -0700 | [diff] [blame] | 102 | wrist_goal_(0.0), |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 103 | vision_align_action_factory_( |
| 104 | actors::VisionAlignActor::MakeFactory(event_loop)), |
| 105 | superstructure_action_factory_( |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 106 | actors::SuperstructureActor::MakeFactory(event_loop)) { |
| 107 | set_vision_align_fn([this](const ::aos::input::driver_station::Data &data) { |
| 108 | return VisionAlign(data); |
| 109 | }); |
| 110 | } |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 111 | |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 112 | // Returns true when we are vision aligning |
| 113 | bool VisionAlign(const ::aos::input::driver_station::Data &data) { |
Austin Schuh | add6d79 | 2016-03-19 01:20:01 -0700 | [diff] [blame] | 114 | vision_valid_ = false; |
| 115 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 116 | vision_status_fetcher_.Fetch(); |
Austin Schuh | add6d79 | 2016-03-19 01:20:01 -0700 | [diff] [blame] | 117 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 118 | if (vision_status_fetcher_.get()) { |
| 119 | vision_valid_ = (vision_status_fetcher_->left_image_valid && |
| 120 | vision_status_fetcher_->right_image_valid); |
| 121 | last_angle_ = vision_status_fetcher_->horizontal_angle; |
Austin Schuh | add6d79 | 2016-03-19 01:20:01 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Austin Schuh | 974cf65 | 2016-04-20 20:18:13 -0700 | [diff] [blame] | 124 | if (data.IsPressed(kVisionAlign)) { |
| 125 | if (vision_valid_ && !vision_action_running_) { |
| 126 | actors::VisionAlignActionParams params; |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 127 | EnqueueAction(vision_align_action_factory_.Make(params)); |
Austin Schuh | 974cf65 | 2016-04-20 20:18:13 -0700 | [diff] [blame] | 128 | vision_action_running_ = true; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 129 | AOS_LOG(INFO, "Starting vision align\n"); |
Austin Schuh | 974cf65 | 2016-04-20 20:18:13 -0700 | [diff] [blame] | 130 | } else { |
| 131 | if (!vision_valid_) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 132 | AOS_LOG(INFO, "Vision align but not valid\n"); |
Austin Schuh | 974cf65 | 2016-04-20 20:18:13 -0700 | [diff] [blame] | 133 | } |
| 134 | } |
Austin Schuh | 1879911 | 2016-03-16 22:09:54 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | if (data.NegEdge(kVisionAlign)) { |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 138 | CancelAllActions(); |
Austin Schuh | 1879911 | 2016-03-16 22:09:54 -0700 | [diff] [blame] | 139 | } |
Austin Schuh | add6d79 | 2016-03-19 01:20:01 -0700 | [diff] [blame] | 140 | if (!data.IsPressed(kVisionAlign)) { |
| 141 | vision_action_running_ = false; |
| 142 | } |
Austin Schuh | 1879911 | 2016-03-16 22:09:54 -0700 | [diff] [blame] | 143 | |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 144 | return vision_action_running_; |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 147 | void HandleTeleop(const ::aos::input::driver_station::Data &data) { |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 148 | if (!data.GetControlBit(ControlBit::kEnabled)) { |
| 149 | // If we are not enabled, reset the waiting for zero bit. |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 150 | AOS_LOG(DEBUG, "Waiting for zero.\n"); |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 151 | waiting_for_zero_ = true; |
| 152 | } |
| 153 | |
Diana Vandenberg | 9cc9ab6 | 2016-04-20 21:27:47 -0700 | [diff] [blame] | 154 | float voltage_climber = 0.0; |
Austin Schuh | 45d07f6 | 2016-03-13 15:33:31 -0700 | [diff] [blame] | 155 | // Default the intake to up. |
| 156 | intake_goal_ = constants::Values::kIntakeRange.upper - 0.04; |
| 157 | |
| 158 | bool force_lights_on = false; |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 159 | if (!data.GetControlBit(ControlBit::kEnabled)) { |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 160 | CancelAllActions(); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 161 | AOS_LOG(DEBUG, "Canceling\n"); |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Austin Schuh | 9481d0d | 2019-06-29 21:56:17 -0700 | [diff] [blame] | 164 | superstructure_status_fetcher_.Fetch(); |
| 165 | if (!superstructure_status_fetcher_.get()) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 166 | AOS_LOG(ERROR, "Got no superstructure status packet.\n"); |
Comran Morshed | 200dd4b | 2016-02-16 17:54:58 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Austin Schuh | 9481d0d | 2019-06-29 21:56:17 -0700 | [diff] [blame] | 169 | if (superstructure_status_fetcher_.get() && |
| 170 | superstructure_status_fetcher_->zeroed) { |
Comran Morshed | 200dd4b | 2016-02-16 17:54:58 +0000 | [diff] [blame] | 171 | if (waiting_for_zero_) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 172 | AOS_LOG(DEBUG, "Zeroed! Starting teleop mode.\n"); |
Comran Morshed | 200dd4b | 2016-02-16 17:54:58 +0000 | [diff] [blame] | 173 | waiting_for_zero_ = false; |
| 174 | } |
| 175 | } else { |
| 176 | waiting_for_zero_ = true; |
| 177 | } |
| 178 | |
Austin Schuh | add6d79 | 2016-03-19 01:20:01 -0700 | [diff] [blame] | 179 | double intake_when_shooting = kMaxIntakeAngleBeforeArmInterference; |
| 180 | bool use_slow_profile = false; |
| 181 | if (vision_action_running_) { |
| 182 | use_slow_profile = true; |
| 183 | if (vision_valid_) { |
| 184 | intake_when_shooting -= 0.5; |
| 185 | } |
| 186 | } |
| 187 | |
Diana Vandenberg | 9cc9ab6 | 2016-04-20 21:27:47 -0700 | [diff] [blame] | 188 | if (data.IsPressed(kFrontLong)) { |
Austin Schuh | 4ea06c1 | 2016-03-12 17:54:31 -0800 | [diff] [blame] | 189 | // Forwards shot |
Austin Schuh | 3d79cc0 | 2016-03-20 21:08:53 -0700 | [diff] [blame] | 190 | shoulder_goal_ = M_PI / 2.0 + 0.1; |
Austin Schuh | 6a871ff | 2016-05-01 12:31:23 -0700 | [diff] [blame] | 191 | wrist_goal_ = M_PI + 0.41 + 0.02 - 0.005; |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 192 | drivetrain_status_fetcher_.Fetch(); |
| 193 | if (drivetrain_status_fetcher_.get()) { |
| 194 | wrist_goal_ += drivetrain_status_fetcher_->ground_angle; |
Austin Schuh | b009964 | 2016-04-03 21:37:27 -0700 | [diff] [blame] | 195 | } |
Austin Schuh | 4ea06c1 | 2016-03-12 17:54:31 -0800 | [diff] [blame] | 196 | shooter_velocity_ = 640.0; |
Austin Schuh | add6d79 | 2016-03-19 01:20:01 -0700 | [diff] [blame] | 197 | intake_goal_ = intake_when_shooting; |
Austin Schuh | 4ea06c1 | 2016-03-12 17:54:31 -0800 | [diff] [blame] | 198 | } else if (data.IsPressed(kBackLong)) { |
| 199 | // Backwards shot |
Austin Schuh | 3d79cc0 | 2016-03-20 21:08:53 -0700 | [diff] [blame] | 200 | shoulder_goal_ = M_PI / 2.0 - 0.4; |
Austin Schuh | e153c5a | 2016-04-20 20:18:42 -0700 | [diff] [blame] | 201 | wrist_goal_ = -0.62 - 0.02; |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 202 | drivetrain_status_fetcher_.Fetch(); |
| 203 | if (drivetrain_status_fetcher_.get()) { |
| 204 | wrist_goal_ += drivetrain_status_fetcher_->ground_angle; |
Austin Schuh | b009964 | 2016-04-03 21:37:27 -0700 | [diff] [blame] | 205 | } |
Austin Schuh | 4ea06c1 | 2016-03-12 17:54:31 -0800 | [diff] [blame] | 206 | shooter_velocity_ = 640.0; |
Austin Schuh | add6d79 | 2016-03-19 01:20:01 -0700 | [diff] [blame] | 207 | intake_goal_ = intake_when_shooting; |
Austin Schuh | 45d07f6 | 2016-03-13 15:33:31 -0700 | [diff] [blame] | 208 | } else if (data.IsPressed(kBackFender)) { |
Adam Snaider | a3271fe | 2016-10-26 21:03:38 -0700 | [diff] [blame] | 209 | // Backwards shot no IMU |
| 210 | shoulder_goal_ = M_PI / 2.0 - 0.4; |
| 211 | wrist_goal_ = -0.62 - 0.02; |
| 212 | shooter_velocity_ = 640.0; |
Austin Schuh | add6d79 | 2016-03-19 01:20:01 -0700 | [diff] [blame] | 213 | intake_goal_ = intake_when_shooting; |
Austin Schuh | 45d07f6 | 2016-03-13 15:33:31 -0700 | [diff] [blame] | 214 | } else if (data.IsPressed(kFrontFender)) { |
Adam Snaider | a3271fe | 2016-10-26 21:03:38 -0700 | [diff] [blame] | 215 | // Forwards shot no IMU |
Austin Schuh | 6a871ff | 2016-05-01 12:31:23 -0700 | [diff] [blame] | 216 | shoulder_goal_ = M_PI / 2.0 + 0.1; |
Adam Snaider | a3271fe | 2016-10-26 21:03:38 -0700 | [diff] [blame] | 217 | wrist_goal_ = M_PI + 0.41 + 0.02 - 0.005; |
Austin Schuh | 6a871ff | 2016-05-01 12:31:23 -0700 | [diff] [blame] | 218 | shooter_velocity_ = 640.0; |
Austin Schuh | add6d79 | 2016-03-19 01:20:01 -0700 | [diff] [blame] | 219 | intake_goal_ = intake_when_shooting; |
Diana Vandenberg | 9cc9ab6 | 2016-04-20 21:27:47 -0700 | [diff] [blame] | 220 | } else if (data.IsPressed(kExpand) || data.IsPressed(kWinch)) { |
| 221 | // Set the goals to the hanging position so when the actor finishes, we |
| 222 | // will still be at the right spot. |
| 223 | shoulder_goal_ = 1.2; |
Austin Schuh | fef64ac | 2016-04-24 19:08:01 -0700 | [diff] [blame] | 224 | wrist_goal_ = 1.0; |
Diana Vandenberg | 9cc9ab6 | 2016-04-20 21:27:47 -0700 | [diff] [blame] | 225 | intake_goal_ = 0.0; |
| 226 | if (data.PosEdge(kExpand)) { |
| 227 | is_expanding_ = true; |
| 228 | actors::SuperstructureActionParams params; |
| 229 | params.partial_angle = 0.3; |
| 230 | params.delay_time = 0.7; |
| 231 | params.full_angle = shoulder_goal_; |
Austin Schuh | fef64ac | 2016-04-24 19:08:01 -0700 | [diff] [blame] | 232 | params.shooter_angle = wrist_goal_; |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 233 | EnqueueAction(superstructure_action_factory_.Make(params)); |
Diana Vandenberg | 9cc9ab6 | 2016-04-20 21:27:47 -0700 | [diff] [blame] | 234 | } |
| 235 | if (data.IsPressed(kWinch)) { |
| 236 | voltage_climber = 12.0; |
| 237 | } |
Austin Schuh | de802e9 | 2016-02-27 14:49:03 -0800 | [diff] [blame] | 238 | } else { |
Austin Schuh | 4ea06c1 | 2016-03-12 17:54:31 -0800 | [diff] [blame] | 239 | wrist_goal_ = 0.0; |
Austin Schuh | 3f0b119 | 2016-03-12 13:03:56 -0800 | [diff] [blame] | 240 | shoulder_goal_ = -0.010; |
Austin Schuh | 4ea06c1 | 2016-03-12 17:54:31 -0800 | [diff] [blame] | 241 | shooter_velocity_ = 0.0; |
Comran Morshed | 200dd4b | 2016-02-16 17:54:58 +0000 | [diff] [blame] | 242 | } |
Diana Vandenberg | 9cc9ab6 | 2016-04-20 21:27:47 -0700 | [diff] [blame] | 243 | if (data.NegEdge(kExpand) || voltage_climber > 1.0) { |
| 244 | is_expanding_ = false; |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 245 | CancelAllActions(); |
Diana Vandenberg | 9cc9ab6 | 2016-04-20 21:27:47 -0700 | [diff] [blame] | 246 | } |
Comran Morshed | 200dd4b | 2016-02-16 17:54:58 +0000 | [diff] [blame] | 247 | |
Comran Morshed | aa0573c | 2016-03-05 19:05:54 +0000 | [diff] [blame] | 248 | bool ball_detected = false; |
Austin Schuh | 4b652c9 | 2019-05-27 13:22:27 -0700 | [diff] [blame] | 249 | ball_detector_fetcher_.Fetch(); |
| 250 | if (ball_detector_fetcher_.get()) { |
| 251 | ball_detected = ball_detector_fetcher_->voltage > 2.5; |
Comran Morshed | aa0573c | 2016-03-05 19:05:54 +0000 | [diff] [blame] | 252 | } |
| 253 | if (data.PosEdge(kIntakeIn)) { |
| 254 | saw_ball_when_started_intaking_ = ball_detected; |
| 255 | } |
| 256 | |
Austin Schuh | 45d07f6 | 2016-03-13 15:33:31 -0700 | [diff] [blame] | 257 | if (data.IsPressed(kIntakeIn)) { |
| 258 | is_intaking_ = (!ball_detected || saw_ball_when_started_intaking_); |
| 259 | if (ball_detected) { |
| 260 | force_lights_on = true; |
| 261 | } |
| 262 | } else { |
| 263 | is_intaking_ = false; |
| 264 | } |
| 265 | |
Austin Schuh | d52df77 | 2016-03-19 15:38:41 -0700 | [diff] [blame] | 266 | fire_ = false; |
Austin Schuh | 4ea06c1 | 2016-03-12 17:54:31 -0800 | [diff] [blame] | 267 | if (data.IsPressed(kFire) && shooter_velocity_ != 0.0) { |
Austin Schuh | d52df77 | 2016-03-19 15:38:41 -0700 | [diff] [blame] | 268 | if (data.IsPressed(kVisionAlign)) { |
| 269 | // Make sure that we are lined up. |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 270 | drivetrain_status_fetcher_.Fetch(); |
| 271 | drivetrain_goal_fetcher_.Fetch(); |
| 272 | if (drivetrain_status_fetcher_.get() && |
| 273 | drivetrain_goal_fetcher_.get()) { |
| 274 | const double left_goal = drivetrain_goal_fetcher_->left_goal; |
| 275 | const double right_goal = drivetrain_goal_fetcher_->right_goal; |
Austin Schuh | d52df77 | 2016-03-19 15:38:41 -0700 | [diff] [blame] | 276 | const double left_current = |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 277 | drivetrain_status_fetcher_->estimated_left_position; |
Austin Schuh | d52df77 | 2016-03-19 15:38:41 -0700 | [diff] [blame] | 278 | const double right_current = |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 279 | drivetrain_status_fetcher_->estimated_right_position; |
Austin Schuh | d52df77 | 2016-03-19 15:38:41 -0700 | [diff] [blame] | 280 | const double left_velocity = |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 281 | drivetrain_status_fetcher_->estimated_left_velocity; |
Austin Schuh | d52df77 | 2016-03-19 15:38:41 -0700 | [diff] [blame] | 282 | const double right_velocity = |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 283 | drivetrain_status_fetcher_->estimated_right_velocity; |
Austin Schuh | d52df77 | 2016-03-19 15:38:41 -0700 | [diff] [blame] | 284 | if (vision_action_running_ && ::std::abs(last_angle_) < 0.02 && |
| 285 | ::std::abs((left_goal - right_goal) - |
| 286 | (left_current - right_current)) / |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 287 | dt_config().robot_radius / 2.0 < |
Austin Schuh | d52df77 | 2016-03-19 15:38:41 -0700 | [diff] [blame] | 288 | 0.02 && |
| 289 | ::std::abs(left_velocity - right_velocity) < 0.01) { |
| 290 | ++ready_to_fire_; |
| 291 | } else { |
| 292 | ready_to_fire_ = 0; |
| 293 | } |
Austin Schuh | 3d79cc0 | 2016-03-20 21:08:53 -0700 | [diff] [blame] | 294 | if (ready_to_fire_ > 9) { |
Austin Schuh | d52df77 | 2016-03-19 15:38:41 -0700 | [diff] [blame] | 295 | fire_ = true; |
| 296 | } |
| 297 | } |
| 298 | } else { |
| 299 | fire_ = true; |
| 300 | } |
Comran Morshed | 200dd4b | 2016-02-16 17:54:58 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Austin Schuh | 4ea06c1 | 2016-03-12 17:54:31 -0800 | [diff] [blame] | 303 | is_outtaking_ = data.IsPressed(kIntakeOut); |
Austin Schuh | 5a6db4d | 2016-02-28 22:02:42 -0800 | [diff] [blame] | 304 | |
Austin Schuh | d52df77 | 2016-03-19 15:38:41 -0700 | [diff] [blame] | 305 | if (is_intaking_ || is_outtaking_) { |
| 306 | recently_intaking_accumulator_ = 20; |
| 307 | } |
| 308 | |
| 309 | if (data.IsPressed(kIntakeDown)) { |
| 310 | if (recently_intaking_accumulator_) { |
| 311 | intake_goal_ = 0.1; |
| 312 | } else { |
| 313 | intake_goal_ = -0.05; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | if (recently_intaking_accumulator_ > 0) { |
| 318 | --recently_intaking_accumulator_; |
| 319 | } |
| 320 | |
Austin Schuh | 843412b | 2016-03-20 16:48:46 -0700 | [diff] [blame] | 321 | if (data.IsPressed(kPortcullis)) { |
| 322 | traverse_unlatched_ = true; |
| 323 | traverse_down_ = true; |
| 324 | } else if (data.IsPressed(kChevalDeFrise)) { |
| 325 | traverse_unlatched_ = false; |
| 326 | traverse_down_ = true; |
| 327 | } else { |
| 328 | traverse_unlatched_ = true; |
| 329 | traverse_down_ = false; |
| 330 | } |
| 331 | |
Comran Morshed | 200dd4b | 2016-02-16 17:54:58 +0000 | [diff] [blame] | 332 | if (!waiting_for_zero_) { |
Diana Vandenberg | 9cc9ab6 | 2016-04-20 21:27:47 -0700 | [diff] [blame] | 333 | if (!is_expanding_) { |
Austin Schuh | 9481d0d | 2019-06-29 21:56:17 -0700 | [diff] [blame] | 334 | auto new_superstructure_goal = |
| 335 | superstructure_goal_sender_.MakeMessage(); |
Diana Vandenberg | 9cc9ab6 | 2016-04-20 21:27:47 -0700 | [diff] [blame] | 336 | new_superstructure_goal->angle_intake = intake_goal_; |
| 337 | new_superstructure_goal->angle_shoulder = shoulder_goal_; |
| 338 | new_superstructure_goal->angle_wrist = wrist_goal_; |
Austin Schuh | 3f0b119 | 2016-03-12 13:03:56 -0800 | [diff] [blame] | 339 | |
Diana Vandenberg | 9cc9ab6 | 2016-04-20 21:27:47 -0700 | [diff] [blame] | 340 | new_superstructure_goal->max_angular_velocity_intake = 7.0; |
| 341 | new_superstructure_goal->max_angular_velocity_shoulder = 4.0; |
| 342 | new_superstructure_goal->max_angular_velocity_wrist = 10.0; |
| 343 | if (use_slow_profile) { |
| 344 | new_superstructure_goal->max_angular_acceleration_intake = 10.0; |
| 345 | } else { |
| 346 | new_superstructure_goal->max_angular_acceleration_intake = 40.0; |
| 347 | } |
| 348 | new_superstructure_goal->max_angular_acceleration_shoulder = 10.0; |
| 349 | if (shoulder_goal_ > 1.0) { |
| 350 | new_superstructure_goal->max_angular_acceleration_wrist = 45.0; |
| 351 | } else { |
| 352 | new_superstructure_goal->max_angular_acceleration_wrist = 25.0; |
| 353 | } |
Austin Schuh | 3f0b119 | 2016-03-12 13:03:56 -0800 | [diff] [blame] | 354 | |
Diana Vandenberg | 9cc9ab6 | 2016-04-20 21:27:47 -0700 | [diff] [blame] | 355 | // Granny mode |
| 356 | /* |
| 357 | new_superstructure_goal->max_angular_velocity_intake = 0.2; |
| 358 | new_superstructure_goal->max_angular_velocity_shoulder = 0.2; |
| 359 | new_superstructure_goal->max_angular_velocity_wrist = 0.2; |
| 360 | new_superstructure_goal->max_angular_acceleration_intake = 1.0; |
| 361 | new_superstructure_goal->max_angular_acceleration_shoulder = 1.0; |
| 362 | new_superstructure_goal->max_angular_acceleration_wrist = 1.0; |
| 363 | */ |
| 364 | if (is_intaking_) { |
| 365 | new_superstructure_goal->voltage_top_rollers = 12.0; |
| 366 | new_superstructure_goal->voltage_bottom_rollers = 12.0; |
| 367 | } else if (is_outtaking_) { |
| 368 | new_superstructure_goal->voltage_top_rollers = -12.0; |
| 369 | new_superstructure_goal->voltage_bottom_rollers = -7.0; |
| 370 | } else { |
| 371 | new_superstructure_goal->voltage_top_rollers = 0.0; |
| 372 | new_superstructure_goal->voltage_bottom_rollers = 0.0; |
| 373 | } |
Comran Morshed | 200dd4b | 2016-02-16 17:54:58 +0000 | [diff] [blame] | 374 | |
Diana Vandenberg | 9cc9ab6 | 2016-04-20 21:27:47 -0700 | [diff] [blame] | 375 | new_superstructure_goal->traverse_unlatched = traverse_unlatched_; |
| 376 | new_superstructure_goal->unfold_climber = false; |
| 377 | new_superstructure_goal->voltage_climber = voltage_climber; |
| 378 | new_superstructure_goal->traverse_down = traverse_down_; |
| 379 | new_superstructure_goal->force_intake = true; |
Austin Schuh | 843412b | 2016-03-20 16:48:46 -0700 | [diff] [blame] | 380 | |
Diana Vandenberg | 9cc9ab6 | 2016-04-20 21:27:47 -0700 | [diff] [blame] | 381 | if (!new_superstructure_goal.Send()) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 382 | AOS_LOG(ERROR, "Sending superstructure goal failed.\n"); |
Diana Vandenberg | 9cc9ab6 | 2016-04-20 21:27:47 -0700 | [diff] [blame] | 383 | } else { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 384 | AOS_LOG(DEBUG, "sending goals: intake: %f, shoulder: %f, wrist: %f\n", |
| 385 | intake_goal_, shoulder_goal_, wrist_goal_); |
Diana Vandenberg | 9cc9ab6 | 2016-04-20 21:27:47 -0700 | [diff] [blame] | 386 | } |
Austin Schuh | add6d79 | 2016-03-19 01:20:01 -0700 | [diff] [blame] | 387 | } |
Comran Morshed | 200dd4b | 2016-02-16 17:54:58 +0000 | [diff] [blame] | 388 | |
Austin Schuh | ae023fb | 2019-06-29 17:11:45 -0700 | [diff] [blame] | 389 | auto shooter_message = shooter_goal_sender_.MakeMessage(); |
| 390 | shooter_message->angular_velocity = shooter_velocity_; |
| 391 | shooter_message->clamp_open = is_intaking_ || is_outtaking_; |
| 392 | shooter_message->push_to_shooter = fire_; |
| 393 | shooter_message->force_lights_on = force_lights_on; |
| 394 | shooter_message->shooting_forwards = wrist_goal_ > 0; |
| 395 | |
| 396 | if (!shooter_message.Send()) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 397 | AOS_LOG(ERROR, "Sending shooter goal failed.\n"); |
Comran Morshed | 200dd4b | 2016-02-16 17:54:58 +0000 | [diff] [blame] | 398 | } |
| 399 | } |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 402 | private: |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 403 | ::aos::Fetcher<::y2016::vision::VisionStatus> vision_status_fetcher_; |
Austin Schuh | 4b652c9 | 2019-05-27 13:22:27 -0700 | [diff] [blame] | 404 | ::aos::Fetcher<::y2016::sensors::BallDetector> ball_detector_fetcher_; |
Austin Schuh | ae023fb | 2019-06-29 17:11:45 -0700 | [diff] [blame] | 405 | ::aos::Sender<::y2016::control_loops::shooter::ShooterQueue::Goal> |
| 406 | shooter_goal_sender_; |
Austin Schuh | 9481d0d | 2019-06-29 21:56:17 -0700 | [diff] [blame] | 407 | ::aos::Fetcher<::y2016::control_loops::SuperstructureQueue::Status> |
| 408 | superstructure_status_fetcher_; |
| 409 | ::aos::Sender<::y2016::control_loops::SuperstructureQueue::Goal> |
| 410 | superstructure_goal_sender_; |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 411 | ::aos::Fetcher<::frc971::control_loops::DrivetrainQueue::Goal> |
| 412 | drivetrain_goal_fetcher_; |
| 413 | ::aos::Fetcher<::frc971::control_loops::DrivetrainQueue::Status> |
| 414 | drivetrain_status_fetcher_; |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 415 | |
Comran Morshed | 200dd4b | 2016-02-16 17:54:58 +0000 | [diff] [blame] | 416 | // Whatever these are set to are our default goals to send out after zeroing. |
| 417 | double intake_goal_; |
| 418 | double shoulder_goal_; |
| 419 | double wrist_goal_; |
Austin Schuh | de802e9 | 2016-02-27 14:49:03 -0800 | [diff] [blame] | 420 | double shooter_velocity_ = 0.0; |
Comran Morshed | 200dd4b | 2016-02-16 17:54:58 +0000 | [diff] [blame] | 421 | |
Austin Schuh | 843412b | 2016-03-20 16:48:46 -0700 | [diff] [blame] | 422 | bool traverse_unlatched_ = false; |
| 423 | bool traverse_down_ = false; |
| 424 | |
Comran Morshed | 200dd4b | 2016-02-16 17:54:58 +0000 | [diff] [blame] | 425 | // If we're waiting for the subsystems to zero. |
| 426 | bool waiting_for_zero_ = true; |
| 427 | |
Comran Morshed | aa0573c | 2016-03-05 19:05:54 +0000 | [diff] [blame] | 428 | // If true, the ball was present when the intaking button was pressed. |
| 429 | bool saw_ball_when_started_intaking_ = false; |
| 430 | |
Austin Schuh | de802e9 | 2016-02-27 14:49:03 -0800 | [diff] [blame] | 431 | bool is_intaking_ = false; |
Austin Schuh | 5a6db4d | 2016-02-28 22:02:42 -0800 | [diff] [blame] | 432 | bool is_outtaking_ = false; |
Austin Schuh | de802e9 | 2016-02-27 14:49:03 -0800 | [diff] [blame] | 433 | bool fire_ = false; |
| 434 | |
Austin Schuh | add6d79 | 2016-03-19 01:20:01 -0700 | [diff] [blame] | 435 | bool vision_action_running_ = false; |
| 436 | bool vision_valid_ = false; |
| 437 | |
Austin Schuh | d52df77 | 2016-03-19 15:38:41 -0700 | [diff] [blame] | 438 | int recently_intaking_accumulator_ = 0; |
| 439 | double last_angle_ = 100; |
| 440 | |
| 441 | int ready_to_fire_ = 0; |
| 442 | |
Diana Vandenberg | 9cc9ab6 | 2016-04-20 21:27:47 -0700 | [diff] [blame] | 443 | bool is_expanding_ = false; |
| 444 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 445 | actors::VisionAlignActor::Factory vision_align_action_factory_; |
| 446 | actors::SuperstructureActor::Factory superstructure_action_factory_; |
| 447 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 448 | ::aos::util::SimpleLogInterval no_drivetrain_status_ = |
Austin Schuh | 61bdc60 | 2016-12-04 19:10:10 -0800 | [diff] [blame] | 449 | ::aos::util::SimpleLogInterval(::std::chrono::milliseconds(200), WARNING, |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 450 | "no drivetrain status"); |
| 451 | }; |
| 452 | |
| 453 | } // namespace joysticks |
| 454 | } // namespace input |
Comran Morshed | 6c6a0a9 | 2016-01-17 12:45:16 +0000 | [diff] [blame] | 455 | } // namespace y2016 |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 456 | |
| 457 | int main() { |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 458 | ::aos::InitNRT(true); |
| 459 | |
Austin Schuh | 3e45c75 | 2019-02-02 12:19:11 -0800 | [diff] [blame] | 460 | ::aos::ShmEventLoop event_loop; |
| 461 | ::y2016::input::joysticks::Reader reader(&event_loop); |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 462 | |
| 463 | event_loop.Run(); |
| 464 | |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 465 | ::aos::Cleanup(); |
| 466 | } |