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 | |
| 7 | #include "aos/common/actions/actions.h" |
| 8 | #include "aos/common/actions/actor.h" |
| 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_; |
| 90 | |
| 91 | if (!new_superstructure_goal.Send()) { |
| 92 | LOG(ERROR, "Sending superstructure goal failed.\n"); |
| 93 | } |
| 94 | } |
Austin Schuh | c231df4 | 2018-03-21 20:43:24 -0700 | [diff] [blame] | 95 | |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 96 | bool WaitForArmTrajectoryOrDriveClose(double drive_threshold, |
| 97 | double arm_threshold) { |
| 98 | ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5), |
| 99 | ::std::chrono::milliseconds(5) / 2); |
| 100 | |
| 101 | constexpr double kPositionTolerance = 0.02; |
| 102 | constexpr double kProfileTolerance = 0.001; |
| 103 | |
| 104 | while (true) { |
| 105 | if (ShouldCancel()) { |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | superstructure_queue.status.FetchLatest(); |
| 110 | drivetrain_queue.status.FetchLatest(); |
| 111 | if (drivetrain_queue.status.get() && superstructure_queue.status.get()) { |
| 112 | const double left_profile_error = |
| 113 | (initial_drivetrain_.left - |
| 114 | drivetrain_queue.status->profiled_left_position_goal); |
| 115 | const double right_profile_error = |
| 116 | (initial_drivetrain_.right - |
| 117 | drivetrain_queue.status->profiled_right_position_goal); |
| 118 | |
| 119 | const double left_error = |
| 120 | (initial_drivetrain_.left - |
| 121 | drivetrain_queue.status->estimated_left_position); |
| 122 | const double right_error = |
| 123 | (initial_drivetrain_.right - |
| 124 | drivetrain_queue.status->estimated_right_position); |
| 125 | |
| 126 | const double profile_distance_to_go = |
| 127 | (left_profile_error + right_profile_error) / 2.0; |
| 128 | |
| 129 | const double distance_to_go = (left_error + right_error) / 2.0; |
| 130 | |
| 131 | // Check superstructure first. |
| 132 | if (superstructure_queue.status->arm.current_node == |
| 133 | arm_goal_position_ && |
| 134 | superstructure_queue.status->arm.path_distance_to_go < |
| 135 | arm_threshold) { |
| 136 | LOG(INFO, "Arm finished first: %f, drivetrain %f distance\n", |
| 137 | superstructure_queue.status->arm.path_distance_to_go, |
| 138 | ::std::abs(distance_to_go)); |
| 139 | return true; |
| 140 | } |
| 141 | |
| 142 | // Now check drivetrain. |
| 143 | if (::std::abs(profile_distance_to_go) < |
| 144 | drive_threshold + kProfileTolerance && |
| 145 | ::std::abs(distance_to_go) < drive_threshold + kPositionTolerance) { |
| 146 | LOG(INFO, |
| 147 | "Drivetrain finished first: arm %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 | phased_loop.SleepUntilNext(); |
| 154 | } |
| 155 | } |
| 156 | |
Austin Schuh | c231df4 | 2018-03-21 20:43:24 -0700 | [diff] [blame] | 157 | bool WaitForArmTrajectoryClose(double threshold) { |
| 158 | ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5), |
| 159 | ::std::chrono::milliseconds(5) / 2); |
| 160 | while (true) { |
| 161 | if (ShouldCancel()) { |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | superstructure_queue.status.FetchLatest(); |
| 166 | if (superstructure_queue.status.get()) { |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 167 | if (superstructure_queue.status->arm.current_node == |
| 168 | arm_goal_position_ && |
Austin Schuh | c231df4 | 2018-03-21 20:43:24 -0700 | [diff] [blame] | 169 | superstructure_queue.status->arm.path_distance_to_go < threshold) { |
| 170 | return true; |
| 171 | } |
| 172 | } |
| 173 | phased_loop.SleepUntilNext(); |
| 174 | } |
| 175 | } |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 176 | |
| 177 | bool WaitForBoxGrabed() { |
| 178 | ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5), |
| 179 | ::std::chrono::milliseconds(5) / 2); |
| 180 | while (true) { |
| 181 | if (ShouldCancel()) { |
| 182 | return false; |
| 183 | } |
| 184 | |
| 185 | superstructure_queue.status.FetchLatest(); |
| 186 | if (superstructure_queue.status.get()) { |
| 187 | if (superstructure_queue.status->arm.grab_state == 4) { |
| 188 | return true; |
| 189 | } |
| 190 | } |
| 191 | phased_loop.SleepUntilNext(); |
| 192 | } |
| 193 | } |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 194 | }; |
| 195 | |
| 196 | } // namespace actors |
| 197 | } // namespace y2018 |
| 198 | |
| 199 | #endif // Y2018_ACTORS_AUTONOMOUS_ACTOR_H_ |