blob: a9c629c6932c0e87f11a17cb1c27173f08df9b99 [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
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080017namespace y2018::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(
Alex Perrycb7da4b2019-08-28 19:35:56 -070024 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
Alex Perrycb7da4b2019-08-28 19:35:56 -070043 ::aos::Sender<::y2018::control_loops::superstructure::Goal>
Austin Schuhd845c972019-06-29 21:20:05 -070044 superstructure_goal_sender_;
Alex Perrycb7da4b2019-08-28 19:35:56 -070045 ::aos::Fetcher<::y2018::control_loops::superstructure::Status>
Austin Schuhd845c972019-06-29 21:20:05 -070046 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() {
Alex Perrycb7da4b2019-08-28 19:35:56 -070085 auto builder = superstructure_goal_sender_.MakeBuilder();
86 control_loops::superstructure::IntakeGoal::Builder intake_goal_builder =
87 builder.MakeBuilder<control_loops::superstructure::IntakeGoal>();
88 intake_goal_builder.add_roller_voltage(roller_voltage_);
89 intake_goal_builder.add_left_intake_angle(left_intake_angle_);
90 intake_goal_builder.add_right_intake_angle(right_intake_angle_);
Austin Schuha3c148e2018-03-09 21:04:05 -080091
Alex Perrycb7da4b2019-08-28 19:35:56 -070092 flatbuffers::Offset<control_loops::superstructure::IntakeGoal>
93 intake_offset = intake_goal_builder.Finish();
Austin Schuha3c148e2018-03-09 21:04:05 -080094
Alex Perrycb7da4b2019-08-28 19:35:56 -070095 control_loops::superstructure::Goal::Builder superstructure_builder =
96 builder.MakeBuilder<control_loops::superstructure::Goal>();
97
98 superstructure_builder.add_intake(intake_offset);
99
100 superstructure_builder.add_arm_goal_position(arm_goal_position_);
101 superstructure_builder.add_grab_box(grab_box_);
102 superstructure_builder.add_open_claw(open_claw_);
103 superstructure_builder.add_close_claw(close_claw_);
104 superstructure_builder.add_deploy_fork(deploy_fork_);
105 superstructure_builder.add_trajectory_override(false);
106
milind1f1dca32021-07-03 13:50:07 -0700107 if (builder.Send(superstructure_builder.Finish()) !=
108 aos::RawSender::Error::kOk) {
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) {
Yash Chainania6fe97b2021-12-15 21:01:11 -0800125 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
Austin Schuhd32b3622019-06-23 18:49:06 -0700126 event_loop()->monotonic_now(),
Stephan Pleines743f83a2024-02-02 18:32:09 -0800127 aos::common::actions::kLoopOffset);
Austin Schuhf79c0e52018-04-04 20:13:21 -0700128
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) {
Yash Chainania6fe97b2021-12-15 21:01:11 -0800187 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
Austin Schuhd32b3622019-06-23 18:49:06 -0700188 event_loop()->monotonic_now(),
Stephan Pleines743f83a2024-02-02 18:32:09 -0800189 aos::common::actions::kLoopOffset);
Austin Schuhc231df42018-03-21 20:43:24 -0700190 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() {
Yash Chainania6fe97b2021-12-15 21:01:11 -0800209 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
Austin Schuhd32b3622019-06-23 18:49:06 -0700210 event_loop()->monotonic_now(),
Stephan Pleines743f83a2024-02-02 18:32:09 -0800211 aos::common::actions::kLoopOffset);
Austin Schuhf79c0e52018-04-04 20:13:21 -0700212 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
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -0800228} // namespace y2018::actors
Austin Schuha3c148e2018-03-09 21:04:05 -0800229
230#endif // Y2018_ACTORS_AUTONOMOUS_ACTOR_H_