blob: a97f0d1e85c56ba78a29094bcf8ad9f818224af8 [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 Schuheb99d072019-05-12 21:03:38 -07009#include "aos/events/event-loop.h"
Austin Schuha3c148e2018-03-09 21:04:05 -080010#include "frc971/autonomous/base_autonomous_actor.h"
11#include "frc971/control_loops/drivetrain/drivetrain.q.h"
12#include "frc971/control_loops/drivetrain/drivetrain_config.h"
13#include "y2018/control_loops/superstructure/arm/generated_graph.h"
14#include "y2018/control_loops/superstructure/superstructure.q.h"
15
16namespace y2018 {
17namespace actors {
18using ::y2018::control_loops::superstructure_queue;
Austin Schuhf79c0e52018-04-04 20:13:21 -070019using ::frc971::control_loops::drivetrain_queue;
Austin Schuha3c148e2018-03-09 21:04:05 -080020
21namespace arm = ::y2018::control_loops::superstructure::arm;
22
23class AutonomousActor : public ::frc971::autonomous::BaseAutonomousActor {
24 public:
Austin Schuh1bf8a212019-05-26 22:13:14 -070025 explicit AutonomousActor(::aos::EventLoop *event_loop);
Austin Schuha3c148e2018-03-09 21:04:05 -080026
27 bool RunAction(
28 const ::frc971::autonomous::AutonomousActionParams &params) override;
29 private:
30 void Reset() {
31 roller_voltage_ = 0.0;
Austin Schuhcf96d322018-04-07 15:52:31 -070032 left_intake_angle_ = -3.2;
33 right_intake_angle_ = -3.2;
Austin Schuha3c148e2018-03-09 21:04:05 -080034 arm_goal_position_ = arm::NeutralIndex();
35 grab_box_ = false;
36 open_claw_ = false;
Austin Schuhf79c0e52018-04-04 20:13:21 -070037 close_claw_ = false;
Austin Schuha3c148e2018-03-09 21:04:05 -080038 deploy_fork_ = false;
Austin Schuhf79c0e52018-04-04 20:13:21 -070039 disable_box_correct_ = false;
Austin Schuha3c148e2018-03-09 21:04:05 -080040 InitializeEncoders();
41 ResetDrivetrain();
42 SendSuperstructureGoal();
43 }
44
45 double roller_voltage_ = 0.0;
Austin Schuhcf96d322018-04-07 15:52:31 -070046 double left_intake_angle_ = -3.2;
47 double right_intake_angle_ = -3.2;
Austin Schuha3c148e2018-03-09 21:04:05 -080048 uint32_t arm_goal_position_ = arm::NeutralIndex();
49 bool grab_box_ = false;
50 bool open_claw_ = false;
Austin Schuhf79c0e52018-04-04 20:13:21 -070051 bool close_claw_ = false;
Austin Schuha3c148e2018-03-09 21:04:05 -080052 bool deploy_fork_ = false;
Austin Schuhf79c0e52018-04-04 20:13:21 -070053 bool disable_box_correct_ = false;
Austin Schuha3c148e2018-03-09 21:04:05 -080054
55 void set_roller_voltage(double roller_voltage) {
56 roller_voltage_ = roller_voltage;
57 }
Austin Schuhf79c0e52018-04-04 20:13:21 -070058 void set_intake_angle(double intake_angle) {
59 set_left_intake_angle(intake_angle);
60 set_right_intake_angle(intake_angle);
61 }
Austin Schuha3c148e2018-03-09 21:04:05 -080062 void set_left_intake_angle(double left_intake_angle) {
63 left_intake_angle_ = left_intake_angle;
64 }
65 void set_right_intake_angle(double right_intake_angle) {
66 right_intake_angle_ = right_intake_angle;
67 }
68 void set_arm_goal_position(uint32_t arm_goal_position) {
69 arm_goal_position_ = arm_goal_position;
70 }
71 void set_grab_box(bool grab_box) { grab_box_ = grab_box; }
72 void set_open_claw(bool open_claw) { open_claw_ = open_claw; }
Austin Schuhf79c0e52018-04-04 20:13:21 -070073 void set_close_claw(bool close_claw) { close_claw_ = close_claw; }
Austin Schuha3c148e2018-03-09 21:04:05 -080074 void set_deploy_fork(bool deploy_fork) { deploy_fork_ = deploy_fork; }
75
Austin Schuhf79c0e52018-04-04 20:13:21 -070076 void set_disable_box_correct(bool disable_box_correct) {
77 disable_box_correct_ = disable_box_correct;
78 }
79
Austin Schuha3c148e2018-03-09 21:04:05 -080080 void SendSuperstructureGoal() {
81 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
82 new_superstructure_goal->intake.roller_voltage = roller_voltage_;
83 new_superstructure_goal->intake.left_intake_angle = left_intake_angle_;
84 new_superstructure_goal->intake.right_intake_angle = right_intake_angle_;
85
86 new_superstructure_goal->arm_goal_position = arm_goal_position_;
87 new_superstructure_goal->grab_box = grab_box_;
88 new_superstructure_goal->open_claw = open_claw_;
Austin Schuhf79c0e52018-04-04 20:13:21 -070089 new_superstructure_goal->close_claw = close_claw_;
Austin Schuha3c148e2018-03-09 21:04:05 -080090 new_superstructure_goal->deploy_fork = deploy_fork_;
Austin Schuh4dad8fb2018-07-08 16:00:13 -070091 new_superstructure_goal->trajectory_override = false;
Austin Schuha3c148e2018-03-09 21:04:05 -080092
93 if (!new_superstructure_goal.Send()) {
94 LOG(ERROR, "Sending superstructure goal failed.\n");
95 }
96 }
Austin Schuhc231df42018-03-21 20:43:24 -070097
Austin Schuh4dad8fb2018-07-08 16:00:13 -070098 bool ThreeCubeAuto(::aos::monotonic_clock::time_point start_time);
99 bool CloseSwitch(::aos::monotonic_clock::time_point start_time,
100 bool left = true);
101 bool FarSwitch(::aos::monotonic_clock::time_point start_time,
102 bool drive_behind = true, bool left = true);
103 bool FarReadyScale(::aos::monotonic_clock::time_point start_time);
104 bool DriveStraight();
105
106 bool FarScale(::aos::monotonic_clock::time_point start_time);
107
Austin Schuhf79c0e52018-04-04 20:13:21 -0700108 bool WaitForArmTrajectoryOrDriveClose(double drive_threshold,
109 double arm_threshold) {
110 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
Austin Schuhd32b3622019-06-23 18:49:06 -0700111 event_loop()->monotonic_now(),
Austin Schuhf79c0e52018-04-04 20:13:21 -0700112 ::std::chrono::milliseconds(5) / 2);
113
114 constexpr double kPositionTolerance = 0.02;
115 constexpr double kProfileTolerance = 0.001;
116
117 while (true) {
118 if (ShouldCancel()) {
119 return false;
120 }
121
122 superstructure_queue.status.FetchLatest();
123 drivetrain_queue.status.FetchLatest();
124 if (drivetrain_queue.status.get() && superstructure_queue.status.get()) {
125 const double left_profile_error =
126 (initial_drivetrain_.left -
127 drivetrain_queue.status->profiled_left_position_goal);
128 const double right_profile_error =
129 (initial_drivetrain_.right -
130 drivetrain_queue.status->profiled_right_position_goal);
131
132 const double left_error =
133 (initial_drivetrain_.left -
134 drivetrain_queue.status->estimated_left_position);
135 const double right_error =
136 (initial_drivetrain_.right -
137 drivetrain_queue.status->estimated_right_position);
138
139 const double profile_distance_to_go =
140 (left_profile_error + right_profile_error) / 2.0;
141
142 const double distance_to_go = (left_error + right_error) / 2.0;
143
144 // Check superstructure first.
145 if (superstructure_queue.status->arm.current_node ==
146 arm_goal_position_ &&
147 superstructure_queue.status->arm.path_distance_to_go <
148 arm_threshold) {
149 LOG(INFO, "Arm finished first: %f, drivetrain %f distance\n",
150 superstructure_queue.status->arm.path_distance_to_go,
151 ::std::abs(distance_to_go));
152 return true;
153 }
154
155 // Now check drivetrain.
156 if (::std::abs(profile_distance_to_go) <
157 drive_threshold + kProfileTolerance &&
158 ::std::abs(distance_to_go) < drive_threshold + kPositionTolerance) {
159 LOG(INFO,
160 "Drivetrain finished first: arm %f, drivetrain %f distance\n",
161 superstructure_queue.status->arm.path_distance_to_go,
162 ::std::abs(distance_to_go));
163 return true;
164 }
165 }
166 phased_loop.SleepUntilNext();
167 }
168 }
169
Austin Schuhc231df42018-03-21 20:43:24 -0700170 bool WaitForArmTrajectoryClose(double threshold) {
171 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
Austin Schuhd32b3622019-06-23 18:49:06 -0700172 event_loop()->monotonic_now(),
Austin Schuhc231df42018-03-21 20:43:24 -0700173 ::std::chrono::milliseconds(5) / 2);
174 while (true) {
175 if (ShouldCancel()) {
176 return false;
177 }
178
179 superstructure_queue.status.FetchLatest();
180 if (superstructure_queue.status.get()) {
Austin Schuhf79c0e52018-04-04 20:13:21 -0700181 if (superstructure_queue.status->arm.current_node ==
182 arm_goal_position_ &&
Austin Schuhc231df42018-03-21 20:43:24 -0700183 superstructure_queue.status->arm.path_distance_to_go < threshold) {
184 return true;
185 }
186 }
187 phased_loop.SleepUntilNext();
188 }
189 }
Austin Schuhf79c0e52018-04-04 20:13:21 -0700190
191 bool WaitForBoxGrabed() {
192 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
Austin Schuhd32b3622019-06-23 18:49:06 -0700193 event_loop()->monotonic_now(),
Austin Schuhf79c0e52018-04-04 20:13:21 -0700194 ::std::chrono::milliseconds(5) / 2);
195 while (true) {
196 if (ShouldCancel()) {
197 return false;
198 }
199
200 superstructure_queue.status.FetchLatest();
201 if (superstructure_queue.status.get()) {
202 if (superstructure_queue.status->arm.grab_state == 4) {
203 return true;
204 }
205 }
206 phased_loop.SleepUntilNext();
207 }
208 }
Austin Schuha3c148e2018-03-09 21:04:05 -0800209};
210
211} // namespace actors
212} // namespace y2018
213
214#endif // Y2018_ACTORS_AUTONOMOUS_ACTOR_H_