Tyler Chatow | f31da68 | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 1 | #ifndef Y2017_ACTORS_AUTONOMOUS_ACTOR_H_ |
| 2 | #define Y2017_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" |
Philipp Schrader | 996a2a2 | 2017-02-22 05:02:48 +0000 | [diff] [blame] | 9 | #include "frc971/autonomous/base_autonomous_actor.h" |
Tyler Chatow | f31da68 | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 10 | #include "frc971/control_loops/drivetrain/drivetrain_config.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 11 | #include "y2017/control_loops/superstructure/superstructure_goal_generated.h" |
| 12 | #include "y2017/control_loops/superstructure/superstructure_status_generated.h" |
Tyler Chatow | f31da68 | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 13 | |
| 14 | namespace y2017 { |
| 15 | namespace actors { |
| 16 | using ::frc971::ProfileParameters; |
| 17 | |
Philipp Schrader | 996a2a2 | 2017-02-22 05:02:48 +0000 | [diff] [blame] | 18 | class AutonomousActor : public ::frc971::autonomous::BaseAutonomousActor { |
Tyler Chatow | f31da68 | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 19 | public: |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 20 | explicit AutonomousActor(::aos::EventLoop *event_loop); |
Tyler Chatow | f31da68 | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 21 | |
Philipp Schrader | 996a2a2 | 2017-02-22 05:02:48 +0000 | [diff] [blame] | 22 | bool RunAction( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 23 | const ::frc971::autonomous::AutonomousActionParams *params) override; |
Austin Schuh | bfb0412 | 2019-05-22 21:16:51 -0700 | [diff] [blame] | 24 | |
Tyler Chatow | f31da68 | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 25 | private: |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame] | 26 | void Reset() { |
| 27 | intake_goal_ = 0.0; |
| 28 | turret_goal_ = 0.0; |
| 29 | vision_track_ = false; |
| 30 | hood_goal_ = 0.6; |
| 31 | shooter_velocity_ = 0.0; |
| 32 | indexer_angular_velocity_ = 0.0; |
| 33 | intake_max_velocity_ = 0.5; |
Austin Schuh | 10475d7 | 2017-04-16 19:17:48 -0700 | [diff] [blame] | 34 | gear_servo_ = 0.66; |
| 35 | use_vision_for_shots_ = false; |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame] | 36 | InitializeEncoders(); |
| 37 | ResetDrivetrain(); |
| 38 | SendSuperstructureGoal(); |
| 39 | } |
| 40 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 41 | ::aos::Fetcher<::y2017::control_loops::superstructure::Status> |
Austin Schuh | 402722c | 2019-06-29 21:27:06 -0700 | [diff] [blame] | 42 | superstructure_status_fetcher_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 43 | ::aos::Sender<::y2017::control_loops::superstructure::Goal> |
Austin Schuh | 402722c | 2019-06-29 21:27:06 -0700 | [diff] [blame] | 44 | superstructure_goal_sender_; |
| 45 | |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame] | 46 | double intake_goal_ = 0.0; |
| 47 | double turret_goal_ = 0.0; |
| 48 | bool vision_track_ = false; |
| 49 | double hood_goal_ = 0.6; |
| 50 | double shooter_velocity_ = 0.0; |
| 51 | double indexer_angular_velocity_ = 0.0; |
| 52 | double intake_max_velocity_ = 0.5; |
Austin Schuh | 10475d7 | 2017-04-16 19:17:48 -0700 | [diff] [blame] | 53 | double gear_servo_ = 0.66; |
| 54 | bool use_vision_for_shots_ = false; |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame] | 55 | |
| 56 | void set_intake_goal(double intake_goal) { intake_goal_ = intake_goal; } |
| 57 | void set_turret_goal(double turret_goal) { turret_goal_ = turret_goal; } |
| 58 | void set_vision_track(bool vision_track) { vision_track_ = vision_track; } |
| 59 | void set_hood_goal(double hood_goal) { hood_goal_ = hood_goal; } |
| 60 | void set_shooter_velocity(double shooter_velocity) { |
| 61 | shooter_velocity_ = shooter_velocity; |
| 62 | } |
| 63 | void set_indexer_angular_velocity(double indexer_angular_velocity) { |
| 64 | indexer_angular_velocity_ = indexer_angular_velocity; |
| 65 | } |
| 66 | void set_intake_max_velocity(double intake_max_velocity) { |
| 67 | intake_max_velocity_ = intake_max_velocity; |
| 68 | } |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 69 | void set_gear_servo(double gear_servo) { gear_servo_ = gear_servo; } |
Austin Schuh | 10475d7 | 2017-04-16 19:17:48 -0700 | [diff] [blame] | 70 | void set_use_vision_for_shots(bool use_vision_for_shots) { |
| 71 | use_vision_for_shots_ = use_vision_for_shots; |
| 72 | } |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame] | 73 | |
Austin Schuh | 19e15d7 | 2017-06-24 13:34:47 -0700 | [diff] [blame] | 74 | void WaitForHoodZeroed() { |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 75 | ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency, |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 76 | event_loop()->monotonic_now(), |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 77 | ActorBase::kLoopOffset); |
Austin Schuh | 19e15d7 | 2017-06-24 13:34:47 -0700 | [diff] [blame] | 78 | while (true) { |
| 79 | if (ShouldCancel()) return; |
| 80 | |
Austin Schuh | 402722c | 2019-06-29 21:27:06 -0700 | [diff] [blame] | 81 | superstructure_status_fetcher_.Fetch(); |
| 82 | if (superstructure_status_fetcher_.get()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 83 | if (superstructure_status_fetcher_->hood()->zeroed()) { |
Austin Schuh | 19e15d7 | 2017-06-24 13:34:47 -0700 | [diff] [blame] | 84 | return; |
| 85 | } |
| 86 | } |
| 87 | phased_loop.SleepUntilNext(); |
| 88 | } |
| 89 | } |
| 90 | |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame] | 91 | void SendSuperstructureGoal() { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 92 | auto builder = superstructure_goal_sender_.MakeBuilder(); |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame] | 93 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 94 | flatbuffers::Offset<frc971::ProfileParameters> |
| 95 | intake_profile_parameters_offset = frc971::CreateProfileParameters( |
| 96 | *builder.fbb(), intake_max_velocity_, 5.0); |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame] | 97 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 98 | control_loops::superstructure::IntakeGoal::Builder intake_builder = |
| 99 | builder.MakeBuilder<control_loops::superstructure::IntakeGoal>(); |
| 100 | intake_builder.add_distance(intake_goal_); |
| 101 | intake_builder.add_disable_intake(false); |
| 102 | intake_builder.add_profile_params(intake_profile_parameters_offset); |
| 103 | intake_builder.add_voltage_rollers(0.0); |
| 104 | intake_builder.add_disable_intake(false); |
| 105 | intake_builder.add_gear_servo(gear_servo_); |
| 106 | flatbuffers::Offset<control_loops::superstructure::IntakeGoal> |
| 107 | intake_offset = intake_builder.Finish(); |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame] | 108 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 109 | control_loops::superstructure::IndexerGoal::Builder indexer_builder = |
| 110 | builder.MakeBuilder<control_loops::superstructure::IndexerGoal>(); |
| 111 | indexer_builder.add_angular_velocity(indexer_angular_velocity_); |
| 112 | indexer_builder.add_voltage_rollers(0.0); |
| 113 | flatbuffers::Offset<control_loops::superstructure::IndexerGoal> |
| 114 | indexer_offset = indexer_builder.Finish(); |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame] | 115 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 116 | flatbuffers::Offset<frc971::ProfileParameters> |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 117 | turret_profile_parameters_offset = |
| 118 | frc971::CreateProfileParameters(*builder.fbb(), 6.0, 15.0); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 119 | control_loops::superstructure::TurretGoal::Builder turret_builder = |
| 120 | builder.MakeBuilder<control_loops::superstructure::TurretGoal>(); |
| 121 | turret_builder.add_angle(turret_goal_); |
| 122 | turret_builder.add_track(vision_track_); |
| 123 | turret_builder.add_profile_params(turret_profile_parameters_offset); |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 124 | flatbuffers::Offset<control_loops::superstructure::TurretGoal> |
| 125 | turret_offset = turret_builder.Finish(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 126 | |
| 127 | flatbuffers::Offset<frc971::ProfileParameters> |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 128 | hood_profile_parameters_offset = |
| 129 | frc971::CreateProfileParameters(*builder.fbb(), 5.0, 25.0); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 130 | control_loops::superstructure::HoodGoal::Builder hood_builder = |
| 131 | builder.MakeBuilder<control_loops::superstructure::HoodGoal>(); |
| 132 | hood_builder.add_angle(hood_goal_); |
| 133 | hood_builder.add_profile_params(hood_profile_parameters_offset); |
| 134 | flatbuffers::Offset<control_loops::superstructure::HoodGoal> hood_offset = |
| 135 | hood_builder.Finish(); |
| 136 | |
| 137 | control_loops::superstructure::ShooterGoal::Builder shooter_builder = |
| 138 | builder.MakeBuilder<control_loops::superstructure::ShooterGoal>(); |
| 139 | shooter_builder.add_angular_velocity(shooter_velocity_); |
| 140 | flatbuffers::Offset<control_loops::superstructure::ShooterGoal> |
| 141 | shooter_offset = shooter_builder.Finish(); |
| 142 | |
| 143 | control_loops::superstructure::Goal::Builder goal_builder = |
| 144 | builder.MakeBuilder<control_loops::superstructure::Goal>(); |
| 145 | goal_builder.add_lights_on(true); |
| 146 | goal_builder.add_use_vision_for_shots(use_vision_for_shots_); |
| 147 | goal_builder.add_intake(intake_offset); |
| 148 | goal_builder.add_indexer(indexer_offset); |
| 149 | goal_builder.add_turret(turret_offset); |
| 150 | goal_builder.add_hood(hood_offset); |
| 151 | goal_builder.add_shooter(shooter_offset); |
| 152 | |
| 153 | flatbuffers::Offset<control_loops::superstructure::Goal> goal_offset = |
| 154 | goal_builder.Finish(); |
| 155 | |
| 156 | control_loops::superstructure::Goal *goal = |
| 157 | GetMutableTemporaryPointer(*builder.fbb(), goal_offset); |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame] | 158 | |
| 159 | if (indexer_angular_velocity_ < -0.1) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 160 | goal->mutable_indexer()->mutate_voltage_rollers(12.0); |
| 161 | goal->mutable_intake()->mutate_voltage_rollers(6.0); |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame] | 162 | } else { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 163 | goal->mutable_indexer()->mutate_voltage_rollers(0.0); |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame] | 164 | } |
| 165 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 166 | if (builder.Send(goal_offset) != aos::RawSender::Error::kOk) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 167 | AOS_LOG(ERROR, "Sending superstructure goal failed.\n"); |
Austin Schuh | 624088a | 2017-03-22 22:36:16 -0700 | [diff] [blame] | 168 | } |
| 169 | } |
Tyler Chatow | f31da68 | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 170 | }; |
| 171 | |
Tyler Chatow | f31da68 | 2017-01-22 01:39:40 +0000 | [diff] [blame] | 172 | } // namespace actors |
| 173 | } // namespace y2017 |
| 174 | |
| 175 | #endif // Y2017_ACTORS_AUTONOMOUS_ACTOR_H_ |