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" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -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" |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 11 | #include "frc971/control_loops/drivetrain/drivetrain_config.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 12 | #include "frc971/control_loops/drivetrain/drivetrain_status_generated.h" |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 13 | #include "y2018/control_loops/superstructure/arm/generated_graph.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 14 | #include "y2018/control_loops/superstructure/superstructure_goal_generated.h" |
| 15 | #include "y2018/control_loops/superstructure/superstructure_status_generated.h" |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 16 | |
| 17 | namespace y2018 { |
| 18 | namespace actors { |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 19 | |
| 20 | class AutonomousActor : public ::frc971::autonomous::BaseAutonomousActor { |
| 21 | public: |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 22 | explicit AutonomousActor(::aos::EventLoop *event_loop); |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 23 | |
| 24 | bool RunAction( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 25 | const ::frc971::autonomous::AutonomousActionParams *params) override; |
Austin Schuh | d845c97 | 2019-06-29 21:20:05 -0700 | [diff] [blame] | 26 | |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 27 | private: |
| 28 | void Reset() { |
| 29 | roller_voltage_ = 0.0; |
Austin Schuh | cf96d32 | 2018-04-07 15:52:31 -0700 | [diff] [blame] | 30 | left_intake_angle_ = -3.2; |
| 31 | right_intake_angle_ = -3.2; |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 32 | arm_goal_position_ = |
| 33 | ::y2018::control_loops::superstructure::arm::NeutralIndex(); |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 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 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 44 | ::aos::Sender<::y2018::control_loops::superstructure::Goal> |
Austin Schuh | d845c97 | 2019-06-29 21:20:05 -0700 | [diff] [blame] | 45 | superstructure_goal_sender_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 46 | ::aos::Fetcher<::y2018::control_loops::superstructure::Status> |
Austin Schuh | d845c97 | 2019-06-29 21:20:05 -0700 | [diff] [blame] | 47 | superstructure_status_fetcher_; |
| 48 | |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 49 | double roller_voltage_ = 0.0; |
Austin Schuh | cf96d32 | 2018-04-07 15:52:31 -0700 | [diff] [blame] | 50 | double left_intake_angle_ = -3.2; |
| 51 | double right_intake_angle_ = -3.2; |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 52 | uint32_t arm_goal_position_ = |
| 53 | ::y2018::control_loops::superstructure::arm::NeutralIndex(); |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 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() { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 86 | auto builder = superstructure_goal_sender_.MakeBuilder(); |
| 87 | control_loops::superstructure::IntakeGoal::Builder intake_goal_builder = |
| 88 | builder.MakeBuilder<control_loops::superstructure::IntakeGoal>(); |
| 89 | intake_goal_builder.add_roller_voltage(roller_voltage_); |
| 90 | intake_goal_builder.add_left_intake_angle(left_intake_angle_); |
| 91 | intake_goal_builder.add_right_intake_angle(right_intake_angle_); |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 92 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 93 | flatbuffers::Offset<control_loops::superstructure::IntakeGoal> |
| 94 | intake_offset = intake_goal_builder.Finish(); |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 95 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 96 | control_loops::superstructure::Goal::Builder superstructure_builder = |
| 97 | builder.MakeBuilder<control_loops::superstructure::Goal>(); |
| 98 | |
| 99 | superstructure_builder.add_intake(intake_offset); |
| 100 | |
| 101 | superstructure_builder.add_arm_goal_position(arm_goal_position_); |
| 102 | superstructure_builder.add_grab_box(grab_box_); |
| 103 | superstructure_builder.add_open_claw(open_claw_); |
| 104 | superstructure_builder.add_close_claw(close_claw_); |
| 105 | superstructure_builder.add_deploy_fork(deploy_fork_); |
| 106 | superstructure_builder.add_trajectory_override(false); |
| 107 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 108 | if (builder.Send(superstructure_builder.Finish()) != |
| 109 | aos::RawSender::Error::kOk) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 110 | AOS_LOG(ERROR, "Sending superstructure goal failed.\n"); |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 111 | } |
| 112 | } |
Austin Schuh | c231df4 | 2018-03-21 20:43:24 -0700 | [diff] [blame] | 113 | |
Austin Schuh | 4dad8fb | 2018-07-08 16:00:13 -0700 | [diff] [blame] | 114 | bool ThreeCubeAuto(::aos::monotonic_clock::time_point start_time); |
| 115 | bool CloseSwitch(::aos::monotonic_clock::time_point start_time, |
| 116 | bool left = true); |
| 117 | bool FarSwitch(::aos::monotonic_clock::time_point start_time, |
| 118 | bool drive_behind = true, bool left = true); |
| 119 | bool FarReadyScale(::aos::monotonic_clock::time_point start_time); |
| 120 | bool DriveStraight(); |
| 121 | |
| 122 | bool FarScale(::aos::monotonic_clock::time_point start_time); |
| 123 | |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 124 | bool WaitForArmTrajectoryOrDriveClose(double drive_threshold, |
| 125 | double arm_threshold) { |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 126 | ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency, |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 127 | event_loop()->monotonic_now(), |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 128 | ActorBase::kLoopOffset); |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 129 | |
| 130 | constexpr double kPositionTolerance = 0.02; |
| 131 | constexpr double kProfileTolerance = 0.001; |
| 132 | |
| 133 | while (true) { |
| 134 | if (ShouldCancel()) { |
| 135 | return false; |
| 136 | } |
| 137 | |
Austin Schuh | d845c97 | 2019-06-29 21:20:05 -0700 | [diff] [blame] | 138 | superstructure_status_fetcher_.Fetch(); |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 139 | drivetrain_status_fetcher_.Fetch(); |
| 140 | if (drivetrain_status_fetcher_.get() && |
Austin Schuh | d845c97 | 2019-06-29 21:20:05 -0700 | [diff] [blame] | 141 | superstructure_status_fetcher_.get()) { |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 142 | const double left_profile_error = |
| 143 | (initial_drivetrain_.left - |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 144 | drivetrain_status_fetcher_->profiled_left_position_goal()); |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 145 | const double right_profile_error = |
| 146 | (initial_drivetrain_.right - |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 147 | drivetrain_status_fetcher_->profiled_right_position_goal()); |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 148 | |
| 149 | const double left_error = |
| 150 | (initial_drivetrain_.left - |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 151 | drivetrain_status_fetcher_->estimated_left_position()); |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 152 | const double right_error = |
| 153 | (initial_drivetrain_.right - |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 154 | drivetrain_status_fetcher_->estimated_right_position()); |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 155 | |
| 156 | const double profile_distance_to_go = |
| 157 | (left_profile_error + right_profile_error) / 2.0; |
| 158 | |
| 159 | const double distance_to_go = (left_error + right_error) / 2.0; |
| 160 | |
| 161 | // Check superstructure first. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 162 | if (superstructure_status_fetcher_->arm()->current_node() == |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 163 | arm_goal_position_ && |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 164 | superstructure_status_fetcher_->arm()->path_distance_to_go() < |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 165 | arm_threshold) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 166 | AOS_LOG(INFO, "Arm finished first: %f, drivetrain %f distance\n", |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 167 | superstructure_status_fetcher_->arm()->path_distance_to_go(), |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 168 | ::std::abs(distance_to_go)); |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 169 | return true; |
| 170 | } |
| 171 | |
| 172 | // Now check drivetrain. |
| 173 | if (::std::abs(profile_distance_to_go) < |
| 174 | drive_threshold + kProfileTolerance && |
| 175 | ::std::abs(distance_to_go) < drive_threshold + kPositionTolerance) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 176 | AOS_LOG(INFO, |
| 177 | "Drivetrain finished first: arm %f, drivetrain %f distance\n", |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 178 | superstructure_status_fetcher_->arm()->path_distance_to_go(), |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 179 | ::std::abs(distance_to_go)); |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 180 | return true; |
| 181 | } |
| 182 | } |
| 183 | phased_loop.SleepUntilNext(); |
| 184 | } |
| 185 | } |
| 186 | |
Austin Schuh | c231df4 | 2018-03-21 20:43:24 -0700 | [diff] [blame] | 187 | bool WaitForArmTrajectoryClose(double threshold) { |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 188 | ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency, |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 189 | event_loop()->monotonic_now(), |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 190 | ActorBase::kLoopOffset); |
Austin Schuh | c231df4 | 2018-03-21 20:43:24 -0700 | [diff] [blame] | 191 | while (true) { |
| 192 | if (ShouldCancel()) { |
| 193 | return false; |
| 194 | } |
| 195 | |
Austin Schuh | d845c97 | 2019-06-29 21:20:05 -0700 | [diff] [blame] | 196 | superstructure_status_fetcher_.Fetch(); |
| 197 | if (superstructure_status_fetcher_.get()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 198 | if (superstructure_status_fetcher_->arm()->current_node() == |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 199 | arm_goal_position_ && |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 200 | superstructure_status_fetcher_->arm()->path_distance_to_go() < |
Austin Schuh | d845c97 | 2019-06-29 21:20:05 -0700 | [diff] [blame] | 201 | threshold) { |
Austin Schuh | c231df4 | 2018-03-21 20:43:24 -0700 | [diff] [blame] | 202 | return true; |
| 203 | } |
| 204 | } |
| 205 | phased_loop.SleepUntilNext(); |
| 206 | } |
| 207 | } |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 208 | |
| 209 | bool WaitForBoxGrabed() { |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 210 | ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency, |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 211 | event_loop()->monotonic_now(), |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 212 | ActorBase::kLoopOffset); |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 213 | while (true) { |
| 214 | if (ShouldCancel()) { |
| 215 | return false; |
| 216 | } |
| 217 | |
Austin Schuh | d845c97 | 2019-06-29 21:20:05 -0700 | [diff] [blame] | 218 | superstructure_status_fetcher_.Fetch(); |
| 219 | if (superstructure_status_fetcher_.get()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 220 | if (superstructure_status_fetcher_->arm()->grab_state() == 4) { |
Austin Schuh | f79c0e5 | 2018-04-04 20:13:21 -0700 | [diff] [blame] | 221 | return true; |
| 222 | } |
| 223 | } |
| 224 | phased_loop.SleepUntilNext(); |
| 225 | } |
| 226 | } |
Austin Schuh | a3c148e | 2018-03-09 21:04:05 -0800 | [diff] [blame] | 227 | }; |
| 228 | |
| 229 | } // namespace actors |
| 230 | } // namespace y2018 |
| 231 | |
| 232 | #endif // Y2018_ACTORS_AUTONOMOUS_ACTOR_H_ |