blob: 85bd355329b9319c7d428aefb9126b27898247bc [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
Austin Schuh47017412013-03-10 11:50:46 -07005#include "aos/common/control_loop/Timing.h"
6#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 Silverman6f621542014-04-06 16:00:41 -070010#include "aos/common/logging/queue_logging.h"
Brian Silverman598800f2013-05-09 17:08:42 -070011
12#include "frc971/autonomous/auto.q.h"
Austin Schuh6be011a2013-03-19 10:07:02 +000013#include "frc971/constants.h"
14#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Austin Schuh80ff2e12014-03-08 12:06:19 -080015#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 Silverman6f621542014-04-06 16:00:41 -070020#include "frc971/queues/hot_goal.q.h"
Austin Schuh47017412013-03-10 11:50:46 -070021
22using ::aos::time::Time;
23
24namespace frc971 {
25namespace autonomous {
26
Austin Schuh80ff2e12014-03-08 12:06:19 -080027namespace time = ::aos::time;
28
Brian Silverman3b89ed82013-03-22 18:59:16 -070029static double left_initial_position, right_initial_position;
30
Austin Schuh6be011a2013-03-19 10:07:02 +000031bool 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 Schuh6be011a2013-03-19 10:07:02 +000040void StopDrivetrain() {
41 LOG(INFO, "Stopping the drivetrain\n");
Austin Schuh47017412013-03-10 11:50:46 -070042 control_loops::drivetrain.goal.MakeWithBuilder()
Brian Silverman3b89ed82013-03-22 18:59:16 -070043 .control_loop_driving(true)
Brian Silvermance86bac2013-03-31 19:07:24 -070044 .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
52void ResetDrivetrain() {
53 LOG(INFO, "resetting the drivetrain\n");
54 control_loops::drivetrain.goal.MakeWithBuilder()
55 .control_loop_driving(false)
Austin Schuh6be011a2013-03-19 10:07:02 +000056 .highgear(false)
57 .steering(0.0)
58 .throttle(0.0)
Austin Schuh6be011a2013-03-19 10:07:02 +000059 .Send();
60}
61
Brian Silverman3b89ed82013-03-22 18:59:16 -070062void 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 Silverman13be6682013-03-22 21:02:07 -070069 // in drivetrain "meters"
Brian Silverman3b89ed82013-03-22 18:59:16 -070070 const double kRobotWidth = 0.4544;
71
Brian Silverman7992d6e2013-03-24 19:20:54 -070072 profile.set_maximum_acceleration(1.5);
73 profile.set_maximum_velocity(0.8);
Brian Silverman3b89ed82013-03-22 18:59:16 -070074
75 const double side_offset = kRobotWidth * radians / 2.0;
76
77 while (true) {
Brian Silverman7f09f972013-03-22 23:11:39 -070078 ::aos::time::PhasedLoop10MS(5000); // wait until next 10ms tick
Brian Silverman3b89ed82013-03-22 18:59:16 -070079 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 Silverman7992d6e2013-03-24 19:20:54 -070085 left_initial_position - driveTrainState(0, 0),
86 right_initial_position + driveTrainState(0, 0));
Brian Silverman3b89ed82013-03-22 18:59:16 -070087 control_loops::drivetrain.goal.MakeWithBuilder()
88 .control_loop_driving(true)
89 .highgear(false)
Brian Silverman7992d6e2013-03-24 19:20:54 -070090 .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 Silverman3b89ed82013-03-22 18:59:16 -070094 .Send();
95 }
Brian Silverman7992d6e2013-03-24 19:20:54 -070096 left_initial_position -= side_offset;
97 right_initial_position += side_offset;
Brian Silverman3b89ed82013-03-22 18:59:16 -070098 LOG(INFO, "Done moving\n");
99}
100
Austin Schuh80ff2e12014-03-08 12:06:19 -0800101void 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 Silvermance86bac2013-03-31 19:07:24 -0700111
Austin Schuh80ff2e12014-03-08 12:06:19 -0800112void 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 Silvermance86bac2013-03-31 19:07:24 -0700122
Austin Schuh80ff2e12014-03-08 12:06:19 -0800123void 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 Silverman31b5b822014-03-14 18:50:39 -0700126 .bottom_angle(0.86)
Austin Schuha4faacc2014-03-09 00:50:50 -0800127 .separation_angle(0.10)
128 .intake(4.0)
Austin Schuh80ff2e12014-03-08 12:06:19 -0800129 .centering(1.0)
130 .Send()) {
131 LOG(WARNING, "sending claw goal failed\n");
132 }
133}
134
135void SetShotPower(double power) {
Austin Schuha4faacc2014-03-09 00:50:50 -0800136 LOG(INFO, "Setting shot power to %f\n", power);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800137 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
147void 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
157void 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 Schuha4faacc2014-03-09 00:50:50 -0800165SetDriveGoal(double distance, double maximum_velocity = 1.7) {
166 LOG(INFO, "Driving to %f\n", distance);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800167 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 Schuha4faacc2014-03-09 00:50:50 -0800173 // Uncomment to make relative again.
Austin Schuh80ff2e12014-03-08 12:06:19 -0800174 left_initial_position += distance;
175 right_initial_position += distance;
176 return ::std::move(drivetrain_action);
177}
178
179void InitializeEncoders() {
Brian Silverman3b89ed82013-03-22 18:59:16 -0700180 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 Schuh80ff2e12014-03-08 12:06:19 -0800190}
191
Austin Schuha4faacc2014-03-09 00:50:50 -0800192void 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 Schuh80ff2e12014-03-08 12:06:19 -0800221void HandleAuto() {
Brian Silverman6f621542014-04-06 16:00:41 -0700222 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 Schuha4faacc2014-03-09 00:50:50 -0800234 // The front of the robot is 1.854 meters from the wall
235 const double kShootDistance = 3.15;
Austin Schuh80ff2e12014-03-08 12:06:19 -0800236 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 Schuha4faacc2014-03-09 00:50:50 -0800243 LOG(INFO, "Claw going up\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800244 PositionClawVertically(12.0, 4.0);
Austin Schuha4faacc2014-03-09 00:50:50 -0800245 SetShotPower(115.0);
Austin Schuh80ff2e12014-03-08 12:06:19 -0800246
247 // Wait for the ball to enter the claw.
Austin Schuha4faacc2014-03-09 00:50:50 -0800248 time::SleepFor(time::Time::InSeconds(0.25));
Austin Schuh80ff2e12014-03-08 12:06:19 -0800249 if (ShouldExitAuto()) return;
Austin Schuha4faacc2014-03-09 00:50:50 -0800250 LOG(INFO, "Readying claw for shot\n");
Austin Schuh80ff2e12014-03-08 12:06:19 -0800251
252 {
253 if (ShouldExitAuto()) return;
254 // Drive to the goal.
Austin Schuha4faacc2014-03-09 00:50:50 -0800255 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 Schuh80ff2e12014-03-08 12:06:19 -0800259 WaitUntilDoneOrCanceled(drivetrain_action.get());
260 if (ShouldExitAuto()) return;
261 }
Brian Silverman6f621542014-04-06 16:00:41 -0700262 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 Schuh80ff2e12014-03-08 12:06:19 -0800275
276 // Shoot.
277 Shoot();
Austin Schuh80ff2e12014-03-08 12:06:19 -0800278
Austin Schuh80ff2e12014-03-08 12:06:19 -0800279 if (ShouldExitAuto()) return;
280
281 // Get ready to zero when we come back up.
282 PositionClawVertically(0.0, 0.0);
Austin Schuh47017412013-03-10 11:50:46 -0700283}
284
285} // namespace autonomous
286} // namespace frc971