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