blob: e6cc9a476bae89b8fd975d88e2cd1e3100867adc [file] [log] [blame]
Austin Schuh80ff2e12014-03-08 12:06:19 -08001#include <stdio.h>
2
3#include <memory>
Austin Schuh47017412013-03-10 11:50:46 -07004
Brian3afd6fc2014-04-02 20:41:49 -07005#include "aos/common/util/phased_loop.h"
Austin Schuh47017412013-03-10 11:50:46 -07006#include "aos/common/time.h"
Austin Schuh6be011a2013-03-19 10:07:02 +00007#include "aos/common/util/trapezoid_profile.h"
Brian Silverman598800f2013-05-09 17:08:42 -07008#include "aos/common/logging/logging.h"
Brian Silverman96d9cea2013-11-12 21:10:50 -08009#include "aos/common/network/team_number.h"
Brian Silverman598800f2013-05-09 17:08:42 -070010
11#include "frc971/autonomous/auto.q.h"
Austin Schuh6be011a2013-03-19 10:07:02 +000012#include "frc971/constants.h"
13#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Austin Schuh80ff2e12014-03-08 12:06:19 -080014#include "frc971/control_loops/shooter/shooter.q.h"
15#include "frc971/control_loops/claw/claw.q.h"
16#include "frc971/actions/action_client.h"
17#include "frc971/actions/shoot_action.h"
18#include "frc971/actions/drivetrain_action.h"
Austin Schuh47017412013-03-10 11:50:46 -070019
20using ::aos::time::Time;
21
22namespace frc971 {
23namespace autonomous {
24
Austin Schuh80ff2e12014-03-08 12:06:19 -080025namespace time = ::aos::time;
26
Brian Silverman3b89ed82013-03-22 18:59:16 -070027static double left_initial_position, right_initial_position;
28
Austin Schuh6be011a2013-03-19 10:07:02 +000029bool ShouldExitAuto() {
30 ::frc971::autonomous::autonomous.FetchLatest();
31 bool ans = !::frc971::autonomous::autonomous->run_auto;
32 if (ans) {
33 LOG(INFO, "Time to exit auto mode\n");
34 }
35 return ans;
36}
37
Austin Schuh6be011a2013-03-19 10:07:02 +000038void StopDrivetrain() {
39 LOG(INFO, "Stopping the drivetrain\n");
Austin Schuh47017412013-03-10 11:50:46 -070040 control_loops::drivetrain.goal.MakeWithBuilder()
Brian Silverman3b89ed82013-03-22 18:59:16 -070041 .control_loop_driving(true)
Brian Silvermance86bac2013-03-31 19:07:24 -070042 .left_goal(left_initial_position)
43 .left_velocity_goal(0)
44 .right_goal(right_initial_position)
45 .right_velocity_goal(0)
46 .quickturn(false)
47 .Send();
48}
49
50void ResetDrivetrain() {
51 LOG(INFO, "resetting the drivetrain\n");
52 control_loops::drivetrain.goal.MakeWithBuilder()
53 .control_loop_driving(false)
Austin Schuh6be011a2013-03-19 10:07:02 +000054 .highgear(false)
55 .steering(0.0)
56 .throttle(0.0)
Austin Schuh6be011a2013-03-19 10:07:02 +000057 .Send();
58}
59
Brian Silverman3b89ed82013-03-22 18:59:16 -070060void DriveSpin(double radians) {
61 LOG(INFO, "going to spin %f\n", radians);
62
63 ::aos::util::TrapezoidProfile profile(::aos::time::Time::InMS(10));
64 ::Eigen::Matrix<double, 2, 1> driveTrainState;
65 const double goal_velocity = 0.0;
66 const double epsilon = 0.01;
Brian Silverman13be6682013-03-22 21:02:07 -070067 // in drivetrain "meters"
Brian Silverman3b89ed82013-03-22 18:59:16 -070068 const double kRobotWidth = 0.4544;
69
Brian Silverman7992d6e2013-03-24 19:20:54 -070070 profile.set_maximum_acceleration(1.5);
71 profile.set_maximum_velocity(0.8);
Brian Silverman3b89ed82013-03-22 18:59:16 -070072
73 const double side_offset = kRobotWidth * radians / 2.0;
74
75 while (true) {
Brian Silverman7f09f972013-03-22 23:11:39 -070076 ::aos::time::PhasedLoop10MS(5000); // wait until next 10ms tick
Brian Silverman3b89ed82013-03-22 18:59:16 -070077 driveTrainState = profile.Update(side_offset, goal_velocity);
78
79 if (::std::abs(driveTrainState(0, 0) - side_offset) < epsilon) break;
80 if (ShouldExitAuto()) return;
81
82 LOG(DEBUG, "Driving left to %f, right to %f\n",
Brian Silverman7992d6e2013-03-24 19:20:54 -070083 left_initial_position - driveTrainState(0, 0),
84 right_initial_position + driveTrainState(0, 0));
Brian Silverman3b89ed82013-03-22 18:59:16 -070085 control_loops::drivetrain.goal.MakeWithBuilder()
86 .control_loop_driving(true)
87 .highgear(false)
Brian Silverman7992d6e2013-03-24 19:20:54 -070088 .left_goal(left_initial_position - driveTrainState(0, 0))
89 .right_goal(right_initial_position + driveTrainState(0, 0))
90 .left_velocity_goal(-driveTrainState(1, 0))
91 .right_velocity_goal(driveTrainState(1, 0))
Brian Silverman3b89ed82013-03-22 18:59:16 -070092 .Send();
93 }
Brian Silverman7992d6e2013-03-24 19:20:54 -070094 left_initial_position -= side_offset;
95 right_initial_position += side_offset;
Brian Silverman3b89ed82013-03-22 18:59:16 -070096 LOG(INFO, "Done moving\n");
97}
98
Austin Schuh80ff2e12014-03-08 12:06:19 -080099void PositionClawVertically(double intake_power = 0.0, double centering_power = 0.0) {
100 if (!control_loops::claw_queue_group.goal.MakeWithBuilder()
101 .bottom_angle(0.0)
102 .separation_angle(0.0)
103 .intake(intake_power)
104 .centering(centering_power)
105 .Send()) {
106 LOG(WARNING, "sending claw goal failed\n");
107 }
108}
Brian Silvermance86bac2013-03-31 19:07:24 -0700109
Austin Schuh80ff2e12014-03-08 12:06:19 -0800110void PositionClawBackIntake() {
111 if (!control_loops::claw_queue_group.goal.MakeWithBuilder()
112 .bottom_angle(-2.273474)
113 .separation_angle(0.0)
114 .intake(12.0)
115 .centering(12.0)
116 .Send()) {
117 LOG(WARNING, "sending claw goal failed\n");
118 }
119}
Brian Silvermance86bac2013-03-31 19:07:24 -0700120
Austin Schuh80ff2e12014-03-08 12:06:19 -0800121void PositionClawForShot() {
122 // Turn the claw on, keep it straight up until the ball has been grabbed.
123 if (!control_loops::claw_queue_group.goal.MakeWithBuilder()
Brian Silverman31b5b822014-03-14 18:50:39 -0700124 .bottom_angle(0.86)
Austin Schuha4faacc2014-03-09 00:50:50 -0800125 .separation_angle(0.10)
126 .intake(4.0)
Austin Schuh80ff2e12014-03-08 12:06:19 -0800127 .centering(1.0)
128 .Send()) {
129 LOG(WARNING, "sending claw goal failed\n");
130 }
131}
132
133void SetShotPower(double power) {
Austin Schuha4faacc2014-03-09 00:50:50 -0800134 LOG(INFO, "Setting shot power to %f\n", power);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800135 if (!control_loops::shooter_queue_group.goal.MakeWithBuilder()
136 .shot_power(power)
137 .shot_requested(false)
138 .unload_requested(false)
139 .load_requested(false)
140 .Send()) {
141 LOG(WARNING, "sending shooter goal failed\n");
142 }
143}
144
145void WaitUntilDoneOrCanceled(Action *action) {
146 while (true) {
147 // Poll the running bit and auto done bits.
148 ::aos::time::PhasedLoop10MS(5000);
149 if (!action->Running() || ShouldExitAuto()) {
150 return;
151 }
152 }
153}
154
155void Shoot() {
156 // Shoot.
157 auto shoot_action = actions::MakeShootAction();
158 shoot_action->Start();
159 WaitUntilDoneOrCanceled(shoot_action.get());
160}
161
162::std::unique_ptr<TypedAction< ::frc971::actions::DrivetrainActionQueueGroup>>
Austin Schuha4faacc2014-03-09 00:50:50 -0800163SetDriveGoal(double distance, double maximum_velocity = 1.7) {
164 LOG(INFO, "Driving to %f\n", distance);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800165 auto drivetrain_action = actions::MakeDrivetrainAction();
166 drivetrain_action->GetGoal()->left_initial_position = left_initial_position;
167 drivetrain_action->GetGoal()->right_initial_position = right_initial_position;
168 drivetrain_action->GetGoal()->y_offset = distance;
169 drivetrain_action->GetGoal()->maximum_velocity = maximum_velocity;
170 drivetrain_action->Start();
Austin Schuha4faacc2014-03-09 00:50:50 -0800171 // Uncomment to make relative again.
Austin Schuh80ff2e12014-03-08 12:06:19 -0800172 left_initial_position += distance;
173 right_initial_position += distance;
174 return ::std::move(drivetrain_action);
175}
176
177void InitializeEncoders() {
Austin Schuh577edf62014-04-13 10:33:05 -0700178 control_loops::drivetrain.status.FetchLatest();
179 while (!control_loops::drivetrain.status.get()) {
Brian Silverman2c1e0342014-04-11 16:15:01 -0700180 LOG(WARNING,
181 "No previous drivetrain position packet, trying to fetch again\n");
Austin Schuh577edf62014-04-13 10:33:05 -0700182 control_loops::drivetrain.status.FetchNextBlocking();
Brian Silverman3b89ed82013-03-22 18:59:16 -0700183 }
184 left_initial_position =
Austin Schuh577edf62014-04-13 10:33:05 -0700185 control_loops::drivetrain.status->filtered_left_position;
Brian Silverman3b89ed82013-03-22 18:59:16 -0700186 right_initial_position =
Austin Schuh577edf62014-04-13 10:33:05 -0700187 control_loops::drivetrain.status->filtered_right_position;
Brian Silverman3b89ed82013-03-22 18:59:16 -0700188
Austin Schuh80ff2e12014-03-08 12:06:19 -0800189}
190
Austin Schuha4faacc2014-03-09 00:50:50 -0800191void WaitUntilClawDone() {
192 while (true) {
193 // Poll the running bit and auto done bits.
194 ::aos::time::PhasedLoop10MS(5000);
195 control_loops::claw_queue_group.status.FetchLatest();
196 control_loops::claw_queue_group.goal.FetchLatest();
197 if (ShouldExitAuto()) {
198 return;
199 }
200 if (control_loops::claw_queue_group.status.get() == nullptr ||
201 control_loops::claw_queue_group.goal.get() == nullptr) {
202 continue;
203 }
204 bool ans =
205 control_loops::claw_queue_group.status->zeroed &&
206 (::std::abs(control_loops::claw_queue_group.status->bottom_velocity) <
207 1.0) &&
208 (::std::abs(control_loops::claw_queue_group.status->bottom -
209 control_loops::claw_queue_group.goal->bottom_angle) <
210 0.10) &&
211 (::std::abs(control_loops::claw_queue_group.status->separation -
212 control_loops::claw_queue_group.goal->separation_angle) <
213 0.4);
214 if (ans) {
215 return;
216 }
217 }
218}
219
Austin Schuh80ff2e12014-03-08 12:06:19 -0800220void HandleAuto() {
Austin Schuha4faacc2014-03-09 00:50:50 -0800221 // The front of the robot is 1.854 meters from the wall
222 const double kShootDistance = 3.15;
223 const double kPickupDistance = 0.5;
Austin Schuh577edf62014-04-13 10:33:05 -0700224 ::aos::time::Time start_time = ::aos::time::Time::Now();
Austin Schuh80ff2e12014-03-08 12:06:19 -0800225 LOG(INFO, "Handling auto mode\n");
226 ResetDrivetrain();
227
228 if (ShouldExitAuto()) return;
229 InitializeEncoders();
230
231 // Turn the claw on, keep it straight up until the ball has been grabbed.
Austin Schuh577edf62014-04-13 10:33:05 -0700232 LOG(INFO, "Claw going up at %f\n",
233 (::aos::time::Time::Now() - start_time).ToSeconds());
Austin Schuh80ff2e12014-03-08 12:06:19 -0800234 PositionClawVertically(12.0, 4.0);
Austin Schuha4faacc2014-03-09 00:50:50 -0800235 SetShotPower(115.0);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800236
237 // Wait for the ball to enter the claw.
Austin Schuha4faacc2014-03-09 00:50:50 -0800238 time::SleepFor(time::Time::InSeconds(0.25));
Austin Schuh80ff2e12014-03-08 12:06:19 -0800239 if (ShouldExitAuto()) return;
Austin Schuh577edf62014-04-13 10:33:05 -0700240 LOG(INFO, "Readying claw for shot at %f\n",
241 (::aos::time::Time::Now() - start_time).ToSeconds());
Austin Schuh80ff2e12014-03-08 12:06:19 -0800242
243 {
244 if (ShouldExitAuto()) return;
245 // Drive to the goal.
Austin Schuha4faacc2014-03-09 00:50:50 -0800246 auto drivetrain_action = SetDriveGoal(-kShootDistance);
247 time::SleepFor(time::Time::InSeconds(0.75));
248 PositionClawForShot();
249 LOG(INFO, "Waiting until drivetrain is finished\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800250 WaitUntilDoneOrCanceled(drivetrain_action.get());
251 if (ShouldExitAuto()) return;
252 }
Austin Schuh577edf62014-04-13 10:33:05 -0700253 LOG(INFO, "Shooting at %f\n",
254 (::aos::time::Time::Now() - start_time).ToSeconds());
Austin Schuh80ff2e12014-03-08 12:06:19 -0800255
256 // Shoot.
257 Shoot();
Austin Schuh577edf62014-04-13 10:33:05 -0700258 time::SleepFor(time::Time::InSeconds(0.05));
Austin Schuh80ff2e12014-03-08 12:06:19 -0800259
260 {
261 if (ShouldExitAuto()) return;
262 // Intake the new ball.
Austin Schuh577edf62014-04-13 10:33:05 -0700263 LOG(INFO, "Claw ready for intake at %f\n",
264 (::aos::time::Time::Now() - start_time).ToSeconds());
Austin Schuh80ff2e12014-03-08 12:06:19 -0800265 PositionClawBackIntake();
Austin Schuha4faacc2014-03-09 00:50:50 -0800266 auto drivetrain_action = SetDriveGoal(kShootDistance + kPickupDistance);
267 LOG(INFO, "Waiting until drivetrain is finished\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800268 WaitUntilDoneOrCanceled(drivetrain_action.get());
269 if (ShouldExitAuto()) return;
Austin Schuh577edf62014-04-13 10:33:05 -0700270 LOG(INFO, "Wait for the claw at %f\n",
271 (::aos::time::Time::Now() - start_time).ToSeconds());
Austin Schuha4faacc2014-03-09 00:50:50 -0800272 WaitUntilClawDone();
273 if (ShouldExitAuto()) return;
Austin Schuh80ff2e12014-03-08 12:06:19 -0800274 }
275
276 // Drive back.
277 {
Austin Schuh577edf62014-04-13 10:33:05 -0700278 LOG(INFO, "Driving back at %f\n",
279 (::aos::time::Time::Now() - start_time).ToSeconds());
Austin Schuha4faacc2014-03-09 00:50:50 -0800280 auto drivetrain_action = SetDriveGoal(-(kShootDistance + kPickupDistance));
Austin Schuh577edf62014-04-13 10:33:05 -0700281 time::SleepFor(time::Time::InSeconds(0.3));
Austin Schuh80ff2e12014-03-08 12:06:19 -0800282 if (ShouldExitAuto()) return;
283 PositionClawForShot();
Austin Schuha4faacc2014-03-09 00:50:50 -0800284 LOG(INFO, "Waiting until drivetrain is finished\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800285 WaitUntilDoneOrCanceled(drivetrain_action.get());
Austin Schuha4faacc2014-03-09 00:50:50 -0800286 WaitUntilClawDone();
Austin Schuh80ff2e12014-03-08 12:06:19 -0800287 if (ShouldExitAuto()) return;
288 }
289
Austin Schuh577edf62014-04-13 10:33:05 -0700290 LOG(INFO, "Shooting at %f\n",
291 (::aos::time::Time::Now() - start_time).ToSeconds());
Austin Schuh80ff2e12014-03-08 12:06:19 -0800292 // Shoot
293 Shoot();
294 if (ShouldExitAuto()) return;
295
296 // Get ready to zero when we come back up.
Austin Schuh577edf62014-04-13 10:33:05 -0700297 time::SleepFor(time::Time::InSeconds(0.05));
Austin Schuh80ff2e12014-03-08 12:06:19 -0800298 PositionClawVertically(0.0, 0.0);
Austin Schuh47017412013-03-10 11:50:46 -0700299}
300
301} // namespace autonomous
302} // namespace frc971