Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [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/prime/input/joystick_input.h" |
Austin Schuh | 06cbbf1 | 2014-02-22 02:07:31 -0800 | [diff] [blame] | 8 | #include "aos/common/input/driver_station_data.h" |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 9 | #include "aos/common/logging/logging.h" |
Brian Silverman | a379f00 | 2014-03-22 19:34:53 -0700 | [diff] [blame] | 10 | #include "aos/common/util/log_interval.h" |
| 11 | #include "aos/common/time.h" |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 12 | |
| 13 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 14 | #include "frc971/constants.h" |
Brian Silverman | 6bf0d3c | 2014-03-08 12:52:54 -0800 | [diff] [blame] | 15 | #include "frc971/queues/other_sensors.q.h" |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 16 | #include "frc971/autonomous/auto.q.h" |
Brian Silverman | fac5c29 | 2014-02-17 15:26:57 -0800 | [diff] [blame] | 17 | #include "frc971/control_loops/claw/claw.q.h" |
| 18 | #include "frc971/control_loops/shooter/shooter.q.h" |
Ben Fredrickson | aa45045 | 2014-03-01 09:41:18 +0000 | [diff] [blame] | 19 | #include "frc971/actions/shoot_action.q.h" |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 20 | #include "frc971/actions/action_client.h" |
| 21 | #include "frc971/actions/catch_action.q.h" |
| 22 | #include "frc971/actions/shoot_action.h" |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 23 | |
| 24 | using ::frc971::control_loops::drivetrain; |
Brian Silverman | 6bf0d3c | 2014-03-08 12:52:54 -0800 | [diff] [blame] | 25 | using ::frc971::sensors::gyro_reading; |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 26 | |
| 27 | using ::aos::input::driver_station::ButtonLocation; |
| 28 | using ::aos::input::driver_station::JoystickAxis; |
| 29 | using ::aos::input::driver_station::ControlBit; |
| 30 | |
Brian Silverman | 5e1eb64 | 2014-07-20 16:37:35 -0700 | [diff] [blame] | 31 | #define OLD_DS 0 |
| 32 | |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 33 | namespace frc971 { |
| 34 | namespace input { |
| 35 | namespace joysticks { |
| 36 | |
| 37 | const ButtonLocation kDriveControlLoopEnable1(1, 7), |
| 38 | kDriveControlLoopEnable2(1, 11); |
| 39 | const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2); |
| 40 | const ButtonLocation kShiftHigh(2, 1), kShiftLow(2, 3); |
| 41 | const ButtonLocation kQuickTurn(1, 5); |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 42 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 43 | const ButtonLocation kCatch(3, 10); |
| 44 | |
Brian Silverman | 5e1eb64 | 2014-07-20 16:37:35 -0700 | [diff] [blame] | 45 | #if OLD_DS |
| 46 | const ButtonLocation kFire(3, 11); |
| 47 | const ButtonLocation kUnload(1, 4); |
| 48 | const ButtonLocation kReload(1, 2); |
| 49 | |
| 50 | const ButtonLocation kRollersOut(3, 12); |
| 51 | const ButtonLocation kRollersIn(3, 7); |
| 52 | |
| 53 | const ButtonLocation kTuck(3, 9); |
| 54 | const ButtonLocation kIntakePosition(3, 8); |
| 55 | const ButtonLocation kIntakeOpenPosition(3, 10); |
| 56 | const ButtonLocation kVerticalTuck(3, 1); |
| 57 | const JoystickAxis kFlipRobot(3, 3); |
| 58 | |
| 59 | const ButtonLocation kLongShot(3, 5); |
| 60 | const ButtonLocation kCloseShot(3, 2); |
Daniel Petti | d1a70f6 | 2014-08-10 16:56:44 -0700 | [diff] [blame] | 61 | const ButtonLocation kFenderShot(3, 3); |
Brian Silverman | 5e1eb64 | 2014-07-20 16:37:35 -0700 | [diff] [blame] | 62 | const ButtonLocation kTrussShot(2, 11); |
| 63 | const ButtonLocation kHumanPlayerShot(3, 2); |
| 64 | #else |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 65 | const ButtonLocation kFire(3, 9); |
Brian | 6faa782 | 2014-03-31 18:00:28 -0700 | [diff] [blame] | 66 | const ButtonLocation kUnload(1, 4); |
| 67 | const ButtonLocation kReload(1, 2); |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 68 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 69 | const ButtonLocation kRollersOut(3, 8); |
| 70 | const ButtonLocation kRollersIn(3, 3); |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 71 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 72 | const ButtonLocation kTuck(3, 4); |
| 73 | const ButtonLocation kIntakePosition(3, 5); |
| 74 | const ButtonLocation kIntakeOpenPosition(3, 11); |
Brian | 6faa782 | 2014-03-31 18:00:28 -0700 | [diff] [blame] | 75 | const ButtonLocation kVerticalTuck(2, 6); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 76 | const JoystickAxis kFlipRobot(3, 3); |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 77 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 78 | const ButtonLocation kLongShot(3, 7); |
Brian Silverman | 4051c6f | 2014-07-19 16:57:37 -0700 | [diff] [blame] | 79 | const ButtonLocation kCloseShot(3, 6); |
| 80 | const ButtonLocation kFenderShot(3, 2); |
Brian | 6faa782 | 2014-03-31 18:00:28 -0700 | [diff] [blame] | 81 | const ButtonLocation kTrussShot(2, 11); |
| 82 | const ButtonLocation kHumanPlayerShot(3, 1); |
Brian Silverman | 5e1eb64 | 2014-07-20 16:37:35 -0700 | [diff] [blame] | 83 | #endif |
Brian | 6faa782 | 2014-03-31 18:00:28 -0700 | [diff] [blame] | 84 | |
| 85 | const ButtonLocation kUserLeft(2, 7); |
| 86 | const ButtonLocation kUserRight(2, 10); |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 87 | |
Brian Silverman | 18f6e64 | 2014-03-13 18:52:47 -0700 | [diff] [blame] | 88 | const JoystickAxis kAdjustClawGoal(3, 2); |
| 89 | const JoystickAxis kAdjustClawSeparation(3, 1); |
| 90 | |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 91 | struct ClawGoal { |
| 92 | double angle; |
| 93 | double separation; |
| 94 | }; |
| 95 | |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 96 | struct ShotGoal { |
| 97 | ClawGoal claw; |
| 98 | double shot_power; |
Brian Silverman | a379f00 | 2014-03-22 19:34:53 -0700 | [diff] [blame] | 99 | double velocity_compensation; |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 100 | double intake_power; |
| 101 | }; |
| 102 | |
Brian Silverman | 545f2ad | 2014-03-14 12:31:42 -0700 | [diff] [blame] | 103 | const double kIntakePower = 4.0; |
Brian Silverman | 255b5c1 | 2014-04-25 09:21:40 -0500 | [diff] [blame] | 104 | // In case we have to quickly adjust it. |
Brian Silverman | b3cf0ef | 2014-03-22 12:45:55 -0700 | [diff] [blame] | 105 | const double kGrabSeparation = 0; |
Brian Silverman | 545f2ad | 2014-03-14 12:31:42 -0700 | [diff] [blame] | 106 | const double kShootSeparation = 0.11 + kGrabSeparation; |
| 107 | |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 108 | const ClawGoal kTuckGoal = {-2.273474, -0.749484}; |
Brian | 6faa782 | 2014-03-31 18:00:28 -0700 | [diff] [blame] | 109 | const ClawGoal kVerticalTuckGoal = {0, kGrabSeparation}; |
Brian Silverman | 63ec750 | 2014-03-30 18:09:10 -0700 | [diff] [blame] | 110 | const ClawGoal kIntakeGoal = {-2.24, kGrabSeparation}; |
| 111 | const ClawGoal kIntakeOpenGoal = {-2.0, 1.1}; |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 112 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 113 | // TODO(austin): Tune these by hand... |
| 114 | const ClawGoal kFlippedTuckGoal = {2.733474, -0.75}; |
Brian Silverman | 545f2ad | 2014-03-14 12:31:42 -0700 | [diff] [blame] | 115 | const ClawGoal kFlippedIntakeGoal = {2.0, kGrabSeparation}; |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 116 | const ClawGoal kFlippedIntakeOpenGoal = {0.95, 1.0}; |
| 117 | |
Brian Silverman | 255b5c1 | 2014-04-25 09:21:40 -0500 | [diff] [blame] | 118 | // 34" between near edge of colored line and rear edge of bumper. |
| 119 | // Only works running? |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 120 | const ShotGoal kLongShotGoal = { |
Brian Silverman | 55d7f57 | 2014-04-17 14:33:54 -0700 | [diff] [blame] | 121 | {-1.08, kShootSeparation}, 145, 0.04, kIntakePower}; |
Brian Silverman | 255b5c1 | 2014-04-25 09:21:40 -0500 | [diff] [blame] | 122 | // old 34" {-1.06, kShootSeparation}, 140, 0.04, kIntakePower}; |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 123 | const ShotGoal kFlippedLongShotGoal = { |
Brian Silverman | 55d7f57 | 2014-04-17 14:33:54 -0700 | [diff] [blame] | 124 | {0.96, kShootSeparation}, 145, 0.09, kIntakePower}; |
Brian Silverman | 255b5c1 | 2014-04-25 09:21:40 -0500 | [diff] [blame] | 125 | // old 34" {0.96, kShootSeparation}, 140, 0.09, kIntakePower}; |
Brian Silverman | 0e7c03e | 2014-03-23 17:06:24 -0700 | [diff] [blame] | 126 | |
Brian Silverman | 255b5c1 | 2014-04-25 09:21:40 -0500 | [diff] [blame] | 127 | // 78" between near edge of colored line and rear edge of bumper. |
Brian Silverman | 4051c6f | 2014-07-19 16:57:37 -0700 | [diff] [blame] | 128 | const ShotGoal kCloseShotGoal = { |
Brian Silverman | 0e7c03e | 2014-03-23 17:06:24 -0700 | [diff] [blame] | 129 | {-0.95, kShootSeparation}, 105, 0.2, kIntakePower}; |
| 130 | // 3/4" plunger {-0.90, kShootSeparation}, 105, 0.2, kIntakePower}; |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 131 | const ShotGoal kFlippedMediumShotGoal = { |
Brian Silverman | 255b5c1 | 2014-04-25 09:21:40 -0500 | [diff] [blame] | 132 | {0.865, kShootSeparation}, 120, 0.2, kIntakePower}; |
Brian Silverman | 0e7c03e | 2014-03-23 17:06:24 -0700 | [diff] [blame] | 133 | // 3/4" plunger {0.80, kShootSeparation}, 105, 0.2, kIntakePower}; |
| 134 | |
Brian Silverman | 255b5c1 | 2014-04-25 09:21:40 -0500 | [diff] [blame] | 135 | // Shot from the fender. |
Brian Silverman | 4051c6f | 2014-07-19 16:57:37 -0700 | [diff] [blame] | 136 | const ShotGoal kFenderShotGoal = { |
Austin Schuh | db3f798 | 2014-10-25 23:09:49 -0700 | [diff] [blame^] | 137 | {-0.68, kShootSeparation}, 115.0, 0.0, kIntakePower}; |
Brian Silverman | 55d7f57 | 2014-04-17 14:33:54 -0700 | [diff] [blame] | 138 | const ShotGoal kFlippedShortShotGoal = { |
Austin Schuh | db3f798 | 2014-10-25 23:09:49 -0700 | [diff] [blame^] | 139 | {0.63, kShootSeparation}, 115.0, 0.0, kIntakePower}; |
Brian Silverman | 0e7c03e | 2014-03-23 17:06:24 -0700 | [diff] [blame] | 140 | |
Brian Silverman | 63ec750 | 2014-03-30 18:09:10 -0700 | [diff] [blame] | 141 | const ShotGoal kHumanShotGoal = { |
| 142 | {-0.90, kShootSeparation}, 140, 0.04, kIntakePower}; |
Brian Silverman | 73c4839 | 2014-04-05 07:07:41 -0700 | [diff] [blame] | 143 | const ShotGoal kFlippedHumanShotGoal = { |
| 144 | {0.90, kShootSeparation}, 140, 0, kIntakePower}; |
Brian Silverman | 0e7c03e | 2014-03-23 17:06:24 -0700 | [diff] [blame] | 145 | const ShotGoal kTrussShotGoal = { |
Brian Silverman | 8d5dda4 | 2014-04-06 15:59:49 -0700 | [diff] [blame] | 146 | {-0.68, kShootSeparation}, 83.0, 0.4, kIntakePower}; |
Brian Silverman | 73c4839 | 2014-04-05 07:07:41 -0700 | [diff] [blame] | 147 | const ShotGoal kFlippedTrussShotGoal = { |
Brian Silverman | 8d5dda4 | 2014-04-06 15:59:49 -0700 | [diff] [blame] | 148 | {0.68, kShootSeparation}, 92.0, 0.4, kIntakePower}; |
| 149 | |
Brian Silverman | 8cd5120 | 2014-07-19 16:58:08 -0700 | [diff] [blame] | 150 | const ShotGoal kFlippedDemoShotGoal = { |
| 151 | {1.0, kShootSeparation}, 65.0, 0.0, kIntakePower}; |
| 152 | const ShotGoal kDemoShotGoal = { |
| 153 | {-1.0, kShootSeparation}, 50.0, 0.0, kIntakePower}; |
| 154 | |
Brian Silverman | 8d5dda4 | 2014-04-06 15:59:49 -0700 | [diff] [blame] | 155 | const ClawGoal k254PassGoal = {-1.95, kGrabSeparation}; |
| 156 | const ClawGoal kFlipped254PassGoal = {1.96, kGrabSeparation}; |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 157 | |
| 158 | // Makes a new ShootAction action. |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 159 | ::std::unique_ptr<TypedAction< ::frc971::actions::CatchActionGroup>> |
| 160 | MakeCatchAction() { |
| 161 | return ::std::unique_ptr<TypedAction< ::frc971::actions::CatchActionGroup>>( |
| 162 | new TypedAction< ::frc971::actions::CatchActionGroup>( |
| 163 | &::frc971::actions::catch_action)); |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | // A queue which queues Actions and cancels them. |
| 167 | class ActionQueue { |
| 168 | public: |
| 169 | // Queues up an action for sending. |
| 170 | void QueueAction(::std::unique_ptr<Action> action) { |
| 171 | if (current_action_) { |
Austin Schuh | c95c2b7 | 2014-03-02 11:56:49 -0800 | [diff] [blame] | 172 | LOG(INFO, "Queueing action, canceling prior\n"); |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 173 | current_action_->Cancel(); |
| 174 | next_action_ = ::std::move(action); |
| 175 | } else { |
Austin Schuh | c95c2b7 | 2014-03-02 11:56:49 -0800 | [diff] [blame] | 176 | LOG(INFO, "Queueing action\n"); |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 177 | current_action_ = ::std::move(action); |
| 178 | current_action_->Start(); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | // Cancels the current action, and runs the next one when the current one has |
| 183 | // finished. |
| 184 | void CancelCurrentAction() { |
Austin Schuh | c95c2b7 | 2014-03-02 11:56:49 -0800 | [diff] [blame] | 185 | LOG(INFO, "Canceling current action\n"); |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 186 | if (current_action_) { |
| 187 | current_action_->Cancel(); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | // Cancels all running actions. |
| 192 | void CancelAllActions() { |
Brian Silverman | 101b964 | 2014-03-08 12:45:16 -0800 | [diff] [blame] | 193 | LOG(DEBUG, "Cancelling all actions\n"); |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 194 | if (current_action_) { |
| 195 | current_action_->Cancel(); |
| 196 | } |
| 197 | next_action_.reset(); |
| 198 | } |
| 199 | |
| 200 | // Runs the next action when the current one is finished running. |
| 201 | void Tick() { |
| 202 | if (current_action_) { |
| 203 | if (!current_action_->Running()) { |
Austin Schuh | c95c2b7 | 2014-03-02 11:56:49 -0800 | [diff] [blame] | 204 | LOG(INFO, "Action is done.\n"); |
| 205 | current_action_ = ::std::move(next_action_); |
| 206 | if (current_action_) { |
| 207 | LOG(INFO, "Running next action\n"); |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 208 | current_action_->Start(); |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | // Returns true if any action is running or could be running. |
| 215 | // For a one cycle faster response, call Tick before running this. |
Brian Silverman | 2c1e034 | 2014-04-11 16:15:01 -0700 | [diff] [blame] | 216 | bool Running() { return static_cast<bool>(current_action_); } |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 217 | |
| 218 | private: |
| 219 | ::std::unique_ptr<Action> current_action_; |
| 220 | ::std::unique_ptr<Action> next_action_; |
| 221 | }; |
| 222 | |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 223 | |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 224 | class Reader : public ::aos::input::JoystickInput { |
| 225 | public: |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 226 | Reader() |
Brian Silverman | e3a277a | 2014-04-13 14:56:57 -0700 | [diff] [blame] | 227 | : is_high_gear_(false), |
Austin Schuh | 9cb836e | 2014-02-23 19:25:55 -0800 | [diff] [blame] | 228 | shot_power_(80.0), |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 229 | goal_angle_(0.0), |
Brian Silverman | 545f2ad | 2014-03-14 12:31:42 -0700 | [diff] [blame] | 230 | separation_angle_(kGrabSeparation), |
Brian Silverman | a379f00 | 2014-03-22 19:34:53 -0700 | [diff] [blame] | 231 | velocity_compensation_(0.0), |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 232 | intake_power_(0.0), |
| 233 | was_running_(false) {} |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 234 | |
| 235 | virtual void RunIteration(const ::aos::input::driver_station::Data &data) { |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 236 | if (data.GetControlBit(ControlBit::kAutonomous)) { |
| 237 | if (data.PosEdge(ControlBit::kEnabled)){ |
| 238 | LOG(INFO, "Starting auto mode\n"); |
| 239 | ::frc971::autonomous::autonomous.MakeWithBuilder() |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 240 | .run_auto(true) |
| 241 | .Send(); |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 242 | } else if (data.NegEdge(ControlBit::kEnabled)) { |
| 243 | LOG(INFO, "Stopping auto mode\n"); |
| 244 | ::frc971::autonomous::autonomous.MakeWithBuilder() |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 245 | .run_auto(false) |
| 246 | .Send(); |
Brian Silverman | 2c764f0 | 2014-04-25 09:21:21 -0500 | [diff] [blame] | 247 | } else if (!data.GetControlBit(ControlBit::kEnabled)) { |
| 248 | { |
| 249 | auto goal = drivetrain.goal.MakeMessage(); |
| 250 | goal->Zero(); |
| 251 | goal->control_loop_driving = false; |
| 252 | goal->left_goal = goal->right_goal = 0; |
| 253 | goal->left_velocity_goal = goal->right_velocity_goal = 0; |
| 254 | if (!goal.Send()) { |
| 255 | LOG(WARNING, "sending 0 drivetrain goal failed\n"); |
| 256 | } |
| 257 | } |
| 258 | { |
| 259 | // TODO(brians): Make sure this doesn't make it unbrake and not move |
| 260 | // or something weird. |
| 261 | auto goal = control_loops::shooter_queue_group.goal.MakeMessage(); |
| 262 | goal->Zero(); |
| 263 | if (!goal.Send()) { |
| 264 | LOG(WARNING, "sending 0 shooter goal failed\n"); |
| 265 | } |
| 266 | } |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 267 | } |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 268 | } else { |
| 269 | HandleTeleop(data); |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 270 | } |
| 271 | } |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 272 | |
| 273 | void HandleDrivetrain(const ::aos::input::driver_station::Data &data) { |
| 274 | bool is_control_loop_driving = false; |
| 275 | double left_goal = 0.0; |
| 276 | double right_goal = 0.0; |
| 277 | const double wheel = -data.GetAxis(kSteeringWheel); |
| 278 | const double throttle = -data.GetAxis(kDriveThrottle); |
| 279 | const double kThrottleGain = 1.0 / 2.5; |
| 280 | if (false && (data.IsPressed(kDriveControlLoopEnable1) || |
| 281 | data.IsPressed(kDriveControlLoopEnable2))) { |
| 282 | // TODO(austin): Static sucks! |
| 283 | static double distance = 0.0; |
| 284 | static double angle = 0.0; |
| 285 | static double filtered_goal_distance = 0.0; |
| 286 | if (data.PosEdge(kDriveControlLoopEnable1) || |
| 287 | data.PosEdge(kDriveControlLoopEnable2)) { |
Brian Silverman | 6bf0d3c | 2014-03-08 12:52:54 -0800 | [diff] [blame] | 288 | if (drivetrain.position.FetchLatest() && gyro_reading.FetchLatest()) { |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 289 | distance = (drivetrain.position->left_encoder + |
| 290 | drivetrain.position->right_encoder) / |
| 291 | 2.0 - |
| 292 | throttle * kThrottleGain / 2.0; |
Brian Silverman | 6bf0d3c | 2014-03-08 12:52:54 -0800 | [diff] [blame] | 293 | angle = gyro_reading->angle; |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 294 | filtered_goal_distance = distance; |
| 295 | } |
| 296 | } |
| 297 | is_control_loop_driving = true; |
| 298 | |
| 299 | // const double gyro_angle = Gyro.View().angle; |
| 300 | const double goal_theta = angle - wheel * 0.27; |
| 301 | const double goal_distance = distance + throttle * kThrottleGain; |
| 302 | const double robot_width = 22.0 / 100.0 * 2.54; |
| 303 | const double kMaxVelocity = 0.6; |
| 304 | if (goal_distance > kMaxVelocity * 0.02 + filtered_goal_distance) { |
| 305 | filtered_goal_distance += kMaxVelocity * 0.02; |
| 306 | } else if (goal_distance < |
| 307 | -kMaxVelocity * 0.02 + filtered_goal_distance) { |
| 308 | filtered_goal_distance -= kMaxVelocity * 0.02; |
| 309 | } else { |
| 310 | filtered_goal_distance = goal_distance; |
| 311 | } |
| 312 | left_goal = filtered_goal_distance - robot_width * goal_theta / 2.0; |
| 313 | right_goal = filtered_goal_distance + robot_width * goal_theta / 2.0; |
| 314 | is_high_gear_ = false; |
| 315 | |
| 316 | LOG(DEBUG, "Left goal %f Right goal %f\n", left_goal, right_goal); |
| 317 | } |
| 318 | if (!drivetrain.goal.MakeWithBuilder() |
| 319 | .steering(wheel) |
| 320 | .throttle(throttle) |
| 321 | .highgear(is_high_gear_) |
Austin Schuh | d21c10d | 2014-10-25 10:32:04 -0700 | [diff] [blame] | 322 | .quickturn(data.IsPressed(kQuickTurn)) |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 323 | .control_loop_driving(is_control_loop_driving) |
| 324 | .left_goal(left_goal) |
| 325 | .right_goal(right_goal) |
Brian Silverman | 2c764f0 | 2014-04-25 09:21:21 -0500 | [diff] [blame] | 326 | .left_velocity_goal(0) |
| 327 | .right_velocity_goal(0) |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 328 | .Send()) { |
| 329 | LOG(WARNING, "sending stick values failed\n"); |
| 330 | } |
| 331 | if (data.PosEdge(kShiftHigh)) { |
| 332 | is_high_gear_ = false; |
| 333 | } |
| 334 | if (data.PosEdge(kShiftLow)) { |
| 335 | is_high_gear_ = true; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | void SetGoal(ClawGoal goal) { |
| 340 | goal_angle_ = goal.angle; |
| 341 | separation_angle_ = goal.separation; |
Brian | 7e29439 | 2014-03-31 12:39:13 -0700 | [diff] [blame] | 342 | moving_for_shot_ = false; |
Brian Silverman | a379f00 | 2014-03-22 19:34:53 -0700 | [diff] [blame] | 343 | velocity_compensation_ = 0.0; |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 344 | intake_power_ = 0.0; |
| 345 | } |
| 346 | |
| 347 | void SetGoal(ShotGoal goal) { |
| 348 | goal_angle_ = goal.claw.angle; |
Brian | 7e29439 | 2014-03-31 12:39:13 -0700 | [diff] [blame] | 349 | shot_separation_angle_ = goal.claw.separation; |
| 350 | separation_angle_ = kGrabSeparation; |
| 351 | moving_for_shot_ = true; |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 352 | shot_power_ = goal.shot_power; |
| 353 | velocity_compensation_ = goal.velocity_compensation; |
| 354 | intake_power_ = goal.intake_power; |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | void HandleTeleop(const ::aos::input::driver_station::Data &data) { |
| 358 | HandleDrivetrain(data); |
Austin Schuh | c95c2b7 | 2014-03-02 11:56:49 -0800 | [diff] [blame] | 359 | if (!data.GetControlBit(ControlBit::kEnabled)) { |
| 360 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 361 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | c95c2b7 | 2014-03-02 11:56:49 -0800 | [diff] [blame] | 362 | } |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 363 | if (data.IsPressed(kRollersIn) || data.IsPressed(kRollersOut)) { |
| 364 | intake_power_ = 0.0; |
Brian Silverman | 545f2ad | 2014-03-14 12:31:42 -0700 | [diff] [blame] | 365 | separation_angle_ = kGrabSeparation; |
Brian | 7e29439 | 2014-03-31 12:39:13 -0700 | [diff] [blame] | 366 | moving_for_shot_ = false; |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 367 | } |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 368 | |
Brian Silverman | 18f6e64 | 2014-03-13 18:52:47 -0700 | [diff] [blame] | 369 | static const double kAdjustClawGoalDeadband = 0.08; |
| 370 | double claw_goal_adjust = data.GetAxis(kAdjustClawGoal); |
Brian Silverman | 5e1eb64 | 2014-07-20 16:37:35 -0700 | [diff] [blame] | 371 | if (OLD_DS || ::std::abs(claw_goal_adjust) < kAdjustClawGoalDeadband) { |
Brian Silverman | 18f6e64 | 2014-03-13 18:52:47 -0700 | [diff] [blame] | 372 | claw_goal_adjust = 0; |
| 373 | } else { |
| 374 | claw_goal_adjust = (claw_goal_adjust - |
| 375 | ((claw_goal_adjust < 0) ? -kAdjustClawGoalDeadband |
| 376 | : kAdjustClawGoalDeadband)) * |
| 377 | 0.035; |
| 378 | } |
| 379 | double claw_separation_adjust = data.GetAxis(kAdjustClawSeparation); |
Brian Silverman | 5e1eb64 | 2014-07-20 16:37:35 -0700 | [diff] [blame] | 380 | if (OLD_DS || |
| 381 | ::std::abs(claw_separation_adjust) < kAdjustClawGoalDeadband) { |
Brian Silverman | 18f6e64 | 2014-03-13 18:52:47 -0700 | [diff] [blame] | 382 | claw_separation_adjust = 0; |
| 383 | } else { |
| 384 | claw_separation_adjust = |
| 385 | (claw_separation_adjust - |
| 386 | ((claw_separation_adjust < 0) ? -kAdjustClawGoalDeadband |
| 387 | : kAdjustClawGoalDeadband)) * |
| 388 | -0.035; |
| 389 | } |
| 390 | |
Daniel Petti | d1a70f6 | 2014-08-10 16:56:44 -0700 | [diff] [blame] | 391 | #if OLD_DS |
| 392 | if (data.IsPressed(kFenderShot)) { |
| 393 | #else |
Brian | 6faa782 | 2014-03-31 18:00:28 -0700 | [diff] [blame] | 394 | if (data.GetAxis(kFlipRobot) > 0.9) { |
Daniel Petti | d1a70f6 | 2014-08-10 16:56:44 -0700 | [diff] [blame] | 395 | #endif |
Brian Silverman | 18f6e64 | 2014-03-13 18:52:47 -0700 | [diff] [blame] | 396 | claw_goal_adjust += claw_separation_adjust; |
| 397 | claw_goal_adjust *= -1; |
| 398 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 399 | if (data.IsPressed(kIntakeOpenPosition)) { |
| 400 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 401 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 402 | SetGoal(kFlippedIntakeOpenGoal); |
| 403 | } else if (data.IsPressed(kIntakePosition)) { |
| 404 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 405 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 406 | SetGoal(kFlippedIntakeGoal); |
Brian | 6faa782 | 2014-03-31 18:00:28 -0700 | [diff] [blame] | 407 | } else if (data.IsPressed(kVerticalTuck)) { |
| 408 | action_queue_.CancelAllActions(); |
| 409 | LOG(DEBUG, "Canceling\n"); |
| 410 | SetGoal(kVerticalTuckGoal); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 411 | } else if (data.IsPressed(kTuck)) { |
| 412 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 413 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 414 | SetGoal(kFlippedTuckGoal); |
| 415 | } else if (data.PosEdge(kLongShot)) { |
| 416 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 417 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 418 | SetGoal(kFlippedLongShotGoal); |
Brian Silverman | 4051c6f | 2014-07-19 16:57:37 -0700 | [diff] [blame] | 419 | } else if (data.PosEdge(kCloseShot)) { |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 420 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 421 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 422 | SetGoal(kFlippedMediumShotGoal); |
Brian Silverman | 4051c6f | 2014-07-19 16:57:37 -0700 | [diff] [blame] | 423 | } else if (data.PosEdge(kFenderShot)) { |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 424 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 425 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 426 | SetGoal(kFlippedShortShotGoal); |
Brian | 6faa782 | 2014-03-31 18:00:28 -0700 | [diff] [blame] | 427 | } else if (data.PosEdge(kHumanPlayerShot)) { |
| 428 | action_queue_.CancelAllActions(); |
| 429 | LOG(DEBUG, "Canceling\n"); |
Brian Silverman | 73c4839 | 2014-04-05 07:07:41 -0700 | [diff] [blame] | 430 | SetGoal(kFlippedHumanShotGoal); |
Brian Silverman | 8d5dda4 | 2014-04-06 15:59:49 -0700 | [diff] [blame] | 431 | } else if (data.PosEdge(kUserLeft)) { |
| 432 | action_queue_.CancelAllActions(); |
| 433 | LOG(DEBUG, "Canceling\n"); |
| 434 | SetGoal(kFlipped254PassGoal); |
Brian Silverman | 8cd5120 | 2014-07-19 16:58:08 -0700 | [diff] [blame] | 435 | } else if (data.PosEdge(kUserRight)) { |
| 436 | action_queue_.CancelAllActions(); |
| 437 | LOG(DEBUG, "Canceling\n"); |
| 438 | SetGoal(kFlippedDemoShotGoal); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 439 | } else if (data.PosEdge(kTrussShot)) { |
| 440 | action_queue_.CancelAllActions(); |
| 441 | LOG(DEBUG, "Canceling\n"); |
Brian Silverman | 73c4839 | 2014-04-05 07:07:41 -0700 | [diff] [blame] | 442 | SetGoal(kFlippedTrussShotGoal); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 443 | } |
| 444 | } else { |
| 445 | if (data.IsPressed(kIntakeOpenPosition)) { |
| 446 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 447 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 448 | SetGoal(kIntakeOpenGoal); |
| 449 | } else if (data.IsPressed(kIntakePosition)) { |
| 450 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 451 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 452 | SetGoal(kIntakeGoal); |
Brian | 6faa782 | 2014-03-31 18:00:28 -0700 | [diff] [blame] | 453 | } else if (data.IsPressed(kVerticalTuck)) { |
| 454 | action_queue_.CancelAllActions(); |
| 455 | LOG(DEBUG, "Canceling\n"); |
| 456 | SetGoal(kVerticalTuckGoal); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 457 | } else if (data.IsPressed(kTuck)) { |
| 458 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 459 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 460 | SetGoal(kTuckGoal); |
| 461 | } else if (data.PosEdge(kLongShot)) { |
| 462 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 463 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 464 | SetGoal(kLongShotGoal); |
Brian Silverman | 4051c6f | 2014-07-19 16:57:37 -0700 | [diff] [blame] | 465 | } else if (data.PosEdge(kCloseShot)) { |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 466 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 467 | LOG(DEBUG, "Canceling\n"); |
Brian Silverman | 4051c6f | 2014-07-19 16:57:37 -0700 | [diff] [blame] | 468 | SetGoal(kCloseShotGoal); |
| 469 | } else if (data.PosEdge(kFenderShot)) { |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 470 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 471 | LOG(DEBUG, "Canceling\n"); |
Brian Silverman | 4051c6f | 2014-07-19 16:57:37 -0700 | [diff] [blame] | 472 | SetGoal(kFenderShotGoal); |
Brian | 6faa782 | 2014-03-31 18:00:28 -0700 | [diff] [blame] | 473 | } else if (data.PosEdge(kHumanPlayerShot)) { |
| 474 | action_queue_.CancelAllActions(); |
| 475 | LOG(DEBUG, "Canceling\n"); |
| 476 | SetGoal(kHumanShotGoal); |
Brian Silverman | 8d5dda4 | 2014-04-06 15:59:49 -0700 | [diff] [blame] | 477 | } else if (data.PosEdge(kUserLeft)) { |
| 478 | action_queue_.CancelAllActions(); |
| 479 | LOG(DEBUG, "Canceling\n"); |
| 480 | SetGoal(k254PassGoal); |
Brian Silverman | 8cd5120 | 2014-07-19 16:58:08 -0700 | [diff] [blame] | 481 | } else if (data.PosEdge(kUserRight)) { |
| 482 | action_queue_.CancelAllActions(); |
| 483 | LOG(DEBUG, "Canceling\n"); |
| 484 | SetGoal(kDemoShotGoal); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 485 | } else if (data.PosEdge(kTrussShot)) { |
| 486 | action_queue_.CancelAllActions(); |
| 487 | LOG(DEBUG, "Canceling\n"); |
| 488 | SetGoal(kTrussShotGoal); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 489 | } |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 490 | } |
| 491 | |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 492 | /* |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 493 | if (data.PosEdge(kCatch)) { |
| 494 | auto catch_action = MakeCatchAction(); |
| 495 | catch_action->GetGoal()->catch_angle = goal_angle_; |
| 496 | action_queue_.QueueAction(::std::move(catch_action)); |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 497 | } |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 498 | */ |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 499 | |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 500 | if (data.PosEdge(kFire)) { |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 501 | action_queue_.QueueAction(actions::MakeShootAction()); |
Brian Silverman | 9f863a0 | 2014-03-29 17:42:36 -0700 | [diff] [blame] | 502 | } else if (data.NegEdge(kFire)) { |
| 503 | action_queue_.CancelCurrentAction(); |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | action_queue_.Tick(); |
Austin Schuh | c95c2b7 | 2014-03-02 11:56:49 -0800 | [diff] [blame] | 507 | if (data.IsPressed(kUnload) || data.IsPressed(kReload)) { |
| 508 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 509 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 510 | intake_power_ = 0.0; |
Brian Silverman | a379f00 | 2014-03-22 19:34:53 -0700 | [diff] [blame] | 511 | velocity_compensation_ = 0.0; |
Austin Schuh | c95c2b7 | 2014-03-02 11:56:49 -0800 | [diff] [blame] | 512 | } |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 513 | |
| 514 | // Send out the claw and shooter goals if no actions are running. |
| 515 | if (!action_queue_.Running()) { |
Brian Silverman | 18f6e64 | 2014-03-13 18:52:47 -0700 | [diff] [blame] | 516 | goal_angle_ += claw_goal_adjust; |
| 517 | separation_angle_ += claw_separation_adjust; |
| 518 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 519 | // If the action just ended, turn the intake off and stop velocity |
| 520 | // compensating. |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 521 | if (was_running_) { |
| 522 | intake_power_ = 0.0; |
Brian Silverman | a379f00 | 2014-03-22 19:34:53 -0700 | [diff] [blame] | 523 | velocity_compensation_ = 0.0; |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | control_loops::drivetrain.status.FetchLatest(); |
Brian Silverman | a379f00 | 2014-03-22 19:34:53 -0700 | [diff] [blame] | 527 | double goal_angle = goal_angle_; |
| 528 | if (control_loops::drivetrain.status.get()) { |
| 529 | goal_angle += |
| 530 | SpeedToAngleOffset(control_loops::drivetrain.status->robot_speed); |
| 531 | } else { |
| 532 | LOG_INTERVAL(no_drivetrain_status_); |
| 533 | } |
Brian | 7e29439 | 2014-03-31 12:39:13 -0700 | [diff] [blame] | 534 | |
| 535 | if (moving_for_shot_) { |
| 536 | auto &claw_status = control_loops::claw_queue_group.status; |
| 537 | claw_status.FetchLatest(); |
| 538 | if (claw_status.get()) { |
Brian | 6faa782 | 2014-03-31 18:00:28 -0700 | [diff] [blame] | 539 | if (::std::abs(claw_status->bottom - goal_angle) < 0.2) { |
Brian | 7e29439 | 2014-03-31 12:39:13 -0700 | [diff] [blame] | 540 | moving_for_shot_ = false; |
| 541 | separation_angle_ = shot_separation_angle_; |
| 542 | } |
| 543 | } |
| 544 | } |
| 545 | |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 546 | double separation_angle = separation_angle_; |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 547 | |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 548 | if (data.IsPressed(kCatch)) { |
| 549 | const double kCatchSeparation = 1.0; |
| 550 | goal_angle -= kCatchSeparation / 2.0; |
| 551 | separation_angle = kCatchSeparation; |
| 552 | } |
| 553 | |
| 554 | bool intaking = |
| 555 | data.IsPressed(kRollersIn) || data.IsPressed(kIntakePosition) || |
| 556 | data.IsPressed(kIntakeOpenPosition) || data.IsPressed(kCatch); |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 557 | if (!control_loops::claw_queue_group.goal.MakeWithBuilder() |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 558 | .bottom_angle(goal_angle) |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 559 | .separation_angle(separation_angle) |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 560 | .intake(intaking ? 12.0 |
| 561 | : (data.IsPressed(kRollersOut) ? -12.0 |
| 562 | : intake_power_)) |
| 563 | .centering(intaking ? 12.0 : 0.0) |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 564 | .Send()) { |
| 565 | LOG(WARNING, "sending claw goal failed\n"); |
| 566 | } |
| 567 | |
| 568 | if (!control_loops::shooter_queue_group.goal.MakeWithBuilder() |
| 569 | .shot_power(shot_power_) |
| 570 | .shot_requested(data.IsPressed(kFire)) |
| 571 | .unload_requested(data.IsPressed(kUnload)) |
| 572 | .load_requested(data.IsPressed(kReload)) |
| 573 | .Send()) { |
| 574 | LOG(WARNING, "sending shooter goal failed\n"); |
| 575 | } |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 576 | } |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 577 | was_running_ = action_queue_.Running(); |
| 578 | } |
| 579 | |
| 580 | double SpeedToAngleOffset(double speed) { |
| 581 | const frc971::constants::Values &values = frc971::constants::GetValues(); |
| 582 | // scale speed to a [0.0-1.0] on something close to the max |
| 583 | // TODO(austin): Change the scale factor for different shots. |
Brian Silverman | a379f00 | 2014-03-22 19:34:53 -0700 | [diff] [blame] | 584 | return (speed / values.drivetrain_max_speed) * velocity_compensation_; |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 585 | } |
| 586 | |
Austin Schuh | 01c652b | 2014-02-21 23:13:42 -0800 | [diff] [blame] | 587 | private: |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 588 | bool is_high_gear_; |
| 589 | double shot_power_; |
| 590 | double goal_angle_; |
Brian | 7e29439 | 2014-03-31 12:39:13 -0700 | [diff] [blame] | 591 | double separation_angle_, shot_separation_angle_; |
Brian Silverman | a379f00 | 2014-03-22 19:34:53 -0700 | [diff] [blame] | 592 | double velocity_compensation_; |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 593 | double intake_power_; |
| 594 | bool was_running_; |
Brian | 7e29439 | 2014-03-31 12:39:13 -0700 | [diff] [blame] | 595 | bool moving_for_shot_ = false; |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 596 | |
| 597 | ActionQueue action_queue_; |
Brian Silverman | a379f00 | 2014-03-22 19:34:53 -0700 | [diff] [blame] | 598 | |
| 599 | ::aos::util::SimpleLogInterval no_drivetrain_status_ = |
| 600 | ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING, |
| 601 | "no drivetrain status"); |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 602 | }; |
| 603 | |
| 604 | } // namespace joysticks |
| 605 | } // namespace input |
| 606 | } // namespace frc971 |
| 607 | |
| 608 | int main() { |
| 609 | ::aos::Init(); |
| 610 | ::frc971::input::joysticks::Reader reader; |
| 611 | reader.Run(); |
| 612 | ::aos::Cleanup(); |
| 613 | } |