milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 1 | #include <unistd.h> |
| 2 | |
| 3 | #include <array> |
| 4 | #include <chrono> |
| 5 | #include <cinttypes> |
| 6 | #include <cmath> |
| 7 | #include <cstdio> |
| 8 | #include <cstring> |
| 9 | #include <functional> |
| 10 | #include <memory> |
| 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" |
| 29 | #include "aos/realtime.h" |
| 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" |
| 34 | #include "ctre/phoenix/motorcontrol/can/TalonFX.h" |
| 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" |
| 38 | #include "frc971/input/robot_state_generated.h" |
| 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 "y2022/constants.h" |
| 52 | #include "y2022/control_loops/superstructure/superstructure_output_generated.h" |
| 53 | #include "y2022/control_loops/superstructure/superstructure_position_generated.h" |
| 54 | |
| 55 | using ::aos::monotonic_clock; |
| 56 | using ::y2022::constants::Values; |
| 57 | namespace superstructure = ::y2022::control_loops::superstructure; |
| 58 | namespace chrono = ::std::chrono; |
| 59 | using std::make_unique; |
| 60 | |
| 61 | namespace y2022 { |
| 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 | |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 71 | double drivetrain_velocity_translate(double in) { |
| 72 | return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) * |
| 73 | (2.0 * M_PI)) * |
| 74 | Values::kDrivetrainEncoderRatio() * |
| 75 | control_loops::drivetrain::kWheelRadius; |
| 76 | } |
| 77 | |
| 78 | constexpr double kMaxFastEncoderPulsesPerSecond = |
| 79 | Values::kMaxDrivetrainEncoderPulsesPerSecond(); |
| 80 | static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000, |
| 81 | "fast encoders are too fast"); |
| 82 | constexpr double kMaxMediumEncoderPulsesPerSecond = |
| 83 | kMaxFastEncoderPulsesPerSecond; |
| 84 | |
| 85 | static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000, |
| 86 | "medium encoders are too fast"); |
| 87 | |
| 88 | } // namespace |
| 89 | |
| 90 | // Class to send position messages with sensor readings to our loops. |
| 91 | class SensorReader : public ::frc971::wpilib::SensorReader { |
| 92 | public: |
| 93 | SensorReader(::aos::ShmEventLoop *event_loop) |
| 94 | : ::frc971::wpilib::SensorReader(event_loop), |
| 95 | auto_mode_sender_( |
| 96 | event_loop->MakeSender<::frc971::autonomous::AutonomousMode>( |
| 97 | "/autonomous")), |
| 98 | superstructure_position_sender_( |
| 99 | event_loop->MakeSender<superstructure::Position>( |
| 100 | "/superstructure")), |
| 101 | drivetrain_position_sender_( |
| 102 | event_loop |
| 103 | ->MakeSender<::frc971::control_loops::drivetrain::Position>( |
| 104 | "/drivetrain")) { |
| 105 | // Set to filter out anything shorter than 1/4 of the minimum pulse width |
| 106 | // we should ever see. |
| 107 | UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond); |
| 108 | UpdateMediumEncoderFilterHz(kMaxMediumEncoderPulsesPerSecond); |
| 109 | } |
| 110 | |
| 111 | // Auto mode switches. |
| 112 | void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) { |
| 113 | autonomous_modes_.at(i) = ::std::move(sensor); |
| 114 | } |
| 115 | |
| 116 | void RunIteration() override { |
| 117 | { |
| 118 | auto builder = drivetrain_position_sender_.MakeBuilder(); |
| 119 | frc971::control_loops::drivetrain::Position::Builder drivetrain_builder = |
| 120 | builder.MakeBuilder<frc971::control_loops::drivetrain::Position>(); |
| 121 | drivetrain_builder.add_left_encoder( |
James Kuszmaul | 53507e1 | 2022-02-12 18:36:40 -0800 | [diff] [blame] | 122 | constants::Values::DrivetrainEncoderToMeters( |
| 123 | drivetrain_left_encoder_->GetRaw())); |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 124 | drivetrain_builder.add_left_speed( |
| 125 | drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod())); |
| 126 | |
| 127 | drivetrain_builder.add_right_encoder( |
James Kuszmaul | 53507e1 | 2022-02-12 18:36:40 -0800 | [diff] [blame] | 128 | -constants::Values::DrivetrainEncoderToMeters( |
| 129 | drivetrain_right_encoder_->GetRaw())); |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 130 | drivetrain_builder.add_right_speed(-drivetrain_velocity_translate( |
| 131 | drivetrain_right_encoder_->GetPeriod())); |
| 132 | |
| 133 | builder.CheckOk(builder.Send(drivetrain_builder.Finish())); |
| 134 | } |
| 135 | |
| 136 | { |
| 137 | auto builder = superstructure_position_sender_.MakeBuilder(); |
| 138 | superstructure::Position::Builder position_builder = |
| 139 | builder.MakeBuilder<superstructure::Position>(); |
| 140 | builder.CheckOk(builder.Send(position_builder.Finish())); |
| 141 | } |
| 142 | |
| 143 | { |
| 144 | auto builder = auto_mode_sender_.MakeBuilder(); |
| 145 | |
| 146 | uint32_t mode = 0; |
| 147 | for (size_t i = 0; i < autonomous_modes_.size(); ++i) { |
| 148 | if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) { |
| 149 | mode |= 1 << i; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | auto auto_mode_builder = |
| 154 | builder.MakeBuilder<frc971::autonomous::AutonomousMode>(); |
| 155 | |
| 156 | auto_mode_builder.add_mode(mode); |
| 157 | |
| 158 | builder.CheckOk(builder.Send(auto_mode_builder.Finish())); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | private: |
| 163 | ::aos::Sender<::frc971::autonomous::AutonomousMode> auto_mode_sender_; |
| 164 | ::aos::Sender<superstructure::Position> superstructure_position_sender_; |
| 165 | ::aos::Sender<::frc971::control_loops::drivetrain::Position> |
| 166 | drivetrain_position_sender_; |
| 167 | |
| 168 | ::std::array<::std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_; |
| 169 | }; |
| 170 | |
| 171 | class SuperstructureWriter |
| 172 | : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> { |
| 173 | public: |
| 174 | SuperstructureWriter(::aos::EventLoop *event_loop) |
| 175 | : ::frc971::wpilib::LoopOutputHandler<superstructure::Output>( |
| 176 | event_loop, "/superstructure") {} |
| 177 | |
Griffin Bui | 67abb91 | 2022-01-22 16:16:21 -0800 | [diff] [blame] | 178 | void set_climber_falcon( |
| 179 | ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> t) { |
| 180 | climber_falcon_ = ::std::move(t); |
| 181 | climber_falcon_->ConfigSupplyCurrentLimit( |
| 182 | {true, Values::kClimberSupplyCurrentLimit(), |
| 183 | Values::kClimberSupplyCurrentLimit(), 0}); |
| 184 | } |
| 185 | |
Jacob Ismael | 322ebb9 | 2022-02-09 20:12:47 -0800 | [diff] [blame] | 186 | void set_turret_falcon(::std::unique_ptr<::frc::TalonFX> t) { |
| 187 | turret_falcon_ = ::std::move(t); |
| 188 | } |
| 189 | |
| 190 | void set_catapult_falcon_1(::std::unique_ptr<::frc::TalonFX> t) { |
| 191 | catapult_falcon_1_ = ::std::move(t); |
| 192 | } |
| 193 | |
| 194 | void set_catapult_falcon_2(::std::unique_ptr<::frc::TalonFX> t) { |
| 195 | catapult_falcon_2_ = ::std::move(t); |
| 196 | } |
| 197 | |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 198 | private: |
Griffin Bui | 67abb91 | 2022-01-22 16:16:21 -0800 | [diff] [blame] | 199 | void WriteToFalconCan(const double voltage, |
| 200 | ::ctre::phoenix::motorcontrol::can::TalonFX *falcon) { |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 201 | falcon->Set( |
| 202 | ctre::phoenix::motorcontrol::ControlMode::PercentOutput, |
| 203 | std::clamp(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0); |
| 204 | } |
| 205 | |
Griffin Bui | 67abb91 | 2022-01-22 16:16:21 -0800 | [diff] [blame] | 206 | void Write(const superstructure::Output &output) override { |
| 207 | WriteToFalconCan(output.climber_voltage(), climber_falcon_.get()); |
Jacob Ismael | 322ebb9 | 2022-02-09 20:12:47 -0800 | [diff] [blame] | 208 | catapult_falcon_1_->SetSpeed(std::clamp(output.catapult_voltage(), |
| 209 | -kMaxBringupPower, |
| 210 | kMaxBringupPower) / |
| 211 | 12.0); |
| 212 | catapult_falcon_2_->SetSpeed(std::clamp(output.catapult_voltage(), |
| 213 | -kMaxBringupPower, |
| 214 | kMaxBringupPower) / |
| 215 | 12.0); |
| 216 | turret_falcon_->SetSpeed(std::clamp(output.turret_voltage(), |
| 217 | -kMaxBringupPower, kMaxBringupPower) / |
| 218 | 12.0); |
Griffin Bui | 67abb91 | 2022-01-22 16:16:21 -0800 | [diff] [blame] | 219 | } |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 220 | |
Griffin Bui | 67abb91 | 2022-01-22 16:16:21 -0800 | [diff] [blame] | 221 | void Stop() override { |
| 222 | AOS_LOG(WARNING, "Superstructure output too old.\n"); |
| 223 | climber_falcon_->Set(ctre::phoenix::motorcontrol::ControlMode::Disabled, 0); |
Jacob Ismael | 322ebb9 | 2022-02-09 20:12:47 -0800 | [diff] [blame] | 224 | catapult_falcon_1_->SetDisabled(); |
| 225 | catapult_falcon_2_->SetDisabled(); |
| 226 | turret_falcon_->SetDisabled(); |
Griffin Bui | 67abb91 | 2022-01-22 16:16:21 -0800 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> |
| 230 | climber_falcon_; |
Jacob Ismael | 322ebb9 | 2022-02-09 20:12:47 -0800 | [diff] [blame] | 231 | |
| 232 | ::std::unique_ptr<::frc::TalonFX> turret_falcon_, catapult_falcon_1_, |
| 233 | catapult_falcon_2_; |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 234 | }; |
| 235 | |
| 236 | class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase { |
| 237 | public: |
| 238 | ::std::unique_ptr<frc::Encoder> make_encoder(int index) { |
| 239 | return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false, |
| 240 | frc::Encoder::k4X); |
| 241 | } |
| 242 | |
| 243 | void Run() override { |
| 244 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 245 | aos::configuration::ReadConfig("config.json"); |
| 246 | |
| 247 | // Thread 1. |
| 248 | ::aos::ShmEventLoop joystick_sender_event_loop(&config.message()); |
| 249 | ::frc971::wpilib::JoystickSender joystick_sender( |
| 250 | &joystick_sender_event_loop); |
| 251 | AddLoop(&joystick_sender_event_loop); |
| 252 | |
| 253 | // Thread 2. |
| 254 | ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message()); |
| 255 | ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop); |
| 256 | AddLoop(&pdp_fetcher_event_loop); |
| 257 | |
| 258 | // Thread 3. |
| 259 | ::aos::ShmEventLoop sensor_reader_event_loop(&config.message()); |
| 260 | SensorReader sensor_reader(&sensor_reader_event_loop); |
| 261 | sensor_reader.set_drivetrain_left_encoder(make_encoder(0)); |
| 262 | sensor_reader.set_drivetrain_right_encoder(make_encoder(1)); |
| 263 | |
| 264 | AddLoop(&sensor_reader_event_loop); |
| 265 | |
| 266 | // Thread 4. |
| 267 | ::aos::ShmEventLoop output_event_loop(&config.message()); |
| 268 | ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop); |
| 269 | drivetrain_writer.set_left_controller0( |
| 270 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), true); |
| 271 | drivetrain_writer.set_right_controller0( |
| 272 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)), false); |
| 273 | |
| 274 | SuperstructureWriter superstructure_writer(&output_event_loop); |
| 275 | |
Griffin Bui | 67abb91 | 2022-01-22 16:16:21 -0800 | [diff] [blame] | 276 | superstructure_writer.set_climber_falcon( |
| 277 | make_unique<::ctre::phoenix::motorcontrol::can::TalonFX>(0)); |
Jacob Ismael | 322ebb9 | 2022-02-09 20:12:47 -0800 | [diff] [blame] | 278 | superstructure_writer.set_turret_falcon(make_unique<::frc::TalonFX>(2)); |
| 279 | superstructure_writer.set_catapult_falcon_1(make_unique<::frc::TalonFX>(3)); |
| 280 | superstructure_writer.set_catapult_falcon_2(make_unique<::frc::TalonFX>(4)); |
Griffin Bui | 67abb91 | 2022-01-22 16:16:21 -0800 | [diff] [blame] | 281 | |
milind-u | 086d726 | 2022-01-19 20:44:18 -0800 | [diff] [blame] | 282 | AddLoop(&output_event_loop); |
| 283 | |
| 284 | RunLoops(); |
| 285 | } |
| 286 | }; |
| 287 | |
| 288 | } // namespace wpilib |
| 289 | } // namespace y2022 |
| 290 | |
| 291 | AOS_ROBOT_CLASS(::y2022::wpilib::WPILibRobot); |