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