blob: a292531b47df7584518d962e3512f968ae2afbdc [file] [log] [blame]
Austin Schuha3c148e2018-03-09 21:04:05 -08001#ifndef Y2018_ACTORS_AUTONOMOUS_ACTOR_H_
2#define Y2018_ACTORS_AUTONOMOUS_ACTOR_H_
3
4#include <chrono>
5#include <memory>
6
7#include "aos/common/actions/actions.h"
8#include "aos/common/actions/actor.h"
9#include "frc971/autonomous/base_autonomous_actor.h"
10#include "frc971/control_loops/drivetrain/drivetrain.q.h"
11#include "frc971/control_loops/drivetrain/drivetrain_config.h"
12#include "y2018/control_loops/superstructure/arm/generated_graph.h"
13#include "y2018/control_loops/superstructure/superstructure.q.h"
14
15namespace y2018 {
16namespace actors {
17using ::y2018::control_loops::superstructure_queue;
Austin Schuhf79c0e52018-04-04 20:13:21 -070018using ::frc971::control_loops::drivetrain_queue;
Austin Schuha3c148e2018-03-09 21:04:05 -080019
20namespace arm = ::y2018::control_loops::superstructure::arm;
21
22class AutonomousActor : public ::frc971::autonomous::BaseAutonomousActor {
23 public:
24 explicit AutonomousActor(::frc971::autonomous::AutonomousActionQueueGroup *s);
25
26 bool RunAction(
27 const ::frc971::autonomous::AutonomousActionParams &params) override;
28 private:
29 void Reset() {
30 roller_voltage_ = 0.0;
31 left_intake_angle_ = -3.3;
32 right_intake_angle_ = -3.3;
33 arm_goal_position_ = arm::NeutralIndex();
34 grab_box_ = false;
35 open_claw_ = false;
Austin Schuhf79c0e52018-04-04 20:13:21 -070036 close_claw_ = false;
Austin Schuha3c148e2018-03-09 21:04:05 -080037 deploy_fork_ = false;
Austin Schuhf79c0e52018-04-04 20:13:21 -070038 disable_box_correct_ = false;
Austin Schuha3c148e2018-03-09 21:04:05 -080039 InitializeEncoders();
40 ResetDrivetrain();
41 SendSuperstructureGoal();
42 }
43
44 double roller_voltage_ = 0.0;
45 double left_intake_angle_ = -3.3;
46 double right_intake_angle_ = -3.3;
47 uint32_t arm_goal_position_ = arm::NeutralIndex();
48 bool grab_box_ = false;
49 bool open_claw_ = false;
Austin Schuhf79c0e52018-04-04 20:13:21 -070050 bool close_claw_ = false;
Austin Schuha3c148e2018-03-09 21:04:05 -080051 bool deploy_fork_ = false;
Austin Schuhf79c0e52018-04-04 20:13:21 -070052 bool disable_box_correct_ = false;
Austin Schuha3c148e2018-03-09 21:04:05 -080053
54 void set_roller_voltage(double roller_voltage) {
55 roller_voltage_ = roller_voltage;
56 }
Austin Schuhf79c0e52018-04-04 20:13:21 -070057 void set_intake_angle(double intake_angle) {
58 set_left_intake_angle(intake_angle);
59 set_right_intake_angle(intake_angle);
60 }
Austin Schuha3c148e2018-03-09 21:04:05 -080061 void set_left_intake_angle(double left_intake_angle) {
62 left_intake_angle_ = left_intake_angle;
63 }
64 void set_right_intake_angle(double right_intake_angle) {
65 right_intake_angle_ = right_intake_angle;
66 }
67 void set_arm_goal_position(uint32_t arm_goal_position) {
68 arm_goal_position_ = arm_goal_position;
69 }
70 void set_grab_box(bool grab_box) { grab_box_ = grab_box; }
71 void set_open_claw(bool open_claw) { open_claw_ = open_claw; }
Austin Schuhf79c0e52018-04-04 20:13:21 -070072 void set_close_claw(bool close_claw) { close_claw_ = close_claw; }
Austin Schuha3c148e2018-03-09 21:04:05 -080073 void set_deploy_fork(bool deploy_fork) { deploy_fork_ = deploy_fork; }
74
Austin Schuhf79c0e52018-04-04 20:13:21 -070075 void set_disable_box_correct(bool disable_box_correct) {
76 disable_box_correct_ = disable_box_correct;
77 }
78
Austin Schuha3c148e2018-03-09 21:04:05 -080079 void SendSuperstructureGoal() {
80 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
81 new_superstructure_goal->intake.roller_voltage = roller_voltage_;
82 new_superstructure_goal->intake.left_intake_angle = left_intake_angle_;
83 new_superstructure_goal->intake.right_intake_angle = right_intake_angle_;
84
85 new_superstructure_goal->arm_goal_position = arm_goal_position_;
86 new_superstructure_goal->grab_box = grab_box_;
87 new_superstructure_goal->open_claw = open_claw_;
Austin Schuhf79c0e52018-04-04 20:13:21 -070088 new_superstructure_goal->close_claw = close_claw_;
Austin Schuha3c148e2018-03-09 21:04:05 -080089 new_superstructure_goal->deploy_fork = deploy_fork_;
90
91 if (!new_superstructure_goal.Send()) {
92 LOG(ERROR, "Sending superstructure goal failed.\n");
93 }
94 }
Austin Schuhc231df42018-03-21 20:43:24 -070095
Austin Schuhf79c0e52018-04-04 20:13:21 -070096 bool WaitForArmTrajectoryOrDriveClose(double drive_threshold,
97 double arm_threshold) {
98 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
99 ::std::chrono::milliseconds(5) / 2);
100
101 constexpr double kPositionTolerance = 0.02;
102 constexpr double kProfileTolerance = 0.001;
103
104 while (true) {
105 if (ShouldCancel()) {
106 return false;
107 }
108
109 superstructure_queue.status.FetchLatest();
110 drivetrain_queue.status.FetchLatest();
111 if (drivetrain_queue.status.get() && superstructure_queue.status.get()) {
112 const double left_profile_error =
113 (initial_drivetrain_.left -
114 drivetrain_queue.status->profiled_left_position_goal);
115 const double right_profile_error =
116 (initial_drivetrain_.right -
117 drivetrain_queue.status->profiled_right_position_goal);
118
119 const double left_error =
120 (initial_drivetrain_.left -
121 drivetrain_queue.status->estimated_left_position);
122 const double right_error =
123 (initial_drivetrain_.right -
124 drivetrain_queue.status->estimated_right_position);
125
126 const double profile_distance_to_go =
127 (left_profile_error + right_profile_error) / 2.0;
128
129 const double distance_to_go = (left_error + right_error) / 2.0;
130
131 // Check superstructure first.
132 if (superstructure_queue.status->arm.current_node ==
133 arm_goal_position_ &&
134 superstructure_queue.status->arm.path_distance_to_go <
135 arm_threshold) {
136 LOG(INFO, "Arm finished first: %f, drivetrain %f distance\n",
137 superstructure_queue.status->arm.path_distance_to_go,
138 ::std::abs(distance_to_go));
139 return true;
140 }
141
142 // Now check drivetrain.
143 if (::std::abs(profile_distance_to_go) <
144 drive_threshold + kProfileTolerance &&
145 ::std::abs(distance_to_go) < drive_threshold + kPositionTolerance) {
146 LOG(INFO,
147 "Drivetrain finished first: arm %f, drivetrain %f distance\n",
148 superstructure_queue.status->arm.path_distance_to_go,
149 ::std::abs(distance_to_go));
150 return true;
151 }
152 }
153 phased_loop.SleepUntilNext();
154 }
155 }
156
Austin Schuhc231df42018-03-21 20:43:24 -0700157 bool WaitForArmTrajectoryClose(double threshold) {
158 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
159 ::std::chrono::milliseconds(5) / 2);
160 while (true) {
161 if (ShouldCancel()) {
162 return false;
163 }
164
165 superstructure_queue.status.FetchLatest();
166 if (superstructure_queue.status.get()) {
Austin Schuhf79c0e52018-04-04 20:13:21 -0700167 if (superstructure_queue.status->arm.current_node ==
168 arm_goal_position_ &&
Austin Schuhc231df42018-03-21 20:43:24 -0700169 superstructure_queue.status->arm.path_distance_to_go < threshold) {
170 return true;
171 }
172 }
173 phased_loop.SleepUntilNext();
174 }
175 }
Austin Schuhf79c0e52018-04-04 20:13:21 -0700176
177 bool WaitForBoxGrabed() {
178 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
179 ::std::chrono::milliseconds(5) / 2);
180 while (true) {
181 if (ShouldCancel()) {
182 return false;
183 }
184
185 superstructure_queue.status.FetchLatest();
186 if (superstructure_queue.status.get()) {
187 if (superstructure_queue.status->arm.grab_state == 4) {
188 return true;
189 }
190 }
191 phased_loop.SleepUntilNext();
192 }
193 }
Austin Schuha3c148e2018-03-09 21:04:05 -0800194};
195
196} // namespace actors
197} // namespace y2018
198
199#endif // Y2018_ACTORS_AUTONOMOUS_ACTOR_H_