blob: 3e6be9f6a8de3965be19f12c18086d6556b61c95 [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
milind1f1dca32021-07-03 13:50:07 -0700108 if (builder.Send(superstructure_builder.Finish()) !=
109 aos::RawSender::Error::kOk) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700110 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
Austin Schuha3c148e2018-03-09 21:04:05 -0800111 }
112 }
Austin Schuhc231df42018-03-21 20:43:24 -0700113
Austin Schuh4dad8fb2018-07-08 16:00:13 -0700114 bool ThreeCubeAuto(::aos::monotonic_clock::time_point start_time);
115 bool CloseSwitch(::aos::monotonic_clock::time_point start_time,
116 bool left = true);
117 bool FarSwitch(::aos::monotonic_clock::time_point start_time,
118 bool drive_behind = true, bool left = true);
119 bool FarReadyScale(::aos::monotonic_clock::time_point start_time);
120 bool DriveStraight();
121
122 bool FarScale(::aos::monotonic_clock::time_point start_time);
123
Austin Schuhf79c0e52018-04-04 20:13:21 -0700124 bool WaitForArmTrajectoryOrDriveClose(double drive_threshold,
125 double arm_threshold) {
Yash Chainania6fe97b2021-12-15 21:01:11 -0800126 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
Austin Schuhd32b3622019-06-23 18:49:06 -0700127 event_loop()->monotonic_now(),
Yash Chainania6fe97b2021-12-15 21:01:11 -0800128 ActorBase::kLoopOffset);
Austin Schuhf79c0e52018-04-04 20:13:21 -0700129
130 constexpr double kPositionTolerance = 0.02;
131 constexpr double kProfileTolerance = 0.001;
132
133 while (true) {
134 if (ShouldCancel()) {
135 return false;
136 }
137
Austin Schuhd845c972019-06-29 21:20:05 -0700138 superstructure_status_fetcher_.Fetch();
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700139 drivetrain_status_fetcher_.Fetch();
140 if (drivetrain_status_fetcher_.get() &&
Austin Schuhd845c972019-06-29 21:20:05 -0700141 superstructure_status_fetcher_.get()) {
Austin Schuhf79c0e52018-04-04 20:13:21 -0700142 const double left_profile_error =
143 (initial_drivetrain_.left -
Alex Perrycb7da4b2019-08-28 19:35:56 -0700144 drivetrain_status_fetcher_->profiled_left_position_goal());
Austin Schuhf79c0e52018-04-04 20:13:21 -0700145 const double right_profile_error =
146 (initial_drivetrain_.right -
Alex Perrycb7da4b2019-08-28 19:35:56 -0700147 drivetrain_status_fetcher_->profiled_right_position_goal());
Austin Schuhf79c0e52018-04-04 20:13:21 -0700148
149 const double left_error =
150 (initial_drivetrain_.left -
Alex Perrycb7da4b2019-08-28 19:35:56 -0700151 drivetrain_status_fetcher_->estimated_left_position());
Austin Schuhf79c0e52018-04-04 20:13:21 -0700152 const double right_error =
153 (initial_drivetrain_.right -
Alex Perrycb7da4b2019-08-28 19:35:56 -0700154 drivetrain_status_fetcher_->estimated_right_position());
Austin Schuhf79c0e52018-04-04 20:13:21 -0700155
156 const double profile_distance_to_go =
157 (left_profile_error + right_profile_error) / 2.0;
158
159 const double distance_to_go = (left_error + right_error) / 2.0;
160
161 // Check superstructure first.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700162 if (superstructure_status_fetcher_->arm()->current_node() ==
Austin Schuhf79c0e52018-04-04 20:13:21 -0700163 arm_goal_position_ &&
Alex Perrycb7da4b2019-08-28 19:35:56 -0700164 superstructure_status_fetcher_->arm()->path_distance_to_go() <
Austin Schuhf79c0e52018-04-04 20:13:21 -0700165 arm_threshold) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700166 AOS_LOG(INFO, "Arm finished first: %f, drivetrain %f distance\n",
Alex Perrycb7da4b2019-08-28 19:35:56 -0700167 superstructure_status_fetcher_->arm()->path_distance_to_go(),
Austin Schuhf257f3c2019-10-27 21:00:43 -0700168 ::std::abs(distance_to_go));
Austin Schuhf79c0e52018-04-04 20:13:21 -0700169 return true;
170 }
171
172 // Now check drivetrain.
173 if (::std::abs(profile_distance_to_go) <
174 drive_threshold + kProfileTolerance &&
175 ::std::abs(distance_to_go) < drive_threshold + kPositionTolerance) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700176 AOS_LOG(INFO,
177 "Drivetrain finished first: arm %f, drivetrain %f distance\n",
Alex Perrycb7da4b2019-08-28 19:35:56 -0700178 superstructure_status_fetcher_->arm()->path_distance_to_go(),
Austin Schuhf257f3c2019-10-27 21:00:43 -0700179 ::std::abs(distance_to_go));
Austin Schuhf79c0e52018-04-04 20:13:21 -0700180 return true;
181 }
182 }
183 phased_loop.SleepUntilNext();
184 }
185 }
186
Austin Schuhc231df42018-03-21 20:43:24 -0700187 bool WaitForArmTrajectoryClose(double threshold) {
Yash Chainania6fe97b2021-12-15 21:01:11 -0800188 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
Austin Schuhd32b3622019-06-23 18:49:06 -0700189 event_loop()->monotonic_now(),
Yash Chainania6fe97b2021-12-15 21:01:11 -0800190 ActorBase::kLoopOffset);
Austin Schuhc231df42018-03-21 20:43:24 -0700191 while (true) {
192 if (ShouldCancel()) {
193 return false;
194 }
195
Austin Schuhd845c972019-06-29 21:20:05 -0700196 superstructure_status_fetcher_.Fetch();
197 if (superstructure_status_fetcher_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700198 if (superstructure_status_fetcher_->arm()->current_node() ==
Austin Schuhf79c0e52018-04-04 20:13:21 -0700199 arm_goal_position_ &&
Alex Perrycb7da4b2019-08-28 19:35:56 -0700200 superstructure_status_fetcher_->arm()->path_distance_to_go() <
Austin Schuhd845c972019-06-29 21:20:05 -0700201 threshold) {
Austin Schuhc231df42018-03-21 20:43:24 -0700202 return true;
203 }
204 }
205 phased_loop.SleepUntilNext();
206 }
207 }
Austin Schuhf79c0e52018-04-04 20:13:21 -0700208
209 bool WaitForBoxGrabed() {
Yash Chainania6fe97b2021-12-15 21:01:11 -0800210 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
Austin Schuhd32b3622019-06-23 18:49:06 -0700211 event_loop()->monotonic_now(),
Yash Chainania6fe97b2021-12-15 21:01:11 -0800212 ActorBase::kLoopOffset);
Austin Schuhf79c0e52018-04-04 20:13:21 -0700213 while (true) {
214 if (ShouldCancel()) {
215 return false;
216 }
217
Austin Schuhd845c972019-06-29 21:20:05 -0700218 superstructure_status_fetcher_.Fetch();
219 if (superstructure_status_fetcher_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700220 if (superstructure_status_fetcher_->arm()->grab_state() == 4) {
Austin Schuhf79c0e52018-04-04 20:13:21 -0700221 return true;
222 }
223 }
224 phased_loop.SleepUntilNext();
225 }
226 }
Austin Schuha3c148e2018-03-09 21:04:05 -0800227};
228
229} // namespace actors
230} // namespace y2018
231
232#endif // Y2018_ACTORS_AUTONOMOUS_ACTOR_H_