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