blob: 014459a92e9a68f02caa0d45c25723d7dd79c68a [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"
Alex Perrycb7da4b2019-08-28 19:35:56 -07009#include "aos/events/event_loop.h"
Austin Schuha3c148e2018-03-09 21:04:05 -080010#include "frc971/autonomous/base_autonomous_actor.h"
Austin Schuha3c148e2018-03-09 21:04:05 -080011#include "frc971/control_loops/drivetrain/drivetrain_config.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070012#include "frc971/control_loops/drivetrain/drivetrain_status_generated.h"
Austin Schuha3c148e2018-03-09 21:04:05 -080013#include "y2018/control_loops/superstructure/arm/generated_graph.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070014#include "y2018/control_loops/superstructure/superstructure_goal_generated.h"
15#include "y2018/control_loops/superstructure/superstructure_status_generated.h"
Austin Schuha3c148e2018-03-09 21:04:05 -080016
17namespace y2018 {
18namespace actors {
Austin Schuha3c148e2018-03-09 21:04:05 -080019
20class AutonomousActor : public ::frc971::autonomous::BaseAutonomousActor {
21 public:
Austin Schuh1bf8a212019-05-26 22:13:14 -070022 explicit AutonomousActor(::aos::EventLoop *event_loop);
Austin Schuha3c148e2018-03-09 21:04:05 -080023
24 bool RunAction(
Alex Perrycb7da4b2019-08-28 19:35:56 -070025 const ::frc971::autonomous::AutonomousActionParams *params) override;
Austin Schuhd845c972019-06-29 21:20:05 -070026
Austin Schuha3c148e2018-03-09 21:04:05 -080027 private:
28 void Reset() {
29 roller_voltage_ = 0.0;
Austin Schuhcf96d322018-04-07 15:52:31 -070030 left_intake_angle_ = -3.2;
31 right_intake_angle_ = -3.2;
Austin Schuhbd0a40f2019-06-30 14:56:31 -070032 arm_goal_position_ =
33 ::y2018::control_loops::superstructure::arm::NeutralIndex();
Austin Schuha3c148e2018-03-09 21:04:05 -080034 grab_box_ = false;
35 open_claw_ = false;
Austin Schuhf79c0e52018-04-04 20:13:21 -070036 close_claw_ = false;
Austin Schuha3c148e2018-03-09 21:04:05 -080037 deploy_fork_ = false;
Austin Schuhf79c0e52018-04-04 20:13:21 -070038 disable_box_correct_ = false;
Austin Schuha3c148e2018-03-09 21:04:05 -080039 InitializeEncoders();
40 ResetDrivetrain();
41 SendSuperstructureGoal();
42 }
43
Alex Perrycb7da4b2019-08-28 19:35:56 -070044 ::aos::Sender<::y2018::control_loops::superstructure::Goal>
Austin Schuhd845c972019-06-29 21:20:05 -070045 superstructure_goal_sender_;
Alex Perrycb7da4b2019-08-28 19:35:56 -070046 ::aos::Fetcher<::y2018::control_loops::superstructure::Status>
Austin Schuhd845c972019-06-29 21:20:05 -070047 superstructure_status_fetcher_;
48
Austin Schuha3c148e2018-03-09 21:04:05 -080049 double roller_voltage_ = 0.0;
Austin Schuhcf96d322018-04-07 15:52:31 -070050 double left_intake_angle_ = -3.2;
51 double right_intake_angle_ = -3.2;
Austin Schuhbd0a40f2019-06-30 14:56:31 -070052 uint32_t arm_goal_position_ =
53 ::y2018::control_loops::superstructure::arm::NeutralIndex();
Austin Schuha3c148e2018-03-09 21:04:05 -080054 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() {
Alex Perrycb7da4b2019-08-28 19:35:56 -070086 auto builder = superstructure_goal_sender_.MakeBuilder();
87 control_loops::superstructure::IntakeGoal::Builder intake_goal_builder =
88 builder.MakeBuilder<control_loops::superstructure::IntakeGoal>();
89 intake_goal_builder.add_roller_voltage(roller_voltage_);
90 intake_goal_builder.add_left_intake_angle(left_intake_angle_);
91 intake_goal_builder.add_right_intake_angle(right_intake_angle_);
Austin Schuha3c148e2018-03-09 21:04:05 -080092
Alex Perrycb7da4b2019-08-28 19:35:56 -070093 flatbuffers::Offset<control_loops::superstructure::IntakeGoal>
94 intake_offset = intake_goal_builder.Finish();
Austin Schuha3c148e2018-03-09 21:04:05 -080095
Alex Perrycb7da4b2019-08-28 19:35:56 -070096 control_loops::superstructure::Goal::Builder superstructure_builder =
97 builder.MakeBuilder<control_loops::superstructure::Goal>();
98
99 superstructure_builder.add_intake(intake_offset);
100
101 superstructure_builder.add_arm_goal_position(arm_goal_position_);
102 superstructure_builder.add_grab_box(grab_box_);
103 superstructure_builder.add_open_claw(open_claw_);
104 superstructure_builder.add_close_claw(close_claw_);
105 superstructure_builder.add_deploy_fork(deploy_fork_);
106 superstructure_builder.add_trajectory_override(false);
107
108 if (!builder.Send(superstructure_builder.Finish())) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700109 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
Austin Schuha3c148e2018-03-09 21:04:05 -0800110 }
111 }
Austin Schuhc231df42018-03-21 20:43:24 -0700112
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700113 bool ThreeCubeAuto(::aos::monotonic_clock::time_point start_time);
114 bool CloseSwitch(::aos::monotonic_clock::time_point start_time,
115 bool left = true);
116 bool FarSwitch(::aos::monotonic_clock::time_point start_time,
117 bool drive_behind = true, bool left = true);
118 bool FarReadyScale(::aos::monotonic_clock::time_point start_time);
119 bool DriveStraight();
120
121 bool FarScale(::aos::monotonic_clock::time_point start_time);
122
Austin Schuhf79c0e52018-04-04 20:13:21 -0700123 bool WaitForArmTrajectoryOrDriveClose(double drive_threshold,
124 double arm_threshold) {
125 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
Austin Schuhd32b3622019-06-23 18:49:06 -0700126 event_loop()->monotonic_now(),
Austin Schuhf79c0e52018-04-04 20:13:21 -0700127 ::std::chrono::milliseconds(5) / 2);
128
129 constexpr double kPositionTolerance = 0.02;
130 constexpr double kProfileTolerance = 0.001;
131
132 while (true) {
133 if (ShouldCancel()) {
134 return false;
135 }
136
Austin Schuhd845c972019-06-29 21:20:05 -0700137 superstructure_status_fetcher_.Fetch();
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700138 drivetrain_status_fetcher_.Fetch();
139 if (drivetrain_status_fetcher_.get() &&
Austin Schuhd845c972019-06-29 21:20:05 -0700140 superstructure_status_fetcher_.get()) {
Austin Schuhf79c0e52018-04-04 20:13:21 -0700141 const double left_profile_error =
142 (initial_drivetrain_.left -
Alex Perrycb7da4b2019-08-28 19:35:56 -0700143 drivetrain_status_fetcher_->profiled_left_position_goal());
Austin Schuhf79c0e52018-04-04 20:13:21 -0700144 const double right_profile_error =
145 (initial_drivetrain_.right -
Alex Perrycb7da4b2019-08-28 19:35:56 -0700146 drivetrain_status_fetcher_->profiled_right_position_goal());
Austin Schuhf79c0e52018-04-04 20:13:21 -0700147
148 const double left_error =
149 (initial_drivetrain_.left -
Alex Perrycb7da4b2019-08-28 19:35:56 -0700150 drivetrain_status_fetcher_->estimated_left_position());
Austin Schuhf79c0e52018-04-04 20:13:21 -0700151 const double right_error =
152 (initial_drivetrain_.right -
Alex Perrycb7da4b2019-08-28 19:35:56 -0700153 drivetrain_status_fetcher_->estimated_right_position());
Austin Schuhf79c0e52018-04-04 20:13:21 -0700154
155 const double profile_distance_to_go =
156 (left_profile_error + right_profile_error) / 2.0;
157
158 const double distance_to_go = (left_error + right_error) / 2.0;
159
160 // Check superstructure first.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700161 if (superstructure_status_fetcher_->arm()->current_node() ==
Austin Schuhf79c0e52018-04-04 20:13:21 -0700162 arm_goal_position_ &&
Alex Perrycb7da4b2019-08-28 19:35:56 -0700163 superstructure_status_fetcher_->arm()->path_distance_to_go() <
Austin Schuhf79c0e52018-04-04 20:13:21 -0700164 arm_threshold) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700165 AOS_LOG(INFO, "Arm finished first: %f, drivetrain %f distance\n",
Alex Perrycb7da4b2019-08-28 19:35:56 -0700166 superstructure_status_fetcher_->arm()->path_distance_to_go(),
Austin Schuhf257f3c2019-10-27 21:00:43 -0700167 ::std::abs(distance_to_go));
Austin Schuhf79c0e52018-04-04 20:13:21 -0700168 return true;
169 }
170
171 // Now check drivetrain.
172 if (::std::abs(profile_distance_to_go) <
173 drive_threshold + kProfileTolerance &&
174 ::std::abs(distance_to_go) < drive_threshold + kPositionTolerance) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700175 AOS_LOG(INFO,
176 "Drivetrain finished first: arm %f, drivetrain %f distance\n",
Alex Perrycb7da4b2019-08-28 19:35:56 -0700177 superstructure_status_fetcher_->arm()->path_distance_to_go(),
Austin Schuhf257f3c2019-10-27 21:00:43 -0700178 ::std::abs(distance_to_go));
Austin Schuhf79c0e52018-04-04 20:13:21 -0700179 return true;
180 }
181 }
182 phased_loop.SleepUntilNext();
183 }
184 }
185
Austin Schuhc231df42018-03-21 20:43:24 -0700186 bool WaitForArmTrajectoryClose(double threshold) {
187 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
Austin Schuhd32b3622019-06-23 18:49:06 -0700188 event_loop()->monotonic_now(),
Austin Schuhc231df42018-03-21 20:43:24 -0700189 ::std::chrono::milliseconds(5) / 2);
190 while (true) {
191 if (ShouldCancel()) {
192 return false;
193 }
194
Austin Schuhd845c972019-06-29 21:20:05 -0700195 superstructure_status_fetcher_.Fetch();
196 if (superstructure_status_fetcher_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700197 if (superstructure_status_fetcher_->arm()->current_node() ==
Austin Schuhf79c0e52018-04-04 20:13:21 -0700198 arm_goal_position_ &&
Alex Perrycb7da4b2019-08-28 19:35:56 -0700199 superstructure_status_fetcher_->arm()->path_distance_to_go() <
Austin Schuhd845c972019-06-29 21:20:05 -0700200 threshold) {
Austin Schuhc231df42018-03-21 20:43:24 -0700201 return true;
202 }
203 }
204 phased_loop.SleepUntilNext();
205 }
206 }
Austin Schuhf79c0e52018-04-04 20:13:21 -0700207
208 bool WaitForBoxGrabed() {
209 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
Austin Schuhd32b3622019-06-23 18:49:06 -0700210 event_loop()->monotonic_now(),
Austin Schuhf79c0e52018-04-04 20:13:21 -0700211 ::std::chrono::milliseconds(5) / 2);
212 while (true) {
213 if (ShouldCancel()) {
214 return false;
215 }
216
Austin Schuhd845c972019-06-29 21:20:05 -0700217 superstructure_status_fetcher_.Fetch();
218 if (superstructure_status_fetcher_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700219 if (superstructure_status_fetcher_->arm()->grab_state() == 4) {
Austin Schuhf79c0e52018-04-04 20:13:21 -0700220 return true;
221 }
222 }
223 phased_loop.SleepUntilNext();
224 }
225 }
Austin Schuha3c148e2018-03-09 21:04:05 -0800226};
227
228} // namespace actors
229} // namespace y2018
230
231#endif // Y2018_ACTORS_AUTONOMOUS_ACTOR_H_