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