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 | |
| 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 | |
| 151 | builder.CheckOk(builder.Send(drivetrain_builder.Finish())); |
| 152 | } |
| 153 | |
| 154 | { |
| 155 | auto builder = superstructure_position_sender_.MakeBuilder(); |
| 156 | superstructure::Position::Builder position_builder = |
| 157 | builder.MakeBuilder<superstructure::Position>(); |
| 158 | builder.CheckOk(builder.Send(position_builder.Finish())); |
| 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 | |
| 176 | builder.CheckOk(builder.Send(auto_mode_builder.Finish())); |
| 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 | |
| 196 | private: |
| 197 | void WriteToFalcon(const double voltage, |
| 198 | ::ctre::phoenix::motorcontrol::can::TalonFX *falcon) { |
| 199 | falcon->Set( |
| 200 | ctre::phoenix::motorcontrol::ControlMode::PercentOutput, |
| 201 | std::clamp(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0); |
| 202 | } |
| 203 | |
| 204 | void Write(const superstructure::Output & /* output */) override {} |
| 205 | |
| 206 | void Stop() override { AOS_LOG(WARNING, "Superstructure output too old.\n"); } |
| 207 | }; |
| 208 | |
| 209 | class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase { |
| 210 | public: |
| 211 | ::std::unique_ptr<frc::Encoder> make_encoder(int index) { |
| 212 | return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false, |
| 213 | frc::Encoder::k4X); |
| 214 | } |
| 215 | |
| 216 | void Run() override { |
| 217 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 218 | aos::configuration::ReadConfig("config.json"); |
| 219 | |
| 220 | // Thread 1. |
| 221 | ::aos::ShmEventLoop joystick_sender_event_loop(&config.message()); |
| 222 | ::frc971::wpilib::JoystickSender joystick_sender( |
| 223 | &joystick_sender_event_loop); |
| 224 | AddLoop(&joystick_sender_event_loop); |
| 225 | |
| 226 | // Thread 2. |
| 227 | ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message()); |
| 228 | ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop); |
| 229 | AddLoop(&pdp_fetcher_event_loop); |
| 230 | |
| 231 | // Thread 3. |
| 232 | ::aos::ShmEventLoop sensor_reader_event_loop(&config.message()); |
| 233 | SensorReader sensor_reader(&sensor_reader_event_loop); |
| 234 | sensor_reader.set_drivetrain_left_encoder(make_encoder(0)); |
| 235 | sensor_reader.set_drivetrain_right_encoder(make_encoder(1)); |
| 236 | |
| 237 | AddLoop(&sensor_reader_event_loop); |
| 238 | |
| 239 | // Thread 4. |
| 240 | ::aos::ShmEventLoop output_event_loop(&config.message()); |
| 241 | ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop); |
| 242 | drivetrain_writer.set_left_controller0( |
| 243 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), true); |
| 244 | drivetrain_writer.set_right_controller0( |
| 245 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)), false); |
| 246 | |
| 247 | SuperstructureWriter superstructure_writer(&output_event_loop); |
| 248 | |
| 249 | AddLoop(&output_event_loop); |
| 250 | |
| 251 | RunLoops(); |
| 252 | } |
| 253 | }; |
| 254 | |
| 255 | } // namespace wpilib |
| 256 | } // namespace y2022 |
| 257 | |
| 258 | AOS_ROBOT_CLASS(::y2022::wpilib::WPILibRobot); |