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 | |
Austin Schuh | 4701741 | 2013-03-10 11:50:46 -0700 | [diff] [blame] | 5 | #include "aos/common/control_loop/Timing.h" |
| 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 | 96d9cea | 2013-11-12 21:10:50 -0800 | [diff] [blame] | 9 | #include "aos/common/network/team_number.h" |
Brian Silverman | 6f62154 | 2014-04-06 16:00:41 -0700 | [diff] [blame^] | 10 | #include "aos/common/logging/queue_logging.h" |
Brian Silverman | 598800f | 2013-05-09 17:08:42 -0700 | [diff] [blame] | 11 | |
| 12 | #include "frc971/autonomous/auto.q.h" |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 13 | #include "frc971/constants.h" |
| 14 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 15 | #include "frc971/control_loops/shooter/shooter.q.h" |
| 16 | #include "frc971/control_loops/claw/claw.q.h" |
| 17 | #include "frc971/actions/action_client.h" |
| 18 | #include "frc971/actions/shoot_action.h" |
| 19 | #include "frc971/actions/drivetrain_action.h" |
Brian Silverman | 6f62154 | 2014-04-06 16:00:41 -0700 | [diff] [blame^] | 20 | #include "frc971/queues/hot_goal.q.h" |
Austin Schuh | 4701741 | 2013-03-10 11:50:46 -0700 | [diff] [blame] | 21 | |
| 22 | using ::aos::time::Time; |
| 23 | |
| 24 | namespace frc971 { |
| 25 | namespace autonomous { |
| 26 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 27 | namespace time = ::aos::time; |
| 28 | |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 29 | static double left_initial_position, right_initial_position; |
| 30 | |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 31 | bool ShouldExitAuto() { |
| 32 | ::frc971::autonomous::autonomous.FetchLatest(); |
| 33 | bool ans = !::frc971::autonomous::autonomous->run_auto; |
| 34 | if (ans) { |
| 35 | LOG(INFO, "Time to exit auto mode\n"); |
| 36 | } |
| 37 | return ans; |
| 38 | } |
| 39 | |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 40 | void StopDrivetrain() { |
| 41 | LOG(INFO, "Stopping the drivetrain\n"); |
Austin Schuh | 4701741 | 2013-03-10 11:50:46 -0700 | [diff] [blame] | 42 | control_loops::drivetrain.goal.MakeWithBuilder() |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 43 | .control_loop_driving(true) |
Brian Silverman | ce86bac | 2013-03-31 19:07:24 -0700 | [diff] [blame] | 44 | .left_goal(left_initial_position) |
| 45 | .left_velocity_goal(0) |
| 46 | .right_goal(right_initial_position) |
| 47 | .right_velocity_goal(0) |
| 48 | .quickturn(false) |
| 49 | .Send(); |
| 50 | } |
| 51 | |
| 52 | void ResetDrivetrain() { |
| 53 | LOG(INFO, "resetting the drivetrain\n"); |
| 54 | control_loops::drivetrain.goal.MakeWithBuilder() |
| 55 | .control_loop_driving(false) |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 56 | .highgear(false) |
| 57 | .steering(0.0) |
| 58 | .throttle(0.0) |
Austin Schuh | 6be011a | 2013-03-19 10:07:02 +0000 | [diff] [blame] | 59 | .Send(); |
| 60 | } |
| 61 | |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 62 | void DriveSpin(double radians) { |
| 63 | LOG(INFO, "going to spin %f\n", radians); |
| 64 | |
| 65 | ::aos::util::TrapezoidProfile profile(::aos::time::Time::InMS(10)); |
| 66 | ::Eigen::Matrix<double, 2, 1> driveTrainState; |
| 67 | const double goal_velocity = 0.0; |
| 68 | const double epsilon = 0.01; |
Brian Silverman | 13be668 | 2013-03-22 21:02:07 -0700 | [diff] [blame] | 69 | // in drivetrain "meters" |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 70 | const double kRobotWidth = 0.4544; |
| 71 | |
Brian Silverman | 7992d6e | 2013-03-24 19:20:54 -0700 | [diff] [blame] | 72 | profile.set_maximum_acceleration(1.5); |
| 73 | profile.set_maximum_velocity(0.8); |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 74 | |
| 75 | const double side_offset = kRobotWidth * radians / 2.0; |
| 76 | |
| 77 | while (true) { |
Brian Silverman | 7f09f97 | 2013-03-22 23:11:39 -0700 | [diff] [blame] | 78 | ::aos::time::PhasedLoop10MS(5000); // wait until next 10ms tick |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 79 | driveTrainState = profile.Update(side_offset, goal_velocity); |
| 80 | |
| 81 | if (::std::abs(driveTrainState(0, 0) - side_offset) < epsilon) break; |
| 82 | if (ShouldExitAuto()) return; |
| 83 | |
| 84 | LOG(DEBUG, "Driving left to %f, right to %f\n", |
Brian Silverman | 7992d6e | 2013-03-24 19:20:54 -0700 | [diff] [blame] | 85 | left_initial_position - driveTrainState(0, 0), |
| 86 | right_initial_position + driveTrainState(0, 0)); |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 87 | control_loops::drivetrain.goal.MakeWithBuilder() |
| 88 | .control_loop_driving(true) |
| 89 | .highgear(false) |
Brian Silverman | 7992d6e | 2013-03-24 19:20:54 -0700 | [diff] [blame] | 90 | .left_goal(left_initial_position - driveTrainState(0, 0)) |
| 91 | .right_goal(right_initial_position + driveTrainState(0, 0)) |
| 92 | .left_velocity_goal(-driveTrainState(1, 0)) |
| 93 | .right_velocity_goal(driveTrainState(1, 0)) |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 94 | .Send(); |
| 95 | } |
Brian Silverman | 7992d6e | 2013-03-24 19:20:54 -0700 | [diff] [blame] | 96 | left_initial_position -= side_offset; |
| 97 | right_initial_position += side_offset; |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 98 | LOG(INFO, "Done moving\n"); |
| 99 | } |
| 100 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 101 | void PositionClawVertically(double intake_power = 0.0, double centering_power = 0.0) { |
| 102 | if (!control_loops::claw_queue_group.goal.MakeWithBuilder() |
| 103 | .bottom_angle(0.0) |
| 104 | .separation_angle(0.0) |
| 105 | .intake(intake_power) |
| 106 | .centering(centering_power) |
| 107 | .Send()) { |
| 108 | LOG(WARNING, "sending claw goal failed\n"); |
| 109 | } |
| 110 | } |
Brian Silverman | ce86bac | 2013-03-31 19:07:24 -0700 | [diff] [blame] | 111 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 112 | void PositionClawBackIntake() { |
| 113 | if (!control_loops::claw_queue_group.goal.MakeWithBuilder() |
| 114 | .bottom_angle(-2.273474) |
| 115 | .separation_angle(0.0) |
| 116 | .intake(12.0) |
| 117 | .centering(12.0) |
| 118 | .Send()) { |
| 119 | LOG(WARNING, "sending claw goal failed\n"); |
| 120 | } |
| 121 | } |
Brian Silverman | ce86bac | 2013-03-31 19:07:24 -0700 | [diff] [blame] | 122 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 123 | void PositionClawForShot() { |
| 124 | // Turn the claw on, keep it straight up until the ball has been grabbed. |
| 125 | if (!control_loops::claw_queue_group.goal.MakeWithBuilder() |
Brian Silverman | 31b5b82 | 2014-03-14 18:50:39 -0700 | [diff] [blame] | 126 | .bottom_angle(0.86) |
Austin Schuh | a4faacc | 2014-03-09 00:50:50 -0800 | [diff] [blame] | 127 | .separation_angle(0.10) |
| 128 | .intake(4.0) |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 129 | .centering(1.0) |
| 130 | .Send()) { |
| 131 | LOG(WARNING, "sending claw goal failed\n"); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | void SetShotPower(double power) { |
Austin Schuh | a4faacc | 2014-03-09 00:50:50 -0800 | [diff] [blame] | 136 | LOG(INFO, "Setting shot power to %f\n", power); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 137 | if (!control_loops::shooter_queue_group.goal.MakeWithBuilder() |
| 138 | .shot_power(power) |
| 139 | .shot_requested(false) |
| 140 | .unload_requested(false) |
| 141 | .load_requested(false) |
| 142 | .Send()) { |
| 143 | LOG(WARNING, "sending shooter goal failed\n"); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | void WaitUntilDoneOrCanceled(Action *action) { |
| 148 | while (true) { |
| 149 | // Poll the running bit and auto done bits. |
| 150 | ::aos::time::PhasedLoop10MS(5000); |
| 151 | if (!action->Running() || ShouldExitAuto()) { |
| 152 | return; |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | void Shoot() { |
| 158 | // Shoot. |
| 159 | auto shoot_action = actions::MakeShootAction(); |
| 160 | shoot_action->Start(); |
| 161 | WaitUntilDoneOrCanceled(shoot_action.get()); |
| 162 | } |
| 163 | |
| 164 | ::std::unique_ptr<TypedAction< ::frc971::actions::DrivetrainActionQueueGroup>> |
Austin Schuh | a4faacc | 2014-03-09 00:50:50 -0800 | [diff] [blame] | 165 | SetDriveGoal(double distance, double maximum_velocity = 1.7) { |
| 166 | LOG(INFO, "Driving to %f\n", distance); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 167 | auto drivetrain_action = actions::MakeDrivetrainAction(); |
| 168 | drivetrain_action->GetGoal()->left_initial_position = left_initial_position; |
| 169 | drivetrain_action->GetGoal()->right_initial_position = right_initial_position; |
| 170 | drivetrain_action->GetGoal()->y_offset = distance; |
| 171 | drivetrain_action->GetGoal()->maximum_velocity = maximum_velocity; |
| 172 | drivetrain_action->Start(); |
Austin Schuh | a4faacc | 2014-03-09 00:50:50 -0800 | [diff] [blame] | 173 | // Uncomment to make relative again. |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 174 | left_initial_position += distance; |
| 175 | right_initial_position += distance; |
| 176 | return ::std::move(drivetrain_action); |
| 177 | } |
| 178 | |
| 179 | void InitializeEncoders() { |
Brian Silverman | 3b89ed8 | 2013-03-22 18:59:16 -0700 | [diff] [blame] | 180 | control_loops::drivetrain.position.FetchLatest(); |
| 181 | while (!control_loops::drivetrain.position.get()) { |
| 182 | LOG(WARNING, "No previous drivetrain position packet, trying to fetch again\n"); |
| 183 | control_loops::drivetrain.position.FetchNextBlocking(); |
| 184 | } |
| 185 | left_initial_position = |
| 186 | control_loops::drivetrain.position->left_encoder; |
| 187 | right_initial_position = |
| 188 | control_loops::drivetrain.position->right_encoder; |
| 189 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 190 | } |
| 191 | |
Austin Schuh | a4faacc | 2014-03-09 00:50:50 -0800 | [diff] [blame] | 192 | void WaitUntilClawDone() { |
| 193 | while (true) { |
| 194 | // Poll the running bit and auto done bits. |
| 195 | ::aos::time::PhasedLoop10MS(5000); |
| 196 | control_loops::claw_queue_group.status.FetchLatest(); |
| 197 | control_loops::claw_queue_group.goal.FetchLatest(); |
| 198 | if (ShouldExitAuto()) { |
| 199 | return; |
| 200 | } |
| 201 | if (control_loops::claw_queue_group.status.get() == nullptr || |
| 202 | control_loops::claw_queue_group.goal.get() == nullptr) { |
| 203 | continue; |
| 204 | } |
| 205 | bool ans = |
| 206 | control_loops::claw_queue_group.status->zeroed && |
| 207 | (::std::abs(control_loops::claw_queue_group.status->bottom_velocity) < |
| 208 | 1.0) && |
| 209 | (::std::abs(control_loops::claw_queue_group.status->bottom - |
| 210 | control_loops::claw_queue_group.goal->bottom_angle) < |
| 211 | 0.10) && |
| 212 | (::std::abs(control_loops::claw_queue_group.status->separation - |
| 213 | control_loops::claw_queue_group.goal->separation_angle) < |
| 214 | 0.4); |
| 215 | if (ans) { |
| 216 | return; |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 221 | void HandleAuto() { |
Brian Silverman | 6f62154 | 2014-04-06 16:00:41 -0700 | [diff] [blame^] | 222 | const ::aos::time::Time start_time = ::aos::time::Time::Now(); |
| 223 | ::frc971::HotGoal start_counts; |
| 224 | hot_goal.FetchLatest(); |
| 225 | bool hot_goal_wait = true; |
| 226 | if (!hot_goal.get()) { |
| 227 | LOG(WARNING, "no hot goal message. not waiting\n"); |
| 228 | hot_goal_wait = false; |
| 229 | } else { |
| 230 | memcpy(&start_counts, hot_goal.get(), sizeof(start_counts)); |
| 231 | LOG_STRUCT(INFO, "counts at start", start_counts); |
| 232 | } |
| 233 | |
Austin Schuh | a4faacc | 2014-03-09 00:50:50 -0800 | [diff] [blame] | 234 | // The front of the robot is 1.854 meters from the wall |
| 235 | const double kShootDistance = 3.15; |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 236 | LOG(INFO, "Handling auto mode\n"); |
| 237 | ResetDrivetrain(); |
| 238 | |
| 239 | if (ShouldExitAuto()) return; |
| 240 | InitializeEncoders(); |
| 241 | |
| 242 | // Turn the claw on, keep it straight up until the ball has been grabbed. |
Austin Schuh | a4faacc | 2014-03-09 00:50:50 -0800 | [diff] [blame] | 243 | LOG(INFO, "Claw going up\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 244 | PositionClawVertically(12.0, 4.0); |
Austin Schuh | a4faacc | 2014-03-09 00:50:50 -0800 | [diff] [blame] | 245 | SetShotPower(115.0); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 246 | |
| 247 | // Wait for the ball to enter the claw. |
Austin Schuh | a4faacc | 2014-03-09 00:50:50 -0800 | [diff] [blame] | 248 | time::SleepFor(time::Time::InSeconds(0.25)); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 249 | if (ShouldExitAuto()) return; |
Austin Schuh | a4faacc | 2014-03-09 00:50:50 -0800 | [diff] [blame] | 250 | LOG(INFO, "Readying claw for shot\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 251 | |
| 252 | { |
| 253 | if (ShouldExitAuto()) return; |
| 254 | // Drive to the goal. |
Austin Schuh | a4faacc | 2014-03-09 00:50:50 -0800 | [diff] [blame] | 255 | auto drivetrain_action = SetDriveGoal(-kShootDistance); |
| 256 | time::SleepFor(time::Time::InSeconds(0.75)); |
| 257 | PositionClawForShot(); |
| 258 | LOG(INFO, "Waiting until drivetrain is finished\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 259 | WaitUntilDoneOrCanceled(drivetrain_action.get()); |
| 260 | if (ShouldExitAuto()) return; |
| 261 | } |
Brian Silverman | 6f62154 | 2014-04-06 16:00:41 -0700 | [diff] [blame^] | 262 | LOG(INFO, "Shooting (after we have a hot goal)\n"); |
| 263 | |
| 264 | time::SleepFor(time::Time::InSeconds(0.1)); |
| 265 | |
| 266 | while (hot_goal_wait && (::aos::time::Time::Now() - start_time) < |
| 267 | ::aos::time::Time::InSeconds(7)) { |
| 268 | hot_goal.FetchNextBlocking(); |
| 269 | if ((hot_goal->right_count - start_counts.right_count) > 10) { |
| 270 | LOG(INFO, "hot goal time!!!\n"); |
| 271 | hot_goal_wait = false; |
| 272 | } |
| 273 | } |
| 274 | LOG(INFO, "done waiting for hot goal\n"); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 275 | |
| 276 | // Shoot. |
| 277 | Shoot(); |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 278 | |
Austin Schuh | 80ff2e1 | 2014-03-08 12:06:19 -0800 | [diff] [blame] | 279 | if (ShouldExitAuto()) return; |
| 280 | |
| 281 | // Get ready to zero when we come back up. |
| 282 | PositionClawVertically(0.0, 0.0); |
Austin Schuh | 4701741 | 2013-03-10 11:50:46 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | } // namespace autonomous |
| 286 | } // namespace frc971 |