Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 1 | #include <inttypes.h> |
| 2 | #include <stdio.h> |
| 3 | #include <string.h> |
| 4 | #include <unistd.h> |
| 5 | |
| 6 | #include <array> |
| 7 | #include <chrono> |
| 8 | #include <cmath> |
| 9 | #include <functional> |
| 10 | #include <mutex> |
| 11 | #include <thread> |
| 12 | |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 13 | #include "frc971/wpilib/ahal/AnalogInput.h" |
| 14 | #include "frc971/wpilib/ahal/Counter.h" |
| 15 | #include "frc971/wpilib/ahal/DigitalGlitchFilter.h" |
| 16 | #include "frc971/wpilib/ahal/DriverStation.h" |
| 17 | #include "frc971/wpilib/ahal/Encoder.h" |
| 18 | #include "frc971/wpilib/ahal/VictorSP.h" |
| 19 | #undef ERROR |
| 20 | |
| 21 | #include "aos/commonmath.h" |
| 22 | #include "aos/events/event_loop.h" |
| 23 | #include "aos/events/shm_event_loop.h" |
| 24 | #include "aos/init.h" |
| 25 | #include "aos/logging/logging.h" |
| 26 | #include "aos/make_unique.h" |
| 27 | #include "aos/realtime.h" |
| 28 | #include "aos/robot_state/robot_state_generated.h" |
| 29 | #include "aos/time/time.h" |
| 30 | #include "aos/util/log_interval.h" |
| 31 | #include "aos/util/phased_loop.h" |
| 32 | #include "aos/util/wrapping_counter.h" |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 33 | #include "frc971/autonomous/auto_mode_generated.h" |
| 34 | #include "frc971/control_loops/drivetrain/drivetrain_position_generated.h" |
James Kuszmaul | a244a91 | 2020-01-18 13:50:50 -0800 | [diff] [blame] | 35 | #include "frc971/wpilib/ADIS16470.h" |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 36 | #include "frc971/wpilib/buffered_pcm.h" |
| 37 | #include "frc971/wpilib/buffered_solenoid.h" |
| 38 | #include "frc971/wpilib/dma.h" |
| 39 | #include "frc971/wpilib/drivetrain_writer.h" |
| 40 | #include "frc971/wpilib/encoder_and_potentiometer.h" |
| 41 | #include "frc971/wpilib/joystick_sender.h" |
| 42 | #include "frc971/wpilib/logging_generated.h" |
| 43 | #include "frc971/wpilib/loop_output_handler.h" |
| 44 | #include "frc971/wpilib/pdp_fetcher.h" |
| 45 | #include "frc971/wpilib/sensor_reader.h" |
| 46 | #include "frc971/wpilib/wpilib_robot_base.h" |
| 47 | #include "y2020/constants.h" |
| 48 | #include "y2020/control_loops/superstructure/superstructure_output_generated.h" |
| 49 | #include "y2020/control_loops/superstructure/superstructure_position_generated.h" |
| 50 | |
| 51 | using ::aos::monotonic_clock; |
| 52 | using ::y2020::constants::Values; |
| 53 | namespace superstructure = ::y2020::control_loops::superstructure; |
| 54 | namespace chrono = ::std::chrono; |
| 55 | using aos::make_unique; |
| 56 | |
| 57 | namespace y2020 { |
| 58 | namespace wpilib { |
| 59 | namespace { |
| 60 | |
| 61 | constexpr double kMaxBringupPower = 12.0; |
| 62 | |
| 63 | // TODO(Brian): Fix the interpretation of the result of GetRaw here and in the |
| 64 | // DMA stuff and then removing the * 2.0 in *_translate. |
| 65 | // The low bit is direction. |
| 66 | |
| 67 | // TODO(brian): Use ::std::max instead once we have C++14 so that can be |
| 68 | // constexpr. |
| 69 | template <typename T> |
| 70 | constexpr T max(T a, T b) { |
| 71 | return (a > b) ? a : b; |
| 72 | } |
| 73 | |
| 74 | template <typename T, typename... Rest> |
| 75 | constexpr T max(T a, T b, T c, Rest... rest) { |
| 76 | return max(max(a, b), c, rest...); |
| 77 | } |
| 78 | |
| 79 | double drivetrain_translate(int32_t in) { |
| 80 | return ((static_cast<double>(in) / |
| 81 | Values::kDrivetrainEncoderCountsPerRevolution()) * |
| 82 | (2.0 * M_PI)) * |
| 83 | Values::kDrivetrainEncoderRatio() * |
| 84 | control_loops::drivetrain::kWheelRadius; |
| 85 | } |
| 86 | |
| 87 | double drivetrain_velocity_translate(double in) { |
| 88 | return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) * |
| 89 | (2.0 * M_PI)) * |
| 90 | Values::kDrivetrainEncoderRatio() * |
| 91 | control_loops::drivetrain::kWheelRadius; |
| 92 | } |
| 93 | |
| 94 | constexpr double kMaxFastEncoderPulsesPerSecond = |
| 95 | Values::kMaxDrivetrainEncoderPulsesPerSecond(); |
| 96 | static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000, |
| 97 | "fast encoders are too fast"); |
| 98 | constexpr double kMaxMediumEncoderPulsesPerSecond = kMaxFastEncoderPulsesPerSecond; |
| 99 | |
| 100 | static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000, |
| 101 | "medium encoders are too fast"); |
| 102 | |
| 103 | } // namespace |
| 104 | |
| 105 | // Class to send position messages with sensor readings to our loops. |
| 106 | class SensorReader : public ::frc971::wpilib::SensorReader { |
| 107 | public: |
| 108 | SensorReader(::aos::ShmEventLoop *event_loop) |
| 109 | : ::frc971::wpilib::SensorReader(event_loop), |
| 110 | auto_mode_sender_( |
| 111 | event_loop->MakeSender<::frc971::autonomous::AutonomousMode>( |
| 112 | "/autonomous")), |
| 113 | superstructure_position_sender_( |
| 114 | event_loop->MakeSender<superstructure::Position>( |
| 115 | "/superstructure")), |
| 116 | drivetrain_position_sender_( |
| 117 | event_loop |
| 118 | ->MakeSender<::frc971::control_loops::drivetrain::Position>( |
| 119 | "/drivetrain")) { |
| 120 | // Set to filter out anything shorter than 1/4 of the minimum pulse width |
| 121 | // we should ever see. |
| 122 | UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond); |
| 123 | UpdateMediumEncoderFilterHz(kMaxMediumEncoderPulsesPerSecond); |
| 124 | } |
| 125 | |
| 126 | // Auto mode switches. |
| 127 | void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) { |
| 128 | autonomous_modes_.at(i) = ::std::move(sensor); |
| 129 | } |
| 130 | |
James Kuszmaul | a244a91 | 2020-01-18 13:50:50 -0800 | [diff] [blame] | 131 | void set_imu(frc971::wpilib::ADIS16470 *imu) { imu_ = imu; } |
| 132 | |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 133 | void RunIteration() override { |
James Kuszmaul | a244a91 | 2020-01-18 13:50:50 -0800 | [diff] [blame] | 134 | CHECK_NOTNULL(imu_)->DoReads(); |
| 135 | |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 136 | { |
| 137 | auto builder = drivetrain_position_sender_.MakeBuilder(); |
| 138 | frc971::control_loops::drivetrain::Position::Builder drivetrain_builder = |
| 139 | builder.MakeBuilder<frc971::control_loops::drivetrain::Position>(); |
| 140 | drivetrain_builder.add_left_encoder( |
| 141 | drivetrain_translate(drivetrain_left_encoder_->GetRaw())); |
| 142 | drivetrain_builder.add_left_speed( |
| 143 | drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod())); |
| 144 | |
| 145 | drivetrain_builder.add_right_encoder( |
| 146 | -drivetrain_translate(drivetrain_right_encoder_->GetRaw())); |
| 147 | drivetrain_builder.add_right_speed(-drivetrain_velocity_translate( |
| 148 | drivetrain_right_encoder_->GetPeriod())); |
| 149 | |
| 150 | builder.Send(drivetrain_builder.Finish()); |
| 151 | } |
| 152 | |
| 153 | { |
| 154 | auto builder = superstructure_position_sender_.MakeBuilder(); |
| 155 | superstructure::Position::Builder position_builder = |
| 156 | builder.MakeBuilder<superstructure::Position>(); |
| 157 | builder.Send(position_builder.Finish()); |
| 158 | } |
| 159 | |
| 160 | { |
| 161 | auto builder = auto_mode_sender_.MakeBuilder(); |
| 162 | |
| 163 | uint32_t mode = 0; |
| 164 | for (size_t i = 0; i < autonomous_modes_.size(); ++i) { |
| 165 | if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) { |
| 166 | mode |= 1 << i; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | auto auto_mode_builder = |
| 171 | builder.MakeBuilder<frc971::autonomous::AutonomousMode>(); |
| 172 | |
| 173 | auto_mode_builder.add_mode(mode); |
| 174 | |
| 175 | builder.Send(auto_mode_builder.Finish()); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | private: |
| 180 | ::aos::Sender<::frc971::autonomous::AutonomousMode> auto_mode_sender_; |
| 181 | ::aos::Sender<superstructure::Position> superstructure_position_sender_; |
| 182 | ::aos::Sender<::frc971::control_loops::drivetrain::Position> |
| 183 | drivetrain_position_sender_; |
| 184 | |
| 185 | ::std::array<::std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_; |
James Kuszmaul | a244a91 | 2020-01-18 13:50:50 -0800 | [diff] [blame] | 186 | |
| 187 | frc971::wpilib::ADIS16470 *imu_ = nullptr; |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 188 | }; |
| 189 | |
| 190 | class SuperstructureWriter |
| 191 | : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> { |
| 192 | public: |
| 193 | SuperstructureWriter(::aos::EventLoop *event_loop) |
| 194 | : ::frc971::wpilib::LoopOutputHandler<superstructure::Output>( |
| 195 | event_loop, "/superstructure") {} |
| 196 | |
| 197 | private: |
| 198 | void Write(const superstructure::Output & /*output*/) override {} |
| 199 | |
| 200 | void Stop() override { AOS_LOG(WARNING, "Superstructure output too old.\n"); } |
| 201 | }; |
| 202 | |
| 203 | class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase { |
| 204 | public: |
| 205 | ::std::unique_ptr<frc::Encoder> make_encoder(int index) { |
| 206 | return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false, |
| 207 | frc::Encoder::k4X); |
| 208 | } |
| 209 | |
| 210 | void Run() override { |
| 211 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 212 | aos::configuration::ReadConfig("config.json"); |
| 213 | |
| 214 | // Thread 1. |
| 215 | ::aos::ShmEventLoop joystick_sender_event_loop(&config.message()); |
| 216 | ::frc971::wpilib::JoystickSender joystick_sender( |
| 217 | &joystick_sender_event_loop); |
| 218 | AddLoop(&joystick_sender_event_loop); |
| 219 | |
| 220 | // Thread 2. |
| 221 | ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message()); |
| 222 | ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop); |
| 223 | AddLoop(&pdp_fetcher_event_loop); |
| 224 | |
| 225 | // Thread 3. |
| 226 | ::aos::ShmEventLoop sensor_reader_event_loop(&config.message()); |
| 227 | SensorReader sensor_reader(&sensor_reader_event_loop); |
| 228 | sensor_reader.set_drivetrain_left_encoder(make_encoder(0)); |
| 229 | sensor_reader.set_drivetrain_right_encoder(make_encoder(1)); |
| 230 | |
James Kuszmaul | a244a91 | 2020-01-18 13:50:50 -0800 | [diff] [blame] | 231 | auto imu_trigger = make_unique<frc::DigitalInput>(0); |
| 232 | auto imu_reset = make_unique<frc::DigitalOutput>(1); |
| 233 | auto spi = make_unique<frc::SPI>(frc::SPI::Port::kOnboardCS2); |
| 234 | frc971::wpilib::ADIS16470 imu(&sensor_reader_event_loop, spi.get(), |
| 235 | imu_trigger.get(), imu_reset.get()); |
| 236 | sensor_reader.set_imu(&imu); |
| 237 | |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 238 | AddLoop(&sensor_reader_event_loop); |
| 239 | |
| 240 | // Thread 4. |
| 241 | ::aos::ShmEventLoop output_event_loop(&config.message()); |
James Kuszmaul | 57c2baa | 2020-01-19 14:52:52 -0800 | [diff] [blame^] | 242 | output_event_loop.set_name("output_writer"); |
Stephan Massalt | d021f97 | 2020-01-05 20:41:23 -0800 | [diff] [blame] | 243 | ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop); |
| 244 | drivetrain_writer.set_left_controller0( |
| 245 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), true); |
| 246 | drivetrain_writer.set_right_controller0( |
| 247 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)), false); |
| 248 | |
| 249 | SuperstructureWriter superstructure_writer(&output_event_loop); |
| 250 | |
| 251 | AddLoop(&output_event_loop); |
| 252 | |
| 253 | RunLoops(); |
| 254 | } |
| 255 | }; |
| 256 | |
| 257 | } // namespace wpilib |
| 258 | } // namespace y2020 |
| 259 | |
| 260 | AOS_ROBOT_CLASS(::y2020::wpilib::WPILibRobot); |