Ravago Jones | 486de80 | 2021-05-19 20:47:55 -0700 | [diff] [blame] | 1 | #include <unistd.h> |
| 2 | |
| 3 | #include <array> |
| 4 | #include <chrono> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 5 | #include <cinttypes> |
Ravago Jones | 486de80 | 2021-05-19 20:47:55 -0700 | [diff] [blame] | 6 | #include <cmath> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 7 | #include <cstdio> |
| 8 | #include <cstring> |
Ravago Jones | 486de80 | 2021-05-19 20:47:55 -0700 | [diff] [blame] | 9 | #include <functional> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 10 | #include <memory> |
Ravago Jones | 486de80 | 2021-05-19 20:47:55 -0700 | [diff] [blame] | 11 | #include <mutex> |
| 12 | #include <thread> |
| 13 | |
| 14 | #include "ctre/phoenix/CANifier.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 15 | |
Ravago Jones | 486de80 | 2021-05-19 20:47:55 -0700 | [diff] [blame] | 16 | #include "frc971/wpilib/ahal/AnalogInput.h" |
| 17 | #include "frc971/wpilib/ahal/Counter.h" |
| 18 | #include "frc971/wpilib/ahal/DigitalGlitchFilter.h" |
| 19 | #include "frc971/wpilib/ahal/DriverStation.h" |
| 20 | #include "frc971/wpilib/ahal/Encoder.h" |
| 21 | #include "frc971/wpilib/ahal/TalonFX.h" |
| 22 | #include "frc971/wpilib/ahal/VictorSP.h" |
| 23 | #undef ERROR |
| 24 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 25 | #include "ctre/phoenix/motorcontrol/can/TalonFX.h" |
| 26 | #include "ctre/phoenix/motorcontrol/can/TalonSRX.h" |
| 27 | |
Ravago Jones | 486de80 | 2021-05-19 20:47:55 -0700 | [diff] [blame] | 28 | #include "aos/commonmath.h" |
| 29 | #include "aos/events/event_loop.h" |
| 30 | #include "aos/events/shm_event_loop.h" |
| 31 | #include "aos/init.h" |
| 32 | #include "aos/logging/logging.h" |
Ravago Jones | 486de80 | 2021-05-19 20:47:55 -0700 | [diff] [blame] | 33 | #include "aos/realtime.h" |
Ravago Jones | 486de80 | 2021-05-19 20:47:55 -0700 | [diff] [blame] | 34 | #include "aos/time/time.h" |
| 35 | #include "aos/util/log_interval.h" |
| 36 | #include "aos/util/phased_loop.h" |
| 37 | #include "aos/util/wrapping_counter.h" |
Ravago Jones | 486de80 | 2021-05-19 20:47:55 -0700 | [diff] [blame] | 38 | #include "frc971/autonomous/auto_mode_generated.h" |
| 39 | #include "frc971/control_loops/drivetrain/drivetrain_position_generated.h" |
James Kuszmaul | 7077d34 | 2021-06-09 20:23:58 -0700 | [diff] [blame] | 40 | #include "frc971/input/robot_state_generated.h" |
Ravago Jones | 486de80 | 2021-05-19 20:47:55 -0700 | [diff] [blame] | 41 | #include "frc971/wpilib/ADIS16448.h" |
| 42 | #include "frc971/wpilib/buffered_pcm.h" |
| 43 | #include "frc971/wpilib/buffered_solenoid.h" |
| 44 | #include "frc971/wpilib/dma.h" |
| 45 | #include "frc971/wpilib/drivetrain_writer.h" |
| 46 | #include "frc971/wpilib/encoder_and_potentiometer.h" |
| 47 | #include "frc971/wpilib/joystick_sender.h" |
| 48 | #include "frc971/wpilib/logging_generated.h" |
| 49 | #include "frc971/wpilib/loop_output_handler.h" |
| 50 | #include "frc971/wpilib/pdp_fetcher.h" |
| 51 | #include "frc971/wpilib/sensor_reader.h" |
| 52 | #include "frc971/wpilib/wpilib_robot_base.h" |
| 53 | #include "y2021_bot3/constants.h" |
| 54 | #include "y2021_bot3/control_loops/superstructure/superstructure_output_generated.h" |
| 55 | #include "y2021_bot3/control_loops/superstructure/superstructure_position_generated.h" |
| 56 | |
| 57 | using ::aos::monotonic_clock; |
| 58 | using ::y2021_bot3::constants::Values; |
| 59 | namespace superstructure = ::y2021_bot3::control_loops::superstructure; |
| 60 | namespace chrono = ::std::chrono; |
Vinay Siva | e52a6b3 | 2021-07-10 15:19:26 -0700 | [diff] [blame] | 61 | using std::make_unique; |
Ravago Jones | 486de80 | 2021-05-19 20:47:55 -0700 | [diff] [blame] | 62 | |
| 63 | namespace y2021_bot3 { |
| 64 | namespace wpilib { |
| 65 | namespace { |
| 66 | |
| 67 | constexpr double kMaxBringupPower = 12.0; |
| 68 | |
| 69 | // TODO(Brian): Fix the interpretation of the result of GetRaw here and in the |
| 70 | // DMA stuff and then removing the * 2.0 in *_translate. |
| 71 | // The low bit is direction. |
| 72 | |
| 73 | // TODO(brian): Use ::std::max instead once we have C++14 so that can be |
| 74 | // constexpr. |
| 75 | template <typename T> |
| 76 | constexpr T max(T a, T b) { |
| 77 | return (a > b) ? a : b; |
| 78 | } |
| 79 | |
| 80 | template <typename T, typename... Rest> |
| 81 | constexpr T max(T a, T b, T c, Rest... rest) { |
| 82 | return max(max(a, b), c, rest...); |
| 83 | } |
| 84 | |
| 85 | double drivetrain_translate(int32_t in) { |
| 86 | return ((static_cast<double>(in) / |
| 87 | Values::kDrivetrainEncoderCountsPerRevolution()) * |
| 88 | (2.0 * M_PI)) * |
| 89 | Values::kDrivetrainEncoderRatio() * |
| 90 | control_loops::drivetrain::kWheelRadius; |
| 91 | } |
| 92 | |
| 93 | double drivetrain_velocity_translate(double in) { |
| 94 | return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) * |
| 95 | (2.0 * M_PI)) * |
| 96 | Values::kDrivetrainEncoderRatio() * |
| 97 | control_loops::drivetrain::kWheelRadius; |
| 98 | } |
| 99 | |
| 100 | constexpr double kMaxFastEncoderPulsesPerSecond = |
| 101 | Values::kMaxDrivetrainEncoderPulsesPerSecond(); |
| 102 | static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000, |
| 103 | "fast encoders are too fast"); |
| 104 | constexpr double kMaxMediumEncoderPulsesPerSecond = |
| 105 | kMaxFastEncoderPulsesPerSecond; |
| 106 | |
| 107 | static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000, |
| 108 | "medium encoders are too fast"); |
| 109 | |
| 110 | } // namespace |
| 111 | |
| 112 | // Class to send position messages with sensor readings to our loops. |
| 113 | class SensorReader : public ::frc971::wpilib::SensorReader { |
| 114 | public: |
| 115 | SensorReader(::aos::ShmEventLoop *event_loop) |
| 116 | : ::frc971::wpilib::SensorReader(event_loop), |
| 117 | auto_mode_sender_( |
| 118 | event_loop->MakeSender<::frc971::autonomous::AutonomousMode>( |
| 119 | "/autonomous")), |
| 120 | superstructure_position_sender_( |
| 121 | event_loop->MakeSender<superstructure::Position>( |
| 122 | "/superstructure")), |
| 123 | drivetrain_position_sender_( |
| 124 | event_loop |
| 125 | ->MakeSender<::frc971::control_loops::drivetrain::Position>( |
| 126 | "/drivetrain")) { |
| 127 | // Set to filter out anything shorter than 1/4 of the minimum pulse width |
| 128 | // we should ever see. |
| 129 | UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond); |
| 130 | UpdateMediumEncoderFilterHz(kMaxMediumEncoderPulsesPerSecond); |
| 131 | } |
| 132 | |
| 133 | // Auto mode switches. |
| 134 | void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) { |
| 135 | autonomous_modes_.at(i) = ::std::move(sensor); |
| 136 | } |
| 137 | |
| 138 | void RunIteration() override { |
| 139 | { |
| 140 | auto builder = drivetrain_position_sender_.MakeBuilder(); |
| 141 | frc971::control_loops::drivetrain::Position::Builder drivetrain_builder = |
| 142 | builder.MakeBuilder<frc971::control_loops::drivetrain::Position>(); |
| 143 | drivetrain_builder.add_left_encoder( |
| 144 | drivetrain_translate(drivetrain_left_encoder_->GetRaw())); |
| 145 | drivetrain_builder.add_left_speed( |
| 146 | drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod())); |
| 147 | |
| 148 | drivetrain_builder.add_right_encoder( |
| 149 | -drivetrain_translate(drivetrain_right_encoder_->GetRaw())); |
| 150 | drivetrain_builder.add_right_speed(-drivetrain_velocity_translate( |
| 151 | drivetrain_right_encoder_->GetPeriod())); |
| 152 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 153 | builder.CheckOk(builder.Send(drivetrain_builder.Finish())); |
Ravago Jones | 486de80 | 2021-05-19 20:47:55 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | { |
| 157 | auto builder = superstructure_position_sender_.MakeBuilder(); |
| 158 | superstructure::Position::Builder position_builder = |
| 159 | builder.MakeBuilder<superstructure::Position>(); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 160 | builder.CheckOk(builder.Send(position_builder.Finish())); |
Ravago Jones | 486de80 | 2021-05-19 20:47:55 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | { |
| 164 | auto builder = auto_mode_sender_.MakeBuilder(); |
| 165 | |
| 166 | uint32_t mode = 0; |
| 167 | for (size_t i = 0; i < autonomous_modes_.size(); ++i) { |
| 168 | if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) { |
| 169 | mode |= 1 << i; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | auto auto_mode_builder = |
| 174 | builder.MakeBuilder<frc971::autonomous::AutonomousMode>(); |
| 175 | |
| 176 | auto_mode_builder.add_mode(mode); |
| 177 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 178 | builder.CheckOk(builder.Send(auto_mode_builder.Finish())); |
Ravago Jones | 486de80 | 2021-05-19 20:47:55 -0700 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
| 182 | private: |
| 183 | ::aos::Sender<::frc971::autonomous::AutonomousMode> auto_mode_sender_; |
| 184 | ::aos::Sender<superstructure::Position> superstructure_position_sender_; |
| 185 | ::aos::Sender<::frc971::control_loops::drivetrain::Position> |
| 186 | drivetrain_position_sender_; |
| 187 | |
| 188 | ::std::array<::std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_; |
| 189 | }; |
| 190 | |
| 191 | class SuperstructureWriter |
| 192 | : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> { |
| 193 | public: |
| 194 | SuperstructureWriter(::aos::EventLoop *event_loop) |
| 195 | : ::frc971::wpilib::LoopOutputHandler<superstructure::Output>( |
| 196 | event_loop, "/superstructure") {} |
| 197 | |
Vinay Siva | 62e3d32 | 2021-08-14 17:37:51 -0700 | [diff] [blame] | 198 | void set_intake_falcon( |
| 199 | ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> t) { |
| 200 | intake_falcon_ = ::std::move(t); |
Vinay Siva | 7cea8a8 | 2021-09-25 15:06:28 -0700 | [diff] [blame] | 201 | ConfigureRollerFalcon(intake_falcon_.get()); |
Vinay Siva | 62e3d32 | 2021-08-14 17:37:51 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | void set_outtake_falcon( |
| 205 | ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> t) { |
| 206 | outtake_falcon_ = ::std::move(t); |
Vinay Siva | 7cea8a8 | 2021-09-25 15:06:28 -0700 | [diff] [blame] | 207 | ConfigureRollerFalcon(outtake_falcon_.get()); |
| 208 | } |
| 209 | |
| 210 | void set_climber_falcon( |
| 211 | ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> t) { |
| 212 | climber_falcon_ = ::std::move(t); |
| 213 | climber_falcon_->ConfigSupplyCurrentLimit( |
| 214 | {true, Values::kClimberSupplyCurrentLimit(), |
| 215 | Values::kClimberSupplyCurrentLimit(), 0}); |
Vinay Siva | 62e3d32 | 2021-08-14 17:37:51 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Ravago Jones | 486de80 | 2021-05-19 20:47:55 -0700 | [diff] [blame] | 218 | private: |
Vinay Siva | 7cea8a8 | 2021-09-25 15:06:28 -0700 | [diff] [blame] | 219 | void ConfigureRollerFalcon( |
| 220 | ::ctre::phoenix::motorcontrol::can::TalonFX *falcon) { |
Vinay Siva | 62e3d32 | 2021-08-14 17:37:51 -0700 | [diff] [blame] | 221 | falcon->ConfigSupplyCurrentLimit({true, Values::kRollerSupplyCurrentLimit(), |
| 222 | Values::kRollerSupplyCurrentLimit(), 0}); |
| 223 | falcon->ConfigStatorCurrentLimit({true, Values::kRollerStatorCurrentLimit(), |
| 224 | Values::kRollerStatorCurrentLimit(), 0}); |
| 225 | } |
| 226 | |
| 227 | void WriteToFalcon(const double voltage, |
| 228 | ::ctre::phoenix::motorcontrol::can::TalonFX *falcon) { |
| 229 | falcon->Set( |
| 230 | ctre::phoenix::motorcontrol::ControlMode::PercentOutput, |
| 231 | std::clamp(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0); |
| 232 | } |
| 233 | |
| 234 | void Write(const superstructure::Output &output) override { |
| 235 | WriteToFalcon(output.intake_volts(), intake_falcon_.get()); |
| 236 | WriteToFalcon(output.outtake_volts(), outtake_falcon_.get()); |
Vinay Siva | 7cea8a8 | 2021-09-25 15:06:28 -0700 | [diff] [blame] | 237 | |
| 238 | WriteToFalcon(-output.climber_volts(), climber_falcon_.get()); |
Vinay Siva | 62e3d32 | 2021-08-14 17:37:51 -0700 | [diff] [blame] | 239 | } |
Ravago Jones | 486de80 | 2021-05-19 20:47:55 -0700 | [diff] [blame] | 240 | |
Vinay Siva | 7cea8a8 | 2021-09-25 15:06:28 -0700 | [diff] [blame] | 241 | void Stop() override { |
| 242 | AOS_LOG(WARNING, "Superstructure output too old.\n"); |
| 243 | climber_falcon_->Set(ctre::phoenix::motorcontrol::ControlMode::Disabled, 0); |
| 244 | intake_falcon_->Set(ctre::phoenix::motorcontrol::ControlMode::Disabled, 0); |
| 245 | outtake_falcon_->Set(ctre::phoenix::motorcontrol::ControlMode::Disabled, 0); |
| 246 | } |
Vinay Siva | 62e3d32 | 2021-08-14 17:37:51 -0700 | [diff] [blame] | 247 | |
| 248 | ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> intake_falcon_, |
| 249 | outtake_falcon_; |
Vinay Siva | 7cea8a8 | 2021-09-25 15:06:28 -0700 | [diff] [blame] | 250 | |
| 251 | ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> |
| 252 | climber_falcon_; |
Ravago Jones | 486de80 | 2021-05-19 20:47:55 -0700 | [diff] [blame] | 253 | }; |
| 254 | |
| 255 | class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase { |
| 256 | public: |
| 257 | ::std::unique_ptr<frc::Encoder> make_encoder(int index) { |
| 258 | return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false, |
| 259 | frc::Encoder::k4X); |
| 260 | } |
| 261 | |
| 262 | void Run() override { |
| 263 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
Austin Schuh | c5fa6d9 | 2022-02-25 14:36:28 -0800 | [diff] [blame] | 264 | aos::configuration::ReadConfig("aos_config.json"); |
Ravago Jones | 486de80 | 2021-05-19 20:47:55 -0700 | [diff] [blame] | 265 | |
| 266 | // Thread 1. |
| 267 | ::aos::ShmEventLoop joystick_sender_event_loop(&config.message()); |
| 268 | ::frc971::wpilib::JoystickSender joystick_sender( |
| 269 | &joystick_sender_event_loop); |
| 270 | AddLoop(&joystick_sender_event_loop); |
| 271 | |
| 272 | // Thread 2. |
| 273 | ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message()); |
| 274 | ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop); |
| 275 | AddLoop(&pdp_fetcher_event_loop); |
| 276 | |
| 277 | // Thread 3. |
| 278 | ::aos::ShmEventLoop sensor_reader_event_loop(&config.message()); |
| 279 | SensorReader sensor_reader(&sensor_reader_event_loop); |
| 280 | sensor_reader.set_drivetrain_left_encoder(make_encoder(0)); |
| 281 | sensor_reader.set_drivetrain_right_encoder(make_encoder(1)); |
| 282 | |
| 283 | AddLoop(&sensor_reader_event_loop); |
| 284 | |
| 285 | // Thread 4. |
| 286 | ::aos::ShmEventLoop output_event_loop(&config.message()); |
| 287 | ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop); |
| 288 | drivetrain_writer.set_left_controller0( |
| 289 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), true); |
| 290 | drivetrain_writer.set_right_controller0( |
| 291 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)), false); |
| 292 | |
| 293 | SuperstructureWriter superstructure_writer(&output_event_loop); |
Vinay Siva | 62e3d32 | 2021-08-14 17:37:51 -0700 | [diff] [blame] | 294 | superstructure_writer.set_intake_falcon( |
| 295 | make_unique<::ctre::phoenix::motorcontrol::can::TalonFX>(0)); |
| 296 | superstructure_writer.set_outtake_falcon( |
| 297 | make_unique<::ctre::phoenix::motorcontrol::can::TalonFX>(1)); |
Vinay Siva | 7cea8a8 | 2021-09-25 15:06:28 -0700 | [diff] [blame] | 298 | superstructure_writer.set_climber_falcon( |
| 299 | make_unique<::ctre::phoenix::motorcontrol::can::TalonFX>(2)); |
Ravago Jones | 486de80 | 2021-05-19 20:47:55 -0700 | [diff] [blame] | 300 | |
| 301 | AddLoop(&output_event_loop); |
| 302 | |
| 303 | RunLoops(); |
| 304 | } |
| 305 | }; |
| 306 | |
| 307 | } // namespace wpilib |
| 308 | } // namespace y2021_bot3 |
| 309 | |
| 310 | AOS_ROBOT_CLASS(::y2021_bot3::wpilib::WPILibRobot); |