Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 1 | #ifndef Y2018_ACTORS_AUTONOMOUS_ACTOR_H_ |
| 2 | #define Y2018_ACTORS_AUTONOMOUS_ACTOR_H_ |
| 3 | |
| 4 | #include <chrono> |
| 5 | #include <memory> |
| 6 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 7 | #include "aos/actions/actions.h" |
| 8 | #include "aos/actions/actor.h" |
Austin Schuh | eb99d07 | 2019-05-12 21:03:38 -0700 | [diff] [blame] | 9 | #include "aos/events/event-loop.h" |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 10 | #include "frc971/autonomous/base_autonomous_actor.h" |
| 11 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
| 12 | #include "frc971/control_loops/drivetrain/drivetrain_config.h" |
| 13 | #include "y2018/control_loops/superstructure/arm/generated_graph.h" |
| 14 | #include "y2018/control_loops/superstructure/superstructure.q.h" |
| 15 | |
| 16 | namespace y2018 { |
| 17 | namespace actors { |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 18 | using ::frc971::control_loops::drivetrain_queue; |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 19 | |
| 20 | namespace arm = ::y2018::control_loops::superstructure::arm; |
| 21 | |
| 22 | class AutonomousActor : public ::frc971::autonomous::BaseAutonomousActor { |
| 23 | public: |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 24 | explicit AutonomousActor(::aos::EventLoop *event_loop); |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 25 | |
| 26 | bool RunAction( |
| 27 | const ::frc971::autonomous::AutonomousActionParams ¶ms) override; |
Austin Schuh | d845c97 | 2019-06-29 21:20:05 -0700 | [diff] [blame] | 28 | |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 29 | private: |
| 30 | void Reset() { |
| 31 | roller_voltage_ = 0.0; |
Austin Schuh | cf96d32 | 2018-04-07 15:52:31 -0700 | [diff] [blame] | 32 | left_intake_angle_ = -3.2; |
| 33 | right_intake_angle_ = -3.2; |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 34 | arm_goal_position_ = arm::NeutralIndex(); |
| 35 | grab_box_ = false; |
| 36 | open_claw_ = false; |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 37 | close_claw_ = false; |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 38 | deploy_fork_ = false; |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 39 | disable_box_correct_ = false; |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 40 | InitializeEncoders(); |
| 41 | ResetDrivetrain(); |
| 42 | SendSuperstructureGoal(); |
| 43 | } |
| 44 | |
Austin Schuh | d845c97 | 2019-06-29 21:20:05 -0700 | [diff] [blame] | 45 | ::aos::Sender<::y2018::control_loops::SuperstructureQueue::Goal> |
| 46 | superstructure_goal_sender_; |
| 47 | ::aos::Fetcher<::y2018::control_loops::SuperstructureQueue::Status> |
| 48 | superstructure_status_fetcher_; |
| 49 | |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 50 | double roller_voltage_ = 0.0; |
Austin Schuh | cf96d32 | 2018-04-07 15:52:31 -0700 | [diff] [blame] | 51 | double left_intake_angle_ = -3.2; |
| 52 | double right_intake_angle_ = -3.2; |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 53 | uint32_t arm_goal_position_ = arm::NeutralIndex(); |
| 54 | bool grab_box_ = false; |
| 55 | bool open_claw_ = false; |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 56 | bool close_claw_ = false; |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 57 | bool deploy_fork_ = false; |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 58 | bool disable_box_correct_ = false; |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 59 | |
| 60 | void set_roller_voltage(double roller_voltage) { |
| 61 | roller_voltage_ = roller_voltage; |
| 62 | } |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 63 | void set_intake_angle(double intake_angle) { |
| 64 | set_left_intake_angle(intake_angle); |
| 65 | set_right_intake_angle(intake_angle); |
| 66 | } |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 67 | void set_left_intake_angle(double left_intake_angle) { |
| 68 | left_intake_angle_ = left_intake_angle; |
| 69 | } |
| 70 | void set_right_intake_angle(double right_intake_angle) { |
| 71 | right_intake_angle_ = right_intake_angle; |
| 72 | } |
| 73 | void set_arm_goal_position(uint32_t arm_goal_position) { |
| 74 | arm_goal_position_ = arm_goal_position; |
| 75 | } |
| 76 | void set_grab_box(bool grab_box) { grab_box_ = grab_box; } |
| 77 | void set_open_claw(bool open_claw) { open_claw_ = open_claw; } |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 78 | void set_close_claw(bool close_claw) { close_claw_ = close_claw; } |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 79 | void set_deploy_fork(bool deploy_fork) { deploy_fork_ = deploy_fork; } |
| 80 | |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 81 | void set_disable_box_correct(bool disable_box_correct) { |
| 82 | disable_box_correct_ = disable_box_correct; |
| 83 | } |
| 84 | |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 85 | void SendSuperstructureGoal() { |
Austin Schuh | d845c97 | 2019-06-29 21:20:05 -0700 | [diff] [blame] | 86 | auto new_superstructure_goal = superstructure_goal_sender_.MakeMessage(); |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 87 | new_superstructure_goal->intake.roller_voltage = roller_voltage_; |
| 88 | new_superstructure_goal->intake.left_intake_angle = left_intake_angle_; |
| 89 | new_superstructure_goal->intake.right_intake_angle = right_intake_angle_; |
| 90 | |
| 91 | new_superstructure_goal->arm_goal_position = arm_goal_position_; |
| 92 | new_superstructure_goal->grab_box = grab_box_; |
| 93 | new_superstructure_goal->open_claw = open_claw_; |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 94 | new_superstructure_goal->close_claw = close_claw_; |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 95 | new_superstructure_goal->deploy_fork = deploy_fork_; |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 96 | new_superstructure_goal->trajectory_override = false; |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 97 | |
| 98 | if (!new_superstructure_goal.Send()) { |
| 99 | LOG(ERROR, "Sending superstructure goal failed.\n"); |
| 100 | } |
| 101 | } |
Austin Schuh | c231df4 | 2018-03-21 20:43:24 -0700 | [diff] [blame] | 102 | |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 103 | bool ThreeCubeAuto(::aos::monotonic_clock::time_point start_time); |
| 104 | bool CloseSwitch(::aos::monotonic_clock::time_point start_time, |
| 105 | bool left = true); |
| 106 | bool FarSwitch(::aos::monotonic_clock::time_point start_time, |
| 107 | bool drive_behind = true, bool left = true); |
| 108 | bool FarReadyScale(::aos::monotonic_clock::time_point start_time); |
| 109 | bool DriveStraight(); |
| 110 | |
| 111 | bool FarScale(::aos::monotonic_clock::time_point start_time); |
| 112 | |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 113 | bool WaitForArmTrajectoryOrDriveClose(double drive_threshold, |
| 114 | double arm_threshold) { |
| 115 | ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5), |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 116 | event_loop()->monotonic_now(), |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 117 | ::std::chrono::milliseconds(5) / 2); |
| 118 | |
| 119 | constexpr double kPositionTolerance = 0.02; |
| 120 | constexpr double kProfileTolerance = 0.001; |
| 121 | |
| 122 | while (true) { |
| 123 | if (ShouldCancel()) { |
| 124 | return false; |
| 125 | } |
| 126 | |
Austin Schuh | d845c97 | 2019-06-29 21:20:05 -0700 | [diff] [blame] | 127 | superstructure_status_fetcher_.Fetch(); |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 128 | drivetrain_queue.status.FetchLatest(); |
Austin Schuh | d845c97 | 2019-06-29 21:20:05 -0700 | [diff] [blame] | 129 | if (drivetrain_queue.status.get() && |
| 130 | superstructure_status_fetcher_.get()) { |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 131 | const double left_profile_error = |
| 132 | (initial_drivetrain_.left - |
| 133 | drivetrain_queue.status->profiled_left_position_goal); |
| 134 | const double right_profile_error = |
| 135 | (initial_drivetrain_.right - |
| 136 | drivetrain_queue.status->profiled_right_position_goal); |
| 137 | |
| 138 | const double left_error = |
| 139 | (initial_drivetrain_.left - |
| 140 | drivetrain_queue.status->estimated_left_position); |
| 141 | const double right_error = |
| 142 | (initial_drivetrain_.right - |
| 143 | drivetrain_queue.status->estimated_right_position); |
| 144 | |
| 145 | const double profile_distance_to_go = |
| 146 | (left_profile_error + right_profile_error) / 2.0; |
| 147 | |
| 148 | const double distance_to_go = (left_error + right_error) / 2.0; |
| 149 | |
| 150 | // Check superstructure first. |
Austin Schuh | d845c97 | 2019-06-29 21:20:05 -0700 | [diff] [blame] | 151 | if (superstructure_status_fetcher_->arm.current_node == |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 152 | arm_goal_position_ && |
Austin Schuh | d845c97 | 2019-06-29 21:20:05 -0700 | [diff] [blame] | 153 | superstructure_status_fetcher_->arm.path_distance_to_go < |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 154 | arm_threshold) { |
| 155 | LOG(INFO, "Arm finished first: %f, drivetrain %f distance\n", |
Austin Schuh | d845c97 | 2019-06-29 21:20:05 -0700 | [diff] [blame] | 156 | superstructure_status_fetcher_->arm.path_distance_to_go, |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 157 | ::std::abs(distance_to_go)); |
| 158 | return true; |
| 159 | } |
| 160 | |
| 161 | // Now check drivetrain. |
| 162 | if (::std::abs(profile_distance_to_go) < |
| 163 | drive_threshold + kProfileTolerance && |
| 164 | ::std::abs(distance_to_go) < drive_threshold + kPositionTolerance) { |
| 165 | LOG(INFO, |
| 166 | "Drivetrain finished first: arm %f, drivetrain %f distance\n", |
Austin Schuh | d845c97 | 2019-06-29 21:20:05 -0700 | [diff] [blame] | 167 | superstructure_status_fetcher_->arm.path_distance_to_go, |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 168 | ::std::abs(distance_to_go)); |
| 169 | return true; |
| 170 | } |
| 171 | } |
| 172 | phased_loop.SleepUntilNext(); |
| 173 | } |
| 174 | } |
| 175 | |
Austin Schuh | c231df4 | 2018-03-21 20:43:24 -0700 | [diff] [blame] | 176 | bool WaitForArmTrajectoryClose(double threshold) { |
| 177 | ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5), |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 178 | event_loop()->monotonic_now(), |
Austin Schuh | c231df4 | 2018-03-21 20:43:24 -0700 | [diff] [blame] | 179 | ::std::chrono::milliseconds(5) / 2); |
| 180 | while (true) { |
| 181 | if (ShouldCancel()) { |
| 182 | return false; |
| 183 | } |
| 184 | |
Austin Schuh | d845c97 | 2019-06-29 21:20:05 -0700 | [diff] [blame] | 185 | superstructure_status_fetcher_.Fetch(); |
| 186 | if (superstructure_status_fetcher_.get()) { |
| 187 | if (superstructure_status_fetcher_->arm.current_node == |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 188 | arm_goal_position_ && |
Austin Schuh | d845c97 | 2019-06-29 21:20:05 -0700 | [diff] [blame] | 189 | superstructure_status_fetcher_->arm.path_distance_to_go < |
| 190 | threshold) { |
Austin Schuh | c231df4 | 2018-03-21 20:43:24 -0700 | [diff] [blame] | 191 | return true; |
| 192 | } |
| 193 | } |
| 194 | phased_loop.SleepUntilNext(); |
| 195 | } |
| 196 | } |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 197 | |
| 198 | bool WaitForBoxGrabed() { |
| 199 | ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5), |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 200 | event_loop()->monotonic_now(), |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 201 | ::std::chrono::milliseconds(5) / 2); |
| 202 | while (true) { |
| 203 | if (ShouldCancel()) { |
| 204 | return false; |
| 205 | } |
| 206 | |
Austin Schuh | d845c97 | 2019-06-29 21:20:05 -0700 | [diff] [blame] | 207 | superstructure_status_fetcher_.Fetch(); |
| 208 | if (superstructure_status_fetcher_.get()) { |
| 209 | if (superstructure_status_fetcher_->arm.grab_state == 4) { |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 210 | return true; |
| 211 | } |
| 212 | } |
| 213 | phased_loop.SleepUntilNext(); |
| 214 | } |
| 215 | } |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 216 | }; |
| 217 | |
| 218 | } // namespace actors |
| 219 | } // namespace y2018 |
| 220 | |
| 221 | #endif // Y2018_ACTORS_AUTONOMOUS_ACTOR_H_ |