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 | |
| 31 | namespace frc971 { |
| 32 | namespace input { |
| 33 | namespace joysticks { |
| 34 | |
| 35 | const ButtonLocation kDriveControlLoopEnable1(1, 7), |
| 36 | kDriveControlLoopEnable2(1, 11); |
| 37 | const JoystickAxis kSteeringWheel(1, 1), kDriveThrottle(2, 2); |
| 38 | const ButtonLocation kShiftHigh(2, 1), kShiftLow(2, 3); |
| 39 | const ButtonLocation kQuickTurn(1, 5); |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 40 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 41 | const ButtonLocation kCatch(3, 10); |
| 42 | |
| 43 | const ButtonLocation kFire(3, 9); |
Austin Schuh | 9cb836e | 2014-02-23 19:25:55 -0800 | [diff] [blame] | 44 | const ButtonLocation kUnload(2, 11); |
| 45 | const ButtonLocation kReload(2, 6); |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 46 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 47 | const ButtonLocation kRollersOut(3, 8); |
| 48 | const ButtonLocation kRollersIn(3, 3); |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 49 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 50 | const ButtonLocation kTuck(3, 4); |
| 51 | const ButtonLocation kIntakePosition(3, 5); |
| 52 | const ButtonLocation kIntakeOpenPosition(3, 11); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 53 | const JoystickAxis kFlipRobot(3, 3); |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 54 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 55 | const ButtonLocation kLongShot(3, 7); |
| 56 | const ButtonLocation kMediumShot(3, 6); |
| 57 | const ButtonLocation kShortShot(3, 2); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 58 | const ButtonLocation kTrussShot(3, 1); |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 59 | |
Brian Silverman | 18f6e64 | 2014-03-13 18:52:47 -0700 | [diff] [blame] | 60 | const JoystickAxis kAdjustClawGoal(3, 2); |
| 61 | const JoystickAxis kAdjustClawSeparation(3, 1); |
| 62 | |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 63 | struct ClawGoal { |
| 64 | double angle; |
| 65 | double separation; |
| 66 | }; |
| 67 | |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 68 | struct ShotGoal { |
| 69 | ClawGoal claw; |
| 70 | double shot_power; |
Brian Silverman | a379f00 | 2014-03-22 19:34:53 -0700 | [diff] [blame^] | 71 | double velocity_compensation; |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 72 | double intake_power; |
| 73 | }; |
| 74 | |
Brian Silverman | 545f2ad | 2014-03-14 12:31:42 -0700 | [diff] [blame] | 75 | const double kIntakePower = 4.0; |
Brian Silverman | b3cf0ef | 2014-03-22 12:45:55 -0700 | [diff] [blame] | 76 | // TODO(brians): This wants to be -0.04 on the comp bot. Make them both the |
| 77 | // same. |
| 78 | const double kGrabSeparation = 0; |
Brian Silverman | 545f2ad | 2014-03-14 12:31:42 -0700 | [diff] [blame] | 79 | const double kShootSeparation = 0.11 + kGrabSeparation; |
| 80 | |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 81 | const ClawGoal kTuckGoal = {-2.273474, -0.749484}; |
Brian Silverman | 545f2ad | 2014-03-14 12:31:42 -0700 | [diff] [blame] | 82 | const ClawGoal kIntakeGoal = {-2.273474, kGrabSeparation}; |
Austin Schuh | 9cb836e | 2014-02-23 19:25:55 -0800 | [diff] [blame] | 83 | const ClawGoal kIntakeOpenGoal = {-2.0, 1.2}; |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 84 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 85 | // TODO(austin): Tune these by hand... |
| 86 | const ClawGoal kFlippedTuckGoal = {2.733474, -0.75}; |
Brian Silverman | 545f2ad | 2014-03-14 12:31:42 -0700 | [diff] [blame] | 87 | const ClawGoal kFlippedIntakeGoal = {2.0, kGrabSeparation}; |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 88 | const ClawGoal kFlippedIntakeOpenGoal = {0.95, 1.0}; |
| 89 | |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 90 | //const ShotGoal kLongShotGoal = { |
| 91 | //{-M_PI / 2.0 + 0.46, kShootSeparation}, 120, false, kIntakePower}; |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 92 | const ShotGoal kLongShotGoal = { |
Brian Silverman | a379f00 | 2014-03-22 19:34:53 -0700 | [diff] [blame^] | 93 | {-1.04, kShootSeparation}, 140, 0.04, kIntakePower}; |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 94 | const ShotGoal kMediumShotGoal = { |
Brian Silverman | a379f00 | 2014-03-22 19:34:53 -0700 | [diff] [blame^] | 95 | {-0.90, kShootSeparation}, 105, 0.2, kIntakePower}; |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 96 | const ShotGoal kShortShotGoal = { |
Brian Silverman | a379f00 | 2014-03-22 19:34:53 -0700 | [diff] [blame^] | 97 | {-0.670, kShootSeparation}, 71.0, 0, kIntakePower}; |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 98 | const ShotGoal kTrussShotGoal = { |
Brian Silverman | a379f00 | 2014-03-22 19:34:53 -0700 | [diff] [blame^] | 99 | {-0.05, kShootSeparation}, 61.0, 0, kIntakePower}; |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 100 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 101 | const ShotGoal kFlippedLongShotGoal = { |
Brian Silverman | a379f00 | 2014-03-22 19:34:53 -0700 | [diff] [blame^] | 102 | {0.97, kShootSeparation}, 140, 0.08, kIntakePower}; |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 103 | const ShotGoal kFlippedMediumShotGoal = { |
Brian Silverman | a379f00 | 2014-03-22 19:34:53 -0700 | [diff] [blame^] | 104 | {0.80, kShootSeparation}, 105, 0.2, kIntakePower}; |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 105 | const ShotGoal kFlippedShortShotGoal = { |
Brian Silverman | a379f00 | 2014-03-22 19:34:53 -0700 | [diff] [blame^] | 106 | {0.57, kShootSeparation}, 80.0, 0, kIntakePower}; |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 107 | |
| 108 | // Makes a new ShootAction action. |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 109 | ::std::unique_ptr<TypedAction< ::frc971::actions::CatchActionGroup>> |
| 110 | MakeCatchAction() { |
| 111 | return ::std::unique_ptr<TypedAction< ::frc971::actions::CatchActionGroup>>( |
| 112 | new TypedAction< ::frc971::actions::CatchActionGroup>( |
| 113 | &::frc971::actions::catch_action)); |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | // A queue which queues Actions and cancels them. |
| 117 | class ActionQueue { |
| 118 | public: |
| 119 | // Queues up an action for sending. |
| 120 | void QueueAction(::std::unique_ptr<Action> action) { |
| 121 | if (current_action_) { |
Austin Schuh | c95c2b7 | 2014-03-02 11:56:49 -0800 | [diff] [blame] | 122 | LOG(INFO, "Queueing action, canceling prior\n"); |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 123 | current_action_->Cancel(); |
| 124 | next_action_ = ::std::move(action); |
| 125 | } else { |
Austin Schuh | c95c2b7 | 2014-03-02 11:56:49 -0800 | [diff] [blame] | 126 | LOG(INFO, "Queueing action\n"); |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 127 | current_action_ = ::std::move(action); |
| 128 | current_action_->Start(); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | // Cancels the current action, and runs the next one when the current one has |
| 133 | // finished. |
| 134 | void CancelCurrentAction() { |
Austin Schuh | c95c2b7 | 2014-03-02 11:56:49 -0800 | [diff] [blame] | 135 | LOG(INFO, "Canceling current action\n"); |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 136 | if (current_action_) { |
| 137 | current_action_->Cancel(); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | // Cancels all running actions. |
| 142 | void CancelAllActions() { |
Brian Silverman | 101b964 | 2014-03-08 12:45:16 -0800 | [diff] [blame] | 143 | LOG(DEBUG, "Cancelling all actions\n"); |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 144 | if (current_action_) { |
| 145 | current_action_->Cancel(); |
| 146 | } |
| 147 | next_action_.reset(); |
| 148 | } |
| 149 | |
| 150 | // Runs the next action when the current one is finished running. |
| 151 | void Tick() { |
| 152 | if (current_action_) { |
| 153 | if (!current_action_->Running()) { |
Austin Schuh | c95c2b7 | 2014-03-02 11:56:49 -0800 | [diff] [blame] | 154 | LOG(INFO, "Action is done.\n"); |
| 155 | current_action_ = ::std::move(next_action_); |
| 156 | if (current_action_) { |
| 157 | LOG(INFO, "Running next action\n"); |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 158 | current_action_->Start(); |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | // Returns true if any action is running or could be running. |
| 165 | // For a one cycle faster response, call Tick before running this. |
| 166 | bool Running() { return (bool)current_action_; } |
| 167 | |
| 168 | private: |
| 169 | ::std::unique_ptr<Action> current_action_; |
| 170 | ::std::unique_ptr<Action> next_action_; |
| 171 | }; |
| 172 | |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 173 | |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 174 | class Reader : public ::aos::input::JoystickInput { |
| 175 | public: |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 176 | Reader() |
| 177 | : is_high_gear_(false), |
Austin Schuh | 9cb836e | 2014-02-23 19:25:55 -0800 | [diff] [blame] | 178 | shot_power_(80.0), |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 179 | goal_angle_(0.0), |
Brian Silverman | 545f2ad | 2014-03-14 12:31:42 -0700 | [diff] [blame] | 180 | separation_angle_(kGrabSeparation), |
Brian Silverman | a379f00 | 2014-03-22 19:34:53 -0700 | [diff] [blame^] | 181 | velocity_compensation_(0.0), |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 182 | intake_power_(0.0), |
| 183 | was_running_(false) {} |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 184 | |
| 185 | virtual void RunIteration(const ::aos::input::driver_station::Data &data) { |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 186 | if (data.GetControlBit(ControlBit::kAutonomous)) { |
| 187 | if (data.PosEdge(ControlBit::kEnabled)){ |
| 188 | LOG(INFO, "Starting auto mode\n"); |
| 189 | ::frc971::autonomous::autonomous.MakeWithBuilder() |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 190 | .run_auto(true) |
| 191 | .Send(); |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 192 | } else if (data.NegEdge(ControlBit::kEnabled)) { |
| 193 | LOG(INFO, "Stopping auto mode\n"); |
| 194 | ::frc971::autonomous::autonomous.MakeWithBuilder() |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 195 | .run_auto(false) |
| 196 | .Send(); |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 197 | } |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 198 | } else { |
| 199 | HandleTeleop(data); |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 200 | } |
| 201 | } |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 202 | |
| 203 | void HandleDrivetrain(const ::aos::input::driver_station::Data &data) { |
| 204 | bool is_control_loop_driving = false; |
| 205 | double left_goal = 0.0; |
| 206 | double right_goal = 0.0; |
| 207 | const double wheel = -data.GetAxis(kSteeringWheel); |
| 208 | const double throttle = -data.GetAxis(kDriveThrottle); |
| 209 | const double kThrottleGain = 1.0 / 2.5; |
| 210 | if (false && (data.IsPressed(kDriveControlLoopEnable1) || |
| 211 | data.IsPressed(kDriveControlLoopEnable2))) { |
| 212 | // TODO(austin): Static sucks! |
| 213 | static double distance = 0.0; |
| 214 | static double angle = 0.0; |
| 215 | static double filtered_goal_distance = 0.0; |
| 216 | if (data.PosEdge(kDriveControlLoopEnable1) || |
| 217 | data.PosEdge(kDriveControlLoopEnable2)) { |
Brian Silverman | 6bf0d3c | 2014-03-08 12:52:54 -0800 | [diff] [blame] | 218 | if (drivetrain.position.FetchLatest() && gyro_reading.FetchLatest()) { |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 219 | distance = (drivetrain.position->left_encoder + |
| 220 | drivetrain.position->right_encoder) / |
| 221 | 2.0 - |
| 222 | throttle * kThrottleGain / 2.0; |
Brian Silverman | 6bf0d3c | 2014-03-08 12:52:54 -0800 | [diff] [blame] | 223 | angle = gyro_reading->angle; |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 224 | filtered_goal_distance = distance; |
| 225 | } |
| 226 | } |
| 227 | is_control_loop_driving = true; |
| 228 | |
| 229 | // const double gyro_angle = Gyro.View().angle; |
| 230 | const double goal_theta = angle - wheel * 0.27; |
| 231 | const double goal_distance = distance + throttle * kThrottleGain; |
| 232 | const double robot_width = 22.0 / 100.0 * 2.54; |
| 233 | const double kMaxVelocity = 0.6; |
| 234 | if (goal_distance > kMaxVelocity * 0.02 + filtered_goal_distance) { |
| 235 | filtered_goal_distance += kMaxVelocity * 0.02; |
| 236 | } else if (goal_distance < |
| 237 | -kMaxVelocity * 0.02 + filtered_goal_distance) { |
| 238 | filtered_goal_distance -= kMaxVelocity * 0.02; |
| 239 | } else { |
| 240 | filtered_goal_distance = goal_distance; |
| 241 | } |
| 242 | left_goal = filtered_goal_distance - robot_width * goal_theta / 2.0; |
| 243 | right_goal = filtered_goal_distance + robot_width * goal_theta / 2.0; |
| 244 | is_high_gear_ = false; |
| 245 | |
| 246 | LOG(DEBUG, "Left goal %f Right goal %f\n", left_goal, right_goal); |
| 247 | } |
| 248 | if (!drivetrain.goal.MakeWithBuilder() |
| 249 | .steering(wheel) |
| 250 | .throttle(throttle) |
| 251 | .highgear(is_high_gear_) |
| 252 | .quickturn(data.IsPressed(kQuickTurn)) |
| 253 | .control_loop_driving(is_control_loop_driving) |
| 254 | .left_goal(left_goal) |
| 255 | .right_goal(right_goal) |
| 256 | .Send()) { |
| 257 | LOG(WARNING, "sending stick values failed\n"); |
| 258 | } |
| 259 | if (data.PosEdge(kShiftHigh)) { |
| 260 | is_high_gear_ = false; |
| 261 | } |
| 262 | if (data.PosEdge(kShiftLow)) { |
| 263 | is_high_gear_ = true; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | void SetGoal(ClawGoal goal) { |
| 268 | goal_angle_ = goal.angle; |
| 269 | separation_angle_ = goal.separation; |
Brian Silverman | a379f00 | 2014-03-22 19:34:53 -0700 | [diff] [blame^] | 270 | velocity_compensation_ = 0.0; |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 271 | intake_power_ = 0.0; |
| 272 | } |
| 273 | |
| 274 | void SetGoal(ShotGoal goal) { |
| 275 | goal_angle_ = goal.claw.angle; |
| 276 | separation_angle_ = goal.claw.separation; |
| 277 | shot_power_ = goal.shot_power; |
| 278 | velocity_compensation_ = goal.velocity_compensation; |
| 279 | intake_power_ = goal.intake_power; |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | void HandleTeleop(const ::aos::input::driver_station::Data &data) { |
| 283 | HandleDrivetrain(data); |
Austin Schuh | c95c2b7 | 2014-03-02 11:56:49 -0800 | [diff] [blame] | 284 | if (!data.GetControlBit(ControlBit::kEnabled)) { |
| 285 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 286 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | c95c2b7 | 2014-03-02 11:56:49 -0800 | [diff] [blame] | 287 | } |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 288 | if (data.IsPressed(kRollersIn) || data.IsPressed(kRollersOut)) { |
| 289 | intake_power_ = 0.0; |
Brian Silverman | 545f2ad | 2014-03-14 12:31:42 -0700 | [diff] [blame] | 290 | separation_angle_ = kGrabSeparation; |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 291 | } |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 292 | |
Brian Silverman | 18f6e64 | 2014-03-13 18:52:47 -0700 | [diff] [blame] | 293 | static const double kAdjustClawGoalDeadband = 0.08; |
| 294 | double claw_goal_adjust = data.GetAxis(kAdjustClawGoal); |
| 295 | if (::std::abs(claw_goal_adjust) < kAdjustClawGoalDeadband) { |
| 296 | claw_goal_adjust = 0; |
| 297 | } else { |
| 298 | claw_goal_adjust = (claw_goal_adjust - |
| 299 | ((claw_goal_adjust < 0) ? -kAdjustClawGoalDeadband |
| 300 | : kAdjustClawGoalDeadband)) * |
| 301 | 0.035; |
| 302 | } |
| 303 | double claw_separation_adjust = data.GetAxis(kAdjustClawSeparation); |
| 304 | if (::std::abs(claw_separation_adjust) < kAdjustClawGoalDeadband) { |
| 305 | claw_separation_adjust = 0; |
| 306 | } else { |
| 307 | claw_separation_adjust = |
| 308 | (claw_separation_adjust - |
| 309 | ((claw_separation_adjust < 0) ? -kAdjustClawGoalDeadband |
| 310 | : kAdjustClawGoalDeadband)) * |
| 311 | -0.035; |
| 312 | } |
| 313 | |
Brian Silverman | 4d1795d | 2014-03-13 15:53:40 -0700 | [diff] [blame] | 314 | if (data.GetAxis(kFlipRobot) > 0.5) { |
Brian Silverman | 18f6e64 | 2014-03-13 18:52:47 -0700 | [diff] [blame] | 315 | claw_goal_adjust += claw_separation_adjust; |
| 316 | claw_goal_adjust *= -1; |
| 317 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 318 | if (data.IsPressed(kIntakeOpenPosition)) { |
| 319 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 320 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 321 | SetGoal(kFlippedIntakeOpenGoal); |
| 322 | } else if (data.IsPressed(kIntakePosition)) { |
| 323 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 324 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 325 | SetGoal(kFlippedIntakeGoal); |
| 326 | } else if (data.IsPressed(kTuck)) { |
| 327 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 328 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 329 | SetGoal(kFlippedTuckGoal); |
| 330 | } else if (data.PosEdge(kLongShot)) { |
| 331 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 332 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 333 | SetGoal(kFlippedLongShotGoal); |
| 334 | } else if (data.PosEdge(kMediumShot)) { |
| 335 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 336 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 337 | SetGoal(kFlippedMediumShotGoal); |
| 338 | } else if (data.PosEdge(kShortShot)) { |
| 339 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 340 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 341 | SetGoal(kFlippedShortShotGoal); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 342 | } else if (data.PosEdge(kTrussShot)) { |
| 343 | action_queue_.CancelAllActions(); |
| 344 | LOG(DEBUG, "Canceling\n"); |
| 345 | SetGoal(kTrussShotGoal); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 346 | } |
| 347 | } else { |
| 348 | if (data.IsPressed(kIntakeOpenPosition)) { |
| 349 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 350 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 351 | SetGoal(kIntakeOpenGoal); |
| 352 | } else if (data.IsPressed(kIntakePosition)) { |
| 353 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 354 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 355 | SetGoal(kIntakeGoal); |
| 356 | } else if (data.IsPressed(kTuck)) { |
| 357 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 358 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 359 | SetGoal(kTuckGoal); |
| 360 | } else if (data.PosEdge(kLongShot)) { |
| 361 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 362 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 363 | SetGoal(kLongShotGoal); |
| 364 | } else if (data.PosEdge(kMediumShot)) { |
| 365 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 366 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 367 | SetGoal(kMediumShotGoal); |
| 368 | } else if (data.PosEdge(kShortShot)) { |
| 369 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 370 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 371 | SetGoal(kShortShotGoal); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 372 | } else if (data.PosEdge(kTrussShot)) { |
| 373 | action_queue_.CancelAllActions(); |
| 374 | LOG(DEBUG, "Canceling\n"); |
| 375 | SetGoal(kTrussShotGoal); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 376 | } |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 377 | } |
| 378 | |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 379 | /* |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 380 | if (data.PosEdge(kCatch)) { |
| 381 | auto catch_action = MakeCatchAction(); |
| 382 | catch_action->GetGoal()->catch_angle = goal_angle_; |
| 383 | action_queue_.QueueAction(::std::move(catch_action)); |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 384 | } |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 385 | */ |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 386 | |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 387 | if (data.PosEdge(kFire)) { |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 388 | action_queue_.QueueAction(actions::MakeShootAction()); |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | action_queue_.Tick(); |
Austin Schuh | c95c2b7 | 2014-03-02 11:56:49 -0800 | [diff] [blame] | 392 | if (data.IsPressed(kUnload) || data.IsPressed(kReload)) { |
| 393 | action_queue_.CancelAllActions(); |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 394 | LOG(DEBUG, "Canceling\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 395 | intake_power_ = 0.0; |
Brian Silverman | a379f00 | 2014-03-22 19:34:53 -0700 | [diff] [blame^] | 396 | velocity_compensation_ = 0.0; |
Austin Schuh | c95c2b7 | 2014-03-02 11:56:49 -0800 | [diff] [blame] | 397 | } |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 398 | |
| 399 | // Send out the claw and shooter goals if no actions are running. |
| 400 | if (!action_queue_.Running()) { |
Brian Silverman | 18f6e64 | 2014-03-13 18:52:47 -0700 | [diff] [blame] | 401 | goal_angle_ += claw_goal_adjust; |
| 402 | separation_angle_ += claw_separation_adjust; |
| 403 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 404 | // If the action just ended, turn the intake off and stop velocity |
| 405 | // compensating. |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 406 | if (was_running_) { |
| 407 | intake_power_ = 0.0; |
Brian Silverman | a379f00 | 2014-03-22 19:34:53 -0700 | [diff] [blame^] | 408 | velocity_compensation_ = 0.0; |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | control_loops::drivetrain.status.FetchLatest(); |
Brian Silverman | a379f00 | 2014-03-22 19:34:53 -0700 | [diff] [blame^] | 412 | double goal_angle = goal_angle_; |
| 413 | if (control_loops::drivetrain.status.get()) { |
| 414 | goal_angle += |
| 415 | SpeedToAngleOffset(control_loops::drivetrain.status->robot_speed); |
| 416 | } else { |
| 417 | LOG_INTERVAL(no_drivetrain_status_); |
| 418 | } |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 419 | double separation_angle = separation_angle_; |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 420 | |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 421 | if (data.IsPressed(kCatch)) { |
| 422 | const double kCatchSeparation = 1.0; |
| 423 | goal_angle -= kCatchSeparation / 2.0; |
| 424 | separation_angle = kCatchSeparation; |
| 425 | } |
| 426 | |
| 427 | bool intaking = |
| 428 | data.IsPressed(kRollersIn) || data.IsPressed(kIntakePosition) || |
| 429 | data.IsPressed(kIntakeOpenPosition) || data.IsPressed(kCatch); |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 430 | if (!control_loops::claw_queue_group.goal.MakeWithBuilder() |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 431 | .bottom_angle(goal_angle) |
Austin Schuh | ade6d08 | 2014-03-09 00:53:06 -0800 | [diff] [blame] | 432 | .separation_angle(separation_angle) |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 433 | .intake(intaking ? 12.0 |
| 434 | : (data.IsPressed(kRollersOut) ? -12.0 |
| 435 | : intake_power_)) |
| 436 | .centering(intaking ? 12.0 : 0.0) |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 437 | .Send()) { |
| 438 | LOG(WARNING, "sending claw goal failed\n"); |
| 439 | } |
| 440 | |
| 441 | if (!control_loops::shooter_queue_group.goal.MakeWithBuilder() |
| 442 | .shot_power(shot_power_) |
| 443 | .shot_requested(data.IsPressed(kFire)) |
| 444 | .unload_requested(data.IsPressed(kUnload)) |
| 445 | .load_requested(data.IsPressed(kReload)) |
| 446 | .Send()) { |
| 447 | LOG(WARNING, "sending shooter goal failed\n"); |
| 448 | } |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 449 | } |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 450 | was_running_ = action_queue_.Running(); |
| 451 | } |
| 452 | |
| 453 | double SpeedToAngleOffset(double speed) { |
| 454 | const frc971::constants::Values &values = frc971::constants::GetValues(); |
| 455 | // scale speed to a [0.0-1.0] on something close to the max |
| 456 | // TODO(austin): Change the scale factor for different shots. |
Brian Silverman | a379f00 | 2014-03-22 19:34:53 -0700 | [diff] [blame^] | 457 | return (speed / values.drivetrain_max_speed) * velocity_compensation_; |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 458 | } |
| 459 | |
Austin Schuh | 01c652b | 2014-02-21 23:13:42 -0800 | [diff] [blame] | 460 | private: |
Austin Schuh | 58d2368 | 2014-02-23 01:39:50 -0800 | [diff] [blame] | 461 | bool is_high_gear_; |
| 462 | double shot_power_; |
| 463 | double goal_angle_; |
| 464 | double separation_angle_; |
Brian Silverman | a379f00 | 2014-03-22 19:34:53 -0700 | [diff] [blame^] | 465 | double velocity_compensation_; |
Austin Schuh | 5d8c5e7 | 2014-03-07 20:24:34 -0800 | [diff] [blame] | 466 | double intake_power_; |
| 467 | bool was_running_; |
Austin Schuh | b7dfabc | 2014-03-01 18:57:42 -0800 | [diff] [blame] | 468 | |
| 469 | ActionQueue action_queue_; |
Brian Silverman | a379f00 | 2014-03-22 19:34:53 -0700 | [diff] [blame^] | 470 | |
| 471 | ::aos::util::SimpleLogInterval no_drivetrain_status_ = |
| 472 | ::aos::util::SimpleLogInterval(::aos::time::Time::InSeconds(0.2), WARNING, |
| 473 | "no drivetrain status"); |
Brian Silverman | 756f9ff | 2014-01-17 23:40:23 -0800 | [diff] [blame] | 474 | }; |
| 475 | |
| 476 | } // namespace joysticks |
| 477 | } // namespace input |
| 478 | } // namespace frc971 |
| 479 | |
| 480 | int main() { |
| 481 | ::aos::Init(); |
| 482 | ::frc971::input::joysticks::Reader reader; |
| 483 | reader.Run(); |
| 484 | ::aos::Cleanup(); |
| 485 | } |