blob: 207ff42794b76aed2d83c31fc87161525379f1f5 [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 Schuheb99d072019-05-12 21:03:38 -070025 explicit AutonomousActor(::aos::EventLoop *event_loop,
26 ::frc971::autonomous::AutonomousActionQueueGroup *s);
Austin Schuha3c148e2018-03-09 21:04:05 -080027
28 bool RunAction(
29 const ::frc971::autonomous::AutonomousActionParams &params) override;
30 private:
31 void Reset() {
32 roller_voltage_ = 0.0;
Austin Schuhcf96d322018-04-07 15:52:31 -070033 left_intake_angle_ = -3.2;
34 right_intake_angle_ = -3.2;
Austin Schuha3c148e2018-03-09 21:04:05 -080035 arm_goal_position_ = arm::NeutralIndex();
36 grab_box_ = false;
37 open_claw_ = false;
Austin Schuhf79c0e52018-04-04 20:13:21 -070038 close_claw_ = false;
Austin Schuha3c148e2018-03-09 21:04:05 -080039 deploy_fork_ = false;
Austin Schuhf79c0e52018-04-04 20:13:21 -070040 disable_box_correct_ = false;
Austin Schuha3c148e2018-03-09 21:04:05 -080041 InitializeEncoders();
42 ResetDrivetrain();
43 SendSuperstructureGoal();
44 }
45
46 double roller_voltage_ = 0.0;
Austin Schuhcf96d322018-04-07 15:52:31 -070047 double left_intake_angle_ = -3.2;
48 double right_intake_angle_ = -3.2;
Austin Schuha3c148e2018-03-09 21:04:05 -080049 uint32_t arm_goal_position_ = arm::NeutralIndex();
50 bool grab_box_ = false;
51 bool open_claw_ = false;
Austin Schuhf79c0e52018-04-04 20:13:21 -070052 bool close_claw_ = false;
Austin Schuha3c148e2018-03-09 21:04:05 -080053 bool deploy_fork_ = false;
Austin Schuhf79c0e52018-04-04 20:13:21 -070054 bool disable_box_correct_ = false;
Austin Schuha3c148e2018-03-09 21:04:05 -080055
56 void set_roller_voltage(double roller_voltage) {
57 roller_voltage_ = roller_voltage;
58 }
Austin Schuhf79c0e52018-04-04 20:13:21 -070059 void set_intake_angle(double intake_angle) {
60 set_left_intake_angle(intake_angle);
61 set_right_intake_angle(intake_angle);
62 }
Austin Schuha3c148e2018-03-09 21:04:05 -080063 void set_left_intake_angle(double left_intake_angle) {
64 left_intake_angle_ = left_intake_angle;
65 }
66 void set_right_intake_angle(double right_intake_angle) {
67 right_intake_angle_ = right_intake_angle;
68 }
69 void set_arm_goal_position(uint32_t arm_goal_position) {
70 arm_goal_position_ = arm_goal_position;
71 }
72 void set_grab_box(bool grab_box) { grab_box_ = grab_box; }
73 void set_open_claw(bool open_claw) { open_claw_ = open_claw; }
Austin Schuhf79c0e52018-04-04 20:13:21 -070074 void set_close_claw(bool close_claw) { close_claw_ = close_claw; }
Austin Schuha3c148e2018-03-09 21:04:05 -080075 void set_deploy_fork(bool deploy_fork) { deploy_fork_ = deploy_fork; }
76
Austin Schuhf79c0e52018-04-04 20:13:21 -070077 void set_disable_box_correct(bool disable_box_correct) {
78 disable_box_correct_ = disable_box_correct;
79 }
80
Austin Schuha3c148e2018-03-09 21:04:05 -080081 void SendSuperstructureGoal() {
82 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
83 new_superstructure_goal->intake.roller_voltage = roller_voltage_;
84 new_superstructure_goal->intake.left_intake_angle = left_intake_angle_;
85 new_superstructure_goal->intake.right_intake_angle = right_intake_angle_;
86
87 new_superstructure_goal->arm_goal_position = arm_goal_position_;
88 new_superstructure_goal->grab_box = grab_box_;
89 new_superstructure_goal->open_claw = open_claw_;
Austin Schuhf79c0e52018-04-04 20:13:21 -070090 new_superstructure_goal->close_claw = close_claw_;
Austin Schuha3c148e2018-03-09 21:04:05 -080091 new_superstructure_goal->deploy_fork = deploy_fork_;
Austin Schuh4dad8fb2018-07-08 16:00:13 -070092 new_superstructure_goal->trajectory_override = false;
Austin Schuha3c148e2018-03-09 21:04:05 -080093
94 if (!new_superstructure_goal.Send()) {
95 LOG(ERROR, "Sending superstructure goal failed.\n");
96 }
97 }
Austin Schuhc231df42018-03-21 20:43:24 -070098
Austin Schuh4dad8fb2018-07-08 16:00:13 -070099 bool ThreeCubeAuto(::aos::monotonic_clock::time_point start_time);
100 bool CloseSwitch(::aos::monotonic_clock::time_point start_time,
101 bool left = true);
102 bool FarSwitch(::aos::monotonic_clock::time_point start_time,
103 bool drive_behind = true, bool left = true);
104 bool FarReadyScale(::aos::monotonic_clock::time_point start_time);
105 bool DriveStraight();
106
107 bool FarScale(::aos::monotonic_clock::time_point start_time);
108
Austin Schuhf79c0e52018-04-04 20:13:21 -0700109 bool WaitForArmTrajectoryOrDriveClose(double drive_threshold,
110 double arm_threshold) {
111 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
112 ::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),
172 ::std::chrono::milliseconds(5) / 2);
173 while (true) {
174 if (ShouldCancel()) {
175 return false;
176 }
177
178 superstructure_queue.status.FetchLatest();
179 if (superstructure_queue.status.get()) {
Austin Schuhf79c0e52018-04-04 20:13:21 -0700180 if (superstructure_queue.status->arm.current_node ==
181 arm_goal_position_ &&
Austin Schuhc231df42018-03-21 20:43:24 -0700182 superstructure_queue.status->arm.path_distance_to_go < threshold) {
183 return true;
184 }
185 }
186 phased_loop.SleepUntilNext();
187 }
188 }
Austin Schuhf79c0e52018-04-04 20:13:21 -0700189
190 bool WaitForBoxGrabed() {
191 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
192 ::std::chrono::milliseconds(5) / 2);
193 while (true) {
194 if (ShouldCancel()) {
195 return false;
196 }
197
198 superstructure_queue.status.FetchLatest();
199 if (superstructure_queue.status.get()) {
200 if (superstructure_queue.status->arm.grab_state == 4) {
201 return true;
202 }
203 }
204 phased_loop.SleepUntilNext();
205 }
206 }
Austin Schuha3c148e2018-03-09 21:04:05 -0800207};
208
209} // namespace actors
210} // namespace y2018
211
212#endif // Y2018_ACTORS_AUTONOMOUS_ACTOR_H_