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" |
| 13 | #include "y2019/control_loops/superstructure/superstructure.q.h" |
| 14 | |
| 15 | namespace y2019 { |
| 16 | namespace actors { |
| 17 | |
| 18 | using ::frc971::ProfileParameters; |
| 19 | using ::y2019::control_loops::superstructure::superstructure_queue; |
| 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: |
| 28 | explicit AutonomousActor(::frc971::autonomous::AutonomousActionQueueGroup *s); |
| 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() { |
| 78 | auto new_superstructure_goal = superstructure_queue.goal.MakeMessage(); |
| 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()) { |
| 97 | LOG(ERROR, "Sending superstructure goal failed.\n"); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | bool IsSucked() { |
| 102 | superstructure_queue.status.FetchLatest(); |
| 103 | |
| 104 | if (superstructure_queue.status.get()) { |
| 105 | return superstructure_queue.status->has_piece; |
| 106 | } |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | bool WaitForGamePiece() { |
| 111 | ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5), |
| 112 | ::std::chrono::milliseconds(5) / 2); |
| 113 | |
| 114 | while (true) { |
| 115 | if (ShouldCancel()) { |
| 116 | return false; |
| 117 | } |
| 118 | phased_loop.SleepUntilNext(); |
| 119 | if (IsSucked()) { |
| 120 | return true; |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
Austin Schuh | b5b79a5 | 2019-05-08 20:32:07 -0700 | [diff] [blame^] | 125 | bool WaitForMilliseconds(std::chrono::milliseconds wait) { |
| 126 | ::aos::monotonic_clock::time_point end_time = |
| 127 | ::aos::monotonic_clock::now() + wait; |
| 128 | |
| 129 | while (::aos::monotonic_clock::now() < end_time) { |
| 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() { |
| 140 | superstructure_queue.status.FetchLatest(); |
| 141 | |
| 142 | double kElevatorTolerance = 0.01; |
| 143 | double kWristTolerance = 0.05; |
| 144 | |
| 145 | if (superstructure_queue.status.get()) { |
| 146 | const bool elevator_at_goal = |
| 147 | ::std::abs(elevator_goal_ - |
| 148 | superstructure_queue.status->elevator.position) < |
| 149 | kElevatorTolerance; |
| 150 | |
| 151 | const bool wrist_at_goal = |
| 152 | ::std::abs(wrist_goal_ - |
| 153 | superstructure_queue.status->wrist.position) < |
| 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), |
| 163 | ::std::chrono::milliseconds(5) / 2); |
| 164 | |
| 165 | while (true) { |
| 166 | if (ShouldCancel()) { |
| 167 | return false; |
| 168 | } |
| 169 | phased_loop.SleepUntilNext(); |
| 170 | superstructure_queue.status.FetchLatest(); |
| 171 | superstructure_queue.goal.FetchLatest(); |
| 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 | 13379ba | 2019-03-12 21:06:46 -0700 | [diff] [blame] | 183 | }; |
| 184 | |
| 185 | } // namespace actors |
| 186 | } // namespace y2019 |
| 187 | |
| 188 | #endif // Y2019_ACTORS_AUTONOMOUS_ACTOR_H_ |