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