Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 1 | #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 Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 10 | #include "frc971/control_loops/control_loops_generated.h" |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 11 | #include "frc971/control_loops/drivetrain/drivetrain_config.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 12 | #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 Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 15 | |
| 16 | namespace y2019 { |
| 17 | namespace actors { |
| 18 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 19 | using ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal; |
| 20 | |
| 21 | namespace superstructure = y2019::control_loops::superstructure; |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 22 | |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 23 | struct ElevatorWristPosition { |
| 24 | double elevator; |
| 25 | double wrist; |
| 26 | }; |
| 27 | |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 28 | class AutonomousActor : public ::frc971::autonomous::BaseAutonomousActor { |
| 29 | public: |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 30 | explicit AutonomousActor(::aos::EventLoop *event_loop); |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 31 | |
| 32 | bool RunAction( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 33 | const ::frc971::autonomous::AutonomousActionParams *params) override; |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 34 | |
| 35 | private: |
Austin Schuh | a964406 | 2019-03-28 14:31:52 -0700 | [diff] [blame] | 36 | void Reset(bool is_left); |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 37 | |
| 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 Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 74 | void set_elevator_wrist_goal(ElevatorWristPosition goal) { |
| 75 | set_elevator_goal(goal.elevator); |
| 76 | set_wrist_goal(goal.wrist); |
| 77 | } |
| 78 | |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 79 | void SendSuperstructureGoal() { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 80 | auto builder = superstructure_goal_sender_.MakeBuilder(); |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 81 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 82 | flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal> |
| 83 | elevator_offset; |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 84 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 85 | { |
| 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 Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 90 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 91 | flatbuffers::Offset<frc971::ProfileParameters> profile_params_offset = |
| 92 | profile_params_builder.Finish(); |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 93 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 94 | 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 Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 157 | AOS_LOG(ERROR, "Sending superstructure goal failed.\n"); |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
| 161 | bool IsSucked() { |
Austin Schuh | 170f495 | 2019-06-29 18:58:30 -0700 | [diff] [blame] | 162 | superstructure_status_fetcher_.Fetch(); |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 163 | |
Austin Schuh | 170f495 | 2019-06-29 18:58:30 -0700 | [diff] [blame] | 164 | if (superstructure_status_fetcher_.get()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 165 | return superstructure_status_fetcher_->has_piece(); |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 166 | } |
| 167 | return false; |
| 168 | } |
| 169 | |
| 170 | bool WaitForGamePiece() { |
| 171 | ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5), |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 172 | event_loop()->monotonic_now(), |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 173 | ::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 Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 186 | bool WaitForMilliseconds(std::chrono::milliseconds wait) { |
Austin Schuh | 77ed543 | 2019-07-07 20:41:36 -0700 | [diff] [blame] | 187 | ::aos::monotonic_clock::time_point end_time = monotonic_now() + wait; |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 188 | |
Austin Schuh | 77ed543 | 2019-07-07 20:41:36 -0700 | [diff] [blame] | 189 | while (monotonic_now() < end_time) { |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 190 | 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 Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 199 | bool IsSuperstructureDone() { |
Austin Schuh | 170f495 | 2019-06-29 18:58:30 -0700 | [diff] [blame] | 200 | superstructure_status_fetcher_.Fetch(); |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 201 | |
| 202 | double kElevatorTolerance = 0.01; |
| 203 | double kWristTolerance = 0.05; |
| 204 | |
Austin Schuh | 170f495 | 2019-06-29 18:58:30 -0700 | [diff] [blame] | 205 | if (superstructure_status_fetcher_.get()) { |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 206 | const bool elevator_at_goal = |
| 207 | ::std::abs(elevator_goal_ - |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 208 | superstructure_status_fetcher_->elevator()->position()) < |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 209 | kElevatorTolerance; |
| 210 | |
| 211 | const bool wrist_at_goal = |
| 212 | ::std::abs(wrist_goal_ - |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 213 | superstructure_status_fetcher_->wrist()->position()) < |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 214 | 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 Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 223 | event_loop()->monotonic_now(), |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 224 | ::std::chrono::milliseconds(5) / 2); |
| 225 | |
| 226 | while (true) { |
| 227 | if (ShouldCancel()) { |
| 228 | return false; |
| 229 | } |
| 230 | phased_loop.SleepUntilNext(); |
Austin Schuh | 170f495 | 2019-06-29 18:58:30 -0700 | [diff] [blame] | 231 | superstructure_status_fetcher_.Fetch(); |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 232 | if (IsSuperstructureDone()) { |
| 233 | return true; |
| 234 | } |
| 235 | } |
| 236 | } |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 237 | |
| 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 Schuh | eb99d07 | 2019-05-12 21:03:38 -0700 | [diff] [blame] | 243 | |
| 244 | ::aos::Sender<::frc971::control_loops::drivetrain::LocalizerControl> |
| 245 | localizer_control_sender_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 246 | ::aos::Sender<::y2019::control_loops::superstructure::Goal> |
Austin Schuh | 170f495 | 2019-06-29 18:58:30 -0700 | [diff] [blame] | 247 | superstructure_goal_sender_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 248 | ::aos::Fetcher<::y2019::control_loops::superstructure::Status> |
Austin Schuh | 170f495 | 2019-06-29 18:58:30 -0700 | [diff] [blame] | 249 | superstructure_status_fetcher_; |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 250 | }; |
| 251 | |
| 252 | } // namespace actors |
| 253 | } // namespace y2019 |
| 254 | |
| 255 | #endif // Y2019_ACTORS_AUTONOMOUS_ACTOR_H_ |