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; |
| 20 | using ::y2019::control_loops::superstructure::superstructure_queue; |
| 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( |
| 32 | const ::frc971::autonomous::AutonomousActionParams ¶ms) override; |
| 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() { |
| 79 | auto new_superstructure_goal = superstructure_queue.goal.MakeMessage(); |
| 80 | new_superstructure_goal->elevator.unsafe_goal = elevator_goal_; |
| 81 | new_superstructure_goal->wrist.unsafe_goal = wrist_goal_; |
| 82 | new_superstructure_goal->intake.unsafe_goal = intake_goal_; |
| 83 | |
| 84 | new_superstructure_goal->suction.grab_piece = suction_on_; |
| 85 | new_superstructure_goal->suction.gamepiece_mode = suction_gamepiece_; |
| 86 | |
| 87 | new_superstructure_goal->elevator.profile_params.max_velocity = |
| 88 | elevator_max_velocity_; |
| 89 | new_superstructure_goal->elevator.profile_params.max_acceleration = |
| 90 | elevator_max_acceleration_; |
| 91 | |
| 92 | new_superstructure_goal->wrist.profile_params.max_velocity = |
| 93 | wrist_max_velocity_; |
| 94 | new_superstructure_goal->wrist.profile_params.max_acceleration = |
| 95 | wrist_max_acceleration_; |
| 96 | |
| 97 | if (!new_superstructure_goal.Send()) { |
| 98 | LOG(ERROR, "Sending superstructure goal failed.\n"); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | bool IsSucked() { |
| 103 | superstructure_queue.status.FetchLatest(); |
| 104 | |
| 105 | if (superstructure_queue.status.get()) { |
| 106 | return superstructure_queue.status->has_piece; |
| 107 | } |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | bool WaitForGamePiece() { |
| 112 | ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5), |
| 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) { |
| 127 | ::aos::monotonic_clock::time_point end_time = |
| 128 | ::aos::monotonic_clock::now() + wait; |
| 129 | |
| 130 | while (::aos::monotonic_clock::now() < end_time) { |
| 131 | if (ShouldCancel()) { |
| 132 | return false; |
| 133 | } |
| 134 | // TODO(james): Allow non-multiples of 5. |
| 135 | ::std::this_thread::sleep_for(::std::chrono::milliseconds(5)); |
| 136 | } |
| 137 | return true; |
| 138 | } |
| 139 | |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 140 | bool IsSuperstructureDone() { |
| 141 | superstructure_queue.status.FetchLatest(); |
| 142 | |
| 143 | double kElevatorTolerance = 0.01; |
| 144 | double kWristTolerance = 0.05; |
| 145 | |
| 146 | if (superstructure_queue.status.get()) { |
| 147 | const bool elevator_at_goal = |
| 148 | ::std::abs(elevator_goal_ - |
| 149 | superstructure_queue.status->elevator.position) < |
| 150 | kElevatorTolerance; |
| 151 | |
| 152 | const bool wrist_at_goal = |
| 153 | ::std::abs(wrist_goal_ - |
| 154 | superstructure_queue.status->wrist.position) < |
| 155 | kWristTolerance; |
| 156 | |
| 157 | return elevator_at_goal && wrist_at_goal; |
| 158 | } |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | bool WaitForSuperstructureDone() { |
| 163 | ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5), |
| 164 | ::std::chrono::milliseconds(5) / 2); |
| 165 | |
| 166 | while (true) { |
| 167 | if (ShouldCancel()) { |
| 168 | return false; |
| 169 | } |
| 170 | phased_loop.SleepUntilNext(); |
| 171 | superstructure_queue.status.FetchLatest(); |
| 172 | superstructure_queue.goal.FetchLatest(); |
| 173 | if (IsSuperstructureDone()) { |
| 174 | return true; |
| 175 | } |
| 176 | } |
| 177 | } |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame] | 178 | |
| 179 | // Waits until the robot's x > x. |
| 180 | bool WaitForDriveXGreater(double x); |
| 181 | |
| 182 | // Waits until y is within y of zero. |
| 183 | bool WaitForDriveYCloseToZero(double y); |
Austin Schuh | eb99d07 | 2019-05-12 21:03:38 -0700 | [diff] [blame] | 184 | |
| 185 | ::aos::Sender<::frc971::control_loops::drivetrain::LocalizerControl> |
| 186 | localizer_control_sender_; |
Austin Schuh | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 187 | }; |
| 188 | |
| 189 | } // namespace actors |
| 190 | } // namespace y2019 |
| 191 | |
| 192 | #endif // Y2019_ACTORS_AUTONOMOUS_ACTOR_H_ |