blob: 0cff3f382a6dd4009193968b14dd5b7fb5a16217 [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
John Park33858a32018-09-28 23:05:48 -07007#include "aos/actions/actions.h"
8#include "aos/actions/actor.h"
Austin Schuha3c148e2018-03-09 21:04:05 -08009#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;
Austin Schuhcf96d322018-04-07 15:52:31 -070031 left_intake_angle_ = -3.2;
32 right_intake_angle_ = -3.2;
Austin Schuha3c148e2018-03-09 21:04:05 -080033 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;
Austin Schuhcf96d322018-04-07 15:52:31 -070045 double left_intake_angle_ = -3.2;
46 double right_intake_angle_ = -3.2;
Austin Schuha3c148e2018-03-09 21:04:05 -080047 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_;
Austin Schuh4dad8fb2018-07-08 16:00:13 -070090 new_superstructure_goal->trajectory_override = false;
Austin Schuha3c148e2018-03-09 21:04:05 -080091
92 if (!new_superstructure_goal.Send()) {
93 LOG(ERROR, "Sending superstructure goal failed.\n");
94 }
95 }
Austin Schuhc231df42018-03-21 20:43:24 -070096
Austin Schuh4dad8fb2018-07-08 16:00:13 -070097 bool ThreeCubeAuto(::aos::monotonic_clock::time_point start_time);
98 bool CloseSwitch(::aos::monotonic_clock::time_point start_time,
99 bool left = true);
100 bool FarSwitch(::aos::monotonic_clock::time_point start_time,
101 bool drive_behind = true, bool left = true);
102 bool FarReadyScale(::aos::monotonic_clock::time_point start_time);
103 bool DriveStraight();
104
105 bool FarScale(::aos::monotonic_clock::time_point start_time);
106
Austin Schuhf79c0e52018-04-04 20:13:21 -0700107 bool WaitForArmTrajectoryOrDriveClose(double drive_threshold,
108 double arm_threshold) {
109 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
110 ::std::chrono::milliseconds(5) / 2);
111
112 constexpr double kPositionTolerance = 0.02;
113 constexpr double kProfileTolerance = 0.001;
114
115 while (true) {
116 if (ShouldCancel()) {
117 return false;
118 }
119
120 superstructure_queue.status.FetchLatest();
121 drivetrain_queue.status.FetchLatest();
122 if (drivetrain_queue.status.get() && superstructure_queue.status.get()) {
123 const double left_profile_error =
124 (initial_drivetrain_.left -
125 drivetrain_queue.status->profiled_left_position_goal);
126 const double right_profile_error =
127 (initial_drivetrain_.right -
128 drivetrain_queue.status->profiled_right_position_goal);
129
130 const double left_error =
131 (initial_drivetrain_.left -
132 drivetrain_queue.status->estimated_left_position);
133 const double right_error =
134 (initial_drivetrain_.right -
135 drivetrain_queue.status->estimated_right_position);
136
137 const double profile_distance_to_go =
138 (left_profile_error + right_profile_error) / 2.0;
139
140 const double distance_to_go = (left_error + right_error) / 2.0;
141
142 // Check superstructure first.
143 if (superstructure_queue.status->arm.current_node ==
144 arm_goal_position_ &&
145 superstructure_queue.status->arm.path_distance_to_go <
146 arm_threshold) {
147 LOG(INFO, "Arm finished first: %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 // Now check drivetrain.
154 if (::std::abs(profile_distance_to_go) <
155 drive_threshold + kProfileTolerance &&
156 ::std::abs(distance_to_go) < drive_threshold + kPositionTolerance) {
157 LOG(INFO,
158 "Drivetrain finished first: arm %f, drivetrain %f distance\n",
159 superstructure_queue.status->arm.path_distance_to_go,
160 ::std::abs(distance_to_go));
161 return true;
162 }
163 }
164 phased_loop.SleepUntilNext();
165 }
166 }
167
Austin Schuhc231df42018-03-21 20:43:24 -0700168 bool WaitForArmTrajectoryClose(double threshold) {
169 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
170 ::std::chrono::milliseconds(5) / 2);
171 while (true) {
172 if (ShouldCancel()) {
173 return false;
174 }
175
176 superstructure_queue.status.FetchLatest();
177 if (superstructure_queue.status.get()) {
Austin Schuhf79c0e52018-04-04 20:13:21 -0700178 if (superstructure_queue.status->arm.current_node ==
179 arm_goal_position_ &&
Austin Schuhc231df42018-03-21 20:43:24 -0700180 superstructure_queue.status->arm.path_distance_to_go < threshold) {
181 return true;
182 }
183 }
184 phased_loop.SleepUntilNext();
185 }
186 }
Austin Schuhf79c0e52018-04-04 20:13:21 -0700187
188 bool WaitForBoxGrabed() {
189 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
190 ::std::chrono::milliseconds(5) / 2);
191 while (true) {
192 if (ShouldCancel()) {
193 return false;
194 }
195
196 superstructure_queue.status.FetchLatest();
197 if (superstructure_queue.status.get()) {
198 if (superstructure_queue.status->arm.grab_state == 4) {
199 return true;
200 }
201 }
202 phased_loop.SleepUntilNext();
203 }
204 }
Austin Schuha3c148e2018-03-09 21:04:05 -0800205};
206
207} // namespace actors
208} // namespace y2018
209
210#endif // Y2018_ACTORS_AUTONOMOUS_ACTOR_H_