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 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 16 | namespace y2019::actors { |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 17 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 18 | using ::frc971::control_loops::StaticZeroingSingleDOFProfiledSubsystemGoal; |
| 19 | |
| 20 | namespace superstructure = y2019::control_loops::superstructure; |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 21 | |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 22 | struct ElevatorWristPosition { |
| 23 | double elevator; |
| 24 | double wrist; |
| 25 | }; |
| 26 | |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 27 | class AutonomousActor : public ::frc971::autonomous::BaseAutonomousActor { |
| 28 | public: |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 29 | explicit AutonomousActor(::aos::EventLoop *event_loop); |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 30 | |
| 31 | bool RunAction( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 32 | const ::frc971::autonomous::AutonomousActionParams *params) override; |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 33 | |
| 34 | private: |
Austin Schuh | a964406 | 2019-03-28 14:31:52 -0700 | [diff] [blame] | 35 | void Reset(bool is_left); |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 36 | |
| 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 Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 73 | void set_elevator_wrist_goal(ElevatorWristPosition goal) { |
| 74 | set_elevator_goal(goal.elevator); |
| 75 | set_wrist_goal(goal.wrist); |
| 76 | } |
| 77 | |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 78 | void SendSuperstructureGoal() { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 79 | auto builder = superstructure_goal_sender_.MakeBuilder(); |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 80 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 81 | flatbuffers::Offset<StaticZeroingSingleDOFProfiledSubsystemGoal> |
| 82 | elevator_offset; |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 83 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 84 | { |
| 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 Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 89 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 90 | flatbuffers::Offset<frc971::ProfileParameters> profile_params_offset = |
| 91 | profile_params_builder.Finish(); |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 92 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 93 | 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 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 155 | if (builder.Send(superstructure_builder.Finish()) != |
| 156 | aos::RawSender::Error::kOk) { |
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() { |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 171 | ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency, |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 172 | event_loop()->monotonic_now(), |
Stephan Pleines | 743f83a | 2024-02-02 18:32:09 -0800 | [diff] [blame] | 173 | aos::common::actions::kLoopOffset); |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 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. |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 194 | ::std::this_thread::sleep_for(frc971::controls::kLoopFrequency); |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 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() { |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 222 | ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency, |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 223 | event_loop()->monotonic_now(), |
Stephan Pleines | 743f83a | 2024-02-02 18:32:09 -0800 | [diff] [blame] | 224 | aos::common::actions::kLoopOffset); |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 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 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 252 | } // namespace y2019::actors |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 253 | |
| 254 | #endif // Y2019_ACTORS_AUTONOMOUS_ACTOR_H_ |