blob: 9ba59fa81a442cbf982c6a9e0552539469d277a1 [file] [log] [blame]
Austin Schuh13379ba2019-03-12 21:06:46 -07001#ifndef Y2019_ACTORS_AUTONOMOUS_ACTOR_H_
2#define Y2019_ACTORS_AUTONOMOUS_ACTOR_H_
3
4#include <chrono>
5#include <memory>
6
7#include "aos/actions/actions.h"
8#include "aos/actions/actor.h"
9#include "frc971/autonomous/base_autonomous_actor.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070010#include "frc971/control_loops/control_loops_generated.h"
Austin Schuh13379ba2019-03-12 21:06:46 -070011#include "frc971/control_loops/drivetrain/drivetrain_config.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070012#include "frc971/control_loops/drivetrain/localizer_generated.h"
13#include "y2019/control_loops/superstructure/superstructure_goal_generated.h"
14#include "y2019/control_loops/superstructure/superstructure_status_generated.h"
Austin Schuh13379ba2019-03-12 21:06:46 -070015
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080016namespace y2019::actors {
Austin Schuh13379ba2019-03-12 21:06:46 -070017
Alex Perrycb7da4b2019-08-28 19:35:56 -070018using ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal;
19
20namespace superstructure = y2019::control_loops::superstructure;
Austin Schuh13379ba2019-03-12 21:06:46 -070021
Austin Schuhb5b79a52019-05-08 20:32:07 -070022struct ElevatorWristPosition {
23 double elevator;
24 double wrist;
25};
26
Austin Schuh13379ba2019-03-12 21:06:46 -070027class AutonomousActor : public ::frc971::autonomous::BaseAutonomousActor {
28 public:
Austin Schuh1bf8a212019-05-26 22:13:14 -070029 explicit AutonomousActor(::aos::EventLoop *event_loop);
Austin Schuh13379ba2019-03-12 21:06:46 -070030
31 bool RunAction(
Alex Perrycb7da4b2019-08-28 19:35:56 -070032 const ::frc971::autonomous::AutonomousActionParams *params) override;
Austin Schuh13379ba2019-03-12 21:06:46 -070033
34 private:
Austin Schuha9644062019-03-28 14:31:52 -070035 void Reset(bool is_left);
Austin Schuh13379ba2019-03-12 21:06:46 -070036
37 double elevator_goal_ = 0.0;
38 double wrist_goal_ = 0.0;
39 double intake_goal_ = 0.0;
40
41 bool suction_on_ = true;
42 int suction_gamepiece_ = 1;
43
44 double elevator_max_velocity_ = 0.0;
45 double elevator_max_acceleration_ = 0.0;
46 double wrist_max_velocity_ = 0.0;
47 double wrist_max_acceleration_ = 0.0;
48
49 void set_elevator_goal(double elevator_goal) {
50 elevator_goal_ = elevator_goal;
51 }
52 void set_wrist_goal(double wrist_goal) { wrist_goal_ = wrist_goal; }
53 void set_intake_goal(double intake_goal) { intake_goal_ = intake_goal; }
54
55 void set_suction_goal(bool on, int gamepiece_mode) {
56 suction_on_ = on;
57 suction_gamepiece_ = gamepiece_mode;
58 }
59
60 void set_elevator_max_velocity(double elevator_max_velocity) {
61 elevator_max_velocity_ = elevator_max_velocity;
62 }
63 void set_elevator_max_acceleration(double elevator_max_acceleration) {
64 elevator_max_acceleration_ = elevator_max_acceleration;
65 }
66 void set_wrist_max_velocity(double wrist_max_velocity) {
67 wrist_max_velocity_ = wrist_max_velocity;
68 }
69 void set_wrist_max_acceleration(double wrist_max_acceleration) {
70 wrist_max_acceleration_ = wrist_max_acceleration;
71 }
72
Austin Schuhb5b79a52019-05-08 20:32:07 -070073 void set_elevator_wrist_goal(ElevatorWristPosition goal) {
74 set_elevator_goal(goal.elevator);
75 set_wrist_goal(goal.wrist);
76 }
77
Austin Schuh13379ba2019-03-12 21:06:46 -070078 void SendSuperstructureGoal() {
Alex Perrycb7da4b2019-08-28 19:35:56 -070079 auto builder = superstructure_goal_sender_.MakeBuilder();
Austin Schuh13379ba2019-03-12 21:06:46 -070080
Alex Perrycb7da4b2019-08-28 19:35:56 -070081 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
82 elevator_offset;
Austin Schuh13379ba2019-03-12 21:06:46 -070083
Alex Perrycb7da4b2019-08-28 19:35:56 -070084 {
85 frc971::ProfileParameters::Builder profile_params_builder =
86 builder.MakeBuilder<frc971::ProfileParameters>();
87 profile_params_builder.add_max_velocity(elevator_max_velocity_);
88 profile_params_builder.add_max_acceleration(elevator_max_acceleration_);
Austin Schuh13379ba2019-03-12 21:06:46 -070089
Alex Perrycb7da4b2019-08-28 19:35:56 -070090 flatbuffers::Offset<frc971::ProfileParameters> profile_params_offset =
91 profile_params_builder.Finish();
Austin Schuh13379ba2019-03-12 21:06:46 -070092
Alex Perrycb7da4b2019-08-28 19:35:56 -070093 StaticZeroingSingleDOFProfiledSubsystemGoal::Builder elevator_builder =
94 builder.MakeBuilder<StaticZeroingSingleDOFProfiledSubsystemGoal>();
95
96 elevator_builder.add_unsafe_goal(elevator_goal_);
97 elevator_builder.add_profile_params(profile_params_offset);
98
99 elevator_offset = elevator_builder.Finish();
100 }
101
102 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
103 wrist_offset;
104
105 {
106 frc971::ProfileParameters::Builder profile_params_builder =
107 builder.MakeBuilder<frc971::ProfileParameters>();
108 profile_params_builder.add_max_velocity(wrist_max_velocity_);
109 profile_params_builder.add_max_acceleration(wrist_max_acceleration_);
110
111 flatbuffers::Offset<frc971::ProfileParameters> profile_params_offset =
112 profile_params_builder.Finish();
113
114 StaticZeroingSingleDOFProfiledSubsystemGoal::Builder wrist_builder =
115 builder.MakeBuilder<StaticZeroingSingleDOFProfiledSubsystemGoal>();
116
117 wrist_builder.add_unsafe_goal(wrist_goal_);
118 wrist_builder.add_profile_params(profile_params_offset);
119
120 wrist_offset = wrist_builder.Finish();
121 }
122
123 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
124 intake_offset;
125
126 {
127 StaticZeroingSingleDOFProfiledSubsystemGoal::Builder intake_builder =
128 builder.MakeBuilder<StaticZeroingSingleDOFProfiledSubsystemGoal>();
129
130 intake_builder.add_unsafe_goal(intake_goal_);
131
132 intake_offset = intake_builder.Finish();
133 }
134
135 flatbuffers::Offset<superstructure::SuctionGoal> suction_offset;
136
137 {
138 superstructure::SuctionGoal::Builder suction_builder =
139 builder.MakeBuilder<superstructure::SuctionGoal>();
140
141 suction_builder.add_grab_piece(suction_on_);
142 suction_builder.add_gamepiece_mode(suction_gamepiece_);
143
144 suction_offset = suction_builder.Finish();
145 }
146
147 superstructure::Goal::Builder superstructure_builder =
148 builder.MakeBuilder<superstructure::Goal>();
149
150 superstructure_builder.add_elevator(elevator_offset);
151 superstructure_builder.add_wrist(wrist_offset);
152 superstructure_builder.add_intake(intake_offset);
153 superstructure_builder.add_suction(suction_offset);
154
milind1f1dca32021-07-03 13:50:07 -0700155 if (builder.Send(superstructure_builder.Finish()) !=
156 aos::RawSender::Error::kOk) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700157 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
Austin Schuh13379ba2019-03-12 21:06:46 -0700158 }
159 }
160
161 bool IsSucked() {
Austin Schuh170f4952019-06-29 18:58:30 -0700162 superstructure_status_fetcher_.Fetch();
Austin Schuh13379ba2019-03-12 21:06:46 -0700163
Austin Schuh170f4952019-06-29 18:58:30 -0700164 if (superstructure_status_fetcher_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700165 return superstructure_status_fetcher_->has_piece();
Austin Schuh13379ba2019-03-12 21:06:46 -0700166 }
167 return false;
168 }
169
170 bool WaitForGamePiece() {
Yash Chainania6fe97b2021-12-15 21:01:11 -0800171 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
Austin Schuhd32b3622019-06-23 18:49:06 -0700172 event_loop()->monotonic_now(),
Stephan Pleines743f83a2024-02-02 18:32:09 -0800173 aos::common::actions::kLoopOffset);
Austin Schuh13379ba2019-03-12 21:06:46 -0700174
175 while (true) {
176 if (ShouldCancel()) {
177 return false;
178 }
179 phased_loop.SleepUntilNext();
180 if (IsSucked()) {
181 return true;
182 }
183 }
184 }
185
Austin Schuhb5b79a52019-05-08 20:32:07 -0700186 bool WaitForMilliseconds(std::chrono::milliseconds wait) {
Austin Schuh77ed5432019-07-07 20:41:36 -0700187 ::aos::monotonic_clock::time_point end_time = monotonic_now() + wait;
Austin Schuhb5b79a52019-05-08 20:32:07 -0700188
Austin Schuh77ed5432019-07-07 20:41:36 -0700189 while (monotonic_now() < end_time) {
Austin Schuhb5b79a52019-05-08 20:32:07 -0700190 if (ShouldCancel()) {
191 return false;
192 }
193 // TODO(james): Allow non-multiples of 5.
Yash Chainania6fe97b2021-12-15 21:01:11 -0800194 ::std::this_thread::sleep_for(frc971::controls::kLoopFrequency);
Austin Schuhb5b79a52019-05-08 20:32:07 -0700195 }
196 return true;
197 }
198
Austin Schuh13379ba2019-03-12 21:06:46 -0700199 bool IsSuperstructureDone() {
Austin Schuh170f4952019-06-29 18:58:30 -0700200 superstructure_status_fetcher_.Fetch();
Austin Schuh13379ba2019-03-12 21:06:46 -0700201
202 double kElevatorTolerance = 0.01;
203 double kWristTolerance = 0.05;
204
Austin Schuh170f4952019-06-29 18:58:30 -0700205 if (superstructure_status_fetcher_.get()) {
Austin Schuh13379ba2019-03-12 21:06:46 -0700206 const bool elevator_at_goal =
207 ::std::abs(elevator_goal_ -
Alex Perrycb7da4b2019-08-28 19:35:56 -0700208 superstructure_status_fetcher_->elevator()->position()) <
Austin Schuh13379ba2019-03-12 21:06:46 -0700209 kElevatorTolerance;
210
211 const bool wrist_at_goal =
212 ::std::abs(wrist_goal_ -
Alex Perrycb7da4b2019-08-28 19:35:56 -0700213 superstructure_status_fetcher_->wrist()->position()) <
Austin Schuh13379ba2019-03-12 21:06:46 -0700214 kWristTolerance;
215
216 return elevator_at_goal && wrist_at_goal;
217 }
218 return false;
219 }
220
221 bool WaitForSuperstructureDone() {
Yash Chainania6fe97b2021-12-15 21:01:11 -0800222 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
Austin Schuhd32b3622019-06-23 18:49:06 -0700223 event_loop()->monotonic_now(),
Stephan Pleines743f83a2024-02-02 18:32:09 -0800224 aos::common::actions::kLoopOffset);
Austin Schuh13379ba2019-03-12 21:06:46 -0700225
226 while (true) {
227 if (ShouldCancel()) {
228 return false;
229 }
230 phased_loop.SleepUntilNext();
Austin Schuh170f4952019-06-29 18:58:30 -0700231 superstructure_status_fetcher_.Fetch();
Austin Schuh13379ba2019-03-12 21:06:46 -0700232 if (IsSuperstructureDone()) {
233 return true;
234 }
235 }
236 }
Austin Schuhb5b79a52019-05-08 20:32:07 -0700237
238 // Waits until the robot's x > x.
239 bool WaitForDriveXGreater(double x);
240
241 // Waits until y is within y of zero.
242 bool WaitForDriveYCloseToZero(double y);
Austin Schuheb99d072019-05-12 21:03:38 -0700243
244 ::aos::Sender<::frc971::control_loops::drivetrain::LocalizerControl>
245 localizer_control_sender_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700246 ::aos::Sender<::y2019::control_loops::superstructure::Goal>
Austin Schuh170f4952019-06-29 18:58:30 -0700247 superstructure_goal_sender_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700248 ::aos::Fetcher<::y2019::control_loops::superstructure::Status>
Austin Schuh170f4952019-06-29 18:58:30 -0700249 superstructure_status_fetcher_;
Austin Schuh13379ba2019-03-12 21:06:46 -0700250};
251
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -0800252} // namespace y2019::actors
Austin Schuh13379ba2019-03-12 21:06:46 -0700253
254#endif // Y2019_ACTORS_AUTONOMOUS_ACTOR_H_