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