blob: 7b6764512eb48c4e23bbafc2a24d8246135b17ef [file] [log] [blame]
Daniel Pettib0733be2014-11-14 22:44:03 -08001#include <complex>
James Kuszmaulc0568f22014-03-05 20:33:00 -08002#include <functional>
3
James Kuszmaulc0568f22014-03-05 20:33:00 -08004#include "aos/common/logging/logging.h"
5
6#include "frc971/actions/catch_action.h"
7#include "frc971/control_loops/claw/claw.q.h"
Brian Silverman6bf0d3c2014-03-08 12:52:54 -08008#include "frc971/queues/other_sensors.q.h"
James Kuszmaulc0568f22014-03-05 20:33:00 -08009
10namespace frc971 {
11namespace actions {
12
13CatchAction::CatchAction(actions::CatchActionGroup* s)
14 : actions::ActionBase<actions::CatchActionGroup>(s) {}
15
16void CatchAction::RunAction() {
Austin Schuha719bf12014-03-09 00:45:02 -080017 control_loops::claw_queue_group.goal.FetchLatest();
18 if (control_loops::claw_queue_group.goal.get() == nullptr) {
19 LOG(WARNING, "no claw goal\n");
20 return;
21 }
22
James Kuszmaulc0568f22014-03-05 20:33:00 -080023 // Set claw angle.
Austin Schuh80ff2e12014-03-08 12:06:19 -080024 if (!control_loops::claw_queue_group.goal.MakeWithBuilder()
Austin Schuha719bf12014-03-09 00:45:02 -080025 .bottom_angle(action_q_->goal->catch_angle - kCatchSeparation / 2.0)
Austin Schuh80ff2e12014-03-08 12:06:19 -080026 .separation_angle(kCatchSeparation)
27 .intake(kCatchIntake)
28 .centering(kCatchCentering)
29 .Send()) {
James Kuszmaulc0568f22014-03-05 20:33:00 -080030 LOG(WARNING, "sending claw goal failed\n");
31 return;
32 }
Austin Schuha719bf12014-03-09 00:45:02 -080033
34 control_loops::claw_queue_group.goal.FetchLatest();
35
Austin Schuh80ff2e12014-03-08 12:06:19 -080036 LOG(INFO, "Waiting for the claw to be ready\n");
James Kuszmaulc0568f22014-03-05 20:33:00 -080037
38 // wait for claw to be ready
39 if (WaitUntil(::std::bind(&CatchAction::DoneSetupCatch, this))) return;
Austin Schuh80ff2e12014-03-08 12:06:19 -080040 LOG(INFO, "Waiting for the sonar\n");
James Kuszmaulc0568f22014-03-05 20:33:00 -080041
Austin Schuha719bf12014-03-09 00:45:02 -080042 close_count_ = 0;
43
James Kuszmaulc0568f22014-03-05 20:33:00 -080044 // wait for the sonar to trigger
45 if (WaitUntil(::std::bind(&CatchAction::DoneFoundSonar, this))) return;
46
Austin Schuh80ff2e12014-03-08 12:06:19 -080047 LOG(INFO, "Closing the claw\n");
48
James Kuszmaulc0568f22014-03-05 20:33:00 -080049 // close the claw
Austin Schuh80ff2e12014-03-08 12:06:19 -080050 if (!control_loops::claw_queue_group.goal.MakeWithBuilder()
Austin Schuha719bf12014-03-09 00:45:02 -080051 .bottom_angle(action_q_->goal->catch_angle)
Austin Schuh80ff2e12014-03-08 12:06:19 -080052 .separation_angle(0.0)
53 .intake(kCatchIntake)
54 .centering(kCatchCentering)
55 .Send()) {
James Kuszmaulc0568f22014-03-05 20:33:00 -080056 LOG(WARNING, "sending claw goal failed\n");
57 return;
58 }
59
Austin Schuha719bf12014-03-09 00:45:02 -080060 control_loops::claw_queue_group.goal.FetchLatest();
61
James Kuszmaulc0568f22014-03-05 20:33:00 -080062 // claw now closed
63 if (WaitUntil(::std::bind(&CatchAction::DoneClawWithBall, this))) return;
James Kuszmaulc0568f22014-03-05 20:33:00 -080064
Austin Schuha719bf12014-03-09 00:45:02 -080065 for (int i = 0; i < 5; ++i) {
66 aos::time::SleepFor(aos::time::Time::InSeconds(0.05));
67 if (ShouldCancel()) return;
68 }
James Kuszmaulc0568f22014-03-05 20:33:00 -080069}
70
71
Austin Schuh80ff2e12014-03-08 12:06:19 -080072/*bool CatchAction::DoneBallIn() {
James Kuszmaulc0568f22014-03-05 20:33:00 -080073 if (!sensors::othersensors.FetchLatest()) {
Austin Schuh80ff2e12014-03-08 12:06:19 -080074 sensors::othersensors.FetchNextBlocking();
James Kuszmaulc0568f22014-03-05 20:33:00 -080075 }
76 if (sensors::othersensors->travis_hall_effect_distance > 0.005) {
77 LOG(INFO, "Ball in at %.2f.\n",
78 sensors::othersensors->travis_hall_effect_distance);
James Kuszmaulc0568f22014-03-05 20:33:00 -080079 return true;
80 }
81 return false;
Austin Schuh80ff2e12014-03-08 12:06:19 -080082}*/
83
84bool CatchAction::DoneClawWithBall() {
85 if (!control_loops::claw_queue_group.status.FetchLatest()) {
86 control_loops::claw_queue_group.status.FetchNextBlocking();
87 }
88
89 bool ans =
90 control_loops::claw_queue_group.status->zeroed &&
91 (::std::abs(control_loops::claw_queue_group.status->bottom_velocity) <
Austin Schuha719bf12014-03-09 00:45:02 -080092 1.0) &&
Austin Schuh80ff2e12014-03-08 12:06:19 -080093 (::std::abs(control_loops::claw_queue_group.status->bottom -
94 control_loops::claw_queue_group.goal->bottom_angle) < 0.10) &&
95 (::std::abs(control_loops::claw_queue_group.status->separation -
96 control_loops::claw_queue_group.goal->separation_angle) <
97 0.4);
98
99 if (!ans) {
100 LOG(INFO,
101 "Claw is ready %d zeroed %d bottom_velocity %f bottom %f sep %f\n", ans,
102 control_loops::claw_queue_group.status->zeroed,
103 ::std::abs(control_loops::claw_queue_group.status->bottom_velocity),
104 ::std::abs(control_loops::claw_queue_group.status->bottom -
105 control_loops::claw_queue_group.goal->bottom_angle),
106 ::std::abs(control_loops::claw_queue_group.status->separation -
107 control_loops::claw_queue_group.goal->separation_angle));
108 }
109 return ans;
James Kuszmaulc0568f22014-03-05 20:33:00 -0800110}
111
112bool CatchAction::DoneFoundSonar() {
Brian Silverman6bf0d3c2014-03-08 12:52:54 -0800113 if (!sensors::other_sensors.FetchLatest()) {
114 sensors::other_sensors.FetchNextBlocking();
James Kuszmaulc0568f22014-03-05 20:33:00 -0800115 }
Brian Silverman6bf0d3c2014-03-08 12:52:54 -0800116 LOG(DEBUG, "Sonar at %.2f.\n", sensors::other_sensors->sonar_distance);
Austin Schuha719bf12014-03-09 00:45:02 -0800117 if (sensors::other_sensors->sonar_distance > 0.1 &&
Brian Silverman6bf0d3c2014-03-08 12:52:54 -0800118 sensors::other_sensors->sonar_distance < kSonarTriggerDist) {
Austin Schuha719bf12014-03-09 00:45:02 -0800119 ++close_count_;
120 } else {
121 close_count_ = 0;
122 }
123 if (close_count_ > 50) {
Austin Schuh80ff2e12014-03-08 12:06:19 -0800124 return true;
James Kuszmaulc0568f22014-03-05 20:33:00 -0800125 }
126 return false;
127}
128
129bool CatchAction::DoneSetupCatch() {
130 if (!control_loops::claw_queue_group.status.FetchLatest()) {
Austin Schuh80ff2e12014-03-08 12:06:19 -0800131 control_loops::claw_queue_group.status.FetchNextBlocking();
James Kuszmaulc0568f22014-03-05 20:33:00 -0800132 }
Austin Schuh80ff2e12014-03-08 12:06:19 -0800133
James Kuszmaulc0568f22014-03-05 20:33:00 -0800134 // Make sure that the shooter and claw has reached the necessary state.
135 // Check the current positions of the various mechanisms to make sure that we
136 // avoid race conditions where we send it a new goal but it still thinks that
137 // it has the old goal and thinks that it is already done.
138 bool claw_angle_correct =
139 ::std::abs(control_loops::claw_queue_group.status->bottom -
Austin Schuha719bf12014-03-09 00:45:02 -0800140 control_loops::claw_queue_group.goal->bottom_angle) < 0.15;
Austin Schuh80ff2e12014-03-08 12:06:19 -0800141 bool open_enough =
142 control_loops::claw_queue_group.status->separation > kCatchMinSeparation;
James Kuszmaulc0568f22014-03-05 20:33:00 -0800143
Austin Schuh80ff2e12014-03-08 12:06:19 -0800144 if (claw_angle_correct && open_enough) {
James Kuszmaulc0568f22014-03-05 20:33:00 -0800145 LOG(INFO, "Claw ready for catching.\n");
146 return true;
147 }
148
149 return false;
150}
151
152} // namespace actions
153} // namespace frc971
154