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