blob: 1b2019032cbf5170312b6c431d4a6029d6279144 [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 {
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:
Austin Schuh1bf8a212019-05-26 22:13:14 -070024 explicit AutonomousActor(::aos::EventLoop *event_loop);
Austin Schuha3c148e2018-03-09 21:04:05 -080025
26 bool RunAction(
27 const ::frc971::autonomous::AutonomousActionParams &params) override;
Austin Schuhd845c972019-06-29 21:20:05 -070028
Austin Schuha3c148e2018-03-09 21:04:05 -080029 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
Austin Schuhd845c972019-06-29 21:20:05 -070045 ::aos::Sender<::y2018::control_loops::SuperstructureQueue::Goal>
46 superstructure_goal_sender_;
47 ::aos::Fetcher<::y2018::control_loops::SuperstructureQueue::Status>
48 superstructure_status_fetcher_;
49
Austin Schuha3c148e2018-03-09 21:04:05 -080050 double roller_voltage_ = 0.0;
Austin Schuhcf96d322018-04-07 15:52:31 -070051 double left_intake_angle_ = -3.2;
52 double right_intake_angle_ = -3.2;
Austin Schuha3c148e2018-03-09 21:04:05 -080053 uint32_t arm_goal_position_ = arm::NeutralIndex();
54 bool grab_box_ = false;
55 bool open_claw_ = false;
Austin Schuhf79c0e52018-04-04 20:13:21 -070056 bool close_claw_ = false;
Austin Schuha3c148e2018-03-09 21:04:05 -080057 bool deploy_fork_ = false;
Austin Schuhf79c0e52018-04-04 20:13:21 -070058 bool disable_box_correct_ = false;
Austin Schuha3c148e2018-03-09 21:04:05 -080059
60 void set_roller_voltage(double roller_voltage) {
61 roller_voltage_ = roller_voltage;
62 }
Austin Schuhf79c0e52018-04-04 20:13:21 -070063 void set_intake_angle(double intake_angle) {
64 set_left_intake_angle(intake_angle);
65 set_right_intake_angle(intake_angle);
66 }
Austin Schuha3c148e2018-03-09 21:04:05 -080067 void set_left_intake_angle(double left_intake_angle) {
68 left_intake_angle_ = left_intake_angle;
69 }
70 void set_right_intake_angle(double right_intake_angle) {
71 right_intake_angle_ = right_intake_angle;
72 }
73 void set_arm_goal_position(uint32_t arm_goal_position) {
74 arm_goal_position_ = arm_goal_position;
75 }
76 void set_grab_box(bool grab_box) { grab_box_ = grab_box; }
77 void set_open_claw(bool open_claw) { open_claw_ = open_claw; }
Austin Schuhf79c0e52018-04-04 20:13:21 -070078 void set_close_claw(bool close_claw) { close_claw_ = close_claw; }
Austin Schuha3c148e2018-03-09 21:04:05 -080079 void set_deploy_fork(bool deploy_fork) { deploy_fork_ = deploy_fork; }
80
Austin Schuhf79c0e52018-04-04 20:13:21 -070081 void set_disable_box_correct(bool disable_box_correct) {
82 disable_box_correct_ = disable_box_correct;
83 }
84
Austin Schuha3c148e2018-03-09 21:04:05 -080085 void SendSuperstructureGoal() {
Austin Schuhd845c972019-06-29 21:20:05 -070086 auto new_superstructure_goal = superstructure_goal_sender_.MakeMessage();
Austin Schuha3c148e2018-03-09 21:04:05 -080087 new_superstructure_goal->intake.roller_voltage = roller_voltage_;
88 new_superstructure_goal->intake.left_intake_angle = left_intake_angle_;
89 new_superstructure_goal->intake.right_intake_angle = right_intake_angle_;
90
91 new_superstructure_goal->arm_goal_position = arm_goal_position_;
92 new_superstructure_goal->grab_box = grab_box_;
93 new_superstructure_goal->open_claw = open_claw_;
Austin Schuhf79c0e52018-04-04 20:13:21 -070094 new_superstructure_goal->close_claw = close_claw_;
Austin Schuha3c148e2018-03-09 21:04:05 -080095 new_superstructure_goal->deploy_fork = deploy_fork_;
Austin Schuh4dad8fb2018-07-08 16:00:13 -070096 new_superstructure_goal->trajectory_override = false;
Austin Schuha3c148e2018-03-09 21:04:05 -080097
98 if (!new_superstructure_goal.Send()) {
99 LOG(ERROR, "Sending superstructure goal failed.\n");
100 }
101 }
Austin Schuhc231df42018-03-21 20:43:24 -0700102
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700103 bool ThreeCubeAuto(::aos::monotonic_clock::time_point start_time);
104 bool CloseSwitch(::aos::monotonic_clock::time_point start_time,
105 bool left = true);
106 bool FarSwitch(::aos::monotonic_clock::time_point start_time,
107 bool drive_behind = true, bool left = true);
108 bool FarReadyScale(::aos::monotonic_clock::time_point start_time);
109 bool DriveStraight();
110
111 bool FarScale(::aos::monotonic_clock::time_point start_time);
112
Austin Schuhf79c0e52018-04-04 20:13:21 -0700113 bool WaitForArmTrajectoryOrDriveClose(double drive_threshold,
114 double arm_threshold) {
115 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
Austin Schuhd32b3622019-06-23 18:49:06 -0700116 event_loop()->monotonic_now(),
Austin Schuhf79c0e52018-04-04 20:13:21 -0700117 ::std::chrono::milliseconds(5) / 2);
118
119 constexpr double kPositionTolerance = 0.02;
120 constexpr double kProfileTolerance = 0.001;
121
122 while (true) {
123 if (ShouldCancel()) {
124 return false;
125 }
126
Austin Schuhd845c972019-06-29 21:20:05 -0700127 superstructure_status_fetcher_.Fetch();
Austin Schuhf79c0e52018-04-04 20:13:21 -0700128 drivetrain_queue.status.FetchLatest();
Austin Schuhd845c972019-06-29 21:20:05 -0700129 if (drivetrain_queue.status.get() &&
130 superstructure_status_fetcher_.get()) {
Austin Schuhf79c0e52018-04-04 20:13:21 -0700131 const double left_profile_error =
132 (initial_drivetrain_.left -
133 drivetrain_queue.status->profiled_left_position_goal);
134 const double right_profile_error =
135 (initial_drivetrain_.right -
136 drivetrain_queue.status->profiled_right_position_goal);
137
138 const double left_error =
139 (initial_drivetrain_.left -
140 drivetrain_queue.status->estimated_left_position);
141 const double right_error =
142 (initial_drivetrain_.right -
143 drivetrain_queue.status->estimated_right_position);
144
145 const double profile_distance_to_go =
146 (left_profile_error + right_profile_error) / 2.0;
147
148 const double distance_to_go = (left_error + right_error) / 2.0;
149
150 // Check superstructure first.
Austin Schuhd845c972019-06-29 21:20:05 -0700151 if (superstructure_status_fetcher_->arm.current_node ==
Austin Schuhf79c0e52018-04-04 20:13:21 -0700152 arm_goal_position_ &&
Austin Schuhd845c972019-06-29 21:20:05 -0700153 superstructure_status_fetcher_->arm.path_distance_to_go <
Austin Schuhf79c0e52018-04-04 20:13:21 -0700154 arm_threshold) {
155 LOG(INFO, "Arm finished first: %f, drivetrain %f distance\n",
Austin Schuhd845c972019-06-29 21:20:05 -0700156 superstructure_status_fetcher_->arm.path_distance_to_go,
Austin Schuhf79c0e52018-04-04 20:13:21 -0700157 ::std::abs(distance_to_go));
158 return true;
159 }
160
161 // Now check drivetrain.
162 if (::std::abs(profile_distance_to_go) <
163 drive_threshold + kProfileTolerance &&
164 ::std::abs(distance_to_go) < drive_threshold + kPositionTolerance) {
165 LOG(INFO,
166 "Drivetrain finished first: arm %f, drivetrain %f distance\n",
Austin Schuhd845c972019-06-29 21:20:05 -0700167 superstructure_status_fetcher_->arm.path_distance_to_go,
Austin Schuhf79c0e52018-04-04 20:13:21 -0700168 ::std::abs(distance_to_go));
169 return true;
170 }
171 }
172 phased_loop.SleepUntilNext();
173 }
174 }
175
Austin Schuhc231df42018-03-21 20:43:24 -0700176 bool WaitForArmTrajectoryClose(double threshold) {
177 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
Austin Schuhd32b3622019-06-23 18:49:06 -0700178 event_loop()->monotonic_now(),
Austin Schuhc231df42018-03-21 20:43:24 -0700179 ::std::chrono::milliseconds(5) / 2);
180 while (true) {
181 if (ShouldCancel()) {
182 return false;
183 }
184
Austin Schuhd845c972019-06-29 21:20:05 -0700185 superstructure_status_fetcher_.Fetch();
186 if (superstructure_status_fetcher_.get()) {
187 if (superstructure_status_fetcher_->arm.current_node ==
Austin Schuhf79c0e52018-04-04 20:13:21 -0700188 arm_goal_position_ &&
Austin Schuhd845c972019-06-29 21:20:05 -0700189 superstructure_status_fetcher_->arm.path_distance_to_go <
190 threshold) {
Austin Schuhc231df42018-03-21 20:43:24 -0700191 return true;
192 }
193 }
194 phased_loop.SleepUntilNext();
195 }
196 }
Austin Schuhf79c0e52018-04-04 20:13:21 -0700197
198 bool WaitForBoxGrabed() {
199 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
Austin Schuhd32b3622019-06-23 18:49:06 -0700200 event_loop()->monotonic_now(),
Austin Schuhf79c0e52018-04-04 20:13:21 -0700201 ::std::chrono::milliseconds(5) / 2);
202 while (true) {
203 if (ShouldCancel()) {
204 return false;
205 }
206
Austin Schuhd845c972019-06-29 21:20:05 -0700207 superstructure_status_fetcher_.Fetch();
208 if (superstructure_status_fetcher_.get()) {
209 if (superstructure_status_fetcher_->arm.grab_state == 4) {
Austin Schuhf79c0e52018-04-04 20:13:21 -0700210 return true;
211 }
212 }
213 phased_loop.SleepUntilNext();
214 }
215 }
Austin Schuha3c148e2018-03-09 21:04:05 -0800216};
217
218} // namespace actors
219} // namespace y2018
220
221#endif // Y2018_ACTORS_AUTONOMOUS_ACTOR_H_