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