blob: 65f0487299dfeba8c8158a5c2a69da83f0c2d775 [file] [log] [blame]
James Kuszmaulc0568f22014-03-05 20:33:00 -08001#include <functional>
2
3#include "aos/common/control_loop/Timing.h"
4#include "aos/common/logging/logging.h"
5
6#include "frc971/actions/catch_action.h"
7#include "frc971/control_loops/claw/claw.q.h"
8#include "frc971/queues/othersensors.q.h"
9
10namespace frc971 {
11namespace actions {
12
13CatchAction::CatchAction(actions::CatchActionGroup* s)
14 : actions::ActionBase<actions::CatchActionGroup>(s) {}
15
16void CatchAction::RunAction() {
James Kuszmaulc0568f22014-03-05 20:33:00 -080017 // Set claw angle.
Austin Schuh80ff2e12014-03-08 12:06:19 -080018 if (!control_loops::claw_queue_group.goal.MakeWithBuilder()
19 .bottom_angle(action_q_->goal->catch_angle)
20 .separation_angle(kCatchSeparation)
21 .intake(kCatchIntake)
22 .centering(kCatchCentering)
23 .Send()) {
James Kuszmaulc0568f22014-03-05 20:33:00 -080024 LOG(WARNING, "sending claw goal failed\n");
25 return;
26 }
Austin Schuh80ff2e12014-03-08 12:06:19 -080027 LOG(INFO, "Waiting for the claw to be ready\n");
James Kuszmaulc0568f22014-03-05 20:33:00 -080028
29 // wait for claw to be ready
30 if (WaitUntil(::std::bind(&CatchAction::DoneSetupCatch, this))) return;
Austin Schuh80ff2e12014-03-08 12:06:19 -080031 LOG(INFO, "Waiting for the sonar\n");
James Kuszmaulc0568f22014-03-05 20:33:00 -080032
33 // wait for the sonar to trigger
34 if (WaitUntil(::std::bind(&CatchAction::DoneFoundSonar, this))) return;
35
Austin Schuh80ff2e12014-03-08 12:06:19 -080036 LOG(INFO, "Closing the claw\n");
37
James Kuszmaulc0568f22014-03-05 20:33:00 -080038 // close the claw
Austin Schuh80ff2e12014-03-08 12:06:19 -080039 if (!control_loops::claw_queue_group.goal.MakeWithBuilder()
40 .bottom_angle(action_q_->goal->catch_angle + kCatchSeparation / 2.0)
41 .separation_angle(0.0)
42 .intake(kCatchIntake)
43 .centering(kCatchCentering)
44 .Send()) {
James Kuszmaulc0568f22014-03-05 20:33:00 -080045 LOG(WARNING, "sending claw goal failed\n");
46 return;
47 }
48
49 // claw now closed
50 if (WaitUntil(::std::bind(&CatchAction::DoneClawWithBall, this))) return;
James Kuszmaulc0568f22014-03-05 20:33:00 -080051
James Kuszmaulc0568f22014-03-05 20:33:00 -080052 return;
53}
54
55
Austin Schuh80ff2e12014-03-08 12:06:19 -080056/*bool CatchAction::DoneBallIn() {
James Kuszmaulc0568f22014-03-05 20:33:00 -080057 if (!sensors::othersensors.FetchLatest()) {
Austin Schuh80ff2e12014-03-08 12:06:19 -080058 sensors::othersensors.FetchNextBlocking();
James Kuszmaulc0568f22014-03-05 20:33:00 -080059 }
60 if (sensors::othersensors->travis_hall_effect_distance > 0.005) {
61 LOG(INFO, "Ball in at %.2f.\n",
62 sensors::othersensors->travis_hall_effect_distance);
James Kuszmaulc0568f22014-03-05 20:33:00 -080063 return true;
64 }
65 return false;
Austin Schuh80ff2e12014-03-08 12:06:19 -080066}*/
67
68bool CatchAction::DoneClawWithBall() {
69 if (!control_loops::claw_queue_group.status.FetchLatest()) {
70 control_loops::claw_queue_group.status.FetchNextBlocking();
71 }
72
73 bool ans =
74 control_loops::claw_queue_group.status->zeroed &&
75 (::std::abs(control_loops::claw_queue_group.status->bottom_velocity) <
76 0.5) &&
77 (::std::abs(control_loops::claw_queue_group.status->bottom -
78 control_loops::claw_queue_group.goal->bottom_angle) < 0.10) &&
79 (::std::abs(control_loops::claw_queue_group.status->separation -
80 control_loops::claw_queue_group.goal->separation_angle) <
81 0.4);
82
83 if (!ans) {
84 LOG(INFO,
85 "Claw is ready %d zeroed %d bottom_velocity %f bottom %f sep %f\n", ans,
86 control_loops::claw_queue_group.status->zeroed,
87 ::std::abs(control_loops::claw_queue_group.status->bottom_velocity),
88 ::std::abs(control_loops::claw_queue_group.status->bottom -
89 control_loops::claw_queue_group.goal->bottom_angle),
90 ::std::abs(control_loops::claw_queue_group.status->separation -
91 control_loops::claw_queue_group.goal->separation_angle));
92 }
93 return ans;
James Kuszmaulc0568f22014-03-05 20:33:00 -080094}
95
96bool CatchAction::DoneFoundSonar() {
97 if (!sensors::othersensors.FetchLatest()) {
Austin Schuh80ff2e12014-03-08 12:06:19 -080098 sensors::othersensors.FetchNextBlocking();
James Kuszmaulc0568f22014-03-05 20:33:00 -080099 }
Austin Schuh80ff2e12014-03-08 12:06:19 -0800100 LOG(INFO, "Sonar at %.2f.\n", sensors::othersensors->sonar_distance);
James Kuszmaulc0568f22014-03-05 20:33:00 -0800101 if (sensors::othersensors->sonar_distance > 0.3 &&
102 sensors::othersensors->sonar_distance < kSonarTriggerDist) {
Austin Schuh80ff2e12014-03-08 12:06:19 -0800103 return true;
James Kuszmaulc0568f22014-03-05 20:33:00 -0800104 }
105 return false;
106}
107
108bool CatchAction::DoneSetupCatch() {
109 if (!control_loops::claw_queue_group.status.FetchLatest()) {
Austin Schuh80ff2e12014-03-08 12:06:19 -0800110 control_loops::claw_queue_group.status.FetchNextBlocking();
James Kuszmaulc0568f22014-03-05 20:33:00 -0800111 }
Austin Schuh80ff2e12014-03-08 12:06:19 -0800112
James Kuszmaulc0568f22014-03-05 20:33:00 -0800113 // Make sure that the shooter and claw has reached the necessary state.
114 // Check the current positions of the various mechanisms to make sure that we
115 // avoid race conditions where we send it a new goal but it still thinks that
116 // it has the old goal and thinks that it is already done.
117 bool claw_angle_correct =
118 ::std::abs(control_loops::claw_queue_group.status->bottom -
Austin Schuh80ff2e12014-03-08 12:06:19 -0800119 action_q_->goal->catch_angle) < 0.15;
120 bool open_enough =
121 control_loops::claw_queue_group.status->separation > kCatchMinSeparation;
James Kuszmaulc0568f22014-03-05 20:33:00 -0800122
Austin Schuh80ff2e12014-03-08 12:06:19 -0800123 if (claw_angle_correct && open_enough) {
James Kuszmaulc0568f22014-03-05 20:33:00 -0800124 LOG(INFO, "Claw ready for catching.\n");
125 return true;
126 }
127
128 return false;
129}
130
131} // namespace actions
132} // namespace frc971
133