blob: 99b136405f0f557a93c7a0140bc7a4950b6ed1c4 [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
milind1f1dca32021-07-03 13:50:07 -0700156 if (builder.Send(superstructure_builder.Finish()) !=
157 aos::RawSender::Error::kOk) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700158 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
Austin Schuh13379ba2019-03-12 21:06:46 -0700159 }
160 }
161
162 bool IsSucked() {
Austin Schuh170f4952019-06-29 18:58:30 -0700163 superstructure_status_fetcher_.Fetch();
Austin Schuh13379ba2019-03-12 21:06:46 -0700164
Austin Schuh170f4952019-06-29 18:58:30 -0700165 if (superstructure_status_fetcher_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700166 return superstructure_status_fetcher_->has_piece();
Austin Schuh13379ba2019-03-12 21:06:46 -0700167 }
168 return false;
169 }
170
171 bool WaitForGamePiece() {
Yash Chainania6fe97b2021-12-15 21:01:11 -0800172 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
Austin Schuhd32b3622019-06-23 18:49:06 -0700173 event_loop()->monotonic_now(),
Yash Chainania6fe97b2021-12-15 21:01:11 -0800174 ActorBase::kLoopOffset);
Austin Schuh13379ba2019-03-12 21:06:46 -0700175
176 while (true) {
177 if (ShouldCancel()) {
178 return false;
179 }
180 phased_loop.SleepUntilNext();
181 if (IsSucked()) {
182 return true;
183 }
184 }
185 }
186
Austin Schuhb5b79a52019-05-08 20:32:07 -0700187 bool WaitForMilliseconds(std::chrono::milliseconds wait) {
Austin Schuh77ed5432019-07-07 20:41:36 -0700188 ::aos::monotonic_clock::time_point end_time = monotonic_now() + wait;
Austin Schuhb5b79a52019-05-08 20:32:07 -0700189
Austin Schuh77ed5432019-07-07 20:41:36 -0700190 while (monotonic_now() < end_time) {
Austin Schuhb5b79a52019-05-08 20:32:07 -0700191 if (ShouldCancel()) {
192 return false;
193 }
194 // TODO(james): Allow non-multiples of 5.
Yash Chainania6fe97b2021-12-15 21:01:11 -0800195 ::std::this_thread::sleep_for(frc971::controls::kLoopFrequency);
Austin Schuhb5b79a52019-05-08 20:32:07 -0700196 }
197 return true;
198 }
199
Austin Schuh13379ba2019-03-12 21:06:46 -0700200 bool IsSuperstructureDone() {
Austin Schuh170f4952019-06-29 18:58:30 -0700201 superstructure_status_fetcher_.Fetch();
Austin Schuh13379ba2019-03-12 21:06:46 -0700202
203 double kElevatorTolerance = 0.01;
204 double kWristTolerance = 0.05;
205
Austin Schuh170f4952019-06-29 18:58:30 -0700206 if (superstructure_status_fetcher_.get()) {
Austin Schuh13379ba2019-03-12 21:06:46 -0700207 const bool elevator_at_goal =
208 ::std::abs(elevator_goal_ -
Alex Perrycb7da4b2019-08-28 19:35:56 -0700209 superstructure_status_fetcher_->elevator()->position()) <
Austin Schuh13379ba2019-03-12 21:06:46 -0700210 kElevatorTolerance;
211
212 const bool wrist_at_goal =
213 ::std::abs(wrist_goal_ -
Alex Perrycb7da4b2019-08-28 19:35:56 -0700214 superstructure_status_fetcher_->wrist()->position()) <
Austin Schuh13379ba2019-03-12 21:06:46 -0700215 kWristTolerance;
216
217 return elevator_at_goal && wrist_at_goal;
218 }
219 return false;
220 }
221
222 bool WaitForSuperstructureDone() {
Yash Chainania6fe97b2021-12-15 21:01:11 -0800223 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
Austin Schuhd32b3622019-06-23 18:49:06 -0700224 event_loop()->monotonic_now(),
Yash Chainania6fe97b2021-12-15 21:01:11 -0800225 ActorBase::kLoopOffset);
Austin Schuh13379ba2019-03-12 21:06:46 -0700226
227 while (true) {
228 if (ShouldCancel()) {
229 return false;
230 }
231 phased_loop.SleepUntilNext();
Austin Schuh170f4952019-06-29 18:58:30 -0700232 superstructure_status_fetcher_.Fetch();
Austin Schuh13379ba2019-03-12 21:06:46 -0700233 if (IsSuperstructureDone()) {
234 return true;
235 }
236 }
237 }
Austin Schuhb5b79a52019-05-08 20:32:07 -0700238
239 // Waits until the robot's x > x.
240 bool WaitForDriveXGreater(double x);
241
242 // Waits until y is within y of zero.
243 bool WaitForDriveYCloseToZero(double y);
Austin Schuheb99d072019-05-12 21:03:38 -0700244
245 ::aos::Sender<::frc971::control_loops::drivetrain::LocalizerControl>
246 localizer_control_sender_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700247 ::aos::Sender<::y2019::control_loops::superstructure::Goal>
Austin Schuh170f4952019-06-29 18:58:30 -0700248 superstructure_goal_sender_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700249 ::aos::Fetcher<::y2019::control_loops::superstructure::Status>
Austin Schuh170f4952019-06-29 18:58:30 -0700250 superstructure_status_fetcher_;
Austin Schuh13379ba2019-03-12 21:06:46 -0700251};
252
253} // namespace actors
254} // namespace y2019
255
256#endif // Y2019_ACTORS_AUTONOMOUS_ACTOR_H_