blob: 4cedff3fcf9f33d01496dd0d28fa47afa3441ebd [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 Schuha3c148e2018-03-09 21:04:05 -080018
19class AutonomousActor : public ::frc971::autonomous::BaseAutonomousActor {
20 public:
Austin Schuh1bf8a212019-05-26 22:13:14 -070021 explicit AutonomousActor(::aos::EventLoop *event_loop);
Austin Schuha3c148e2018-03-09 21:04:05 -080022
23 bool RunAction(
24 const ::frc971::autonomous::AutonomousActionParams &params) override;
Austin Schuhd845c972019-06-29 21:20:05 -070025
Austin Schuha3c148e2018-03-09 21:04:05 -080026 private:
27 void Reset() {
28 roller_voltage_ = 0.0;
Austin Schuhcf96d322018-04-07 15:52:31 -070029 left_intake_angle_ = -3.2;
30 right_intake_angle_ = -3.2;
Austin Schuhbd0a40f2019-06-30 14:56:31 -070031 arm_goal_position_ =
32 ::y2018::control_loops::superstructure::arm::NeutralIndex();
Austin Schuha3c148e2018-03-09 21:04:05 -080033 grab_box_ = false;
34 open_claw_ = false;
Austin Schuhf79c0e52018-04-04 20:13:21 -070035 close_claw_ = false;
Austin Schuha3c148e2018-03-09 21:04:05 -080036 deploy_fork_ = false;
Austin Schuhf79c0e52018-04-04 20:13:21 -070037 disable_box_correct_ = false;
Austin Schuha3c148e2018-03-09 21:04:05 -080038 InitializeEncoders();
39 ResetDrivetrain();
40 SendSuperstructureGoal();
41 }
42
Austin Schuhd845c972019-06-29 21:20:05 -070043 ::aos::Sender<::y2018::control_loops::SuperstructureQueue::Goal>
44 superstructure_goal_sender_;
45 ::aos::Fetcher<::y2018::control_loops::SuperstructureQueue::Status>
46 superstructure_status_fetcher_;
47
Austin Schuha3c148e2018-03-09 21:04:05 -080048 double roller_voltage_ = 0.0;
Austin Schuhcf96d322018-04-07 15:52:31 -070049 double left_intake_angle_ = -3.2;
50 double right_intake_angle_ = -3.2;
Austin Schuhbd0a40f2019-06-30 14:56:31 -070051 uint32_t arm_goal_position_ =
52 ::y2018::control_loops::superstructure::arm::NeutralIndex();
Austin Schuha3c148e2018-03-09 21:04:05 -080053 bool grab_box_ = false;
54 bool open_claw_ = false;
Austin Schuhf79c0e52018-04-04 20:13:21 -070055 bool close_claw_ = false;
Austin Schuha3c148e2018-03-09 21:04:05 -080056 bool deploy_fork_ = false;
Austin Schuhf79c0e52018-04-04 20:13:21 -070057 bool disable_box_correct_ = false;
Austin Schuha3c148e2018-03-09 21:04:05 -080058
59 void set_roller_voltage(double roller_voltage) {
60 roller_voltage_ = roller_voltage;
61 }
Austin Schuhf79c0e52018-04-04 20:13:21 -070062 void set_intake_angle(double intake_angle) {
63 set_left_intake_angle(intake_angle);
64 set_right_intake_angle(intake_angle);
65 }
Austin Schuha3c148e2018-03-09 21:04:05 -080066 void set_left_intake_angle(double left_intake_angle) {
67 left_intake_angle_ = left_intake_angle;
68 }
69 void set_right_intake_angle(double right_intake_angle) {
70 right_intake_angle_ = right_intake_angle;
71 }
72 void set_arm_goal_position(uint32_t arm_goal_position) {
73 arm_goal_position_ = arm_goal_position;
74 }
75 void set_grab_box(bool grab_box) { grab_box_ = grab_box; }
76 void set_open_claw(bool open_claw) { open_claw_ = open_claw; }
Austin Schuhf79c0e52018-04-04 20:13:21 -070077 void set_close_claw(bool close_claw) { close_claw_ = close_claw; }
Austin Schuha3c148e2018-03-09 21:04:05 -080078 void set_deploy_fork(bool deploy_fork) { deploy_fork_ = deploy_fork; }
79
Austin Schuhf79c0e52018-04-04 20:13:21 -070080 void set_disable_box_correct(bool disable_box_correct) {
81 disable_box_correct_ = disable_box_correct;
82 }
83
Austin Schuha3c148e2018-03-09 21:04:05 -080084 void SendSuperstructureGoal() {
Austin Schuhd845c972019-06-29 21:20:05 -070085 auto new_superstructure_goal = superstructure_goal_sender_.MakeMessage();
Austin Schuha3c148e2018-03-09 21:04:05 -080086 new_superstructure_goal->intake.roller_voltage = roller_voltage_;
87 new_superstructure_goal->intake.left_intake_angle = left_intake_angle_;
88 new_superstructure_goal->intake.right_intake_angle = right_intake_angle_;
89
90 new_superstructure_goal->arm_goal_position = arm_goal_position_;
91 new_superstructure_goal->grab_box = grab_box_;
92 new_superstructure_goal->open_claw = open_claw_;
Austin Schuhf79c0e52018-04-04 20:13:21 -070093 new_superstructure_goal->close_claw = close_claw_;
Austin Schuha3c148e2018-03-09 21:04:05 -080094 new_superstructure_goal->deploy_fork = deploy_fork_;
Austin Schuh4dad8fb2018-07-08 16:00:13 -070095 new_superstructure_goal->trajectory_override = false;
Austin Schuha3c148e2018-03-09 21:04:05 -080096
97 if (!new_superstructure_goal.Send()) {
98 LOG(ERROR, "Sending superstructure goal failed.\n");
99 }
100 }
Austin Schuhc231df42018-03-21 20:43:24 -0700101
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700102 bool ThreeCubeAuto(::aos::monotonic_clock::time_point start_time);
103 bool CloseSwitch(::aos::monotonic_clock::time_point start_time,
104 bool left = true);
105 bool FarSwitch(::aos::monotonic_clock::time_point start_time,
106 bool drive_behind = true, bool left = true);
107 bool FarReadyScale(::aos::monotonic_clock::time_point start_time);
108 bool DriveStraight();
109
110 bool FarScale(::aos::monotonic_clock::time_point start_time);
111
Austin Schuhf79c0e52018-04-04 20:13:21 -0700112 bool WaitForArmTrajectoryOrDriveClose(double drive_threshold,
113 double arm_threshold) {
114 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
Austin Schuhd32b3622019-06-23 18:49:06 -0700115 event_loop()->monotonic_now(),
Austin Schuhf79c0e52018-04-04 20:13:21 -0700116 ::std::chrono::milliseconds(5) / 2);
117
118 constexpr double kPositionTolerance = 0.02;
119 constexpr double kProfileTolerance = 0.001;
120
121 while (true) {
122 if (ShouldCancel()) {
123 return false;
124 }
125
Austin Schuhd845c972019-06-29 21:20:05 -0700126 superstructure_status_fetcher_.Fetch();
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700127 drivetrain_status_fetcher_.Fetch();
128 if (drivetrain_status_fetcher_.get() &&
Austin Schuhd845c972019-06-29 21:20:05 -0700129 superstructure_status_fetcher_.get()) {
Austin Schuhf79c0e52018-04-04 20:13:21 -0700130 const double left_profile_error =
131 (initial_drivetrain_.left -
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700132 drivetrain_status_fetcher_->profiled_left_position_goal);
Austin Schuhf79c0e52018-04-04 20:13:21 -0700133 const double right_profile_error =
134 (initial_drivetrain_.right -
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700135 drivetrain_status_fetcher_->profiled_right_position_goal);
Austin Schuhf79c0e52018-04-04 20:13:21 -0700136
137 const double left_error =
138 (initial_drivetrain_.left -
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700139 drivetrain_status_fetcher_->estimated_left_position);
Austin Schuhf79c0e52018-04-04 20:13:21 -0700140 const double right_error =
141 (initial_drivetrain_.right -
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700142 drivetrain_status_fetcher_->estimated_right_position);
Austin Schuhf79c0e52018-04-04 20:13:21 -0700143
144 const double profile_distance_to_go =
145 (left_profile_error + right_profile_error) / 2.0;
146
147 const double distance_to_go = (left_error + right_error) / 2.0;
148
149 // Check superstructure first.
Austin Schuhd845c972019-06-29 21:20:05 -0700150 if (superstructure_status_fetcher_->arm.current_node ==
Austin Schuhf79c0e52018-04-04 20:13:21 -0700151 arm_goal_position_ &&
Austin Schuhd845c972019-06-29 21:20:05 -0700152 superstructure_status_fetcher_->arm.path_distance_to_go <
Austin Schuhf79c0e52018-04-04 20:13:21 -0700153 arm_threshold) {
154 LOG(INFO, "Arm finished first: %f, drivetrain %f distance\n",
Austin Schuhd845c972019-06-29 21:20:05 -0700155 superstructure_status_fetcher_->arm.path_distance_to_go,
Austin Schuhf79c0e52018-04-04 20:13:21 -0700156 ::std::abs(distance_to_go));
157 return true;
158 }
159
160 // Now check drivetrain.
161 if (::std::abs(profile_distance_to_go) <
162 drive_threshold + kProfileTolerance &&
163 ::std::abs(distance_to_go) < drive_threshold + kPositionTolerance) {
164 LOG(INFO,
165 "Drivetrain finished first: arm %f, drivetrain %f distance\n",
Austin Schuhd845c972019-06-29 21:20:05 -0700166 superstructure_status_fetcher_->arm.path_distance_to_go,
Austin Schuhf79c0e52018-04-04 20:13:21 -0700167 ::std::abs(distance_to_go));
168 return true;
169 }
170 }
171 phased_loop.SleepUntilNext();
172 }
173 }
174
Austin Schuhc231df42018-03-21 20:43:24 -0700175 bool WaitForArmTrajectoryClose(double threshold) {
176 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
Austin Schuhd32b3622019-06-23 18:49:06 -0700177 event_loop()->monotonic_now(),
Austin Schuhc231df42018-03-21 20:43:24 -0700178 ::std::chrono::milliseconds(5) / 2);
179 while (true) {
180 if (ShouldCancel()) {
181 return false;
182 }
183
Austin Schuhd845c972019-06-29 21:20:05 -0700184 superstructure_status_fetcher_.Fetch();
185 if (superstructure_status_fetcher_.get()) {
186 if (superstructure_status_fetcher_->arm.current_node ==
Austin Schuhf79c0e52018-04-04 20:13:21 -0700187 arm_goal_position_ &&
Austin Schuhd845c972019-06-29 21:20:05 -0700188 superstructure_status_fetcher_->arm.path_distance_to_go <
189 threshold) {
Austin Schuhc231df42018-03-21 20:43:24 -0700190 return true;
191 }
192 }
193 phased_loop.SleepUntilNext();
194 }
195 }
Austin Schuhf79c0e52018-04-04 20:13:21 -0700196
197 bool WaitForBoxGrabed() {
198 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
Austin Schuhd32b3622019-06-23 18:49:06 -0700199 event_loop()->monotonic_now(),
Austin Schuhf79c0e52018-04-04 20:13:21 -0700200 ::std::chrono::milliseconds(5) / 2);
201 while (true) {
202 if (ShouldCancel()) {
203 return false;
204 }
205
Austin Schuhd845c972019-06-29 21:20:05 -0700206 superstructure_status_fetcher_.Fetch();
207 if (superstructure_status_fetcher_.get()) {
208 if (superstructure_status_fetcher_->arm.grab_state == 4) {
Austin Schuhf79c0e52018-04-04 20:13:21 -0700209 return true;
210 }
211 }
212 phased_loop.SleepUntilNext();
213 }
214 }
Austin Schuha3c148e2018-03-09 21:04:05 -0800215};
216
217} // namespace actors
218} // namespace y2018
219
220#endif // Y2018_ACTORS_AUTONOMOUS_ACTOR_H_