blob: 51a5da7da2eba5aa67d2af720550f11a484960d0 [file] [log] [blame]
Tyler Chatowf31da682017-01-22 01:39:40 +00001#ifndef Y2017_ACTORS_AUTONOMOUS_ACTOR_H_
2#define Y2017_ACTORS_AUTONOMOUS_ACTOR_H_
3
4#include <chrono>
5#include <memory>
6
John Park33858a32018-09-28 23:05:48 -07007#include "aos/actions/actions.h"
8#include "aos/actions/actor.h"
Philipp Schrader996a2a22017-02-22 05:02:48 +00009#include "frc971/autonomous/base_autonomous_actor.h"
Tyler Chatowf31da682017-01-22 01:39:40 +000010#include "frc971/control_loops/drivetrain/drivetrain_config.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070011#include "y2017/control_loops/superstructure/superstructure_goal_generated.h"
12#include "y2017/control_loops/superstructure/superstructure_status_generated.h"
Tyler Chatowf31da682017-01-22 01:39:40 +000013
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080014namespace y2017::actors {
Tyler Chatowf31da682017-01-22 01:39:40 +000015using ::frc971::ProfileParameters;
16
Philipp Schrader996a2a22017-02-22 05:02:48 +000017class AutonomousActor : public ::frc971::autonomous::BaseAutonomousActor {
Tyler Chatowf31da682017-01-22 01:39:40 +000018 public:
Austin Schuh1bf8a212019-05-26 22:13:14 -070019 explicit AutonomousActor(::aos::EventLoop *event_loop);
Tyler Chatowf31da682017-01-22 01:39:40 +000020
Philipp Schrader996a2a22017-02-22 05:02:48 +000021 bool RunAction(
Alex Perrycb7da4b2019-08-28 19:35:56 -070022 const ::frc971::autonomous::AutonomousActionParams *params) override;
Austin Schuhbfb04122019-05-22 21:16:51 -070023
Tyler Chatowf31da682017-01-22 01:39:40 +000024 private:
Austin Schuh624088a2017-03-22 22:36:16 -070025 void Reset() {
26 intake_goal_ = 0.0;
27 turret_goal_ = 0.0;
28 vision_track_ = false;
29 hood_goal_ = 0.6;
30 shooter_velocity_ = 0.0;
31 indexer_angular_velocity_ = 0.0;
32 intake_max_velocity_ = 0.5;
Austin Schuh10475d72017-04-16 19:17:48 -070033 gear_servo_ = 0.66;
34 use_vision_for_shots_ = false;
Austin Schuh624088a2017-03-22 22:36:16 -070035 InitializeEncoders();
36 ResetDrivetrain();
37 SendSuperstructureGoal();
38 }
39
Alex Perrycb7da4b2019-08-28 19:35:56 -070040 ::aos::Fetcher<::y2017::control_loops::superstructure::Status>
Austin Schuh402722c2019-06-29 21:27:06 -070041 superstructure_status_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -070042 ::aos::Sender<::y2017::control_loops::superstructure::Goal>
Austin Schuh402722c2019-06-29 21:27:06 -070043 superstructure_goal_sender_;
44
Austin Schuh624088a2017-03-22 22:36:16 -070045 double intake_goal_ = 0.0;
46 double turret_goal_ = 0.0;
47 bool vision_track_ = false;
48 double hood_goal_ = 0.6;
49 double shooter_velocity_ = 0.0;
50 double indexer_angular_velocity_ = 0.0;
51 double intake_max_velocity_ = 0.5;
Austin Schuh10475d72017-04-16 19:17:48 -070052 double gear_servo_ = 0.66;
53 bool use_vision_for_shots_ = false;
Austin Schuh624088a2017-03-22 22:36:16 -070054
55 void set_intake_goal(double intake_goal) { intake_goal_ = intake_goal; }
56 void set_turret_goal(double turret_goal) { turret_goal_ = turret_goal; }
57 void set_vision_track(bool vision_track) { vision_track_ = vision_track; }
58 void set_hood_goal(double hood_goal) { hood_goal_ = hood_goal; }
59 void set_shooter_velocity(double shooter_velocity) {
60 shooter_velocity_ = shooter_velocity;
61 }
62 void set_indexer_angular_velocity(double indexer_angular_velocity) {
63 indexer_angular_velocity_ = indexer_angular_velocity;
64 }
65 void set_intake_max_velocity(double intake_max_velocity) {
66 intake_max_velocity_ = intake_max_velocity;
67 }
Philipp Schrader790cb542023-07-05 21:06:52 -070068 void set_gear_servo(double gear_servo) { gear_servo_ = gear_servo; }
Austin Schuh10475d72017-04-16 19:17:48 -070069 void set_use_vision_for_shots(bool use_vision_for_shots) {
70 use_vision_for_shots_ = use_vision_for_shots;
71 }
Austin Schuh624088a2017-03-22 22:36:16 -070072
Austin Schuh19e15d72017-06-24 13:34:47 -070073 void WaitForHoodZeroed() {
Yash Chainania6fe97b2021-12-15 21:01:11 -080074 ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency,
Austin Schuhd32b3622019-06-23 18:49:06 -070075 event_loop()->monotonic_now(),
Stephan Pleines743f83a2024-02-02 18:32:09 -080076 aos::common::actions::kLoopOffset);
Austin Schuh19e15d72017-06-24 13:34:47 -070077 while (true) {
78 if (ShouldCancel()) return;
79
Austin Schuh402722c2019-06-29 21:27:06 -070080 superstructure_status_fetcher_.Fetch();
81 if (superstructure_status_fetcher_.get()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070082 if (superstructure_status_fetcher_->hood()->zeroed()) {
Austin Schuh19e15d72017-06-24 13:34:47 -070083 return;
84 }
85 }
86 phased_loop.SleepUntilNext();
87 }
88 }
89
Austin Schuh624088a2017-03-22 22:36:16 -070090 void SendSuperstructureGoal() {
Alex Perrycb7da4b2019-08-28 19:35:56 -070091 auto builder = superstructure_goal_sender_.MakeBuilder();
Austin Schuh624088a2017-03-22 22:36:16 -070092
Alex Perrycb7da4b2019-08-28 19:35:56 -070093 flatbuffers::Offset<frc971::ProfileParameters>
94 intake_profile_parameters_offset = frc971::CreateProfileParameters(
95 *builder.fbb(), intake_max_velocity_, 5.0);
Austin Schuh624088a2017-03-22 22:36:16 -070096
Alex Perrycb7da4b2019-08-28 19:35:56 -070097 control_loops::superstructure::IntakeGoal::Builder intake_builder =
98 builder.MakeBuilder<control_loops::superstructure::IntakeGoal>();
99 intake_builder.add_distance(intake_goal_);
100 intake_builder.add_disable_intake(false);
101 intake_builder.add_profile_params(intake_profile_parameters_offset);
102 intake_builder.add_voltage_rollers(0.0);
103 intake_builder.add_disable_intake(false);
104 intake_builder.add_gear_servo(gear_servo_);
105 flatbuffers::Offset<control_loops::superstructure::IntakeGoal>
106 intake_offset = intake_builder.Finish();
Austin Schuh624088a2017-03-22 22:36:16 -0700107
Alex Perrycb7da4b2019-08-28 19:35:56 -0700108 control_loops::superstructure::IndexerGoal::Builder indexer_builder =
109 builder.MakeBuilder<control_loops::superstructure::IndexerGoal>();
110 indexer_builder.add_angular_velocity(indexer_angular_velocity_);
111 indexer_builder.add_voltage_rollers(0.0);
112 flatbuffers::Offset<control_loops::superstructure::IndexerGoal>
113 indexer_offset = indexer_builder.Finish();
Austin Schuh624088a2017-03-22 22:36:16 -0700114
Alex Perrycb7da4b2019-08-28 19:35:56 -0700115 flatbuffers::Offset<frc971::ProfileParameters>
Philipp Schrader790cb542023-07-05 21:06:52 -0700116 turret_profile_parameters_offset =
117 frc971::CreateProfileParameters(*builder.fbb(), 6.0, 15.0);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700118 control_loops::superstructure::TurretGoal::Builder turret_builder =
119 builder.MakeBuilder<control_loops::superstructure::TurretGoal>();
120 turret_builder.add_angle(turret_goal_);
121 turret_builder.add_track(vision_track_);
122 turret_builder.add_profile_params(turret_profile_parameters_offset);
Philipp Schrader790cb542023-07-05 21:06:52 -0700123 flatbuffers::Offset<control_loops::superstructure::TurretGoal>
124 turret_offset = turret_builder.Finish();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700125
126 flatbuffers::Offset<frc971::ProfileParameters>
Philipp Schrader790cb542023-07-05 21:06:52 -0700127 hood_profile_parameters_offset =
128 frc971::CreateProfileParameters(*builder.fbb(), 5.0, 25.0);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700129 control_loops::superstructure::HoodGoal::Builder hood_builder =
130 builder.MakeBuilder<control_loops::superstructure::HoodGoal>();
131 hood_builder.add_angle(hood_goal_);
132 hood_builder.add_profile_params(hood_profile_parameters_offset);
133 flatbuffers::Offset<control_loops::superstructure::HoodGoal> hood_offset =
134 hood_builder.Finish();
135
136 control_loops::superstructure::ShooterGoal::Builder shooter_builder =
137 builder.MakeBuilder<control_loops::superstructure::ShooterGoal>();
138 shooter_builder.add_angular_velocity(shooter_velocity_);
139 flatbuffers::Offset<control_loops::superstructure::ShooterGoal>
140 shooter_offset = shooter_builder.Finish();
141
142 control_loops::superstructure::Goal::Builder goal_builder =
143 builder.MakeBuilder<control_loops::superstructure::Goal>();
144 goal_builder.add_lights_on(true);
145 goal_builder.add_use_vision_for_shots(use_vision_for_shots_);
146 goal_builder.add_intake(intake_offset);
147 goal_builder.add_indexer(indexer_offset);
148 goal_builder.add_turret(turret_offset);
149 goal_builder.add_hood(hood_offset);
150 goal_builder.add_shooter(shooter_offset);
151
152 flatbuffers::Offset<control_loops::superstructure::Goal> goal_offset =
153 goal_builder.Finish();
154
155 control_loops::superstructure::Goal *goal =
156 GetMutableTemporaryPointer(*builder.fbb(), goal_offset);
Austin Schuh624088a2017-03-22 22:36:16 -0700157
158 if (indexer_angular_velocity_ < -0.1) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700159 goal->mutable_indexer()->mutate_voltage_rollers(12.0);
160 goal->mutable_intake()->mutate_voltage_rollers(6.0);
Austin Schuh624088a2017-03-22 22:36:16 -0700161 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700162 goal->mutable_indexer()->mutate_voltage_rollers(0.0);
Austin Schuh624088a2017-03-22 22:36:16 -0700163 }
164
milind1f1dca32021-07-03 13:50:07 -0700165 if (builder.Send(goal_offset) != aos::RawSender::Error::kOk) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700166 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
Austin Schuh624088a2017-03-22 22:36:16 -0700167 }
168 }
Tyler Chatowf31da682017-01-22 01:39:40 +0000169};
170
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -0800171} // namespace y2017::actors
Tyler Chatowf31da682017-01-22 01:39:40 +0000172
173#endif // Y2017_ACTORS_AUTONOMOUS_ACTOR_H_