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 | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 12 | #include "frc971/constants.h" |
| 13 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
Daniel Petti | 3b1e48f | 2015-02-15 15:57:53 -0800 | [diff] [blame] | 14 | #include "frc971/actors/drivetrain_actor.h" |
Ben Fredrickson | 0a6ad25 | 2015-03-15 18:51:42 -0700 | [diff] [blame] | 15 | #include "frc971/control_loops/claw/claw.q.h" |
Brian Silverman | 5bfda6c | 2015-03-21 23:40:13 -0700 | [diff] [blame] | 16 | #include "frc971/control_loops/fridge/fridge.q.h" |
Ben Fredrickson | 0a6ad25 | 2015-03-15 18:51:42 -0700 | [diff] [blame] | 17 | #include "frc971/actors/pickup_actor.h" |
| 18 | #include "frc971/actors/stack_actor.h" |
Austin Schuh | 4701741 | 2013-03-10 11:50:46 -0700 | [diff] [blame] | 19 | |
| 20 | using ::aos::time::Time; |
Ben Fredrickson | 0a6ad25 | 2015-03-15 18:51:42 -0700 | [diff] [blame] | 21 | using ::frc971::control_loops::claw_queue; |
Brian Silverman | 5bfda6c | 2015-03-21 23:40:13 -0700 | [diff] [blame] | 22 | using ::frc971::control_loops::fridge_queue; |
Brian Silverman | 93936f7 | 2015-03-19 23:38:30 -0700 | [diff] [blame^] | 23 | using ::frc971::control_loops::drivetrain_queue; |
Austin Schuh | 4701741 | 2013-03-10 11:50:46 -0700 | [diff] [blame] | 24 | |
| 25 | namespace frc971 { |
| 26 | namespace autonomous { |
| 27 | |
Ben Fredrickson | 0a6ad25 | 2015-03-15 18:51:42 -0700 | [diff] [blame] | 28 | constexpr double kClawAutoVelocity = 3.00; |
| 29 | constexpr double kClawAutoAcceleration = 6.0; |
| 30 | constexpr double kAngleEpsilon = 0.10; |
| 31 | double kClawTotePackAngle = 0.95; |
| 32 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 33 | namespace time = ::aos::time; |
| 34 | |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 35 | static double left_initial_position, right_initial_position; |
| 36 | |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 37 | bool ShouldExitAuto() { |
| 38 | ::frc971::autonomous::autonomous.FetchLatest(); |
| 39 | bool ans = !::frc971::autonomous::autonomous->run_auto; |
| 40 | if (ans) { |
| 41 | LOG(INFO, "Time to exit auto mode\n"); |
| 42 | } |
| 43 | return ans; |
| 44 | } |
| 45 | |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 46 | void StopDrivetrain() { |
| 47 | LOG(INFO, "Stopping the drivetrain\n"); |
Brian Silverman | ada5f2c | 2015-02-01 02:41:14 -0500 | [diff] [blame] | 48 | control_loops::drivetrain_queue.goal.MakeWithBuilder() |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 49 | .control_loop_driving(true) |
Brian Silverman | ce86bac | 2013-03-31 19:07:24 -0700 | [diff] [blame] | 50 | .left_goal(left_initial_position) |
| 51 | .left_velocity_goal(0) |
| 52 | .right_goal(right_initial_position) |
| 53 | .right_velocity_goal(0) |
| 54 | .quickturn(false) |
| 55 | .Send(); |
| 56 | } |
| 57 | |
| 58 | void ResetDrivetrain() { |
| 59 | LOG(INFO, "resetting the drivetrain\n"); |
Brian Silverman | ada5f2c | 2015-02-01 02:41:14 -0500 | [diff] [blame] | 60 | control_loops::drivetrain_queue.goal.MakeWithBuilder() |
Brian Silverman | ce86bac | 2013-03-31 19:07:24 -0700 | [diff] [blame] | 61 | .control_loop_driving(false) |
Brian Silverman | 93335ae | 2015-01-26 20:43:39 -0500 | [diff] [blame] | 62 | //.highgear(false) |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 63 | .steering(0.0) |
| 64 | .throttle(0.0) |
Brian Silverman | 38ea9bf | 2014-04-19 22:57:54 -0700 | [diff] [blame] | 65 | .left_goal(left_initial_position) |
| 66 | .left_velocity_goal(0) |
| 67 | .right_goal(right_initial_position) |
| 68 | .right_velocity_goal(0) |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 69 | .Send(); |
| 70 | } |
| 71 | |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 72 | void DriveSpin(double radians) { |
| 73 | LOG(INFO, "going to spin %f\n", radians); |
| 74 | |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 75 | // TODO(sensors): update this time thing maybe? |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 76 | ::aos::util::TrapezoidProfile profile(::aos::time::Time::InMS(10)); |
| 77 | ::Eigen::Matrix<double, 2, 1> driveTrainState; |
| 78 | const double goal_velocity = 0.0; |
| 79 | const double epsilon = 0.01; |
Brian Silverman | 13be668 | 2013-03-22 21:02:07 -0700 | [diff] [blame] | 80 | // in drivetrain "meters" |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 81 | const double kRobotWidth = 0.4544; |
| 82 | |
Brian Silverman | 7992d6e | 2013-03-24 19:20:54 -0700 | [diff] [blame] | 83 | profile.set_maximum_acceleration(1.5); |
| 84 | profile.set_maximum_velocity(0.8); |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 85 | |
| 86 | const double side_offset = kRobotWidth * radians / 2.0; |
| 87 | |
| 88 | while (true) { |
Brian Silverman | 547abb3 | 2015-02-16 00:37:48 -0500 | [diff] [blame] | 89 | ::aos::time::PhasedLoopXMS(5, 2500); |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 90 | driveTrainState = profile.Update(side_offset, goal_velocity); |
| 91 | |
| 92 | if (::std::abs(driveTrainState(0, 0) - side_offset) < epsilon) break; |
| 93 | if (ShouldExitAuto()) return; |
| 94 | |
| 95 | LOG(DEBUG, "Driving left to %f, right to %f\n", |
Brian Silverman | 7992d6e | 2013-03-24 19:20:54 -0700 | [diff] [blame] | 96 | left_initial_position - driveTrainState(0, 0), |
| 97 | right_initial_position + driveTrainState(0, 0)); |
Brian Silverman | ada5f2c | 2015-02-01 02:41:14 -0500 | [diff] [blame] | 98 | control_loops::drivetrain_queue.goal.MakeWithBuilder() |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 99 | .control_loop_driving(true) |
Brian Silverman | 93335ae | 2015-01-26 20:43:39 -0500 | [diff] [blame] | 100 | //.highgear(false) |
Brian Silverman | 7992d6e | 2013-03-24 19:20:54 -0700 | [diff] [blame] | 101 | .left_goal(left_initial_position - driveTrainState(0, 0)) |
| 102 | .right_goal(right_initial_position + driveTrainState(0, 0)) |
| 103 | .left_velocity_goal(-driveTrainState(1, 0)) |
| 104 | .right_velocity_goal(driveTrainState(1, 0)) |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 105 | .Send(); |
| 106 | } |
Brian Silverman | 7992d6e | 2013-03-24 19:20:54 -0700 | [diff] [blame] | 107 | left_initial_position -= side_offset; |
| 108 | right_initial_position += side_offset; |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 109 | LOG(INFO, "Done moving\n"); |
| 110 | } |
| 111 | |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 112 | void WaitUntilDoneOrCanceled(aos::common::actions::Action *action) { |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 113 | while (true) { |
| 114 | // Poll the running bit and auto done bits. |
Brian Silverman | 547abb3 | 2015-02-16 00:37:48 -0500 | [diff] [blame] | 115 | ::aos::time::PhasedLoopXMS(5, 2500); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 116 | if (!action->Running() || ShouldExitAuto()) { |
| 117 | return; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 122 | ::std::unique_ptr<::frc971::actors::DrivetrainAction> SetDriveGoal( |
Brian Silverman | 5bfda6c | 2015-03-21 23:40:13 -0700 | [diff] [blame] | 123 | double distance, bool slow_acceleration, double maximum_velocity = 0.7, |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 124 | double theta = 0) { |
Austin Schuh | a4faacc | 2014-03-09 00:50:50 -0800 | [diff] [blame] | 125 | LOG(INFO, "Driving to %f\n", distance); |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 126 | |
| 127 | ::frc971::actors::DrivetrainActionParams params; |
| 128 | params.left_initial_position = left_initial_position; |
| 129 | params.right_initial_position = right_initial_position; |
| 130 | params.y_offset = distance; |
| 131 | params.theta_offset = theta; |
| 132 | params.maximum_velocity = maximum_velocity; |
Brian Silverman | 5bfda6c | 2015-03-21 23:40:13 -0700 | [diff] [blame] | 133 | params.maximum_acceleration = slow_acceleration ? 0.3 : 0.2; |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 134 | auto drivetrain_action = actors::MakeDrivetrainAction(params); |
| 135 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 136 | drivetrain_action->Start(); |
Brian Silverman | ad9e000 | 2014-04-13 14:55:57 -0700 | [diff] [blame] | 137 | left_initial_position += |
| 138 | distance - theta * constants::GetValues().turn_width / 2.0; |
| 139 | right_initial_position += |
Daniel Petti | b0733be | 2014-11-14 22:44:03 -0800 | [diff] [blame] | 140 | distance + theta * constants::GetValues().turn_width / 2.0; |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 141 | return ::std::move(drivetrain_action); |
| 142 | } |
| 143 | |
| 144 | void InitializeEncoders() { |
Brian Silverman | ada5f2c | 2015-02-01 02:41:14 -0500 | [diff] [blame] | 145 | control_loops::drivetrain_queue.status.FetchLatest(); |
| 146 | while (!control_loops::drivetrain_queue.status.get()) { |
Brian Silverman | 2c1e034 | 2014-04-11 16:15:01 -0700 | [diff] [blame] | 147 | LOG(WARNING, |
| 148 | "No previous drivetrain position packet, trying to fetch again\n"); |
Brian Silverman | ada5f2c | 2015-02-01 02:41:14 -0500 | [diff] [blame] | 149 | control_loops::drivetrain_queue.status.FetchNextBlocking(); |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 150 | } |
| 151 | left_initial_position = |
Brian Silverman | ada5f2c | 2015-02-01 02:41:14 -0500 | [diff] [blame] | 152 | control_loops::drivetrain_queue.status->filtered_left_position; |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 153 | right_initial_position = |
Brian Silverman | ada5f2c | 2015-02-01 02:41:14 -0500 | [diff] [blame] | 154 | control_loops::drivetrain_queue.status->filtered_right_position; |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Ben Fredrickson | 0a6ad25 | 2015-03-15 18:51:42 -0700 | [diff] [blame] | 157 | void SetClawState(double angle, double intake_voltage, double rollers_closed) { |
| 158 | auto message = control_loops::claw_queue.goal.MakeMessage(); |
| 159 | message->angle = angle; |
| 160 | message->max_velocity = kClawAutoVelocity; |
| 161 | message->max_acceleration = kClawAutoAcceleration; |
| 162 | message->angular_velocity = 0.0; |
| 163 | message->intake = intake_voltage; |
| 164 | message->rollers_closed = rollers_closed; |
| 165 | |
| 166 | LOG_STRUCT(DEBUG, "Sending claw goal", *message); |
| 167 | message.Send(); |
| 168 | |
| 169 | while (true) { |
| 170 | control_loops::claw_queue.status.FetchAnother(); |
| 171 | const double current_angle = control_loops::claw_queue.status->angle; |
| 172 | LOG_STRUCT(DEBUG, "Got claw status", *control_loops::claw_queue.status); |
| 173 | |
| 174 | // I believe all we can care about here is the angle. Other values will |
| 175 | // either be set or not, but there is nothing we can do about it. If it |
| 176 | // never gets there we do not care, auto is over for us. |
| 177 | if (::std::abs(current_angle - angle) < kAngleEpsilon) { |
| 178 | break; |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 183 | void HandleAuto() { |
Austin Schuh | 577edf6 | 2014-04-13 10:33:05 -0700 | [diff] [blame] | 184 | ::aos::time::Time start_time = ::aos::time::Time::Now(); |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 185 | LOG(INFO, "Handling auto mode at %f\n", start_time.ToSeconds()); |
Ben Fredrickson | 0a6ad25 | 2015-03-15 18:51:42 -0700 | [diff] [blame] | 186 | ::std::unique_ptr<::frc971::actors::DrivetrainAction> drive; |
| 187 | ::std::unique_ptr<::frc971::actors::PickupAction> pickup; |
| 188 | ::std::unique_ptr<::frc971::actors::StackAction> stack; |
| 189 | // TODO(austin): Score! |
| 190 | //::std::unique_ptr<ScoreAction> score; |
Brian Silverman | 6f62154 | 2014-04-06 16:00:41 -0700 | [diff] [blame] | 191 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 192 | ResetDrivetrain(); |
| 193 | |
| 194 | if (ShouldExitAuto()) return; |
| 195 | InitializeEncoders(); |
Ben Fredrickson | 0a6ad25 | 2015-03-15 18:51:42 -0700 | [diff] [blame] | 196 | |
Brian Silverman | 93936f7 | 2015-03-19 23:38:30 -0700 | [diff] [blame^] | 197 | time::SleepFor(time::Time::InSeconds(0.55)); |
| 198 | |
| 199 | if (!drivetrain_queue.goal.MakeWithBuilder() |
| 200 | .steering(0) |
| 201 | .throttle(0.5) |
| 202 | .quickturn(false) |
| 203 | .control_loop_driving(false) |
| 204 | .Send()) { |
| 205 | LOG(WARNING, "sending drivetrain goal failed\n"); |
| 206 | } |
| 207 | time::SleepFor(time::Time::InSeconds(1.0)); |
| 208 | if (!drivetrain_queue.goal.MakeWithBuilder() |
| 209 | .steering(0) |
| 210 | .throttle(0) |
| 211 | .quickturn(false) |
| 212 | .control_loop_driving(false) |
| 213 | .Send()) { |
| 214 | LOG(WARNING, "sending drivetrain goal failed\n"); |
| 215 | } |
| 216 | |
| 217 | return; |
| 218 | |
Brian Silverman | 5bfda6c | 2015-03-21 23:40:13 -0700 | [diff] [blame] | 219 | { |
| 220 | auto new_fridge_goal = fridge_queue.goal.MakeMessage(); |
| 221 | new_fridge_goal->max_velocity = 0.0; |
| 222 | new_fridge_goal->max_acceleration = 0.0; |
| 223 | new_fridge_goal->profiling_type = 0; |
| 224 | new_fridge_goal->height = 0.3; |
| 225 | new_fridge_goal->velocity = 0.0; |
| 226 | new_fridge_goal->max_angular_velocity = 0.0; |
| 227 | new_fridge_goal->max_angular_acceleration = 0.0; |
| 228 | new_fridge_goal->angle = 0.0; |
| 229 | new_fridge_goal->angular_velocity = 0.0; |
| 230 | new_fridge_goal->grabbers.top_front = true; |
| 231 | new_fridge_goal->grabbers.top_back = true; |
| 232 | new_fridge_goal->grabbers.bottom_front = true; |
| 233 | new_fridge_goal->grabbers.bottom_back = true; |
| 234 | |
| 235 | if (!new_fridge_goal.Send()) { |
| 236 | LOG(ERROR, "Sending fridge goal failed.\n"); |
| 237 | return; |
| 238 | } |
Ben Fredrickson | 0a6ad25 | 2015-03-15 18:51:42 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Brian Silverman | 5bfda6c | 2015-03-21 23:40:13 -0700 | [diff] [blame] | 241 | // Pick up the tote. |
| 242 | SetClawState(0.0, 7.0, true); |
| 243 | if (ShouldExitAuto()) return; |
| 244 | time::SleepFor(time::Time::InSeconds(0.1)); |
| 245 | if (ShouldExitAuto()) return; |
| 246 | |
| 247 | // now pick it up |
| 248 | { |
| 249 | actors::PickupParams params; |
| 250 | // Lift to here initially. |
| 251 | params.pickup_angle = 0.9; |
| 252 | // Start sucking here |
| 253 | params.suck_angle = 0.8; |
| 254 | // Go back down to here to finish sucking. |
| 255 | params.suck_angle_finish = 0.4; |
| 256 | // Pack the box back in here. |
| 257 | params.pickup_finish_angle = kClawTotePackAngle; |
| 258 | params.intake_time = 0.8; |
| 259 | params.intake_voltage = 7.0; |
| 260 | pickup = actors::MakePickupAction(params); |
| 261 | pickup->Start(); |
| 262 | WaitUntilDoneOrCanceled(pickup.get()); |
| 263 | } |
| 264 | if (ShouldExitAuto()) return; |
| 265 | |
| 266 | drive = SetDriveGoal(1.0, false); |
| 267 | WaitUntilDoneOrCanceled(drive.get()); |
| 268 | |
| 269 | SetClawState(0.0, 0.0, true); |
| 270 | return; |
| 271 | |
Ben Fredrickson | 0a6ad25 | 2015-03-15 18:51:42 -0700 | [diff] [blame] | 272 | if (ShouldExitAuto()) return; |
| 273 | |
| 274 | if (false) { |
| 275 | // drive up to the next tote |
| 276 | drive = SetDriveGoal(1.0, false); |
| 277 | WaitUntilDoneOrCanceled(drive.get()); |
| 278 | if (ShouldExitAuto()) return; |
| 279 | |
| 280 | // suck in the tote |
| 281 | SetClawState(0.0, 7.0, true); |
| 282 | drive = SetDriveGoal(0.2, false); |
| 283 | WaitUntilDoneOrCanceled(drive.get()); |
| 284 | SetClawState(0.0, 0.0, true); |
| 285 | if (ShouldExitAuto()) return; |
| 286 | |
Ben Fredrickson | 0a6ad25 | 2015-03-15 18:51:42 -0700 | [diff] [blame] | 287 | |
| 288 | // we should have the tote, now stack it |
| 289 | { |
| 290 | actors::StackParams params; |
| 291 | params.claw_out_angle = 0.6; |
| 292 | params.bottom = 0.020; |
| 293 | params.over_box_before_place_height = 0.39; |
| 294 | stack = actors::MakeStackAction(params); |
| 295 | WaitUntilDoneOrCanceled(stack.get()); |
| 296 | } |
| 297 | if (ShouldExitAuto()) return; |
| 298 | |
| 299 | // turn 90 |
| 300 | DriveSpin(M_PI / 4.0); |
| 301 | if (ShouldExitAuto()) return; |
| 302 | |
| 303 | // place the new stack |
| 304 | // TODO(austin): Score! |
| 305 | // score = MakeScoreAction(score_params); |
| 306 | // WaitUntilDoneOrCanceled(score.get()); |
| 307 | } |
Austin Schuh | 4701741 | 2013-03-10 11:50:46 -0700 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | } // namespace autonomous |
| 311 | } // namespace frc971 |