blob: af2a60f80f3ffc6d27f88e93bbe26f3a827f0582 [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
16namespace y2019 {
17namespace actors {
18
Alex Perrycb7da4b2019-08-28 19:35:56 -070019using ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal;
20
21namespace superstructure = y2019::control_loops::superstructure;
Austin Schuh13379ba2019-03-12 21:06:46 -070022
Austin Schuhb5b79a52019-05-08 20:32:07 -070023struct ElevatorWristPosition {
24 double elevator;
25 double wrist;
26};
27
Austin Schuh13379ba2019-03-12 21:06:46 -070028class AutonomousActor : public ::frc971::autonomous::BaseAutonomousActor {
29 public:
Austin Schuh1bf8a212019-05-26 22:13:14 -070030 explicit AutonomousActor(::aos::EventLoop *event_loop);
Austin Schuh13379ba2019-03-12 21:06:46 -070031
32 bool RunAction(
Alex Perrycb7da4b2019-08-28 19:35:56 -070033 const ::frc971::autonomous::AutonomousActionParams *params) override;
Austin Schuh13379ba2019-03-12 21:06:46 -070034
35 private:
Austin Schuha9644062019-03-28 14:31:52 -070036 void Reset(bool is_left);
Austin Schuh13379ba2019-03-12 21:06:46 -070037
38 double elevator_goal_ = 0.0;
39 double wrist_goal_ = 0.0;
40 double intake_goal_ = 0.0;
41
42 bool suction_on_ = true;
43 int suction_gamepiece_ = 1;
44
45 double elevator_max_velocity_ = 0.0;
46 double elevator_max_acceleration_ = 0.0;
47 double wrist_max_velocity_ = 0.0;
48 double wrist_max_acceleration_ = 0.0;
49
50 void set_elevator_goal(double elevator_goal) {
51 elevator_goal_ = elevator_goal;
52 }
53 void set_wrist_goal(double wrist_goal) { wrist_goal_ = wrist_goal; }
54 void set_intake_goal(double intake_goal) { intake_goal_ = intake_goal; }
55
56 void set_suction_goal(bool on, int gamepiece_mode) {
57 suction_on_ = on;
58 suction_gamepiece_ = gamepiece_mode;
59 }
60
61 void set_elevator_max_velocity(double elevator_max_velocity) {
62 elevator_max_velocity_ = elevator_max_velocity;
63 }
64 void set_elevator_max_acceleration(double elevator_max_acceleration) {
65 elevator_max_acceleration_ = elevator_max_acceleration;
66 }
67 void set_wrist_max_velocity(double wrist_max_velocity) {
68 wrist_max_velocity_ = wrist_max_velocity;
69 }
70 void set_wrist_max_acceleration(double wrist_max_acceleration) {
71 wrist_max_acceleration_ = wrist_max_acceleration;
72 }
73
Austin Schuhb5b79a52019-05-08 20:32:07 -070074 void set_elevator_wrist_goal(ElevatorWristPosition goal) {
75 set_elevator_goal(goal.elevator);
76 set_wrist_goal(goal.wrist);
77 }
78
Austin Schuh13379ba2019-03-12 21:06:46 -070079 void SendSuperstructureGoal() {
Alex Perrycb7da4b2019-08-28 19:35:56 -070080 auto builder = superstructure_goal_sender_.MakeBuilder();
Austin Schuh13379ba2019-03-12 21:06:46 -070081
Alex Perrycb7da4b2019-08-28 19:35:56 -070082 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
83 elevator_offset;
Austin Schuh13379ba2019-03-12 21:06:46 -070084
Alex Perrycb7da4b2019-08-28 19:35:56 -070085 {
86 frc971::ProfileParameters::Builder profile_params_builder =
87 builder.MakeBuilder<frc971::ProfileParameters>();
88 profile_params_builder.add_max_velocity(elevator_max_velocity_);
89 profile_params_builder.add_max_acceleration(elevator_max_acceleration_);
Austin Schuh13379ba2019-03-12 21:06:46 -070090
Alex Perrycb7da4b2019-08-28 19:35:56 -070091 flatbuffers::Offset<frc971::ProfileParameters> profile_params_offset =
92 profile_params_builder.Finish();
Austin Schuh13379ba2019-03-12 21:06:46 -070093
Alex Perrycb7da4b2019-08-28 19:35:56 -070094 StaticZeroingSingleDOFProfiledSubsystemGoal::Builder elevator_builder =
95 builder.MakeBuilder<StaticZeroingSingleDOFProfiledSubsystemGoal>();
96
97 elevator_builder.add_unsafe_goal(elevator_goal_);
98 elevator_builder.add_profile_params(profile_params_offset);
99
100 elevator_offset = elevator_builder.Finish();
101 }
102
103 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
104 wrist_offset;
105
106 {
107 frc971::ProfileParameters::Builder profile_params_builder =
108 builder.MakeBuilder<frc971::ProfileParameters>();
109 profile_params_builder.add_max_velocity(wrist_max_velocity_);
110 profile_params_builder.add_max_acceleration(wrist_max_acceleration_);
111
112 flatbuffers::Offset<frc971::ProfileParameters> profile_params_offset =
113 profile_params_builder.Finish();
114
115 StaticZeroingSingleDOFProfiledSubsystemGoal::Builder wrist_builder =
116 builder.MakeBuilder<StaticZeroingSingleDOFProfiledSubsystemGoal>();
117
118 wrist_builder.add_unsafe_goal(wrist_goal_);
119 wrist_builder.add_profile_params(profile_params_offset);
120
121 wrist_offset = wrist_builder.Finish();
122 }
123
124 flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal>
125 intake_offset;
126
127 {
128 StaticZeroingSingleDOFProfiledSubsystemGoal::Builder intake_builder =
129 builder.MakeBuilder<StaticZeroingSingleDOFProfiledSubsystemGoal>();
130
131 intake_builder.add_unsafe_goal(intake_goal_);
132
133 intake_offset = intake_builder.Finish();
134 }
135
136 flatbuffers::Offset<superstructure::SuctionGoal> suction_offset;
137
138 {
139 superstructure::SuctionGoal::Builder suction_builder =
140 builder.MakeBuilder<superstructure::SuctionGoal>();
141
142 suction_builder.add_grab_piece(suction_on_);
143 suction_builder.add_gamepiece_mode(suction_gamepiece_);
144
145 suction_offset = suction_builder.Finish();
146 }
147
148 superstructure::Goal::Builder superstructure_builder =
149 builder.MakeBuilder<superstructure::Goal>();
150
151 superstructure_builder.add_elevator(elevator_offset);
152 superstructure_builder.add_wrist(wrist_offset);
153 superstructure_builder.add_intake(intake_offset);
154 superstructure_builder.add_suction(suction_offset);
155
156 if (!builder.Send(superstructure_builder.Finish())) {
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() {
171 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
Austin Schuhd32b3622019-06-23 18:49:06 -0700172 event_loop()->monotonic_now(),
Austin Schuh13379ba2019-03-12 21:06:46 -0700173 ::std::chrono::milliseconds(5) / 2);
174
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.
194 ::std::this_thread::sleep_for(::std::chrono::milliseconds(5));
195 }
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() {
222 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
Austin Schuhd32b3622019-06-23 18:49:06 -0700223 event_loop()->monotonic_now(),
Austin Schuh13379ba2019-03-12 21:06:46 -0700224 ::std::chrono::milliseconds(5) / 2);
225
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
252} // namespace actors
253} // namespace y2019
254
255#endif // Y2019_ACTORS_AUTONOMOUS_ACTOR_H_