blob: 50218bb597965458d91c8088ac56d6abe82bd491 [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),
111 ::std::chrono::milliseconds(5) / 2);
112
113 constexpr double kPositionTolerance = 0.02;
114 constexpr double kProfileTolerance = 0.001;
115
116 while (true) {
117 if (ShouldCancel()) {
118 return false;
119 }
120
121 superstructure_queue.status.FetchLatest();
122 drivetrain_queue.status.FetchLatest();
123 if (drivetrain_queue.status.get() && superstructure_queue.status.get()) {
124 const double left_profile_error =
125 (initial_drivetrain_.left -
126 drivetrain_queue.status->profiled_left_position_goal);
127 const double right_profile_error =
128 (initial_drivetrain_.right -
129 drivetrain_queue.status->profiled_right_position_goal);
130
131 const double left_error =
132 (initial_drivetrain_.left -
133 drivetrain_queue.status->estimated_left_position);
134 const double right_error =
135 (initial_drivetrain_.right -
136 drivetrain_queue.status->estimated_right_position);
137
138 const double profile_distance_to_go =
139 (left_profile_error + right_profile_error) / 2.0;
140
141 const double distance_to_go = (left_error + right_error) / 2.0;
142
143 // Check superstructure first.
144 if (superstructure_queue.status->arm.current_node ==
145 arm_goal_position_ &&
146 superstructure_queue.status->arm.path_distance_to_go <
147 arm_threshold) {
148 LOG(INFO, "Arm finished first: %f, drivetrain %f distance\n",
149 superstructure_queue.status->arm.path_distance_to_go,
150 ::std::abs(distance_to_go));
151 return true;
152 }
153
154 // Now check drivetrain.
155 if (::std::abs(profile_distance_to_go) <
156 drive_threshold + kProfileTolerance &&
157 ::std::abs(distance_to_go) < drive_threshold + kPositionTolerance) {
158 LOG(INFO,
159 "Drivetrain finished first: arm %f, drivetrain %f distance\n",
160 superstructure_queue.status->arm.path_distance_to_go,
161 ::std::abs(distance_to_go));
162 return true;
163 }
164 }
165 phased_loop.SleepUntilNext();
166 }
167 }
168
Austin Schuhc231df42018-03-21 20:43:24 -0700169 bool WaitForArmTrajectoryClose(double threshold) {
170 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
171 ::std::chrono::milliseconds(5) / 2);
172 while (true) {
173 if (ShouldCancel()) {
174 return false;
175 }
176
177 superstructure_queue.status.FetchLatest();
178 if (superstructure_queue.status.get()) {
Austin Schuhf79c0e52018-04-04 20:13:21 -0700179 if (superstructure_queue.status->arm.current_node ==
180 arm_goal_position_ &&
Austin Schuhc231df42018-03-21 20:43:24 -0700181 superstructure_queue.status->arm.path_distance_to_go < threshold) {
182 return true;
183 }
184 }
185 phased_loop.SleepUntilNext();
186 }
187 }
Austin Schuhf79c0e52018-04-04 20:13:21 -0700188
189 bool WaitForBoxGrabed() {
190 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
191 ::std::chrono::milliseconds(5) / 2);
192 while (true) {
193 if (ShouldCancel()) {
194 return false;
195 }
196
197 superstructure_queue.status.FetchLatest();
198 if (superstructure_queue.status.get()) {
199 if (superstructure_queue.status->arm.grab_state == 4) {
200 return true;
201 }
202 }
203 phased_loop.SleepUntilNext();
204 }
205 }
Austin Schuha3c148e2018-03-09 21:04:05 -0800206};
207
208} // namespace actors
209} // namespace y2018
210
211#endif // Y2018_ACTORS_AUTONOMOUS_ACTOR_H_