blob: 350b6f6d0c945ac67a103e38daf28b8645004676 [file] [log] [blame]
James Kuszmaulc0568f22014-03-05 20:33:00 -08001#include <functional>
2
James Kuszmaulc0568f22014-03-05 20:33:00 -08003#include "aos/common/logging/logging.h"
4
5#include "frc971/actions/catch_action.h"
6#include "frc971/control_loops/claw/claw.q.h"
Brian Silverman6bf0d3c2014-03-08 12:52:54 -08007#include "frc971/queues/other_sensors.q.h"
James Kuszmaulc0568f22014-03-05 20:33:00 -08008
9namespace frc971 {
10namespace actions {
11
12CatchAction::CatchAction(actions::CatchActionGroup* s)
13 : actions::ActionBase<actions::CatchActionGroup>(s) {}
14
15void CatchAction::RunAction() {
Austin Schuha719bf12014-03-09 00:45:02 -080016 control_loops::claw_queue_group.goal.FetchLatest();
17 if (control_loops::claw_queue_group.goal.get() == nullptr) {
18 LOG(WARNING, "no claw goal\n");
19 return;
20 }
21
James Kuszmaulc0568f22014-03-05 20:33:00 -080022 // Set claw angle.
Austin Schuh80ff2e12014-03-08 12:06:19 -080023 if (!control_loops::claw_queue_group.goal.MakeWithBuilder()
Austin Schuha719bf12014-03-09 00:45:02 -080024 .bottom_angle(action_q_->goal->catch_angle - kCatchSeparation / 2.0)
Austin Schuh80ff2e12014-03-08 12:06:19 -080025 .separation_angle(kCatchSeparation)
26 .intake(kCatchIntake)
27 .centering(kCatchCentering)
28 .Send()) {
James Kuszmaulc0568f22014-03-05 20:33:00 -080029 LOG(WARNING, "sending claw goal failed\n");
30 return;
31 }
Austin Schuha719bf12014-03-09 00:45:02 -080032
33 control_loops::claw_queue_group.goal.FetchLatest();
34
Austin Schuh80ff2e12014-03-08 12:06:19 -080035 LOG(INFO, "Waiting for the claw to be ready\n");
James Kuszmaulc0568f22014-03-05 20:33:00 -080036
37 // wait for claw to be ready
38 if (WaitUntil(::std::bind(&CatchAction::DoneSetupCatch, this))) return;
Austin Schuh80ff2e12014-03-08 12:06:19 -080039 LOG(INFO, "Waiting for the sonar\n");
James Kuszmaulc0568f22014-03-05 20:33:00 -080040
Austin Schuha719bf12014-03-09 00:45:02 -080041 close_count_ = 0;
42
James Kuszmaulc0568f22014-03-05 20:33:00 -080043 // wait for the sonar to trigger
44 if (WaitUntil(::std::bind(&CatchAction::DoneFoundSonar, this))) return;
45
Austin Schuh80ff2e12014-03-08 12:06:19 -080046 LOG(INFO, "Closing the claw\n");
47
James Kuszmaulc0568f22014-03-05 20:33:00 -080048 // close the claw
Austin Schuh80ff2e12014-03-08 12:06:19 -080049 if (!control_loops::claw_queue_group.goal.MakeWithBuilder()
Austin Schuha719bf12014-03-09 00:45:02 -080050 .bottom_angle(action_q_->goal->catch_angle)
Austin Schuh80ff2e12014-03-08 12:06:19 -080051 .separation_angle(0.0)
52 .intake(kCatchIntake)
53 .centering(kCatchCentering)
54 .Send()) {
James Kuszmaulc0568f22014-03-05 20:33:00 -080055 LOG(WARNING, "sending claw goal failed\n");
56 return;
57 }
58
Austin Schuha719bf12014-03-09 00:45:02 -080059 control_loops::claw_queue_group.goal.FetchLatest();
60
James Kuszmaulc0568f22014-03-05 20:33:00 -080061 // claw now closed
62 if (WaitUntil(::std::bind(&CatchAction::DoneClawWithBall, this))) return;
James Kuszmaulc0568f22014-03-05 20:33:00 -080063
Austin Schuha719bf12014-03-09 00:45:02 -080064 for (int i = 0; i < 5; ++i) {
65 aos::time::SleepFor(aos::time::Time::InSeconds(0.05));
66 if (ShouldCancel()) return;
67 }
James Kuszmaulc0568f22014-03-05 20:33:00 -080068}
69
70
Austin Schuh80ff2e12014-03-08 12:06:19 -080071/*bool CatchAction::DoneBallIn() {
James Kuszmaulc0568f22014-03-05 20:33:00 -080072 if (!sensors::othersensors.FetchLatest()) {
Austin Schuh80ff2e12014-03-08 12:06:19 -080073 sensors::othersensors.FetchNextBlocking();
James Kuszmaulc0568f22014-03-05 20:33:00 -080074 }
75 if (sensors::othersensors->travis_hall_effect_distance > 0.005) {
76 LOG(INFO, "Ball in at %.2f.\n",
77 sensors::othersensors->travis_hall_effect_distance);
James Kuszmaulc0568f22014-03-05 20:33:00 -080078 return true;
79 }
80 return false;
Austin Schuh80ff2e12014-03-08 12:06:19 -080081}*/
82
83bool CatchAction::DoneClawWithBall() {
84 if (!control_loops::claw_queue_group.status.FetchLatest()) {
85 control_loops::claw_queue_group.status.FetchNextBlocking();
86 }
87
88 bool ans =
89 control_loops::claw_queue_group.status->zeroed &&
90 (::std::abs(control_loops::claw_queue_group.status->bottom_velocity) <
Austin Schuha719bf12014-03-09 00:45:02 -080091 1.0) &&
Austin Schuh80ff2e12014-03-08 12:06:19 -080092 (::std::abs(control_loops::claw_queue_group.status->bottom -
93 control_loops::claw_queue_group.goal->bottom_angle) < 0.10) &&
94 (::std::abs(control_loops::claw_queue_group.status->separation -
95 control_loops::claw_queue_group.goal->separation_angle) <
96 0.4);
97
98 if (!ans) {
99 LOG(INFO,
100 "Claw is ready %d zeroed %d bottom_velocity %f bottom %f sep %f\n", ans,
101 control_loops::claw_queue_group.status->zeroed,
102 ::std::abs(control_loops::claw_queue_group.status->bottom_velocity),
103 ::std::abs(control_loops::claw_queue_group.status->bottom -
104 control_loops::claw_queue_group.goal->bottom_angle),
105 ::std::abs(control_loops::claw_queue_group.status->separation -
106 control_loops::claw_queue_group.goal->separation_angle));
107 }
108 return ans;
James Kuszmaulc0568f22014-03-05 20:33:00 -0800109}
110
111bool CatchAction::DoneFoundSonar() {
Brian Silverman6bf0d3c2014-03-08 12:52:54 -0800112 if (!sensors::other_sensors.FetchLatest()) {
113 sensors::other_sensors.FetchNextBlocking();
James Kuszmaulc0568f22014-03-05 20:33:00 -0800114 }
Brian Silverman6bf0d3c2014-03-08 12:52:54 -0800115 LOG(DEBUG, "Sonar at %.2f.\n", sensors::other_sensors->sonar_distance);
Austin Schuha719bf12014-03-09 00:45:02 -0800116 if (sensors::other_sensors->sonar_distance > 0.1 &&
Brian Silverman6bf0d3c2014-03-08 12:52:54 -0800117 sensors::other_sensors->sonar_distance < kSonarTriggerDist) {
Austin Schuha719bf12014-03-09 00:45:02 -0800118 ++close_count_;
119 } else {
120 close_count_ = 0;
121 }
122 if (close_count_ > 50) {
Austin Schuh80ff2e12014-03-08 12:06:19 -0800123 return true;
James Kuszmaulc0568f22014-03-05 20:33:00 -0800124 }
125 return false;
126}
127
128bool CatchAction::DoneSetupCatch() {
129 if (!control_loops::claw_queue_group.status.FetchLatest()) {
Austin Schuh80ff2e12014-03-08 12:06:19 -0800130 control_loops::claw_queue_group.status.FetchNextBlocking();
James Kuszmaulc0568f22014-03-05 20:33:00 -0800131 }
Austin Schuh80ff2e12014-03-08 12:06:19 -0800132
James Kuszmaulc0568f22014-03-05 20:33:00 -0800133 // Make sure that the shooter and claw has reached the necessary state.
134 // Check the current positions of the various mechanisms to make sure that we
135 // avoid race conditions where we send it a new goal but it still thinks that
136 // it has the old goal and thinks that it is already done.
137 bool claw_angle_correct =
138 ::std::abs(control_loops::claw_queue_group.status->bottom -
Austin Schuha719bf12014-03-09 00:45:02 -0800139 control_loops::claw_queue_group.goal->bottom_angle) < 0.15;
Austin Schuh80ff2e12014-03-08 12:06:19 -0800140 bool open_enough =
141 control_loops::claw_queue_group.status->separation > kCatchMinSeparation;
James Kuszmaulc0568f22014-03-05 20:33:00 -0800142
Austin Schuh80ff2e12014-03-08 12:06:19 -0800143 if (claw_angle_correct && open_enough) {
James Kuszmaulc0568f22014-03-05 20:33:00 -0800144 LOG(INFO, "Claw ready for catching.\n");
145 return true;
146 }
147
148 return false;
149}
150
151} // namespace actions
152} // namespace frc971
153