James (Peilun) Li | a70e575 | 2024-09-18 20:43:00 -0700 | [diff] [blame] | 1 | #include <unistd.h> |
| 2 | |
| 3 | #include <array> |
| 4 | #include <chrono> |
| 5 | #include <cinttypes> |
| 6 | #include <cstdio> |
| 7 | #include <cstring> |
| 8 | #include <functional> |
| 9 | #include <memory> |
| 10 | #include <mutex> |
| 11 | #include <thread> |
| 12 | |
| 13 | #include "absl/flags/flag.h" |
| 14 | |
| 15 | #include "frc971/wpilib/ahal/AnalogInput.h" |
| 16 | #include "frc971/wpilib/ahal/DriverStation.h" |
| 17 | #include "frc971/wpilib/ahal/Encoder.h" |
| 18 | #include "frc971/wpilib/ahal/TalonFX.h" |
| 19 | #include "frc971/wpilib/ahal/VictorSP.h" |
| 20 | #undef ERROR |
| 21 | |
| 22 | #include "ctre/phoenix/cci/Diagnostics_CCI.h" |
| 23 | |
| 24 | #include "aos/commonmath.h" |
| 25 | #include "aos/containers/sized_array.h" |
| 26 | #include "aos/events/event_loop.h" |
| 27 | #include "aos/events/shm_event_loop.h" |
| 28 | #include "aos/init.h" |
| 29 | #include "aos/logging/logging.h" |
| 30 | #include "aos/realtime.h" |
| 31 | #include "aos/time/time.h" |
| 32 | #include "aos/util/log_interval.h" |
| 33 | #include "aos/util/phased_loop.h" |
| 34 | #include "aos/util/wrapping_counter.h" |
| 35 | #include "frc971/can_configuration_generated.h" |
| 36 | #include "frc971/constants/constants_sender_lib.h" |
| 37 | #include "frc971/input/robot_state_generated.h" |
| 38 | #include "frc971/queues/gyro_generated.h" |
| 39 | #include "frc971/wpilib/buffered_pcm.h" |
| 40 | #include "frc971/wpilib/buffered_solenoid.h" |
| 41 | #include "frc971/wpilib/can_sensor_reader.h" |
| 42 | #include "frc971/wpilib/dma.h" |
| 43 | #include "frc971/wpilib/encoder_and_potentiometer.h" |
| 44 | #include "frc971/wpilib/generic_can_writer.h" |
| 45 | #include "frc971/wpilib/joystick_sender.h" |
| 46 | #include "frc971/wpilib/logging_generated.h" |
| 47 | #include "frc971/wpilib/loop_output_handler.h" |
| 48 | #include "frc971/wpilib/pdp_fetcher.h" |
| 49 | #include "frc971/wpilib/sensor_reader.h" |
| 50 | #include "frc971/wpilib/talonfx.h" |
| 51 | #include "frc971/wpilib/wpilib_robot_base.h" |
| 52 | #include "y2024_bot3/constants.h" |
| 53 | #include "y2024_bot3/constants/constants_generated.h" |
| 54 | #include "y2024_bot3/control_loops/superstructure/superstructure_can_position_static.h" |
| 55 | #include "y2024_bot3/control_loops/superstructure/superstructure_output_generated.h" |
| 56 | #include "y2024_bot3/control_loops/superstructure/superstructure_position_generated.h" |
| 57 | #include "y2024_bot3/control_loops/superstructure/superstructure_position_static.h" |
| 58 | |
| 59 | ABSL_FLAG(bool, ctre_diag_server, false, |
| 60 | "If true, enable the diagnostics server for interacting with " |
| 61 | "devices on the CAN bus using Phoenix Tuner"); |
| 62 | |
| 63 | using ::aos::monotonic_clock; |
| 64 | using ::frc971::CANConfiguration; |
| 65 | using ::frc971::wpilib::TalonFX; |
| 66 | using ::y2024_bot3::constants::Values; |
| 67 | namespace superstructure = ::y2024_bot3::control_loops::superstructure; |
| 68 | namespace chrono = ::std::chrono; |
| 69 | using std::make_unique; |
| 70 | |
| 71 | namespace y2024_bot3::wpilib { |
| 72 | namespace { |
| 73 | |
| 74 | constexpr double kMaxBringupPower = 12.0; |
| 75 | |
| 76 | constexpr double kMaxFastEncoderPulsesPerSecond = std::max({ |
| 77 | 1000000 // arbitrary number because we deleted all the stuff in this array |
| 78 | }); |
| 79 | |
| 80 | static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000, |
| 81 | "fast encoders are too fast"); |
| 82 | |
| 83 | } // namespace |
| 84 | |
| 85 | // Class to send position messages with sensor readings to our loops. |
| 86 | class SensorReader : public ::frc971::wpilib::SensorReader { |
| 87 | public: |
| 88 | SensorReader(::aos::ShmEventLoop *event_loop, |
| 89 | const Constants *robot_constants) |
| 90 | : ::frc971::wpilib::SensorReader(event_loop), |
| 91 | robot_constants_(robot_constants), |
| 92 | superstructure_position_sender_( |
| 93 | event_loop->MakeSender<superstructure::PositionStatic>( |
| 94 | "/superstructure")) { |
| 95 | UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond); |
| 96 | event_loop->SetRuntimeAffinity(aos::MakeCpusetFromCpus({0})); |
| 97 | }; |
| 98 | void Start() override { AddToDMA(&imu_yaw_rate_reader_); } |
| 99 | |
| 100 | void set_yaw_rate_input(::std::unique_ptr<frc::DigitalInput> sensor) { |
| 101 | imu_yaw_rate_input_ = ::std::move(sensor); |
| 102 | imu_yaw_rate_reader_.set_input(imu_yaw_rate_input_.get()); |
| 103 | } |
| 104 | |
| 105 | void RunIteration() override { |
| 106 | aos::Sender<superstructure::PositionStatic>::StaticBuilder builder = |
| 107 | superstructure_position_sender_.MakeStaticBuilder(); |
| 108 | |
| 109 | builder.CheckOk(builder.Send()); |
| 110 | |
| 111 | { |
| 112 | auto builder = gyro_sender_.MakeBuilder(); |
| 113 | ::frc971::sensors::GyroReading::Builder gyro_reading_builder = |
| 114 | builder.MakeBuilder<::frc971::sensors::GyroReading>(); |
| 115 | |
| 116 | builder.CheckOk(builder.Send(gyro_reading_builder.Finish())); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | private: |
| 121 | const Constants *robot_constants_; |
| 122 | |
| 123 | aos::Sender<superstructure::PositionStatic> superstructure_position_sender_; |
| 124 | ::aos::Sender<::frc971::sensors::GyroReading> gyro_sender_; |
| 125 | |
| 126 | std::unique_ptr<frc::DigitalInput> imu_yaw_rate_input_; |
| 127 | |
| 128 | frc971::wpilib::DMAPulseWidthReader imu_yaw_rate_reader_; |
| 129 | }; |
| 130 | |
| 131 | class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase { |
| 132 | public: |
| 133 | ::std::unique_ptr<frc::Encoder> make_encoder(int index) { |
| 134 | return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false, |
| 135 | frc::Encoder::k4X); |
| 136 | } |
| 137 | |
| 138 | void Run() override { |
| 139 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 140 | aos::configuration::ReadConfig("aos_config.json"); |
| 141 | |
| 142 | frc971::constants::WaitForConstants<y2024_bot3::Constants>( |
| 143 | &config.message()); |
| 144 | |
| 145 | ::aos::ShmEventLoop constant_fetcher_event_loop(&config.message()); |
| 146 | frc971::constants::ConstantsFetcher<Constants> constants_fetcher( |
| 147 | &constant_fetcher_event_loop); |
| 148 | const Constants *robot_constants = &constants_fetcher.constants(); |
| 149 | |
| 150 | AddLoop(&constant_fetcher_event_loop); |
| 151 | |
| 152 | // Thread 1. |
| 153 | ::aos::ShmEventLoop joystick_sender_event_loop(&config.message()); |
| 154 | ::frc971::wpilib::JoystickSender joystick_sender( |
| 155 | &joystick_sender_event_loop); |
| 156 | AddLoop(&joystick_sender_event_loop); |
| 157 | |
| 158 | // Thread 2. |
| 159 | ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message()); |
| 160 | ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop); |
| 161 | AddLoop(&pdp_fetcher_event_loop); |
| 162 | |
| 163 | // Thread 3. |
| 164 | ::aos::ShmEventLoop sensor_reader_event_loop(&config.message()); |
| 165 | SensorReader sensor_reader(&sensor_reader_event_loop, robot_constants); |
| 166 | sensor_reader.set_pwm_trigger(false); |
| 167 | sensor_reader.set_yaw_rate_input(make_unique<frc::DigitalInput>(25)); |
| 168 | |
| 169 | AddLoop(&sensor_reader_event_loop); |
| 170 | |
| 171 | // Thread 4. |
| 172 | // Set up CAN. |
| 173 | if (!absl::GetFlag(FLAGS_ctre_diag_server)) { |
| 174 | c_Phoenix_Diagnostics_SetSecondsToStart(-1); |
| 175 | c_Phoenix_Diagnostics_Dispose(); |
| 176 | } |
| 177 | |
| 178 | std::vector<ctre::phoenix6::BaseStatusSignal *> canivore_signal_registry; |
| 179 | std::vector<ctre::phoenix6::BaseStatusSignal *> rio_signal_registry; |
| 180 | |
| 181 | ::aos::ShmEventLoop can_sensor_reader_event_loop(&config.message()); |
| 182 | can_sensor_reader_event_loop.set_name("CANSensorReader"); |
| 183 | |
| 184 | ::aos::ShmEventLoop rio_sensor_reader_event_loop(&config.message()); |
| 185 | rio_sensor_reader_event_loop.set_name("RioSensorReader"); |
| 186 | |
| 187 | // Creating list of talonfx for CANSensorReader |
| 188 | std::vector<std::shared_ptr<TalonFX>> canivore_talonfxs; |
| 189 | std::vector<std::shared_ptr<TalonFX>> rio_talonfxs; |
| 190 | |
| 191 | aos::Sender<y2024_bot3::control_loops::superstructure::CANPositionStatic> |
| 192 | superstructure_can_position_sender = |
| 193 | can_sensor_reader_event_loop.MakeSender< |
| 194 | y2024_bot3::control_loops::superstructure::CANPositionStatic>( |
| 195 | "/superstructure/canivore"); |
| 196 | |
| 197 | aos::Sender<y2024_bot3::control_loops::superstructure::CANPositionStatic> |
| 198 | superstructure_rio_position_sender = |
| 199 | rio_sensor_reader_event_loop.MakeSender< |
| 200 | y2024_bot3::control_loops::superstructure::CANPositionStatic>( |
| 201 | "/superstructure/rio"); |
| 202 | |
| 203 | frc971::wpilib::CANSensorReader rio_can_sensor_reader( |
| 204 | &rio_sensor_reader_event_loop, std::move(rio_signal_registry), |
| 205 | rio_talonfxs, |
| 206 | [&superstructure_rio_position_sender]( |
| 207 | ctre::phoenix::StatusCode status) { |
| 208 | aos::Sender< |
| 209 | y2024_bot3::control_loops::superstructure::CANPositionStatic>:: |
| 210 | StaticBuilder superstructure_can_builder = |
| 211 | superstructure_rio_position_sender.MakeStaticBuilder(); |
| 212 | |
| 213 | superstructure_can_builder->set_status(static_cast<int>(status)); |
| 214 | superstructure_can_builder.CheckOk(superstructure_can_builder.Send()); |
| 215 | }, |
| 216 | frc971::wpilib::CANSensorReader::SignalSync::kNoSync); |
| 217 | |
| 218 | AddLoop(&can_sensor_reader_event_loop); |
| 219 | AddLoop(&rio_sensor_reader_event_loop); |
| 220 | |
| 221 | // Thread 5. |
| 222 | ::aos::ShmEventLoop can_output_event_loop(&config.message()); |
| 223 | can_output_event_loop.set_name("CANOutputWriter"); |
| 224 | |
| 225 | frc971::wpilib::GenericCANWriter<control_loops::superstructure::Output> |
| 226 | can_superstructure_writer( |
| 227 | &can_output_event_loop, |
| 228 | [](const control_loops::superstructure::Output &output, |
| 229 | const std::map<std::string_view, std::shared_ptr<TalonFX>> |
| 230 | &talonfx_map) { |
| 231 | (void)output; |
| 232 | (void)talonfx_map; |
| 233 | }); |
| 234 | |
| 235 | can_output_event_loop.MakeWatcher( |
| 236 | "/roborio", [&can_superstructure_writer]( |
| 237 | const frc971::CANConfiguration &configuration) { |
| 238 | can_superstructure_writer.HandleCANConfiguration(configuration); |
| 239 | }); |
| 240 | |
| 241 | AddLoop(&can_output_event_loop); |
| 242 | |
| 243 | RunLoops(); |
| 244 | } |
| 245 | }; |
| 246 | |
| 247 | } // namespace y2024_bot3::wpilib |
| 248 | |
| 249 | AOS_ROBOT_CLASS(::y2024_bot3::wpilib::WPILibRobot); |