Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | |
| 3 | #include <memory> |
Austin Schuh | 4701741 | 2013-03-10 11:50:46 -0700 | [diff] [blame] | 4 | |
Brian | 3afd6fc | 2014-04-02 20:41:49 -0700 | [diff] [blame] | 5 | #include "aos/common/util/phased_loop.h" |
Austin Schuh | 4701741 | 2013-03-10 11:50:46 -0700 | [diff] [blame] | 6 | #include "aos/common/time.h" |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 7 | #include "aos/common/util/trapezoid_profile.h" |
Brian Silverman | 598800f | 2013-05-09 17:08:42 -0700 | [diff] [blame] | 8 | #include "aos/common/logging/logging.h" |
Brian Silverman | 6f62154 | 2014-04-06 16:00:41 -0700 | [diff] [blame] | 9 | #include "aos/common/logging/queue_logging.h" |
Brian Silverman | 598800f | 2013-05-09 17:08:42 -0700 | [diff] [blame] | 10 | |
| 11 | #include "frc971/autonomous/auto.q.h" |
Austin Schuh | bb227f8 | 2015-09-06 15:27:52 -0700 | [diff] [blame^] | 12 | #include "y2015/autonomous/auto.q.h" |
Brian Silverman | b691f5e | 2015-08-02 11:37:55 -0700 | [diff] [blame] | 13 | #include "y2015/constants.h" |
| 14 | #include "y2015/control_loops/drivetrain/drivetrain.q.h" |
| 15 | #include "y2015/actors/drivetrain_actor.h" |
| 16 | #include "y2015/control_loops/claw/claw.q.h" |
| 17 | #include "y2015/control_loops/fridge/fridge.q.h" |
| 18 | #include "y2015/actors/pickup_actor.h" |
| 19 | #include "y2015/actors/stack_actor.h" |
| 20 | #include "y2015/actors/held_to_lift_actor.h" |
Austin Schuh | 4701741 | 2013-03-10 11:50:46 -0700 | [diff] [blame] | 21 | |
| 22 | using ::aos::time::Time; |
Ben Fredrickson | 0a6ad25 | 2015-03-15 18:51:42 -0700 | [diff] [blame] | 23 | using ::frc971::control_loops::claw_queue; |
Brian Silverman | 5bfda6c | 2015-03-21 23:40:13 -0700 | [diff] [blame] | 24 | using ::frc971::control_loops::fridge_queue; |
Brian Silverman | 93936f7 | 2015-03-19 23:38:30 -0700 | [diff] [blame] | 25 | using ::frc971::control_loops::drivetrain_queue; |
Austin Schuh | 4701741 | 2013-03-10 11:50:46 -0700 | [diff] [blame] | 26 | |
| 27 | namespace frc971 { |
| 28 | namespace autonomous { |
| 29 | |
Ben Fredrickson | 0a6ad25 | 2015-03-15 18:51:42 -0700 | [diff] [blame] | 30 | constexpr double kClawAutoVelocity = 3.00; |
| 31 | constexpr double kClawAutoAcceleration = 6.0; |
| 32 | constexpr double kAngleEpsilon = 0.10; |
Austin Schuh | 1f3764e | 2015-04-18 23:00:09 -0700 | [diff] [blame] | 33 | const double kClawTotePackAngle = 0.90; |
| 34 | const double kArmRaiseLowerClearance = -0.08; |
| 35 | const double kClawStackClearance = 0.55; |
| 36 | const double kStackUpHeight = 0.60; |
| 37 | const double kStackUpArm = 0.0; |
| 38 | |
| 39 | struct ProfileParams { |
| 40 | double velocity; |
| 41 | double acceleration; |
| 42 | }; |
Ben Fredrickson | 0a6ad25 | 2015-03-15 18:51:42 -0700 | [diff] [blame] | 43 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 44 | namespace time = ::aos::time; |
| 45 | |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 46 | static double left_initial_position, right_initial_position; |
| 47 | |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 48 | bool ShouldExitAuto() { |
| 49 | ::frc971::autonomous::autonomous.FetchLatest(); |
| 50 | bool ans = !::frc971::autonomous::autonomous->run_auto; |
| 51 | if (ans) { |
| 52 | LOG(INFO, "Time to exit auto mode\n"); |
| 53 | } |
| 54 | return ans; |
| 55 | } |
| 56 | |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 57 | void StopDrivetrain() { |
| 58 | LOG(INFO, "Stopping the drivetrain\n"); |
Brian Silverman | ada5f2c | 2015-02-01 02:41:14 -0500 | [diff] [blame] | 59 | control_loops::drivetrain_queue.goal.MakeWithBuilder() |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 60 | .control_loop_driving(true) |
Brian Silverman | ce86bac | 2013-03-31 19:07:24 -0700 | [diff] [blame] | 61 | .left_goal(left_initial_position) |
| 62 | .left_velocity_goal(0) |
| 63 | .right_goal(right_initial_position) |
| 64 | .right_velocity_goal(0) |
| 65 | .quickturn(false) |
| 66 | .Send(); |
| 67 | } |
| 68 | |
| 69 | void ResetDrivetrain() { |
| 70 | LOG(INFO, "resetting the drivetrain\n"); |
Brian Silverman | ada5f2c | 2015-02-01 02:41:14 -0500 | [diff] [blame] | 71 | control_loops::drivetrain_queue.goal.MakeWithBuilder() |
Brian Silverman | ce86bac | 2013-03-31 19:07:24 -0700 | [diff] [blame] | 72 | .control_loop_driving(false) |
Brian Silverman | 93335ae | 2015-01-26 20:43:39 -0500 | [diff] [blame] | 73 | //.highgear(false) |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 74 | .steering(0.0) |
| 75 | .throttle(0.0) |
Brian Silverman | 38ea9bf | 2014-04-19 22:57:54 -0700 | [diff] [blame] | 76 | .left_goal(left_initial_position) |
| 77 | .left_velocity_goal(0) |
| 78 | .right_goal(right_initial_position) |
| 79 | .right_velocity_goal(0) |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 80 | .Send(); |
| 81 | } |
| 82 | |
Austin Schuh | 1f3764e | 2015-04-18 23:00:09 -0700 | [diff] [blame] | 83 | void WaitUntilDoneOrCanceled( |
| 84 | ::std::unique_ptr<aos::common::actions::Action> action) { |
| 85 | if (!action) { |
| 86 | LOG(ERROR, "No action, not waiting\n"); |
| 87 | return; |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 88 | } |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 89 | while (true) { |
| 90 | // Poll the running bit and auto done bits. |
Brian Silverman | 547abb3 | 2015-02-16 00:37:48 -0500 | [diff] [blame] | 91 | ::aos::time::PhasedLoopXMS(5, 2500); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 92 | if (!action->Running() || ShouldExitAuto()) { |
| 93 | return; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
Austin Schuh | 1f3764e | 2015-04-18 23:00:09 -0700 | [diff] [blame] | 98 | void StepDrive(double distance, double theta) { |
| 99 | double left_goal = (left_initial_position + distance - |
| 100 | theta * constants::GetValues().turn_width / 2.0); |
| 101 | double right_goal = (right_initial_position + distance + |
| 102 | theta * constants::GetValues().turn_width / 2.0); |
| 103 | control_loops::drivetrain_queue.goal.MakeWithBuilder() |
| 104 | .control_loop_driving(true) |
| 105 | .left_goal(left_goal) |
| 106 | .right_goal(right_goal) |
| 107 | .left_velocity_goal(0.0) |
| 108 | .right_velocity_goal(0.0) |
| 109 | .Send(); |
| 110 | left_initial_position = left_goal; |
| 111 | right_initial_position = right_goal; |
| 112 | } |
| 113 | |
| 114 | void WaitUntilNear(double distance) { |
| 115 | while (true) { |
| 116 | if (ShouldExitAuto()) return; |
| 117 | control_loops::drivetrain_queue.status.FetchAnother(); |
| 118 | double left_error = ::std::abs( |
| 119 | left_initial_position - |
| 120 | control_loops::drivetrain_queue.status->filtered_left_position); |
| 121 | double right_error = ::std::abs( |
| 122 | right_initial_position - |
| 123 | control_loops::drivetrain_queue.status->filtered_right_position); |
| 124 | const double kPositionThreshold = 0.05 + distance; |
| 125 | if (right_error < kPositionThreshold && left_error < kPositionThreshold) { |
| 126 | LOG(INFO, "At the goal\n"); |
| 127 | return; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | const ProfileParams kFastDrive = {2.0, 3.5}; |
| 133 | const ProfileParams kFastKnockDrive = {2.0, 3.0}; |
Austin Schuh | 9c414f4 | 2015-04-19 20:01:56 -0700 | [diff] [blame] | 134 | const ProfileParams kStackingSecondDrive = {2.0, 1.5}; |
Austin Schuh | 1f3764e | 2015-04-18 23:00:09 -0700 | [diff] [blame] | 135 | const ProfileParams kFastTurn = {3.0, 10.0}; |
Austin Schuh | 9c414f4 | 2015-04-19 20:01:56 -0700 | [diff] [blame] | 136 | const ProfileParams kStackingFirstTurn = {1.0, 1.0}; |
| 137 | const ProfileParams kStackingSecondTurn = {2.0, 6.0}; |
Austin Schuh | 1f3764e | 2015-04-18 23:00:09 -0700 | [diff] [blame] | 138 | const ProfileParams kComboTurn = {1.2, 8.0}; |
| 139 | const ProfileParams kRaceTurn = {4.0, 10.0}; |
| 140 | const ProfileParams kRaceDrive = {2.0, 2.0}; |
| 141 | const ProfileParams kRaceBackupDrive = {2.0, 5.0}; |
| 142 | |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 143 | ::std::unique_ptr<::frc971::actors::DrivetrainAction> SetDriveGoal( |
Austin Schuh | 1f3764e | 2015-04-18 23:00:09 -0700 | [diff] [blame] | 144 | double distance, const ProfileParams drive_params, double theta = 0, const ProfileParams &turn_params = kFastTurn) { |
Austin Schuh | a4faacc | 2014-03-09 00:50:50 -0800 | [diff] [blame] | 145 | LOG(INFO, "Driving to %f\n", distance); |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 146 | |
| 147 | ::frc971::actors::DrivetrainActionParams params; |
| 148 | params.left_initial_position = left_initial_position; |
| 149 | params.right_initial_position = right_initial_position; |
| 150 | params.y_offset = distance; |
| 151 | params.theta_offset = theta; |
Austin Schuh | 1f3764e | 2015-04-18 23:00:09 -0700 | [diff] [blame] | 152 | params.maximum_turn_acceleration = turn_params.acceleration; |
| 153 | params.maximum_turn_velocity = turn_params.velocity; |
| 154 | params.maximum_velocity = drive_params.velocity; |
| 155 | params.maximum_acceleration = drive_params.acceleration; |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 156 | auto drivetrain_action = actors::MakeDrivetrainAction(params); |
| 157 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 158 | drivetrain_action->Start(); |
Brian Silverman | ad9e000 | 2014-04-13 14:55:57 -0700 | [diff] [blame] | 159 | left_initial_position += |
| 160 | distance - theta * constants::GetValues().turn_width / 2.0; |
| 161 | right_initial_position += |
Daniel Petti | b0733be | 2014-11-14 22:44:03 -0800 | [diff] [blame] | 162 | distance + theta * constants::GetValues().turn_width / 2.0; |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 163 | return ::std::move(drivetrain_action); |
| 164 | } |
| 165 | |
Austin Schuh | 1f3764e | 2015-04-18 23:00:09 -0700 | [diff] [blame] | 166 | const ProfileParams kFridgeYProfile{1.0, 4.0}; |
| 167 | const ProfileParams kFridgeXProfile{1.0, 2.0}; |
| 168 | const ProfileParams kFridgeFastXProfile{1.2, 5.0}; |
| 169 | |
| 170 | static double fridge_goal_x = 0.0; |
| 171 | static double fridge_goal_y = 0.0; |
| 172 | |
| 173 | void MoveFridge(double x, double y, bool grabbers, const ProfileParams x_params, |
| 174 | const ProfileParams y_params) { |
| 175 | auto new_fridge_goal = fridge_queue.goal.MakeMessage(); |
| 176 | new_fridge_goal->profiling_type = 1; |
| 177 | |
| 178 | new_fridge_goal->max_y_velocity = y_params.velocity; |
| 179 | new_fridge_goal->max_y_acceleration = y_params.acceleration; |
| 180 | new_fridge_goal->y = y; |
| 181 | fridge_goal_y = y; |
| 182 | new_fridge_goal->y_velocity = 0.0; |
| 183 | |
| 184 | new_fridge_goal->max_x_velocity = x_params.velocity; |
| 185 | new_fridge_goal->max_x_acceleration = x_params.acceleration; |
| 186 | new_fridge_goal->x = x; |
| 187 | fridge_goal_x = x; |
| 188 | new_fridge_goal->x_velocity = 0.0; |
| 189 | |
| 190 | new_fridge_goal->grabbers.top_front = grabbers; |
| 191 | new_fridge_goal->grabbers.top_back = grabbers; |
| 192 | new_fridge_goal->grabbers.bottom_front = grabbers; |
| 193 | new_fridge_goal->grabbers.bottom_back = grabbers; |
| 194 | |
| 195 | if (!new_fridge_goal.Send()) { |
| 196 | LOG(ERROR, "Sending fridge goal failed.\n"); |
| 197 | return; |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 198 | } |
Austin Schuh | 1f3764e | 2015-04-18 23:00:09 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | void WaitForFridge() { |
| 202 | while (true) { |
| 203 | if (ShouldExitAuto()) return; |
| 204 | control_loops::fridge_queue.status.FetchAnother(); |
| 205 | |
| 206 | constexpr double kProfileError = 1e-5; |
| 207 | constexpr double kXEpsilon = 0.03, kYEpsilon = 0.03; |
| 208 | |
| 209 | if (control_loops::fridge_queue.status->state != 4) { |
| 210 | LOG(ERROR, "Fridge no longer running, aborting action\n"); |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | if (::std::abs(control_loops::fridge_queue.status->goal_x - fridge_goal_x) < |
| 215 | kProfileError && |
| 216 | ::std::abs(control_loops::fridge_queue.status->goal_y - fridge_goal_y) < |
| 217 | kProfileError && |
| 218 | ::std::abs(control_loops::fridge_queue.status->goal_x_velocity) < |
| 219 | kProfileError && |
| 220 | ::std::abs(control_loops::fridge_queue.status->goal_y_velocity) < |
| 221 | kProfileError) { |
| 222 | LOG(INFO, "Profile done.\n"); |
| 223 | if (::std::abs(control_loops::fridge_queue.status->x - fridge_goal_x) < |
| 224 | kXEpsilon && |
| 225 | ::std::abs(control_loops::fridge_queue.status->y - fridge_goal_y) < |
| 226 | kYEpsilon) { |
| 227 | LOG(INFO, "Near goal, done.\n"); |
| 228 | return; |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | void InitializeEncoders() { |
| 235 | control_loops::drivetrain_queue.status.FetchAnother(); |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 236 | left_initial_position = |
Brian Silverman | ada5f2c | 2015-02-01 02:41:14 -0500 | [diff] [blame] | 237 | control_loops::drivetrain_queue.status->filtered_left_position; |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 238 | right_initial_position = |
Brian Silverman | ada5f2c | 2015-02-01 02:41:14 -0500 | [diff] [blame] | 239 | control_loops::drivetrain_queue.status->filtered_right_position; |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 240 | } |
| 241 | |
Austin Schuh | 1f3764e | 2015-04-18 23:00:09 -0700 | [diff] [blame] | 242 | void WaitForClawZero() { |
| 243 | LOG(INFO, "Waiting for claw to zero.\n"); |
| 244 | while (true) { |
| 245 | control_loops::claw_queue.status.FetchAnother(); |
| 246 | LOG_STRUCT(DEBUG, "Got claw status", *control_loops::claw_queue.status); |
| 247 | if (control_loops::claw_queue.status->zeroed) { |
| 248 | LOG(INFO, "Claw zeroed\n"); |
| 249 | return; |
| 250 | } |
| 251 | |
| 252 | if (ShouldExitAuto()) return; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | void WaitForFridgeZero() { |
| 257 | LOG(INFO, "Waiting for claw to zero.\n"); |
| 258 | while (true) { |
| 259 | control_loops::fridge_queue.status.FetchAnother(); |
| 260 | LOG_STRUCT(DEBUG, "Got fridge status", *control_loops::fridge_queue.status); |
| 261 | if (control_loops::fridge_queue.status->zeroed) { |
| 262 | LOG(INFO, "Fridge zeroed\n"); |
| 263 | return; |
| 264 | } |
| 265 | |
| 266 | if (ShouldExitAuto()) return; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | constexpr ProfileParams kDefaultClawParams = {kClawAutoVelocity, |
| 271 | kClawAutoAcceleration}; |
| 272 | |
| 273 | // Move the claw in a very small number of cycles. |
| 274 | constexpr ProfileParams kInstantaneousClaw = {100.0, 100.0}; |
| 275 | constexpr ProfileParams kFastClaw = {8.0, 20.0}; |
| 276 | |
| 277 | void SetClawStateNoWait(double angle, double intake_voltage, |
| 278 | double rollers_closed, |
| 279 | const ProfileParams &claw_params = kDefaultClawParams) { |
Ben Fredrickson | 0a6ad25 | 2015-03-15 18:51:42 -0700 | [diff] [blame] | 280 | auto message = control_loops::claw_queue.goal.MakeMessage(); |
| 281 | message->angle = angle; |
Austin Schuh | 1f3764e | 2015-04-18 23:00:09 -0700 | [diff] [blame] | 282 | message->max_velocity = claw_params.velocity; |
| 283 | message->max_acceleration = claw_params.acceleration; |
Ben Fredrickson | 0a6ad25 | 2015-03-15 18:51:42 -0700 | [diff] [blame] | 284 | message->angular_velocity = 0.0; |
| 285 | message->intake = intake_voltage; |
| 286 | message->rollers_closed = rollers_closed; |
| 287 | |
| 288 | LOG_STRUCT(DEBUG, "Sending claw goal", *message); |
| 289 | message.Send(); |
Austin Schuh | 1f3764e | 2015-04-18 23:00:09 -0700 | [diff] [blame] | 290 | } |
Ben Fredrickson | 0a6ad25 | 2015-03-15 18:51:42 -0700 | [diff] [blame] | 291 | |
Austin Schuh | 1f3764e | 2015-04-18 23:00:09 -0700 | [diff] [blame] | 292 | void SetClawState(double angle, double intake_voltage, double rollers_closed, |
| 293 | const ProfileParams &claw_params = kDefaultClawParams) { |
| 294 | SetClawStateNoWait(angle, intake_voltage, rollers_closed, claw_params); |
Ben Fredrickson | 0a6ad25 | 2015-03-15 18:51:42 -0700 | [diff] [blame] | 295 | while (true) { |
| 296 | control_loops::claw_queue.status.FetchAnother(); |
| 297 | const double current_angle = control_loops::claw_queue.status->angle; |
| 298 | LOG_STRUCT(DEBUG, "Got claw status", *control_loops::claw_queue.status); |
| 299 | |
| 300 | // I believe all we can care about here is the angle. Other values will |
| 301 | // either be set or not, but there is nothing we can do about it. If it |
| 302 | // never gets there we do not care, auto is over for us. |
| 303 | if (::std::abs(current_angle - angle) < kAngleEpsilon) { |
| 304 | break; |
| 305 | } |
Austin Schuh | 1f3764e | 2015-04-18 23:00:09 -0700 | [diff] [blame] | 306 | if (ShouldExitAuto()) return; |
Ben Fredrickson | 0a6ad25 | 2015-03-15 18:51:42 -0700 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
Austin Schuh | bb227f8 | 2015-09-06 15:27:52 -0700 | [diff] [blame^] | 310 | void TripleCanAuto() { |
Ben Fredrickson | 0a6ad25 | 2015-03-15 18:51:42 -0700 | [diff] [blame] | 311 | ::std::unique_ptr<::frc971::actors::DrivetrainAction> drive; |
| 312 | ::std::unique_ptr<::frc971::actors::PickupAction> pickup; |
| 313 | ::std::unique_ptr<::frc971::actors::StackAction> stack; |
Austin Schuh | 1f3764e | 2015-04-18 23:00:09 -0700 | [diff] [blame] | 314 | ::std::unique_ptr<::frc971::actors::HeldToLiftAction> lift; |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 315 | |
Austin Schuh | 9c414f4 | 2015-04-19 20:01:56 -0700 | [diff] [blame] | 316 | actors::PickupParams pickup_params; |
| 317 | // Lift to here initially. |
| 318 | pickup_params.pickup_angle = 0.9; |
| 319 | // Start sucking here |
| 320 | pickup_params.suck_angle = 0.8; |
| 321 | // Go back down to here to finish sucking. |
| 322 | pickup_params.suck_angle_finish = 0.4; |
| 323 | // Pack the box back in here. |
| 324 | pickup_params.pickup_finish_angle = kClawTotePackAngle; |
| 325 | pickup_params.intake_time = 0.70; |
| 326 | pickup_params.intake_voltage = 7.0; |
| 327 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 328 | if (ShouldExitAuto()) return; |
| 329 | InitializeEncoders(); |
Austin Schuh | 1f3764e | 2015-04-18 23:00:09 -0700 | [diff] [blame] | 330 | ResetDrivetrain(); |
Ben Fredrickson | 0a6ad25 | 2015-03-15 18:51:42 -0700 | [diff] [blame] | 331 | |
Austin Schuh | 1f3764e | 2015-04-18 23:00:09 -0700 | [diff] [blame] | 332 | WaitForClawZero(); |
| 333 | WaitForFridgeZero(); |
Brian Silverman | 93936f7 | 2015-03-19 23:38:30 -0700 | [diff] [blame] | 334 | |
Austin Schuh | 1f3764e | 2015-04-18 23:00:09 -0700 | [diff] [blame] | 335 | // Initialize the fridge. |
| 336 | MoveFridge(0.0, 0.3, true, kFridgeXProfile, kFridgeYProfile); |
Brian Silverman | 93936f7 | 2015-03-19 23:38:30 -0700 | [diff] [blame] | 337 | |
Austin Schuh | 1f3764e | 2015-04-18 23:00:09 -0700 | [diff] [blame] | 338 | LOG(INFO, "Lowering claw into position.\n"); |
| 339 | SetClawState(0.0, 2.0, false, kInstantaneousClaw); |
Austin Schuh | 9c414f4 | 2015-04-19 20:01:56 -0700 | [diff] [blame] | 340 | |
Austin Schuh | 1f3764e | 2015-04-18 23:00:09 -0700 | [diff] [blame] | 341 | LOG(INFO, "Sucking in tote.\n"); |
Austin Schuh | 9c414f4 | 2015-04-19 20:01:56 -0700 | [diff] [blame] | 342 | SetClawState(0.0, 6.0, true, kInstantaneousClaw); |
Brian Silverman | 93936f7 | 2015-03-19 23:38:30 -0700 | [diff] [blame] | 343 | |
Austin Schuh | 1f3764e | 2015-04-18 23:00:09 -0700 | [diff] [blame] | 344 | time::SleepFor(time::Time::InSeconds(0.7)); |
| 345 | LOG(INFO, "Done sucking in tote\n"); |
Brian Silverman | 5bfda6c | 2015-03-21 23:40:13 -0700 | [diff] [blame] | 346 | |
Austin Schuh | 1f3764e | 2015-04-18 23:00:09 -0700 | [diff] [blame] | 347 | // Now pick it up |
Austin Schuh | 9c414f4 | 2015-04-19 20:01:56 -0700 | [diff] [blame] | 348 | pickup = actors::MakePickupAction(pickup_params); |
| 349 | pickup->Start(); |
Austin Schuh | 1f3764e | 2015-04-18 23:00:09 -0700 | [diff] [blame] | 350 | |
| 351 | time::SleepFor(time::Time::InSeconds(0.9)); |
| 352 | // Start turning. |
| 353 | LOG(INFO, "Turning in place\n"); |
Austin Schuh | 9c414f4 | 2015-04-19 20:01:56 -0700 | [diff] [blame] | 354 | drive = SetDriveGoal(0.0, kFastDrive, -0.23, kStackingFirstTurn); |
Austin Schuh | 1f3764e | 2015-04-18 23:00:09 -0700 | [diff] [blame] | 355 | |
| 356 | WaitUntilDoneOrCanceled(::std::move(drive)); |
Brian Silverman | 5bfda6c | 2015-03-21 23:40:13 -0700 | [diff] [blame] | 357 | if (ShouldExitAuto()) return; |
| 358 | |
Austin Schuh | 1f3764e | 2015-04-18 23:00:09 -0700 | [diff] [blame] | 359 | LOG(INFO, "Now driving next to the can\n"); |
| 360 | drive = SetDriveGoal(0.60, kFastDrive); |
Brian Silverman | 5bfda6c | 2015-03-21 23:40:13 -0700 | [diff] [blame] | 361 | |
Austin Schuh | 1f3764e | 2015-04-18 23:00:09 -0700 | [diff] [blame] | 362 | WaitUntilDoneOrCanceled(::std::move(pickup)); |
| 363 | if (ShouldExitAuto()) return; |
| 364 | |
| 365 | // Now grab it in the fridge. |
| 366 | { |
| 367 | actors::StackParams params; |
| 368 | |
| 369 | params.claw_out_angle = kClawTotePackAngle; |
| 370 | params.bottom = 0.020; |
| 371 | params.only_place = false; |
| 372 | params.arm_clearance = kArmRaiseLowerClearance; |
| 373 | params.over_box_before_place_height = 0.39; |
| 374 | |
| 375 | stack = actors::MakeStackAction(params); |
| 376 | stack->Start(); |
| 377 | } |
| 378 | WaitUntilDoneOrCanceled(::std::move(stack)); |
| 379 | if (ShouldExitAuto()) return; |
| 380 | |
| 381 | // Lower the claw to knock the tote. |
| 382 | LOG(INFO, "Lowering the claw to knock the tote\n"); |
| 383 | SetClawStateNoWait(0.0, 0.0, true, kFastClaw); |
| 384 | |
| 385 | time::SleepFor(time::Time::InSeconds(0.1)); |
| 386 | if (ShouldExitAuto()) return; |
| 387 | |
| 388 | WaitUntilDoneOrCanceled(::std::move(drive)); |
| 389 | if (ShouldExitAuto()) return; |
| 390 | |
| 391 | LOG(INFO, "Knocking the can over\n"); |
Austin Schuh | 9c414f4 | 2015-04-19 20:01:56 -0700 | [diff] [blame] | 392 | drive = SetDriveGoal(0.40, kFastKnockDrive, 1.05, kComboTurn); |
Austin Schuh | 1f3764e | 2015-04-18 23:00:09 -0700 | [diff] [blame] | 393 | WaitUntilDoneOrCanceled(::std::move(drive)); |
| 394 | if (ShouldExitAuto()) return; |
| 395 | { |
| 396 | actors::HeldToLiftParams params; |
| 397 | params.arm_clearance = kArmRaiseLowerClearance; |
| 398 | params.clamp_pause_time = 0.1; |
| 399 | params.before_lift_settle_time = 0.1; |
| 400 | params.bottom_height = 0.020; |
| 401 | params.claw_out_angle = kClawStackClearance; |
| 402 | params.lift_params.lift_height = kStackUpHeight; |
| 403 | params.lift_params.lift_arm = kStackUpArm; |
Austin Schuh | 3cba379 | 2015-04-19 20:01:22 -0700 | [diff] [blame] | 404 | params.lift_params.second_lift = false; |
Austin Schuh | 1f3764e | 2015-04-18 23:00:09 -0700 | [diff] [blame] | 405 | |
| 406 | lift = actors::MakeHeldToLiftAction(params); |
| 407 | lift->Start(); |
| 408 | } |
| 409 | |
| 410 | LOG(INFO, "Turning back to aim\n"); |
| 411 | drive = SetDriveGoal(0.0, kFastDrive, -0.70); |
| 412 | WaitUntilDoneOrCanceled(::std::move(drive)); |
| 413 | if (ShouldExitAuto()) return; |
| 414 | |
| 415 | SetClawStateNoWait(0.0, 4.0, false, kFastClaw); |
| 416 | LOG(INFO, "Now driving the second tote\n"); |
| 417 | drive = SetDriveGoal(1.05, kFastDrive); |
| 418 | |
| 419 | // Wait until we are almost at the tote, and then start intaking. |
Austin Schuh | 9c414f4 | 2015-04-19 20:01:56 -0700 | [diff] [blame] | 420 | WaitUntilNear(0.35); |
Austin Schuh | 1f3764e | 2015-04-18 23:00:09 -0700 | [diff] [blame] | 421 | |
| 422 | SetClawState(0.0, 6.0, true, kFastClaw); |
| 423 | WaitUntilDoneOrCanceled(::std::move(drive)); |
| 424 | if (ShouldExitAuto()) return; |
| 425 | |
| 426 | if (ShouldExitAuto()) return; |
Austin Schuh | 9c414f4 | 2015-04-19 20:01:56 -0700 | [diff] [blame] | 427 | time::SleepFor(time::Time::InSeconds(0.30)); |
| 428 | if (ShouldExitAuto()) return; |
| 429 | |
| 430 | SetClawStateNoWait(0.0, 4.0, true, kFastClaw); |
| 431 | if (ShouldExitAuto()) return; |
| 432 | time::SleepFor(time::Time::InSeconds(0.10)); |
Austin Schuh | 1f3764e | 2015-04-18 23:00:09 -0700 | [diff] [blame] | 433 | |
| 434 | WaitUntilDoneOrCanceled(::std::move(lift)); |
| 435 | if (ShouldExitAuto()) return; |
| 436 | |
| 437 | LOG(INFO, "Done sucking in tote\n"); |
| 438 | SetClawState(0.0, 0.0, true, kFastClaw); |
| 439 | if (ShouldExitAuto()) return; |
| 440 | |
| 441 | // Now pick it up |
Austin Schuh | 9c414f4 | 2015-04-19 20:01:56 -0700 | [diff] [blame] | 442 | pickup = actors::MakePickupAction(pickup_params); |
| 443 | pickup->Start(); |
Austin Schuh | 1f3764e | 2015-04-18 23:00:09 -0700 | [diff] [blame] | 444 | |
Austin Schuh | 9c414f4 | 2015-04-19 20:01:56 -0700 | [diff] [blame] | 445 | time::SleepFor(time::Time::InSeconds(1.0)); |
Austin Schuh | 1f3764e | 2015-04-18 23:00:09 -0700 | [diff] [blame] | 446 | if (ShouldExitAuto()) return; |
| 447 | |
| 448 | // Start turning. |
| 449 | LOG(INFO, "Turning in place\n"); |
Austin Schuh | 9c414f4 | 2015-04-19 20:01:56 -0700 | [diff] [blame] | 450 | drive = SetDriveGoal(0.0, kStackingSecondDrive, -0.40, kStackingSecondTurn); |
Austin Schuh | 1f3764e | 2015-04-18 23:00:09 -0700 | [diff] [blame] | 451 | |
| 452 | WaitUntilDoneOrCanceled(::std::move(pickup)); |
| 453 | if (ShouldExitAuto()) return; |
| 454 | |
| 455 | // Now grab it in the fridge. |
| 456 | { |
| 457 | actors::StackParams params; |
| 458 | |
| 459 | params.claw_out_angle = kClawTotePackAngle; |
| 460 | params.bottom = 0.020; |
| 461 | params.only_place = false; |
| 462 | params.arm_clearance = kArmRaiseLowerClearance; |
| 463 | params.over_box_before_place_height = 0.39; |
| 464 | |
| 465 | stack = actors::MakeStackAction(params); |
| 466 | stack->Start(); |
| 467 | } |
| 468 | |
Austin Schuh | 1f3764e | 2015-04-18 23:00:09 -0700 | [diff] [blame] | 469 | WaitUntilDoneOrCanceled(::std::move(drive)); |
| 470 | if (ShouldExitAuto()) return; |
Austin Schuh | 9c414f4 | 2015-04-19 20:01:56 -0700 | [diff] [blame] | 471 | LOG(INFO, "Driving next to the can.\n"); |
| 472 | drive = SetDriveGoal(0.65, kStackingSecondDrive); |
| 473 | |
| 474 | WaitUntilDoneOrCanceled(::std::move(stack)); |
| 475 | if (ShouldExitAuto()) return; |
Austin Schuh | 1f3764e | 2015-04-18 23:00:09 -0700 | [diff] [blame] | 476 | |
| 477 | // Lower the claw to knock the tote. |
| 478 | LOG(INFO, "Lowering the claw to knock the tote\n"); |
| 479 | SetClawStateNoWait(0.0, 0.0, true, kFastClaw); |
| 480 | |
| 481 | // Lift the fridge. |
| 482 | MoveFridge(0.0, 0.3, true, kFridgeXProfile, kFridgeYProfile); |
| 483 | |
| 484 | time::SleepFor(time::Time::InSeconds(0.1)); |
| 485 | if (ShouldExitAuto()) return; |
| 486 | |
Austin Schuh | 9c414f4 | 2015-04-19 20:01:56 -0700 | [diff] [blame] | 487 | WaitUntilDoneOrCanceled(::std::move(drive)); |
| 488 | if (ShouldExitAuto()) return; |
| 489 | |
Austin Schuh | 1f3764e | 2015-04-18 23:00:09 -0700 | [diff] [blame] | 490 | LOG(INFO, "Knocking the can over\n"); |
Austin Schuh | 9c414f4 | 2015-04-19 20:01:56 -0700 | [diff] [blame] | 491 | drive = SetDriveGoal(0.40, kFastKnockDrive, 1.05, kComboTurn); |
Austin Schuh | 1f3764e | 2015-04-18 23:00:09 -0700 | [diff] [blame] | 492 | WaitUntilDoneOrCanceled(::std::move(drive)); |
| 493 | if (ShouldExitAuto()) return; |
| 494 | |
| 495 | LOG(INFO, "Turning back to aim\n"); |
| 496 | drive = SetDriveGoal(0.0, kFastDrive, -0.60); |
| 497 | WaitUntilDoneOrCanceled(::std::move(drive)); |
| 498 | if (ShouldExitAuto()) return; |
| 499 | |
| 500 | |
| 501 | SetClawStateNoWait(0.0, 4.0, false, kFastClaw); |
| 502 | LOG(INFO, "Now driving to the last tote\n"); |
| 503 | drive = SetDriveGoal(1.05, kFastDrive); |
| 504 | WaitUntilNear(0.05); |
| 505 | |
| 506 | SetClawState(0.0, 7.0, true, kFastClaw); |
| 507 | if (ShouldExitAuto()) return; |
| 508 | |
| 509 | time::SleepFor(time::Time::InSeconds(0.2)); |
| 510 | if (ShouldExitAuto()) return; |
| 511 | |
| 512 | WaitUntilDoneOrCanceled(::std::move(drive)); |
| 513 | if (ShouldExitAuto()) return; |
| 514 | SetClawState(0.0, 6.0, true, kFastClaw); |
| 515 | |
| 516 | LOG(INFO, "Racing over\n"); |
| 517 | //StepDrive(2.5, -1.4); |
| 518 | drive = SetDriveGoal(2.5, kRaceDrive, -1.4, kRaceTurn); |
| 519 | |
| 520 | time::SleepFor(time::Time::InSeconds(0.5)); |
| 521 | |
| 522 | LOG(INFO, "Moving totes out\n"); |
| 523 | MoveFridge(0.6, 0.32, true, kFridgeXProfile, kFridgeYProfile); |
| 524 | |
| 525 | WaitForFridge(); |
| 526 | if (ShouldExitAuto()) return; |
| 527 | |
| 528 | LOG(INFO, "Lowering totes\n"); |
| 529 | MoveFridge(0.6, 0.15, false, kFridgeXProfile, kFridgeYProfile); |
| 530 | |
| 531 | WaitForFridge(); |
| 532 | if (ShouldExitAuto()) return; |
| 533 | |
| 534 | time::SleepFor(time::Time::InSeconds(0.1)); |
Brian Silverman | 5bfda6c | 2015-03-21 23:40:13 -0700 | [diff] [blame] | 535 | |
Ben Fredrickson | 0a6ad25 | 2015-03-15 18:51:42 -0700 | [diff] [blame] | 536 | if (ShouldExitAuto()) return; |
| 537 | |
Austin Schuh | 1f3764e | 2015-04-18 23:00:09 -0700 | [diff] [blame] | 538 | LOG(INFO, "Retracting\n"); |
| 539 | MoveFridge(0.0, 0.10, false, kFridgeFastXProfile, kFridgeYProfile); |
Ben Fredrickson | 0a6ad25 | 2015-03-15 18:51:42 -0700 | [diff] [blame] | 540 | |
Austin Schuh | 1f3764e | 2015-04-18 23:00:09 -0700 | [diff] [blame] | 541 | SetClawState(0.0, 0.0, false, kFastClaw); |
Ben Fredrickson | 0a6ad25 | 2015-03-15 18:51:42 -0700 | [diff] [blame] | 542 | |
Austin Schuh | 1f3764e | 2015-04-18 23:00:09 -0700 | [diff] [blame] | 543 | if (ShouldExitAuto()) return; |
Ben Fredrickson | 0a6ad25 | 2015-03-15 18:51:42 -0700 | [diff] [blame] | 544 | |
Austin Schuh | 1f3764e | 2015-04-18 23:00:09 -0700 | [diff] [blame] | 545 | WaitUntilDoneOrCanceled(::std::move(drive)); |
| 546 | if (ShouldExitAuto()) return; |
Ben Fredrickson | 0a6ad25 | 2015-03-15 18:51:42 -0700 | [diff] [blame] | 547 | |
Austin Schuh | 1f3764e | 2015-04-18 23:00:09 -0700 | [diff] [blame] | 548 | LOG(INFO, "Backing away to let the stack ago\n"); |
| 549 | drive = SetDriveGoal(-0.1, kRaceBackupDrive); |
| 550 | WaitUntilDoneOrCanceled(::std::move(drive)); |
Austin Schuh | 9c414f4 | 2015-04-19 20:01:56 -0700 | [diff] [blame] | 551 | |
| 552 | WaitForFridge(); |
| 553 | if (ShouldExitAuto()) return; |
Austin Schuh | 4701741 | 2013-03-10 11:50:46 -0700 | [diff] [blame] | 554 | } |
| 555 | |
Austin Schuh | bb227f8 | 2015-09-06 15:27:52 -0700 | [diff] [blame^] | 556 | void GrabberForTime(double voltage, double wait_time) { |
| 557 | ::aos::time::Time now = ::aos::time::Time::Now(); |
| 558 | ::aos::time::Time end_time = now + time::Time::InSeconds(wait_time); |
| 559 | LOG(INFO, "Starting to grab at %f for %f seconds\n", voltage, wait_time); |
| 560 | while (true) { |
| 561 | autonomous::can_control.MakeWithBuilder().can_voltage(voltage).Send(); |
| 562 | // Poll the running bit and auto done bits. |
| 563 | if (ShouldExitAuto()) { |
| 564 | return; |
| 565 | } |
| 566 | if (::aos::time::Time::Now() > end_time) { |
| 567 | LOG(INFO, "Done grabbing\n"); |
| 568 | return; |
| 569 | } |
| 570 | ::aos::time::PhasedLoopXMS(5, 2500); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | void CanGrabberAuto() { |
| 575 | ResetDrivetrain(); |
| 576 | GrabberForTime(12.0, 0.18); |
| 577 | if (ShouldExitAuto()) return; |
| 578 | |
| 579 | //GrabberForTime(4.0, 0.10); |
| 580 | if (ShouldExitAuto()) return; |
| 581 | InitializeEncoders(); |
| 582 | ResetDrivetrain(); |
| 583 | if (ShouldExitAuto()) return; |
| 584 | control_loops::drivetrain_queue.goal.MakeWithBuilder() |
| 585 | .control_loop_driving(true) |
| 586 | //.highgear(false) |
| 587 | .steering(0.0) |
| 588 | .throttle(0.0) |
| 589 | .left_goal(left_initial_position + 1.5) |
| 590 | .left_velocity_goal(0) |
| 591 | .right_goal(right_initial_position + 1.5) |
| 592 | .right_velocity_goal(0) |
| 593 | .Send(); |
| 594 | GrabberForTime(12.0, 0.02); |
| 595 | |
| 596 | GrabberForTime(4.0, 14.0); |
| 597 | if (ShouldExitAuto()) return; |
| 598 | } |
| 599 | |
| 600 | void HandleAuto() { |
| 601 | ::aos::time::Time start_time = ::aos::time::Time::Now(); |
| 602 | LOG(INFO, "Starting auto mode at %f\n", start_time.ToSeconds()); |
| 603 | //TripleCanAuto(); |
| 604 | CanGrabberAuto(); |
| 605 | } |
| 606 | |
Austin Schuh | 4701741 | 2013-03-10 11:50:46 -0700 | [diff] [blame] | 607 | } // namespace autonomous |
| 608 | } // namespace frc971 |