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