Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [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> |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 10 | #include <thread> |
| 11 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 12 | #include "frc971/wpilib/ahal/AnalogInput.h" |
| 13 | #include "frc971/wpilib/ahal/Compressor.h" |
| 14 | #include "frc971/wpilib/ahal/DigitalGlitchFilter.h" |
| 15 | #include "frc971/wpilib/ahal/DriverStation.h" |
| 16 | #include "frc971/wpilib/ahal/Encoder.h" |
| 17 | #include "frc971/wpilib/ahal/VictorSP.h" |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 18 | #undef ERROR |
| 19 | |
Brian Silverman | f819b44 | 2019-01-20 16:51:04 -0800 | [diff] [blame] | 20 | #include "aos/init.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 21 | #include "aos/logging/logging.h" |
| 22 | #include "aos/logging/queue_logging.h" |
Brian Silverman | f819b44 | 2019-01-20 16:51:04 -0800 | [diff] [blame] | 23 | #include "aos/make_unique.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 24 | #include "aos/time/time.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 25 | #include "aos/util/phased_loop.h" |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 26 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 27 | #include "frc971/wpilib/buffered_pcm.h" |
| 28 | #include "frc971/wpilib/buffered_solenoid.h" |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 29 | #include "frc971/wpilib/dma.h" |
Austin Schuh | 9e4374d | 2019-02-02 17:08:32 -0800 | [diff] [blame] | 30 | #include "frc971/wpilib/gyro_sender.h" |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 31 | #include "frc971/wpilib/joystick_sender.h" |
| 32 | #include "frc971/wpilib/logging.q.h" |
| 33 | #include "frc971/wpilib/loop_output_handler.h" |
| 34 | #include "frc971/wpilib/pdp_fetcher.h" |
Austin Schuh | 9e4374d | 2019-02-02 17:08:32 -0800 | [diff] [blame] | 35 | #include "frc971/wpilib/sensor_reader.h" |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 36 | #include "frc971/wpilib/wpilib_robot_base.h" |
Sabina Davis | b6b987d | 2017-10-22 20:50:21 -0700 | [diff] [blame] | 37 | #include "y2017_bot3/control_loops/drivetrain/drivetrain_dog_motor_plant.h" |
| 38 | #include "y2017_bot3/control_loops/superstructure/superstructure.q.h" |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 39 | |
| 40 | #ifndef M_PI |
| 41 | #define M_PI 3.14159265358979323846 |
| 42 | #endif |
| 43 | |
| 44 | using ::frc971::control_loops::drivetrain_queue; |
Sabina Davis | b6b987d | 2017-10-22 20:50:21 -0700 | [diff] [blame] | 45 | using ::y2017_bot3::control_loops::superstructure_queue; |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 46 | using ::aos::monotonic_clock; |
| 47 | namespace chrono = ::std::chrono; |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 48 | using namespace frc; |
Brian Silverman | f819b44 | 2019-01-20 16:51:04 -0800 | [diff] [blame] | 49 | using aos::make_unique; |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 50 | |
| 51 | namespace y2017_bot3 { |
| 52 | namespace wpilib { |
| 53 | namespace { |
| 54 | |
| 55 | constexpr double kMaxBringupPower = 12.0; |
| 56 | |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 57 | constexpr double kDrivetrainCyclesPerRevolution = 128.0; |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 58 | constexpr double kDrivetrainEncoderCountsPerRevolution = |
| 59 | kDrivetrainCyclesPerRevolution * 4; |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 60 | constexpr double kDrivetrainEncoderRatio = 1.0; |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 61 | constexpr double kMaxDrivetrainEncoderPulsesPerSecond = |
| 62 | control_loops::drivetrain::kFreeSpeed * |
| 63 | control_loops::drivetrain::kHighOutputRatio / |
| 64 | kDrivetrainEncoderRatio * |
| 65 | kDrivetrainEncoderCountsPerRevolution; |
| 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 | |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 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 | template <typename T, typename... Rest> |
| 78 | constexpr T max(T a, T b, T c, Rest... rest) { |
| 79 | return max(max(a, b), c, rest...); |
| 80 | } |
| 81 | |
| 82 | double hall_translate(double in) { |
| 83 | // Turn voltage from our 3-state halls into a ratio that the loop can use. |
| 84 | return in / 5.0; |
| 85 | } |
| 86 | |
| 87 | double drivetrain_translate(int32_t in) { |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 88 | return -static_cast<double>(in) / (kDrivetrainCyclesPerRevolution /*cpr*/ * 4.0 /*4x*/) * |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 89 | kDrivetrainEncoderRatio * |
Sabina Davis | b6b987d | 2017-10-22 20:50:21 -0700 | [diff] [blame] | 90 | control_loops::drivetrain::kWheelRadius * |
| 91 | 2.0 * M_PI; |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | double drivetrain_velocity_translate(double in) { |
| 95 | return (1.0 / in) / 256.0 /*cpr*/ * |
| 96 | kDrivetrainEncoderRatio * |
| 97 | control_loops::drivetrain::kWheelRadius * 2.0 * M_PI; |
| 98 | } |
| 99 | |
| 100 | constexpr double kMaxFastEncoderPulsesPerSecond = |
| 101 | kMaxDrivetrainEncoderPulsesPerSecond; |
| 102 | static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000, |
| 103 | "fast encoders are too fast"); |
| 104 | |
| 105 | // Class to send position messages with sensor readings to our loops. |
Austin Schuh | 9e4374d | 2019-02-02 17:08:32 -0800 | [diff] [blame] | 106 | class SensorReader : public ::frc971::wpilib::SensorReader { |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 107 | public: |
| 108 | SensorReader() { |
| 109 | // Set to filter out anything shorter than 1/4 of the minimum pulse width |
| 110 | // we should ever see. |
Austin Schuh | 9e4374d | 2019-02-02 17:08:32 -0800 | [diff] [blame] | 111 | UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond); |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | void set_drivetrain_left_hall(::std::unique_ptr<AnalogInput> analog) { |
| 115 | drivetrain_left_hall_ = ::std::move(analog); |
| 116 | } |
| 117 | |
| 118 | void set_drivetrain_right_hall(::std::unique_ptr<AnalogInput> analog) { |
| 119 | drivetrain_right_hall_ = ::std::move(analog); |
| 120 | } |
| 121 | |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 122 | void RunIteration() { |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 123 | { |
| 124 | auto drivetrain_message = drivetrain_queue.position.MakeMessage(); |
| 125 | drivetrain_message->right_encoder = |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 126 | -drivetrain_translate(drivetrain_right_encoder_->GetRaw()); |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 127 | drivetrain_message->right_speed = |
| 128 | drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod()); |
| 129 | |
| 130 | drivetrain_message->left_encoder = |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 131 | drivetrain_translate(drivetrain_left_encoder_->GetRaw()); |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 132 | drivetrain_message->left_speed = |
| 133 | drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()); |
| 134 | |
| 135 | drivetrain_message->left_shifter_position = |
| 136 | hall_translate(drivetrain_left_hall_->GetVoltage()); |
| 137 | drivetrain_message->right_shifter_position = |
| 138 | hall_translate(drivetrain_right_hall_->GetVoltage()); |
| 139 | |
| 140 | drivetrain_message.Send(); |
| 141 | } |
| 142 | |
Sabina Davis | b6b987d | 2017-10-22 20:50:21 -0700 | [diff] [blame] | 143 | { |
| 144 | auto superstructure_message = superstructure_queue.position.MakeMessage(); |
| 145 | superstructure_message.Send(); |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 146 | } |
Sabina Davis | b6b987d | 2017-10-22 20:50:21 -0700 | [diff] [blame] | 147 | } |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 148 | |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 149 | private: |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 150 | ::std::unique_ptr<AnalogInput> drivetrain_left_hall_, drivetrain_right_hall_; |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | class SolenoidWriter { |
| 154 | public: |
| 155 | SolenoidWriter(const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm) |
| 156 | : pcm_(pcm), |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 157 | drivetrain_(".frc971.control_loops.drivetrain_queue.output"), |
Sabina Davis | b6b987d | 2017-10-22 20:50:21 -0700 | [diff] [blame] | 158 | superstructure_( |
| 159 | ".y2017_bot3.control_loops.superstructure_queue.output") {} |
| 160 | |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 161 | void set_compressor(::std::unique_ptr<Compressor> compressor) { |
Sabina Davis | b6b987d | 2017-10-22 20:50:21 -0700 | [diff] [blame] | 162 | compressor_ = ::std::move(compressor); |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 165 | void set_left_drivetrain_shifter( |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 166 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 167 | left_drivetrain_shifter_ = ::std::move(s); |
| 168 | } |
| 169 | |
| 170 | void set_right_drivetrain_shifter( |
| 171 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 172 | right_drivetrain_shifter_ = ::std::move(s); |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Sabina Davis | b6b987d | 2017-10-22 20:50:21 -0700 | [diff] [blame] | 175 | void set_fingers(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 176 | fingers_ = ::std::move(s); |
| 177 | } |
| 178 | |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 179 | void operator()() { |
| 180 | compressor_->Start(); |
| 181 | ::aos::SetCurrentThreadName("Solenoids"); |
| 182 | ::aos::SetCurrentThreadRealtimePriority(27); |
| 183 | |
| 184 | ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20), |
| 185 | ::std::chrono::milliseconds(1)); |
| 186 | |
| 187 | while (run_) { |
| 188 | { |
| 189 | int iterations = phased_loop.SleepUntilNext(); |
| 190 | if (iterations != 1) { |
| 191 | LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | { |
| 196 | drivetrain_.FetchLatest(); |
| 197 | if (drivetrain_.get()) { |
| 198 | LOG_STRUCT(DEBUG, "solenoids", *drivetrain_); |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 199 | left_drivetrain_shifter_->Set(!drivetrain_->left_high); |
| 200 | right_drivetrain_shifter_->Set(!drivetrain_->right_high); |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
| 204 | { |
Sabina Davis | b6b987d | 2017-10-22 20:50:21 -0700 | [diff] [blame] | 205 | superstructure_.FetchLatest(); |
| 206 | if (superstructure_.get()) { |
| 207 | LOG_STRUCT(DEBUG, "solenoids", *superstructure_); |
| 208 | fingers_->Set(superstructure_->fingers_out); |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 209 | } |
Sabina Davis | b6b987d | 2017-10-22 20:50:21 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | { |
| 213 | ::frc971::wpilib::PneumaticsToLog to_log; |
| 214 | { to_log.compressor_on = compressor_->Enabled(); } |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 215 | |
| 216 | pcm_->Flush(); |
| 217 | to_log.read_solenoids = pcm_->GetAll(); |
| 218 | LOG_STRUCT(DEBUG, "pneumatics info", to_log); |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | void Quit() { run_ = false; } |
| 224 | |
| 225 | private: |
| 226 | const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm_; |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 227 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> |
| 228 | left_drivetrain_shifter_; |
| 229 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> |
| 230 | right_drivetrain_shifter_; |
Sabina Davis | b6b987d | 2017-10-22 20:50:21 -0700 | [diff] [blame] | 231 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> fingers_; |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 232 | |
| 233 | ::std::unique_ptr<Compressor> compressor_; |
| 234 | |
| 235 | ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_; |
Sabina Davis | b6b987d | 2017-10-22 20:50:21 -0700 | [diff] [blame] | 236 | ::aos::Queue<::y2017_bot3::control_loops::SuperstructureQueue::Output> |
| 237 | superstructure_; |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 238 | |
| 239 | ::std::atomic<bool> run_{true}; |
| 240 | }; |
| 241 | |
| 242 | class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler { |
| 243 | public: |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 244 | void set_drivetrain_left0_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 245 | drivetrain_left0_victor_ = ::std::move(t); |
| 246 | } |
| 247 | void set_drivetrain_left1_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 248 | drivetrain_left1_victor_ = ::std::move(t); |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 251 | void set_drivetrain_right0_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 252 | drivetrain_right0_victor_ = ::std::move(t); |
| 253 | } |
| 254 | void set_drivetrain_right1_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 255 | drivetrain_right1_victor_ = ::std::move(t); |
| 256 | } |
Sabina Davis | b6b987d | 2017-10-22 20:50:21 -0700 | [diff] [blame] | 257 | |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 258 | private: |
| 259 | virtual void Read() override { |
| 260 | ::frc971::control_loops::drivetrain_queue.output.FetchAnother(); |
| 261 | } |
| 262 | |
| 263 | virtual void Write() override { |
| 264 | auto &queue = ::frc971::control_loops::drivetrain_queue.output; |
| 265 | LOG_STRUCT(DEBUG, "will output", *queue); |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 266 | drivetrain_left0_victor_->SetSpeed(queue->left_voltage / 12.0); |
| 267 | drivetrain_left1_victor_->SetSpeed(queue->left_voltage / 12.0); |
| 268 | drivetrain_right0_victor_->SetSpeed(-queue->right_voltage / 12.0); |
| 269 | drivetrain_right1_victor_->SetSpeed(-queue->right_voltage / 12.0); |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | virtual void Stop() override { |
| 273 | LOG(WARNING, "drivetrain output too old\n"); |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 274 | drivetrain_left0_victor_->SetDisabled(); |
| 275 | drivetrain_left1_victor_->SetDisabled(); |
| 276 | drivetrain_right0_victor_->SetDisabled(); |
| 277 | drivetrain_right1_victor_->SetDisabled(); |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 280 | ::std::unique_ptr<::frc::VictorSP> drivetrain_left0_victor_, |
| 281 | drivetrain_left1_victor_, drivetrain_right0_victor_, |
| 282 | drivetrain_right1_victor_; |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 283 | }; |
| 284 | |
Sabina Davis | b6b987d | 2017-10-22 20:50:21 -0700 | [diff] [blame] | 285 | class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler { |
| 286 | public: |
| 287 | void set_rollers_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 288 | rollers_victor_ = ::std::move(t); |
| 289 | } |
| 290 | |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 291 | void set_hanger0_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 292 | hanger0_victor_ = ::std::move(t); |
| 293 | } |
| 294 | void set_hanger1_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 295 | hanger1_victor_ = ::std::move(t); |
| 296 | } |
| 297 | |
Sabina Davis | b6b987d | 2017-10-22 20:50:21 -0700 | [diff] [blame] | 298 | private: |
| 299 | virtual void Read() override { |
| 300 | ::y2017_bot3::control_loops::superstructure_queue.output.FetchAnother(); |
| 301 | } |
| 302 | |
| 303 | virtual void Write() override { |
| 304 | auto &queue = ::y2017_bot3::control_loops::superstructure_queue.output; |
| 305 | LOG_STRUCT(DEBUG, "will output", *queue); |
| 306 | rollers_victor_->SetSpeed(queue->voltage_rollers / 12.0); |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 307 | hanger0_victor_->SetSpeed(queue->hanger_voltage / 12.0); |
| 308 | hanger1_victor_->SetSpeed(queue->hanger_voltage / 12.0); |
Sabina Davis | b6b987d | 2017-10-22 20:50:21 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | virtual void Stop() override { |
| 312 | LOG(WARNING, "Superstructure output too old.\n"); |
| 313 | rollers_victor_->SetDisabled(); |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 314 | hanger0_victor_->SetDisabled(); |
| 315 | hanger1_victor_->SetDisabled(); |
Sabina Davis | b6b987d | 2017-10-22 20:50:21 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | ::std::unique_ptr<::frc::VictorSP> rollers_victor_; |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 319 | ::std::unique_ptr<::frc::VictorSP> hanger0_victor_, hanger1_victor_; |
Sabina Davis | b6b987d | 2017-10-22 20:50:21 -0700 | [diff] [blame] | 320 | }; |
| 321 | |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 322 | class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase { |
| 323 | public: |
| 324 | ::std::unique_ptr<Encoder> make_encoder(int index) { |
| 325 | return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false, |
| 326 | Encoder::k4X); |
| 327 | } |
| 328 | |
| 329 | void Run() override { |
| 330 | ::aos::InitNRT(); |
| 331 | ::aos::SetCurrentThreadName("StartCompetition"); |
| 332 | |
| 333 | ::frc971::wpilib::JoystickSender joystick_sender; |
| 334 | ::std::thread joystick_thread(::std::ref(joystick_sender)); |
| 335 | |
| 336 | ::frc971::wpilib::PDPFetcher pdp_fetcher; |
| 337 | ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher)); |
| 338 | SensorReader reader; |
| 339 | |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 340 | reader.set_drivetrain_left_encoder(make_encoder(0)); |
| 341 | reader.set_drivetrain_right_encoder(make_encoder(1)); |
| 342 | reader.set_drivetrain_left_hall(make_unique<AnalogInput>(0)); |
| 343 | reader.set_drivetrain_right_hall(make_unique<AnalogInput>(1)); |
| 344 | |
Sabina Davis | b6b987d | 2017-10-22 20:50:21 -0700 | [diff] [blame] | 345 | reader.set_pwm_trigger(make_unique<DigitalInput>(0)); |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 346 | reader.set_dma(make_unique<DMA>()); |
| 347 | ::std::thread reader_thread(::std::ref(reader)); |
| 348 | |
| 349 | ::frc971::wpilib::GyroSender gyro_sender; |
| 350 | ::std::thread gyro_thread(::std::ref(gyro_sender)); |
| 351 | |
| 352 | DrivetrainWriter drivetrain_writer; |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 353 | drivetrain_writer.set_drivetrain_left0_victor( |
| 354 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(5))); |
| 355 | drivetrain_writer.set_drivetrain_left1_victor( |
| 356 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(6))); |
| 357 | drivetrain_writer.set_drivetrain_right0_victor( |
| 358 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(4))); |
| 359 | drivetrain_writer.set_drivetrain_right1_victor( |
| 360 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3))); |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 361 | ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer)); |
| 362 | |
Sabina Davis | b6b987d | 2017-10-22 20:50:21 -0700 | [diff] [blame] | 363 | SuperstructureWriter superstructure_writer; |
| 364 | superstructure_writer.set_rollers_victor( |
| 365 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2))); |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 366 | superstructure_writer.set_hanger0_victor( |
| 367 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0))); |
| 368 | superstructure_writer.set_hanger1_victor( |
| 369 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1))); |
| 370 | ::std::thread superstructure_writer_thread(::std::ref(superstructure_writer)); |
Sabina Davis | b6b987d | 2017-10-22 20:50:21 -0700 | [diff] [blame] | 371 | |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 372 | ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm( |
| 373 | new ::frc971::wpilib::BufferedPcm()); |
| 374 | SolenoidWriter solenoid_writer(pcm); |
Sabina Davis | 82b1918 | 2017-11-10 09:30:25 -0800 | [diff] [blame] | 375 | solenoid_writer.set_left_drivetrain_shifter(pcm->MakeSolenoid(3)); |
| 376 | solenoid_writer.set_right_drivetrain_shifter(pcm->MakeSolenoid(1)); |
Sabina Davis | b6b987d | 2017-10-22 20:50:21 -0700 | [diff] [blame] | 377 | solenoid_writer.set_fingers(pcm->MakeSolenoid(2)); |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 378 | |
| 379 | solenoid_writer.set_compressor(make_unique<Compressor>()); |
| 380 | |
| 381 | ::std::thread solenoid_thread(::std::ref(solenoid_writer)); |
| 382 | |
| 383 | // Wait forever. Not much else to do... |
| 384 | while (true) { |
| 385 | const int r = select(0, nullptr, nullptr, nullptr, nullptr); |
| 386 | if (r != 0) { |
| 387 | PLOG(WARNING, "infinite select failed"); |
| 388 | } else { |
| 389 | PLOG(WARNING, "infinite select succeeded??\n"); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | LOG(ERROR, "Exiting WPILibRobot\n"); |
| 394 | |
| 395 | joystick_sender.Quit(); |
| 396 | joystick_thread.join(); |
| 397 | pdp_fetcher.Quit(); |
| 398 | pdp_fetcher_thread.join(); |
| 399 | reader.Quit(); |
| 400 | reader_thread.join(); |
| 401 | gyro_sender.Quit(); |
| 402 | gyro_thread.join(); |
| 403 | |
| 404 | drivetrain_writer.Quit(); |
| 405 | drivetrain_writer_thread.join(); |
| 406 | solenoid_writer.Quit(); |
| 407 | solenoid_thread.join(); |
| 408 | |
| 409 | ::aos::Cleanup(); |
| 410 | } |
| 411 | }; |
| 412 | |
| 413 | } // namespace |
| 414 | } // namespace wpilib |
| 415 | } // namespace y2017_bot3 |
| 416 | |
| 417 | AOS_ROBOT_CLASS(::y2017_bot3::wpilib::WPILibRobot); |