Maxwell Henderson | ad31234 | 2023-01-10 12:07:47 -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/Servo.h" |
| 21 | #include "frc971/wpilib/ahal/TalonFX.h" |
| 22 | #include "frc971/wpilib/ahal/VictorSP.h" |
| 23 | #undef ERROR |
| 24 | |
| 25 | #include "aos/commonmath.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 "ctre/phoenix/motorcontrol/can/TalonFX.h" |
| 36 | #include "ctre/phoenix/motorcontrol/can/TalonSRX.h" |
| 37 | #include "frc971/autonomous/auto_mode_generated.h" |
| 38 | #include "frc971/control_loops/drivetrain/drivetrain_position_generated.h" |
| 39 | #include "frc971/input/robot_state_generated.h" |
| 40 | #include "frc971/queues/gyro_generated.h" |
| 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 "y2023/constants.h" |
| 54 | #include "y2023/control_loops/superstructure/superstructure_output_generated.h" |
| 55 | #include "y2023/control_loops/superstructure/superstructure_position_generated.h" |
| 56 | |
| 57 | using ::aos::monotonic_clock; |
| 58 | using ::y2023::constants::Values; |
| 59 | namespace superstructure = ::y2023::control_loops::superstructure; |
| 60 | namespace chrono = ::std::chrono; |
| 61 | using std::make_unique; |
| 62 | |
| 63 | namespace y2023 { |
| 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 | double drivetrain_velocity_translate(double in) { |
| 74 | return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) * |
| 75 | (2.0 * M_PI)) * |
| 76 | Values::kDrivetrainEncoderRatio() * |
| 77 | control_loops::drivetrain::kWheelRadius; |
| 78 | } |
| 79 | |
| 80 | constexpr double kMaxFastEncoderPulsesPerSecond = |
| 81 | /*std::max({*/ Values::kMaxDrivetrainEncoderPulsesPerSecond() /*, |
| 82 | Values::kMaxIntakeEncoderPulsesPerSecond()})*/ |
| 83 | ; |
| 84 | /*static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000, |
| 85 | "fast encoders are too fast");*/ |
| 86 | |
| 87 | } // namespace |
| 88 | |
| 89 | // Class to send position messages with sensor readings to our loops. |
| 90 | class SensorReader : public ::frc971::wpilib::SensorReader { |
| 91 | public: |
| 92 | SensorReader(::aos::ShmEventLoop *event_loop, |
| 93 | std::shared_ptr<const Values> values) |
| 94 | : ::frc971::wpilib::SensorReader(event_loop), |
| 95 | values_(std::move(values)), |
| 96 | auto_mode_sender_( |
| 97 | event_loop->MakeSender<::frc971::autonomous::AutonomousMode>( |
| 98 | "/autonomous")), |
| 99 | superstructure_position_sender_( |
| 100 | event_loop->MakeSender<superstructure::Position>( |
| 101 | "/superstructure")), |
| 102 | drivetrain_position_sender_( |
| 103 | event_loop |
| 104 | ->MakeSender<::frc971::control_loops::drivetrain::Position>( |
| 105 | "/drivetrain")), |
| 106 | gyro_sender_(event_loop->MakeSender<::frc971::sensors::GyroReading>( |
| 107 | "/drivetrain")) { |
| 108 | // Set to filter out anything shorter than 1/4 of the minimum pulse width |
| 109 | // we should ever see. |
| 110 | UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond); |
| 111 | } |
| 112 | |
| 113 | void Start() override { |
| 114 | // TODO(Ravago): Figure out why adding multiple DMA readers results in weird |
| 115 | // behavior |
| 116 | // AddToDMA(&imu_heading_reader_); |
| 117 | AddToDMA(&imu_yaw_rate_reader_); |
| 118 | } |
| 119 | |
| 120 | // Auto mode switches. |
| 121 | void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) { |
| 122 | autonomous_modes_.at(i) = ::std::move(sensor); |
| 123 | } |
| 124 | |
| 125 | void RunIteration() override { |
| 126 | superstructure_reading_->Set(true); |
| 127 | { auto builder = superstructure_position_sender_.MakeBuilder(); } |
| 128 | |
| 129 | { |
| 130 | auto builder = drivetrain_position_sender_.MakeBuilder(); |
| 131 | frc971::control_loops::drivetrain::Position::Builder drivetrain_builder = |
| 132 | builder.MakeBuilder<frc971::control_loops::drivetrain::Position>(); |
| 133 | drivetrain_builder.add_left_encoder( |
| 134 | constants::Values::DrivetrainEncoderToMeters( |
| 135 | drivetrain_left_encoder_->GetRaw())); |
| 136 | drivetrain_builder.add_left_speed( |
| 137 | drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod())); |
| 138 | |
| 139 | drivetrain_builder.add_right_encoder( |
| 140 | -constants::Values::DrivetrainEncoderToMeters( |
| 141 | drivetrain_right_encoder_->GetRaw())); |
| 142 | drivetrain_builder.add_right_speed(-drivetrain_velocity_translate( |
| 143 | drivetrain_right_encoder_->GetPeriod())); |
| 144 | |
| 145 | builder.CheckOk(builder.Send(drivetrain_builder.Finish())); |
| 146 | } |
| 147 | |
| 148 | { |
| 149 | auto builder = gyro_sender_.MakeBuilder(); |
| 150 | ::frc971::sensors::GyroReading::Builder gyro_reading_builder = |
| 151 | builder.MakeBuilder<::frc971::sensors::GyroReading>(); |
| 152 | // +/- 2000 deg / sec |
| 153 | constexpr double kMaxVelocity = 4000; // degrees / second |
| 154 | constexpr double kVelocityRadiansPerSecond = |
| 155 | kMaxVelocity / 360 * (2.0 * M_PI); |
| 156 | |
| 157 | // Only part of the full range is used to prevent being 100% on or off. |
| 158 | constexpr double kScaledRangeLow = 0.1; |
| 159 | constexpr double kScaledRangeHigh = 0.9; |
| 160 | |
| 161 | constexpr double kPWMFrequencyHz = 200; |
| 162 | double heading_duty_cycle = |
| 163 | imu_heading_reader_.last_width() * kPWMFrequencyHz; |
| 164 | double velocity_duty_cycle = |
| 165 | imu_yaw_rate_reader_.last_width() * kPWMFrequencyHz; |
| 166 | |
| 167 | constexpr double kDutyCycleScale = |
| 168 | 1 / (kScaledRangeHigh - kScaledRangeLow); |
| 169 | // scale from 0.1 - 0.9 to 0 - 1 |
| 170 | double rescaled_heading_duty_cycle = |
| 171 | (heading_duty_cycle - kScaledRangeLow) * kDutyCycleScale; |
| 172 | double rescaled_velocity_duty_cycle = |
| 173 | (velocity_duty_cycle - kScaledRangeLow) * kDutyCycleScale; |
| 174 | |
| 175 | if (!std::isnan(rescaled_heading_duty_cycle)) { |
| 176 | gyro_reading_builder.add_angle(rescaled_heading_duty_cycle * |
| 177 | (2.0 * M_PI)); |
| 178 | } |
| 179 | if (!std::isnan(rescaled_velocity_duty_cycle)) { |
| 180 | gyro_reading_builder.add_velocity((rescaled_velocity_duty_cycle - 0.5) * |
| 181 | kVelocityRadiansPerSecond); |
| 182 | } |
| 183 | builder.CheckOk(builder.Send(gyro_reading_builder.Finish())); |
| 184 | } |
| 185 | |
| 186 | { |
| 187 | auto builder = auto_mode_sender_.MakeBuilder(); |
| 188 | |
| 189 | uint32_t mode = 0; |
| 190 | for (size_t i = 0; i < autonomous_modes_.size(); ++i) { |
| 191 | if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) { |
| 192 | mode |= 1 << i; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | auto auto_mode_builder = |
| 197 | builder.MakeBuilder<frc971::autonomous::AutonomousMode>(); |
| 198 | |
| 199 | auto_mode_builder.add_mode(mode); |
| 200 | |
| 201 | builder.CheckOk(builder.Send(auto_mode_builder.Finish())); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | std::shared_ptr<frc::DigitalOutput> superstructure_reading_; |
| 206 | |
| 207 | void set_superstructure_reading( |
| 208 | std::shared_ptr<frc::DigitalOutput> superstructure_reading) { |
| 209 | superstructure_reading_ = superstructure_reading; |
| 210 | } |
| 211 | |
| 212 | private: |
| 213 | std::shared_ptr<const Values> values_; |
| 214 | |
| 215 | aos::Sender<frc971::autonomous::AutonomousMode> auto_mode_sender_; |
| 216 | aos::Sender<superstructure::Position> superstructure_position_sender_; |
| 217 | aos::Sender<frc971::control_loops::drivetrain::Position> |
| 218 | drivetrain_position_sender_; |
| 219 | ::aos::Sender<::frc971::sensors::GyroReading> gyro_sender_; |
| 220 | |
| 221 | std::array<std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_; |
| 222 | |
| 223 | frc971::wpilib::DMAPulseWidthReader imu_heading_reader_, imu_yaw_rate_reader_; |
| 224 | }; |
| 225 | |
| 226 | class SuperstructureWriter |
| 227 | : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> { |
| 228 | public: |
| 229 | SuperstructureWriter(aos::EventLoop *event_loop) |
| 230 | : frc971::wpilib::LoopOutputHandler<superstructure::Output>( |
| 231 | event_loop, "/superstructure") {} |
| 232 | |
| 233 | std::shared_ptr<frc::DigitalOutput> superstructure_reading_; |
| 234 | |
| 235 | void set_superstructure_reading( |
| 236 | std::shared_ptr<frc::DigitalOutput> superstructure_reading) { |
| 237 | superstructure_reading_ = superstructure_reading; |
| 238 | } |
| 239 | |
| 240 | private: |
| 241 | void Stop() override { AOS_LOG(WARNING, "Superstructure output too old.\n"); } |
| 242 | |
| 243 | void Write(const superstructure::Output &output) override { (void)output; } |
| 244 | |
| 245 | static void WriteCan(const double voltage, |
| 246 | ::ctre::phoenix::motorcontrol::can::TalonFX *falcon) { |
| 247 | falcon->Set( |
| 248 | ctre::phoenix::motorcontrol::ControlMode::PercentOutput, |
| 249 | std::clamp(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0); |
| 250 | } |
| 251 | |
| 252 | template <typename T> |
| 253 | static void WritePwm(const double voltage, T *motor) { |
| 254 | motor->SetSpeed(std::clamp(voltage, -kMaxBringupPower, kMaxBringupPower) / |
| 255 | 12.0); |
| 256 | } |
| 257 | }; |
| 258 | |
| 259 | |
| 260 | class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase { |
| 261 | public: |
| 262 | ::std::unique_ptr<frc::Encoder> make_encoder(int index) { |
| 263 | return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false, |
| 264 | frc::Encoder::k4X); |
| 265 | } |
| 266 | |
| 267 | void Run() override { |
| 268 | std::shared_ptr<const Values> values = |
| 269 | std::make_shared<const Values>(constants::MakeValues()); |
| 270 | |
| 271 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 272 | aos::configuration::ReadConfig("aos_config.json"); |
| 273 | |
| 274 | // Thread 1. |
| 275 | // Thread 2. |
| 276 | ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message()); |
| 277 | ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop); |
| 278 | AddLoop(&pdp_fetcher_event_loop); |
| 279 | |
| 280 | std::shared_ptr<frc::DigitalOutput> superstructure_reading = |
| 281 | make_unique<frc::DigitalOutput>(25); |
| 282 | |
| 283 | // Thread 3. |
| 284 | ::aos::ShmEventLoop sensor_reader_event_loop(&config.message()); |
| 285 | SensorReader sensor_reader(&sensor_reader_event_loop, values); |
| 286 | sensor_reader.set_pwm_trigger(true); |
| 287 | sensor_reader.set_drivetrain_left_encoder(make_encoder(1)); |
| 288 | sensor_reader.set_drivetrain_right_encoder(make_encoder(0)); |
| 289 | sensor_reader.set_superstructure_reading(superstructure_reading); |
| 290 | |
| 291 | AddLoop(&sensor_reader_event_loop); |
| 292 | |
| 293 | // Thread 4. |
| 294 | ::aos::ShmEventLoop output_event_loop(&config.message()); |
| 295 | ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop); |
| 296 | |
| 297 | SuperstructureWriter superstructure_writer(&output_event_loop); |
| 298 | |
| 299 | superstructure_writer.set_superstructure_reading(superstructure_reading); |
| 300 | |
| 301 | AddLoop(&output_event_loop); |
| 302 | |
| 303 | RunLoops(); |
| 304 | } |
| 305 | }; |
| 306 | |
| 307 | } // namespace wpilib |
| 308 | } // namespace y2023 |
| 309 | |
| 310 | AOS_ROBOT_CLASS(::y2023::wpilib::WPILibRobot); |