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" |
| 10 | #include "frc971/control_loops/control_loops.q.h" |
| 11 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
| 12 | #include "frc971/control_loops/drivetrain/drivetrain_config.h" |
Austin Schuh | eb99d07 | 2019-05-12 21:03:38 -0700 | [diff] [blame] | 13 | #include "frc971/control_loops/drivetrain/localizer.q.h" |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 14 | #include "y2019/control_loops/superstructure/superstructure.q.h" |
| 15 | |
| 16 | namespace y2019 { |
| 17 | namespace actors { |
| 18 | |
| 19 | using ::frc971::ProfileParameters; |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 20 | |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 21 | struct ElevatorWristPosition { |
| 22 | double elevator; |
| 23 | double wrist; |
| 24 | }; |
| 25 | |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 26 | class AutonomousActor : public ::frc971::autonomous::BaseAutonomousActor { |
| 27 | public: |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 28 | explicit AutonomousActor(::aos::EventLoop *event_loop); |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 29 | |
| 30 | bool RunAction( |
| 31 | const ::frc971::autonomous::AutonomousActionParams ¶ms) override; |
| 32 | |
| 33 | private: |
Austin Schuh | a964406 | 2019-03-28 14:31:52 -0700 | [diff] [blame] | 34 | void Reset(bool is_left); |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 35 | |
| 36 | double elevator_goal_ = 0.0; |
| 37 | double wrist_goal_ = 0.0; |
| 38 | double intake_goal_ = 0.0; |
| 39 | |
| 40 | bool suction_on_ = true; |
| 41 | int suction_gamepiece_ = 1; |
| 42 | |
| 43 | double elevator_max_velocity_ = 0.0; |
| 44 | double elevator_max_acceleration_ = 0.0; |
| 45 | double wrist_max_velocity_ = 0.0; |
| 46 | double wrist_max_acceleration_ = 0.0; |
| 47 | |
| 48 | void set_elevator_goal(double elevator_goal) { |
| 49 | elevator_goal_ = elevator_goal; |
| 50 | } |
| 51 | void set_wrist_goal(double wrist_goal) { wrist_goal_ = wrist_goal; } |
| 52 | void set_intake_goal(double intake_goal) { intake_goal_ = intake_goal; } |
| 53 | |
| 54 | void set_suction_goal(bool on, int gamepiece_mode) { |
| 55 | suction_on_ = on; |
| 56 | suction_gamepiece_ = gamepiece_mode; |
| 57 | } |
| 58 | |
| 59 | void set_elevator_max_velocity(double elevator_max_velocity) { |
| 60 | elevator_max_velocity_ = elevator_max_velocity; |
| 61 | } |
| 62 | void set_elevator_max_acceleration(double elevator_max_acceleration) { |
| 63 | elevator_max_acceleration_ = elevator_max_acceleration; |
| 64 | } |
| 65 | void set_wrist_max_velocity(double wrist_max_velocity) { |
| 66 | wrist_max_velocity_ = wrist_max_velocity; |
| 67 | } |
| 68 | void set_wrist_max_acceleration(double wrist_max_acceleration) { |
| 69 | wrist_max_acceleration_ = wrist_max_acceleration; |
| 70 | } |
| 71 | |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 72 | void set_elevator_wrist_goal(ElevatorWristPosition goal) { |
| 73 | set_elevator_goal(goal.elevator); |
| 74 | set_wrist_goal(goal.wrist); |
| 75 | } |
| 76 | |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 77 | void SendSuperstructureGoal() { |
Austin Schuh | 170f495 | 2019-06-29 18:58:30 -0700 | [diff] [blame] | 78 | auto new_superstructure_goal = superstructure_goal_sender_.MakeMessage(); |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 79 | new_superstructure_goal->elevator.unsafe_goal = elevator_goal_; |
| 80 | new_superstructure_goal->wrist.unsafe_goal = wrist_goal_; |
| 81 | new_superstructure_goal->intake.unsafe_goal = intake_goal_; |
| 82 | |
| 83 | new_superstructure_goal->suction.grab_piece = suction_on_; |
| 84 | new_superstructure_goal->suction.gamepiece_mode = suction_gamepiece_; |
| 85 | |
| 86 | new_superstructure_goal->elevator.profile_params.max_velocity = |
| 87 | elevator_max_velocity_; |
| 88 | new_superstructure_goal->elevator.profile_params.max_acceleration = |
| 89 | elevator_max_acceleration_; |
| 90 | |
| 91 | new_superstructure_goal->wrist.profile_params.max_velocity = |
| 92 | wrist_max_velocity_; |
| 93 | new_superstructure_goal->wrist.profile_params.max_acceleration = |
| 94 | wrist_max_acceleration_; |
| 95 | |
| 96 | if (!new_superstructure_goal.Send()) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 97 | AOS_LOG(ERROR, "Sending superstructure goal failed.\n"); |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
| 101 | bool IsSucked() { |
Austin Schuh | 170f495 | 2019-06-29 18:58:30 -0700 | [diff] [blame] | 102 | superstructure_status_fetcher_.Fetch(); |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 103 | |
Austin Schuh | 170f495 | 2019-06-29 18:58:30 -0700 | [diff] [blame] | 104 | if (superstructure_status_fetcher_.get()) { |
| 105 | return superstructure_status_fetcher_->has_piece; |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 106 | } |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | bool WaitForGamePiece() { |
| 111 | ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5), |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 112 | event_loop()->monotonic_now(), |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 113 | ::std::chrono::milliseconds(5) / 2); |
| 114 | |
| 115 | while (true) { |
| 116 | if (ShouldCancel()) { |
| 117 | return false; |
| 118 | } |
| 119 | phased_loop.SleepUntilNext(); |
| 120 | if (IsSucked()) { |
| 121 | return true; |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 126 | bool WaitForMilliseconds(std::chrono::milliseconds wait) { |
Austin Schuh | 77ed543 | 2019-07-07 20:41:36 -0700 | [diff] [blame] | 127 | ::aos::monotonic_clock::time_point end_time = monotonic_now() + wait; |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 128 | |
Austin Schuh | 77ed543 | 2019-07-07 20:41:36 -0700 | [diff] [blame] | 129 | while (monotonic_now() < end_time) { |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 130 | if (ShouldCancel()) { |
| 131 | return false; |
| 132 | } |
| 133 | // TODO(james): Allow non-multiples of 5. |
| 134 | ::std::this_thread::sleep_for(::std::chrono::milliseconds(5)); |
| 135 | } |
| 136 | return true; |
| 137 | } |
| 138 | |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 139 | bool IsSuperstructureDone() { |
Austin Schuh | 170f495 | 2019-06-29 18:58:30 -0700 | [diff] [blame] | 140 | superstructure_status_fetcher_.Fetch(); |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 141 | |
| 142 | double kElevatorTolerance = 0.01; |
| 143 | double kWristTolerance = 0.05; |
| 144 | |
Austin Schuh | 170f495 | 2019-06-29 18:58:30 -0700 | [diff] [blame] | 145 | if (superstructure_status_fetcher_.get()) { |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 146 | const bool elevator_at_goal = |
| 147 | ::std::abs(elevator_goal_ - |
Austin Schuh | 170f495 | 2019-06-29 18:58:30 -0700 | [diff] [blame] | 148 | superstructure_status_fetcher_->elevator.position) < |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 149 | kElevatorTolerance; |
| 150 | |
| 151 | const bool wrist_at_goal = |
| 152 | ::std::abs(wrist_goal_ - |
Austin Schuh | 170f495 | 2019-06-29 18:58:30 -0700 | [diff] [blame] | 153 | superstructure_status_fetcher_->wrist.position) < |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 154 | kWristTolerance; |
| 155 | |
| 156 | return elevator_at_goal && wrist_at_goal; |
| 157 | } |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | bool WaitForSuperstructureDone() { |
| 162 | ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5), |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 163 | event_loop()->monotonic_now(), |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 164 | ::std::chrono::milliseconds(5) / 2); |
| 165 | |
| 166 | while (true) { |
| 167 | if (ShouldCancel()) { |
| 168 | return false; |
| 169 | } |
| 170 | phased_loop.SleepUntilNext(); |
Austin Schuh | 170f495 | 2019-06-29 18:58:30 -0700 | [diff] [blame] | 171 | superstructure_status_fetcher_.Fetch(); |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 172 | if (IsSuperstructureDone()) { |
| 173 | return true; |
| 174 | } |
| 175 | } |
| 176 | } |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 177 | |
| 178 | // Waits until the robot's x > x. |
| 179 | bool WaitForDriveXGreater(double x); |
| 180 | |
| 181 | // Waits until y is within y of zero. |
| 182 | bool WaitForDriveYCloseToZero(double y); |
Austin Schuh | eb99d07 | 2019-05-12 21:03:38 -0700 | [diff] [blame] | 183 | |
| 184 | ::aos::Sender<::frc971::control_loops::drivetrain::LocalizerControl> |
| 185 | localizer_control_sender_; |
Austin Schuh | 170f495 | 2019-06-29 18:58:30 -0700 | [diff] [blame] | 186 | ::aos::Sender< |
| 187 | ::y2019::control_loops::superstructure::SuperstructureQueue::Goal> |
| 188 | superstructure_goal_sender_; |
| 189 | ::aos::Fetcher< |
| 190 | ::y2019::control_loops::superstructure::SuperstructureQueue::Status> |
| 191 | superstructure_status_fetcher_; |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 192 | }; |
| 193 | |
| 194 | } // namespace actors |
| 195 | } // namespace y2019 |
| 196 | |
| 197 | #endif // Y2019_ACTORS_AUTONOMOUS_ACTOR_H_ |