Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 1 | #include <inttypes.h> |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 2 | #include <stdio.h> |
| 3 | #include <string.h> |
| 4 | #include <unistd.h> |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 5 | |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 6 | #include <array> |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 7 | #include <chrono> |
Brian Silverman | 50826c0 | 2017-02-18 14:40:25 -0800 | [diff] [blame] | 8 | #include <cmath> |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 9 | #include <functional> |
| 10 | #include <mutex> |
| 11 | #include <thread> |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 12 | |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 13 | #include "AnalogInput.h" |
| 14 | #include "Compressor.h" |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 15 | #include "Counter.h" |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 16 | #include "DigitalGlitchFilter.h" |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 17 | #include "DriverStation.h" |
| 18 | #include "Encoder.h" |
| 19 | #include "Relay.h" |
| 20 | #include "Servo.h" |
| 21 | #include "VictorSP.h" |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 22 | #undef ERROR |
| 23 | |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 24 | #include "aos/common/commonmath.h" |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 25 | #include "aos/common/logging/logging.h" |
| 26 | #include "aos/common/logging/queue_logging.h" |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 27 | #include "aos/common/messages/robot_state.q.h" |
| 28 | #include "aos/common/stl_mutex.h" |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 29 | #include "aos/common/time.h" |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 30 | #include "aos/common/util/compiler_memory_barrier.h" |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 31 | #include "aos/common/util/log_interval.h" |
| 32 | #include "aos/common/util/phased_loop.h" |
| 33 | #include "aos/common/util/wrapping_counter.h" |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 34 | #include "aos/linux_code/init.h" |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 35 | |
Philipp Schrader | 996a2a2 | 2017-02-22 05:02:48 +0000 | [diff] [blame] | 36 | #include "frc971/autonomous/auto.q.h" |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 37 | #include "frc971/control_loops/control_loops.q.h" |
| 38 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 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/dma_edge_counting.h" |
| 44 | #include "frc971/wpilib/encoder_and_potentiometer.h" |
| 45 | #include "frc971/wpilib/interrupt_edge_counting.h" |
| 46 | #include "frc971/wpilib/joystick_sender.h" |
| 47 | #include "frc971/wpilib/logging.q.h" |
| 48 | #include "frc971/wpilib/loop_output_handler.h" |
| 49 | #include "frc971/wpilib/pdp_fetcher.h" |
| 50 | #include "frc971/wpilib/wpilib_interface.h" |
| 51 | #include "frc971/wpilib/wpilib_robot_base.h" |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 52 | #include "y2017/constants.h" |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 53 | #include "y2017/control_loops/superstructure/superstructure.q.h" |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 54 | |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 55 | #ifndef M_PI |
| 56 | #define M_PI 3.14159265358979323846 |
| 57 | #endif |
| 58 | |
| 59 | using ::frc971::control_loops::drivetrain_queue; |
| 60 | using ::y2017::control_loops::superstructure_queue; |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 61 | using ::y2017::constants::Values; |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 62 | using ::aos::monotonic_clock; |
| 63 | namespace chrono = ::std::chrono; |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 64 | |
| 65 | namespace y2017 { |
| 66 | namespace wpilib { |
| 67 | namespace { |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 68 | |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 69 | constexpr double kMaxBringupPower = 12.0; |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 70 | |
| 71 | // TODO(Brian): Fix the interpretation of the result of GetRaw here and in the |
| 72 | // DMA stuff and then removing the * 2.0 in *_translate. |
| 73 | // The low bit is direction. |
| 74 | |
| 75 | // TODO(brian): Replace this with ::std::make_unique once all our toolchains |
| 76 | // have support. |
| 77 | template <class T, class... U> |
| 78 | std::unique_ptr<T> make_unique(U &&... u) { |
| 79 | return std::unique_ptr<T>(new T(std::forward<U>(u)...)); |
| 80 | } |
| 81 | |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 82 | // TODO(brian): Use ::std::max instead once we have C++14 so that can be |
| 83 | // constexpr. |
| 84 | template <typename T> |
| 85 | constexpr T max(T a, T b) { |
| 86 | return (a > b) ? a : b; |
| 87 | } |
| 88 | template <typename T, typename... Rest> |
| 89 | constexpr T max(T a, T b, T c, Rest... rest) { |
| 90 | return max(max(a, b), c, rest...); |
| 91 | } |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 92 | |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 93 | double drivetrain_translate(int32_t in) { |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 94 | return static_cast<double>(in) / |
| 95 | Values::kDrivetrainEncoderCountsPerRevolution * |
| 96 | Values::kDrivetrainEncoderRatio * 2.0 * M_PI; |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | double drivetrain_velocity_translate(double in) { |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 100 | return (1.0 / in) / Values::kDrivetrainCyclesPerRevolution * |
| 101 | Values::kDrivetrainEncoderRatio * 2.0 * M_PI; |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Brian Silverman | 50826c0 | 2017-02-18 14:40:25 -0800 | [diff] [blame] | 104 | // TODO(Travis): Make sure the number of turns is right. |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 105 | double intake_pot_translate(double voltage) { |
Brian Silverman | 50826c0 | 2017-02-18 14:40:25 -0800 | [diff] [blame] | 106 | return voltage * Values::kIntakePotRatio * (3.0 /*turns*/ / 5.0 /*volts*/) * |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 107 | (2 * M_PI /*radians*/); |
| 108 | } |
| 109 | |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 110 | constexpr double kMaxFastEncoderPulsesPerSecond = |
| 111 | max(Values::kMaxDrivetrainEncoderPulsesPerSecond, |
| 112 | Values::kMaxShooterEncoderPulsesPerSecond); |
| 113 | static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000, |
| 114 | "fast encoders are too fast"); |
| 115 | constexpr double kMaxMediumEncoderPulsesPerSecond = |
| 116 | max(Values::kMaxIntakeEncoderPulsesPerSecond, |
| 117 | Values::kMaxTurretEncoderPulsesPerSecond, |
| 118 | Values::kMaxIndexerEncoderPulsesPerSecond); |
| 119 | static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000, |
| 120 | "medium encoders are too fast"); |
| 121 | constexpr double kMaxSlowEncoderPulsesPerSecond = |
| 122 | Values::kMaxHoodEncoderPulsesPerSecond; |
| 123 | static_assert(kMaxSlowEncoderPulsesPerSecond <= 100000, |
| 124 | "slow encoders are too fast"); |
Brian | ef030df | 2017-03-05 15:06:04 -0800 | [diff] [blame] | 125 | static_assert(kMaxSlowEncoderPulsesPerSecond < kMaxMediumEncoderPulsesPerSecond, |
| 126 | "slow encoders are faster than medium?"); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 127 | |
| 128 | // Class to send position messages with sensor readings to our loops. |
| 129 | class SensorReader { |
| 130 | public: |
| 131 | SensorReader() { |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 132 | // Set to filter out anything shorter than 1/4 of the minimum pulse width |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 133 | // we should ever see. |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 134 | fast_encoder_filter_.SetPeriodNanoSeconds( |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 135 | static_cast<int>(1 / 4.0 /* built-in tolerance */ / |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 136 | kMaxFastEncoderPulsesPerSecond * 1e9 + |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 137 | 0.5)); |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 138 | medium_encoder_filter_.SetPeriodNanoSeconds( |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 139 | static_cast<int>(1 / 4.0 /* built-in tolerance */ / |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 140 | kMaxMediumEncoderPulsesPerSecond * 1e9 + |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 141 | 0.5)); |
Brian | ef030df | 2017-03-05 15:06:04 -0800 | [diff] [blame] | 142 | hall_filter_.SetPeriodNanoSeconds(100000); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 145 | void set_drivetrain_left_encoder(::std::unique_ptr<Encoder> encoder) { |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 146 | fast_encoder_filter_.Add(encoder.get()); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 147 | drivetrain_left_encoder_ = ::std::move(encoder); |
| 148 | } |
| 149 | |
| 150 | void set_drivetrain_right_encoder(::std::unique_ptr<Encoder> encoder) { |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 151 | fast_encoder_filter_.Add(encoder.get()); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 152 | drivetrain_right_encoder_ = ::std::move(encoder); |
| 153 | } |
| 154 | |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 155 | void set_shooter_encoder(::std::unique_ptr<Encoder> encoder) { |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 156 | fast_encoder_filter_.Add(encoder.get()); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 157 | shooter_encoder_ = ::std::move(encoder); |
| 158 | } |
| 159 | |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 160 | void set_intake_encoder(::std::unique_ptr<Encoder> encoder) { |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 161 | medium_encoder_filter_.Add(encoder.get()); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 162 | intake_encoder_.set_encoder(::std::move(encoder)); |
| 163 | } |
| 164 | |
| 165 | void set_intake_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) { |
| 166 | intake_encoder_.set_potentiometer(::std::move(potentiometer)); |
| 167 | } |
| 168 | |
Brian Silverman | 50826c0 | 2017-02-18 14:40:25 -0800 | [diff] [blame] | 169 | void set_intake_absolute(::std::unique_ptr<DigitalInput> input) { |
| 170 | intake_encoder_.set_absolute_pwm(::std::move(input)); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 173 | void set_indexer_encoder(::std::unique_ptr<Encoder> encoder) { |
| 174 | medium_encoder_filter_.Add(encoder.get()); |
Brian | ef030df | 2017-03-05 15:06:04 -0800 | [diff] [blame] | 175 | indexer_counter_.set_encoder(encoder.get()); |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 176 | indexer_encoder_ = ::std::move(encoder); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Brian | ef030df | 2017-03-05 15:06:04 -0800 | [diff] [blame] | 179 | void set_indexer_hall(::std::unique_ptr<DigitalInput> input) { |
| 180 | hall_filter_.Add(input.get()); |
| 181 | indexer_counter_.set_input(input.get()); |
| 182 | indexer_hall_ = ::std::move(input); |
| 183 | } |
| 184 | |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 185 | void set_turret_encoder(::std::unique_ptr<Encoder> encoder) { |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 186 | medium_encoder_filter_.Add(encoder.get()); |
Brian | ef030df | 2017-03-05 15:06:04 -0800 | [diff] [blame] | 187 | turret_counter_.set_encoder(encoder.get()); |
| 188 | turret_encoder_ = ::std::move(encoder); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Brian | ef030df | 2017-03-05 15:06:04 -0800 | [diff] [blame] | 191 | void set_turret_hall(::std::unique_ptr<DigitalInput> input) { |
| 192 | hall_filter_.Add(input.get()); |
| 193 | turret_counter_.set_input(input.get()); |
| 194 | turret_hall_ = ::std::move(input); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 195 | } |
| 196 | |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 197 | void set_hood_encoder(::std::unique_ptr<Encoder> encoder) { |
Brian | ef030df | 2017-03-05 15:06:04 -0800 | [diff] [blame] | 198 | medium_encoder_filter_.Add(encoder.get()); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 199 | hood_encoder_.set_encoder(::std::move(encoder)); |
| 200 | } |
| 201 | |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 202 | void set_hood_index(::std::unique_ptr<DigitalInput> index) { |
Brian | ef030df | 2017-03-05 15:06:04 -0800 | [diff] [blame] | 203 | medium_encoder_filter_.Add(index.get()); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 204 | hood_encoder_.set_index(::std::move(index)); |
| 205 | } |
| 206 | |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 207 | void set_autonomous_mode(int i, ::std::unique_ptr<DigitalInput> sensor) { |
| 208 | autonomous_modes_.at(i) = ::std::move(sensor); |
| 209 | } |
| 210 | |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 211 | void set_pwm_trigger(::std::unique_ptr<DigitalInput> pwm_trigger) { |
| 212 | medium_encoder_filter_.Add(pwm_trigger.get()); |
| 213 | pwm_trigger_ = ::std::move(pwm_trigger); |
| 214 | } |
| 215 | |
| 216 | // All of the DMA-related set_* calls must be made before this, and it |
| 217 | // doesn't |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 218 | // hurt to do all of them. |
| 219 | void set_dma(::std::unique_ptr<DMA> dma) { |
| 220 | dma_synchronizer_.reset( |
| 221 | new ::frc971::wpilib::DMASynchronizer(::std::move(dma))); |
Brian | ef030df | 2017-03-05 15:06:04 -0800 | [diff] [blame] | 222 | dma_synchronizer_->Add(&indexer_counter_); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 223 | dma_synchronizer_->Add(&hood_encoder_); |
Brian | ef030df | 2017-03-05 15:06:04 -0800 | [diff] [blame] | 224 | dma_synchronizer_->Add(&turret_counter_); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 225 | } |
| 226 | |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 227 | void RunPWMDetecter() { |
| 228 | ::aos::SetCurrentThreadRealtimePriority(41); |
| 229 | |
| 230 | pwm_trigger_->RequestInterrupts(); |
| 231 | // Rising edge only. |
| 232 | pwm_trigger_->SetUpSourceEdge(true, false); |
| 233 | |
| 234 | monotonic_clock::time_point last_posedge_monotonic = |
| 235 | monotonic_clock::min_time; |
| 236 | |
| 237 | while (run_) { |
| 238 | auto ret = pwm_trigger_->WaitForInterrupt(1.0, true); |
| 239 | if (ret == InterruptableSensorBase::WaitResult::kRisingEdge) { |
| 240 | // Grab all the clocks. |
| 241 | const double pwm_fpga_time = pwm_trigger_->ReadRisingTimestamp(); |
| 242 | |
| 243 | aos_compiler_memory_barrier(); |
| 244 | const double fpga_time_before = GetFPGATime() * 1e-6; |
| 245 | aos_compiler_memory_barrier(); |
| 246 | const monotonic_clock::time_point monotonic_now = |
| 247 | monotonic_clock::now(); |
| 248 | aos_compiler_memory_barrier(); |
| 249 | const double fpga_time_after = GetFPGATime() * 1e-6; |
| 250 | aos_compiler_memory_barrier(); |
| 251 | |
| 252 | const double fpga_offset = |
| 253 | (fpga_time_after + fpga_time_before) / 2.0 - pwm_fpga_time; |
| 254 | |
| 255 | // Compute when the edge was. |
| 256 | const monotonic_clock::time_point monotonic_edge = |
| 257 | monotonic_now - chrono::duration_cast<chrono::nanoseconds>( |
| 258 | chrono::duration<double>(fpga_offset)); |
| 259 | |
| 260 | LOG(INFO, "Got PWM pulse %f spread, %f offset, %lld trigger\n", |
| 261 | fpga_time_after - fpga_time_before, fpga_offset, |
| 262 | monotonic_edge.time_since_epoch().count()); |
| 263 | |
| 264 | // Compute bounds on the timestep and sampling times. |
| 265 | const double fpga_sample_length = fpga_time_after - fpga_time_before; |
| 266 | const chrono::nanoseconds elapsed_time = |
| 267 | monotonic_edge - last_posedge_monotonic; |
| 268 | |
| 269 | last_posedge_monotonic = monotonic_edge; |
| 270 | |
| 271 | // Verify that the values are sane. |
| 272 | if (fpga_sample_length > 2e-5 || fpga_sample_length < 0) { |
| 273 | continue; |
| 274 | } |
| 275 | if (fpga_offset < 0 || fpga_offset > 0.00015) { |
| 276 | continue; |
| 277 | } |
| 278 | if (elapsed_time > |
| 279 | chrono::microseconds(5050) + chrono::microseconds(4) || |
| 280 | elapsed_time < |
| 281 | chrono::microseconds(5050) - chrono::microseconds(4)) { |
| 282 | continue; |
| 283 | } |
| 284 | // Good edge! |
| 285 | { |
| 286 | ::std::unique_lock<::aos::stl_mutex> locker(tick_time_mutex_); |
| 287 | last_tick_time_monotonic_timepoint_ = last_posedge_monotonic; |
| 288 | last_period_ = elapsed_time; |
| 289 | } |
| 290 | } else { |
| 291 | LOG(INFO, "PWM triggered %d\n", ret); |
| 292 | } |
| 293 | } |
| 294 | pwm_trigger_->CancelInterrupts(); |
| 295 | } |
| 296 | |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 297 | void operator()() { |
| 298 | ::aos::SetCurrentThreadName("SensorReader"); |
| 299 | |
| 300 | my_pid_ = getpid(); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 301 | |
| 302 | dma_synchronizer_->Start(); |
| 303 | |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 304 | ::aos::time::PhasedLoop phased_loop(last_period_, |
| 305 | ::std::chrono::milliseconds(3)); |
| 306 | chrono::nanoseconds filtered_period = last_period_; |
| 307 | |
| 308 | ::std::thread pwm_detecter_thread( |
| 309 | ::std::bind(&SensorReader::RunPWMDetecter, this)); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 310 | |
| 311 | ::aos::SetCurrentThreadRealtimePriority(40); |
| 312 | while (run_) { |
| 313 | { |
| 314 | const int iterations = phased_loop.SleepUntilNext(); |
| 315 | if (iterations != 1) { |
| 316 | LOG(WARNING, "SensorReader skipped %d iterations\n", iterations - 1); |
| 317 | } |
| 318 | } |
| 319 | RunIteration(); |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 320 | |
| 321 | monotonic_clock::time_point last_tick_timepoint; |
| 322 | chrono::nanoseconds period; |
| 323 | { |
| 324 | ::std::unique_lock<::aos::stl_mutex> locker(tick_time_mutex_); |
| 325 | last_tick_timepoint = last_tick_time_monotonic_timepoint_; |
| 326 | period = last_period_; |
| 327 | } |
| 328 | |
| 329 | if (last_tick_timepoint == monotonic_clock::min_time) { |
| 330 | continue; |
| 331 | } |
| 332 | chrono::nanoseconds new_offset = phased_loop.OffsetFromIntervalAndTime( |
| 333 | period, last_tick_timepoint + chrono::microseconds(2050)); |
| 334 | |
| 335 | // TODO(austin): If this is the first edge in a while, skip to it (plus |
| 336 | // an offset). Otherwise, slowly drift time to line up. |
| 337 | |
| 338 | phased_loop.set_interval_and_offset(period, new_offset); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 339 | } |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 340 | pwm_detecter_thread.join(); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | void RunIteration() { |
Austin Schuh | 94f51e9 | 2017-10-30 19:25:32 -0700 | [diff] [blame] | 344 | ::frc971::wpilib::SendRobotState(my_pid_); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 345 | |
| 346 | const auto values = constants::GetValues(); |
| 347 | |
| 348 | { |
| 349 | auto drivetrain_message = drivetrain_queue.position.MakeMessage(); |
| 350 | drivetrain_message->right_encoder = |
| 351 | drivetrain_translate(drivetrain_right_encoder_->GetRaw()); |
Austin Schuh | 0fc1e6d | 2017-02-21 02:04:10 -0800 | [diff] [blame] | 352 | drivetrain_message->right_speed = |
| 353 | drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod()); |
| 354 | |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 355 | drivetrain_message->left_encoder = |
| 356 | -drivetrain_translate(drivetrain_left_encoder_->GetRaw()); |
| 357 | drivetrain_message->left_speed = |
| 358 | drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 359 | |
| 360 | drivetrain_message.Send(); |
| 361 | } |
| 362 | |
| 363 | dma_synchronizer_->RunIteration(); |
| 364 | |
| 365 | { |
| 366 | auto superstructure_message = superstructure_queue.position.MakeMessage(); |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 367 | CopyPosition(intake_encoder_, &superstructure_message->intake, |
Brian Silverman | 50826c0 | 2017-02-18 14:40:25 -0800 | [diff] [blame] | 368 | Values::kIntakeEncoderCountsPerRevolution, |
Austin Schuh | 0fc1e6d | 2017-02-21 02:04:10 -0800 | [diff] [blame] | 369 | Values::kIntakeEncoderRatio, intake_pot_translate, true, |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 370 | values.intake.pot_offset); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 371 | |
Brian | ef030df | 2017-03-05 15:06:04 -0800 | [diff] [blame] | 372 | CopyPosition(indexer_counter_, &superstructure_message->column.indexer, |
| 373 | Values::kIndexerEncoderCountsPerRevolution, |
Austin Schuh | 546a038 | 2017-04-16 19:10:18 -0700 | [diff] [blame] | 374 | Values::kIndexerEncoderRatio, true); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 375 | |
Brian Silverman | 50826c0 | 2017-02-18 14:40:25 -0800 | [diff] [blame] | 376 | superstructure_message->theta_shooter = |
| 377 | encoder_translate(shooter_encoder_->GetRaw(), |
| 378 | Values::kShooterEncoderCountsPerRevolution, |
| 379 | Values::kShooterEncoderRatio); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 380 | |
Brian Silverman | 50826c0 | 2017-02-18 14:40:25 -0800 | [diff] [blame] | 381 | CopyPosition(hood_encoder_, &superstructure_message->hood, |
| 382 | Values::kHoodEncoderCountsPerRevolution, |
Austin Schuh | 0fc1e6d | 2017-02-21 02:04:10 -0800 | [diff] [blame] | 383 | Values::kHoodEncoderRatio, true); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 384 | |
Brian | ef030df | 2017-03-05 15:06:04 -0800 | [diff] [blame] | 385 | CopyPosition(turret_counter_, &superstructure_message->column.turret, |
Brian Silverman | 50826c0 | 2017-02-18 14:40:25 -0800 | [diff] [blame] | 386 | Values::kTurretEncoderCountsPerRevolution, |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 387 | Values::kTurretEncoderRatio, false); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 388 | |
| 389 | superstructure_message.Send(); |
| 390 | } |
| 391 | |
| 392 | { |
Philipp Schrader | 996a2a2 | 2017-02-22 05:02:48 +0000 | [diff] [blame] | 393 | auto auto_mode_message = ::frc971::autonomous::auto_mode.MakeMessage(); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 394 | auto_mode_message->mode = 0; |
| 395 | for (size_t i = 0; i < autonomous_modes_.size(); ++i) { |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 396 | if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) { |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 397 | auto_mode_message->mode |= 1 << i; |
| 398 | } |
| 399 | } |
| 400 | LOG_STRUCT(DEBUG, "auto mode", *auto_mode_message); |
| 401 | auto_mode_message.Send(); |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | void Quit() { run_ = false; } |
| 406 | |
| 407 | private: |
Brian Silverman | 50826c0 | 2017-02-18 14:40:25 -0800 | [diff] [blame] | 408 | double encoder_translate(int32_t value, double counts_per_revolution, |
| 409 | double ratio) { |
| 410 | return static_cast<double>(value) / counts_per_revolution * ratio * |
| 411 | (2.0 * M_PI); |
| 412 | } |
| 413 | |
Brian Silverman | 7cce2d3 | 2017-02-19 21:48:48 -0800 | [diff] [blame] | 414 | void CopyPosition(const ::frc971::wpilib::DMAEncoder &encoder, |
| 415 | ::frc971::IndexPosition *position, |
Brian Silverman | 50826c0 | 2017-02-18 14:40:25 -0800 | [diff] [blame] | 416 | double encoder_counts_per_revolution, double encoder_ratio, |
Brian Silverman | 7cce2d3 | 2017-02-19 21:48:48 -0800 | [diff] [blame] | 417 | bool reverse) { |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 418 | const double multiplier = reverse ? -1.0 : 1.0; |
| 419 | position->encoder = |
Brian Silverman | 50826c0 | 2017-02-18 14:40:25 -0800 | [diff] [blame] | 420 | multiplier * encoder_translate(encoder.polled_encoder_value(), |
| 421 | encoder_counts_per_revolution, |
| 422 | encoder_ratio); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 423 | position->latched_encoder = |
Brian Silverman | 50826c0 | 2017-02-18 14:40:25 -0800 | [diff] [blame] | 424 | multiplier * encoder_translate(encoder.last_encoder_value(), |
| 425 | encoder_counts_per_revolution, |
| 426 | encoder_ratio); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 427 | position->index_pulses = encoder.index_posedge_count(); |
| 428 | } |
| 429 | |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 430 | void CopyPosition( |
| 431 | const ::frc971::wpilib::AbsoluteEncoderAndPotentiometer &encoder, |
| 432 | ::frc971::PotAndAbsolutePosition *position, |
| 433 | double encoder_counts_per_revolution, double encoder_ratio, |
| 434 | ::std::function<double(double)> potentiometer_translate, bool reverse, |
| 435 | double pot_offset) { |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 436 | const double multiplier = reverse ? -1.0 : 1.0; |
| 437 | position->pot = multiplier * potentiometer_translate( |
Brian Silverman | 50826c0 | 2017-02-18 14:40:25 -0800 | [diff] [blame] | 438 | encoder.ReadPotentiometerVoltage()) + |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 439 | pot_offset; |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 440 | position->encoder = |
Brian Silverman | 50826c0 | 2017-02-18 14:40:25 -0800 | [diff] [blame] | 441 | multiplier * encoder_translate(encoder.ReadRelativeEncoder(), |
| 442 | encoder_counts_per_revolution, |
| 443 | encoder_ratio); |
Austin Schuh | 0fc1e6d | 2017-02-21 02:04:10 -0800 | [diff] [blame] | 444 | |
| 445 | position->absolute_encoder = |
| 446 | (reverse ? (1.0 - encoder.ReadAbsoluteEncoder()) |
| 447 | : encoder.ReadAbsoluteEncoder()) * |
| 448 | encoder_ratio * (2.0 * M_PI); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 449 | } |
| 450 | |
Brian | ef030df | 2017-03-05 15:06:04 -0800 | [diff] [blame] | 451 | void CopyPosition(const ::frc971::wpilib::DMAEdgeCounter &counter, |
| 452 | ::frc971::HallEffectAndPosition *position, |
| 453 | double encoder_counts_per_revolution, double encoder_ratio, |
| 454 | bool reverse) { |
| 455 | const double multiplier = reverse ? -1.0 : 1.0; |
| 456 | position->position = |
| 457 | multiplier * encoder_translate(counter.polled_encoder(), |
| 458 | encoder_counts_per_revolution, |
| 459 | encoder_ratio); |
| 460 | position->current = !counter.polled_value(); |
| 461 | position->posedge_count = counter.negative_count(); |
| 462 | position->negedge_count = counter.positive_count(); |
| 463 | position->posedge_value = |
| 464 | multiplier * encoder_translate(counter.last_negative_encoder_value(), |
| 465 | encoder_counts_per_revolution, |
| 466 | encoder_ratio); |
| 467 | position->negedge_value = |
| 468 | multiplier * encoder_translate(counter.last_positive_encoder_value(), |
| 469 | encoder_counts_per_revolution, |
| 470 | encoder_ratio); |
| 471 | } |
| 472 | |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 473 | int32_t my_pid_; |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 474 | |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 475 | // Mutex to manage access to the period and tick time variables. |
| 476 | ::aos::stl_mutex tick_time_mutex_; |
| 477 | monotonic_clock::time_point last_tick_time_monotonic_timepoint_ = |
| 478 | monotonic_clock::min_time; |
| 479 | chrono::nanoseconds last_period_ = chrono::microseconds(5050); |
| 480 | |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 481 | ::std::unique_ptr<::frc971::wpilib::DMASynchronizer> dma_synchronizer_; |
| 482 | |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 483 | DigitalGlitchFilter fast_encoder_filter_, medium_encoder_filter_, |
Brian | ef030df | 2017-03-05 15:06:04 -0800 | [diff] [blame] | 484 | hall_filter_; |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 485 | |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 486 | ::std::unique_ptr<Encoder> drivetrain_left_encoder_, |
| 487 | drivetrain_right_encoder_; |
| 488 | |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 489 | ::frc971::wpilib::AbsoluteEncoderAndPotentiometer intake_encoder_; |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 490 | |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 491 | ::std::unique_ptr<Encoder> indexer_encoder_; |
Brian | ef030df | 2017-03-05 15:06:04 -0800 | [diff] [blame] | 492 | ::std::unique_ptr<DigitalInput> indexer_hall_; |
| 493 | ::frc971::wpilib::DMAEdgeCounter indexer_counter_; |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 494 | |
Brian | ef030df | 2017-03-05 15:06:04 -0800 | [diff] [blame] | 495 | ::std::unique_ptr<Encoder> turret_encoder_; |
| 496 | ::std::unique_ptr<DigitalInput> turret_hall_; |
| 497 | ::frc971::wpilib::DMAEdgeCounter turret_counter_; |
| 498 | |
Brian Silverman | 7cce2d3 | 2017-02-19 21:48:48 -0800 | [diff] [blame] | 499 | ::frc971::wpilib::DMAEncoder hood_encoder_; |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 500 | ::std::unique_ptr<Encoder> shooter_encoder_; |
| 501 | |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 502 | ::std::unique_ptr<DigitalInput> pwm_trigger_; |
| 503 | |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 504 | ::std::array<::std::unique_ptr<DigitalInput>, 4> autonomous_modes_; |
| 505 | |
| 506 | ::std::atomic<bool> run_{true}; |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 507 | }; |
| 508 | |
Adam Snaider | e0554ef | 2017-03-11 23:02:45 -0800 | [diff] [blame] | 509 | class SolenoidWriter { |
| 510 | public: |
| 511 | SolenoidWriter() |
| 512 | : superstructure_(".y2017.control_loops.superstructure_queue.output") {} |
| 513 | |
| 514 | ::frc971::wpilib::BufferedPcm *pcm() { return &pcm_; } |
| 515 | |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 516 | void set_lights(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
Adam Snaider | e0554ef | 2017-03-11 23:02:45 -0800 | [diff] [blame] | 517 | lights_ = ::std::move(s); |
| 518 | } |
| 519 | |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 520 | void set_rgb_light(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
Austin Schuh | c587c88 | 2017-03-29 21:33:10 -0700 | [diff] [blame] | 521 | rgb_lights_ = ::std::move(s); |
| 522 | } |
| 523 | |
Adam Snaider | e0554ef | 2017-03-11 23:02:45 -0800 | [diff] [blame] | 524 | void operator()() { |
| 525 | ::aos::SetCurrentThreadName("Solenoids"); |
| 526 | ::aos::SetCurrentThreadRealtimePriority(27); |
| 527 | |
| 528 | ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20), |
| 529 | ::std::chrono::milliseconds(1)); |
| 530 | |
| 531 | while (run_) { |
| 532 | { |
| 533 | const int iterations = phased_loop.SleepUntilNext(); |
| 534 | if (iterations != 1) { |
| 535 | LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1); |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | { |
| 540 | superstructure_.FetchLatest(); |
| 541 | if (superstructure_.get()) { |
| 542 | LOG_STRUCT(DEBUG, "solenoids", *superstructure_); |
| 543 | lights_->Set(superstructure_->lights_on); |
Austin Schuh | c587c88 | 2017-03-29 21:33:10 -0700 | [diff] [blame] | 544 | rgb_lights_->Set(superstructure_->red_light_on | |
| 545 | superstructure_->green_light_on | |
| 546 | superstructure_->blue_light_on); |
Adam Snaider | e0554ef | 2017-03-11 23:02:45 -0800 | [diff] [blame] | 547 | } |
| 548 | } |
| 549 | |
| 550 | pcm_.Flush(); |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | void Quit() { run_ = false; } |
| 555 | |
| 556 | private: |
| 557 | ::frc971::wpilib::BufferedPcm pcm_; |
| 558 | |
Austin Schuh | c587c88 | 2017-03-29 21:33:10 -0700 | [diff] [blame] | 559 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> lights_, rgb_lights_; |
Adam Snaider | e0554ef | 2017-03-11 23:02:45 -0800 | [diff] [blame] | 560 | |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 561 | ::aos::Queue<::y2017::control_loops::SuperstructureQueue::Output> |
Adam Snaider | e0554ef | 2017-03-11 23:02:45 -0800 | [diff] [blame] | 562 | superstructure_; |
| 563 | |
| 564 | ::std::atomic<bool> run_{true}; |
| 565 | }; |
| 566 | |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 567 | class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler { |
| 568 | public: |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 569 | void set_drivetrain_left_victor(::std::unique_ptr<::frc::VictorSP> t) { |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 570 | drivetrain_left_victor_ = ::std::move(t); |
| 571 | } |
| 572 | |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 573 | void set_drivetrain_right_victor(::std::unique_ptr<::frc::VictorSP> t) { |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 574 | drivetrain_right_victor_ = ::std::move(t); |
| 575 | } |
| 576 | |
| 577 | private: |
| 578 | virtual void Read() override { |
| 579 | ::frc971::control_loops::drivetrain_queue.output.FetchAnother(); |
| 580 | } |
| 581 | |
| 582 | virtual void Write() override { |
| 583 | auto &queue = ::frc971::control_loops::drivetrain_queue.output; |
| 584 | LOG_STRUCT(DEBUG, "will output", *queue); |
Austin Schuh | 410e381 | 2017-02-21 16:44:03 -0800 | [diff] [blame] | 585 | drivetrain_left_victor_->SetSpeed(-queue->left_voltage / 12.0); |
| 586 | drivetrain_right_victor_->SetSpeed(queue->right_voltage / 12.0); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | virtual void Stop() override { |
| 590 | LOG(WARNING, "drivetrain output too old\n"); |
| 591 | drivetrain_left_victor_->SetDisabled(); |
| 592 | drivetrain_right_victor_->SetDisabled(); |
| 593 | } |
| 594 | |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 595 | ::std::unique_ptr<::frc::VictorSP> drivetrain_left_victor_, |
| 596 | drivetrain_right_victor_; |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 597 | }; |
| 598 | |
| 599 | class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler { |
| 600 | public: |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 601 | void set_intake_victor(::std::unique_ptr<::frc::VictorSP> t) { |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 602 | intake_victor_ = ::std::move(t); |
| 603 | } |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 604 | void set_intake_rollers_victor(::std::unique_ptr<::frc::VictorSP> t) { |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 605 | intake_rollers_victor_ = ::std::move(t); |
| 606 | } |
| 607 | |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 608 | void set_indexer_victor(::std::unique_ptr<::frc::VictorSP> t) { |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 609 | indexer_victor_ = ::std::move(t); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 610 | } |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 611 | void set_indexer_roller_victor(::std::unique_ptr<::frc::VictorSP> t) { |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 612 | indexer_roller_victor_ = ::std::move(t); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 613 | } |
| 614 | |
Austin Schuh | 6a8131b | 2017-04-08 15:39:22 -0700 | [diff] [blame] | 615 | void set_gear_servo(::std::unique_ptr<::frc::Servo> t) { |
| 616 | gear_servo_ = ::std::move(t); |
| 617 | } |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 618 | void set_shooter_victor(::std::unique_ptr<::frc::VictorSP> t) { |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 619 | shooter_victor_ = ::std::move(t); |
| 620 | } |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 621 | void set_turret_victor(::std::unique_ptr<::frc::VictorSP> t) { |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 622 | turret_victor_ = ::std::move(t); |
| 623 | } |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 624 | void set_hood_victor(::std::unique_ptr<::frc::VictorSP> t) { |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 625 | hood_victor_ = ::std::move(t); |
| 626 | } |
| 627 | |
Austin Schuh | c587c88 | 2017-03-29 21:33:10 -0700 | [diff] [blame] | 628 | void set_red_light(::std::unique_ptr<DigitalOutput> t) { |
| 629 | red_light_ = ::std::move(t); |
| 630 | } |
| 631 | void set_green_light(::std::unique_ptr<DigitalOutput> t) { |
| 632 | green_light_ = ::std::move(t); |
| 633 | } |
| 634 | void set_blue_light(::std::unique_ptr<DigitalOutput> t) { |
| 635 | blue_light_ = ::std::move(t); |
| 636 | } |
| 637 | |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 638 | private: |
| 639 | virtual void Read() override { |
| 640 | ::y2017::control_loops::superstructure_queue.output.FetchAnother(); |
| 641 | } |
| 642 | |
| 643 | virtual void Write() override { |
| 644 | auto &queue = ::y2017::control_loops::superstructure_queue.output; |
| 645 | LOG_STRUCT(DEBUG, "will output", *queue); |
| 646 | intake_victor_->SetSpeed(::aos::Clip(queue->voltage_intake, |
| 647 | -kMaxBringupPower, kMaxBringupPower) / |
| 648 | 12.0); |
| 649 | intake_rollers_victor_->SetSpeed(queue->voltage_intake_rollers / 12.0); |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 650 | indexer_victor_->SetSpeed(-queue->voltage_indexer / 12.0); |
| 651 | indexer_roller_victor_->SetSpeed(queue->voltage_indexer_rollers / 12.0); |
Austin Schuh | 410e381 | 2017-02-21 16:44:03 -0800 | [diff] [blame] | 652 | turret_victor_->SetSpeed(::aos::Clip(-queue->voltage_turret, |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 653 | -kMaxBringupPower, kMaxBringupPower) / |
| 654 | 12.0); |
| 655 | hood_victor_->SetSpeed( |
| 656 | ::aos::Clip(queue->voltage_hood, -kMaxBringupPower, kMaxBringupPower) / |
| 657 | 12.0); |
| 658 | shooter_victor_->SetSpeed(queue->voltage_shooter / 12.0); |
Austin Schuh | c587c88 | 2017-03-29 21:33:10 -0700 | [diff] [blame] | 659 | |
| 660 | red_light_->Set(queue->red_light_on); |
| 661 | green_light_->Set(queue->green_light_on); |
| 662 | blue_light_->Set(queue->blue_light_on); |
Austin Schuh | 6a8131b | 2017-04-08 15:39:22 -0700 | [diff] [blame] | 663 | |
| 664 | gear_servo_->Set(queue->gear_servo); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | virtual void Stop() override { |
| 668 | LOG(WARNING, "Superstructure output too old.\n"); |
| 669 | intake_victor_->SetDisabled(); |
| 670 | intake_rollers_victor_->SetDisabled(); |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 671 | indexer_victor_->SetDisabled(); |
| 672 | indexer_roller_victor_->SetDisabled(); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 673 | turret_victor_->SetDisabled(); |
| 674 | hood_victor_->SetDisabled(); |
| 675 | shooter_victor_->SetDisabled(); |
Austin Schuh | c587c88 | 2017-03-29 21:33:10 -0700 | [diff] [blame] | 676 | |
Austin Schuh | 6a8131b | 2017-04-08 15:39:22 -0700 | [diff] [blame] | 677 | gear_servo_->SetOffline(); |
| 678 | |
Austin Schuh | c587c88 | 2017-03-29 21:33:10 -0700 | [diff] [blame] | 679 | red_light_->Set(true); |
| 680 | green_light_->Set(true); |
| 681 | blue_light_->Set(true); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 682 | } |
| 683 | |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 684 | ::std::unique_ptr<::frc::VictorSP> intake_victor_, intake_rollers_victor_, |
| 685 | indexer_victor_, indexer_roller_victor_, shooter_victor_, turret_victor_, |
| 686 | hood_victor_; |
Austin Schuh | c587c88 | 2017-03-29 21:33:10 -0700 | [diff] [blame] | 687 | |
Austin Schuh | 6a8131b | 2017-04-08 15:39:22 -0700 | [diff] [blame] | 688 | ::std::unique_ptr<::frc::Servo> gear_servo_; |
| 689 | |
Austin Schuh | c587c88 | 2017-03-29 21:33:10 -0700 | [diff] [blame] | 690 | ::std::unique_ptr<DigitalOutput> red_light_, green_light_, blue_light_; |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 691 | }; |
| 692 | |
| 693 | class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase { |
| 694 | public: |
| 695 | ::std::unique_ptr<Encoder> make_encoder(int index) { |
| 696 | return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false, |
| 697 | Encoder::k4X); |
| 698 | } |
| 699 | |
| 700 | void Run() override { |
| 701 | ::aos::InitNRT(); |
| 702 | ::aos::SetCurrentThreadName("StartCompetition"); |
| 703 | |
| 704 | ::frc971::wpilib::JoystickSender joystick_sender; |
| 705 | ::std::thread joystick_thread(::std::ref(joystick_sender)); |
| 706 | |
| 707 | ::frc971::wpilib::PDPFetcher pdp_fetcher; |
| 708 | ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher)); |
| 709 | SensorReader reader; |
| 710 | |
| 711 | // TODO(campbell): Update port numbers |
| 712 | reader.set_drivetrain_left_encoder(make_encoder(0)); |
| 713 | reader.set_drivetrain_right_encoder(make_encoder(1)); |
| 714 | |
Austin Schuh | 0fc1e6d | 2017-02-21 02:04:10 -0800 | [diff] [blame] | 715 | reader.set_intake_encoder(make_encoder(3)); |
Brian Silverman | 50826c0 | 2017-02-18 14:40:25 -0800 | [diff] [blame] | 716 | reader.set_intake_absolute(make_unique<DigitalInput>(0)); |
Austin Schuh | 0fc1e6d | 2017-02-21 02:04:10 -0800 | [diff] [blame] | 717 | reader.set_intake_potentiometer(make_unique<AnalogInput>(4)); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 718 | |
Austin Schuh | 0fc1e6d | 2017-02-21 02:04:10 -0800 | [diff] [blame] | 719 | reader.set_indexer_encoder(make_encoder(5)); |
Brian | ef030df | 2017-03-05 15:06:04 -0800 | [diff] [blame] | 720 | reader.set_indexer_hall(make_unique<DigitalInput>(4)); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 721 | |
Austin Schuh | 0fc1e6d | 2017-02-21 02:04:10 -0800 | [diff] [blame] | 722 | reader.set_turret_encoder(make_encoder(6)); |
Brian | ef030df | 2017-03-05 15:06:04 -0800 | [diff] [blame] | 723 | reader.set_turret_hall(make_unique<DigitalInput>(2)); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 724 | |
Austin Schuh | 0fc1e6d | 2017-02-21 02:04:10 -0800 | [diff] [blame] | 725 | reader.set_hood_encoder(make_encoder(4)); |
| 726 | reader.set_hood_index(make_unique<DigitalInput>(1)); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 727 | |
Austin Schuh | 0fc1e6d | 2017-02-21 02:04:10 -0800 | [diff] [blame] | 728 | reader.set_shooter_encoder(make_encoder(2)); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 729 | |
Austin Schuh | 0fc1e6d | 2017-02-21 02:04:10 -0800 | [diff] [blame] | 730 | reader.set_autonomous_mode(0, make_unique<DigitalInput>(9)); |
| 731 | reader.set_autonomous_mode(1, make_unique<DigitalInput>(8)); |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 732 | |
| 733 | reader.set_pwm_trigger(make_unique<DigitalInput>(7)); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 734 | |
| 735 | reader.set_dma(make_unique<DMA>()); |
| 736 | ::std::thread reader_thread(::std::ref(reader)); |
| 737 | |
Brian Silverman | b443985 | 2017-02-24 19:49:09 -0800 | [diff] [blame] | 738 | auto imu_trigger = make_unique<DigitalInput>(3); |
Austin Schuh | 0fc1e6d | 2017-02-21 02:04:10 -0800 | [diff] [blame] | 739 | ::frc971::wpilib::ADIS16448 imu(SPI::Port::kOnboardCS1, imu_trigger.get()); |
Brian Silverman | a70994f | 2017-03-16 22:32:55 -0700 | [diff] [blame] | 740 | imu.SetDummySPI(SPI::Port::kOnboardCS2); |
| 741 | auto imu_reset = make_unique<DigitalOutput>(6); |
| 742 | imu.set_reset(imu_reset.get()); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 743 | ::std::thread imu_thread(::std::ref(imu)); |
| 744 | |
| 745 | DrivetrainWriter drivetrain_writer; |
| 746 | drivetrain_writer.set_drivetrain_left_victor( |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 747 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(7))); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 748 | drivetrain_writer.set_drivetrain_right_victor( |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 749 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3))); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 750 | ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer)); |
| 751 | |
| 752 | SuperstructureWriter superstructure_writer; |
| 753 | superstructure_writer.set_intake_victor( |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 754 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1))); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 755 | superstructure_writer.set_intake_rollers_victor( |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 756 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(4))); |
Austin Schuh | 0fc1e6d | 2017-02-21 02:04:10 -0800 | [diff] [blame] | 757 | superstructure_writer.set_indexer_victor( |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 758 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(6))); |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 759 | superstructure_writer.set_indexer_roller_victor( |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 760 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(5))); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 761 | superstructure_writer.set_turret_victor( |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 762 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(9))); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 763 | superstructure_writer.set_hood_victor( |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 764 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2))); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 765 | superstructure_writer.set_shooter_victor( |
Austin Schuh | 8347cb6 | 2017-04-08 14:37:34 -0700 | [diff] [blame] | 766 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(8))); |
Austin Schuh | c587c88 | 2017-03-29 21:33:10 -0700 | [diff] [blame] | 767 | |
Austin Schuh | 6a8131b | 2017-04-08 15:39:22 -0700 | [diff] [blame] | 768 | superstructure_writer.set_gear_servo( |
| 769 | ::std::unique_ptr<Servo>(new Servo(0))); |
| 770 | |
Austin Schuh | c587c88 | 2017-03-29 21:33:10 -0700 | [diff] [blame] | 771 | superstructure_writer.set_red_light( |
| 772 | ::std::unique_ptr<DigitalOutput>(new DigitalOutput(5))); |
| 773 | superstructure_writer.set_green_light( |
| 774 | ::std::unique_ptr<DigitalOutput>(new DigitalOutput(24))); |
| 775 | superstructure_writer.set_blue_light( |
| 776 | ::std::unique_ptr<DigitalOutput>(new DigitalOutput(25))); |
| 777 | |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 778 | ::std::thread superstructure_writer_thread( |
| 779 | ::std::ref(superstructure_writer)); |
| 780 | |
Adam Snaider | e0554ef | 2017-03-11 23:02:45 -0800 | [diff] [blame] | 781 | SolenoidWriter solenoid_writer; |
| 782 | solenoid_writer.set_lights(solenoid_writer.pcm()->MakeSolenoid(0)); |
Austin Schuh | c587c88 | 2017-03-29 21:33:10 -0700 | [diff] [blame] | 783 | solenoid_writer.set_rgb_light(solenoid_writer.pcm()->MakeSolenoid(1)); |
Adam Snaider | e0554ef | 2017-03-11 23:02:45 -0800 | [diff] [blame] | 784 | |
| 785 | ::std::thread solenoid_thread(::std::ref(solenoid_writer)); |
| 786 | |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 787 | // Wait forever. Not much else to do... |
| 788 | while (true) { |
| 789 | const int r = select(0, nullptr, nullptr, nullptr, nullptr); |
| 790 | if (r != 0) { |
| 791 | PLOG(WARNING, "infinite select failed"); |
| 792 | } else { |
| 793 | PLOG(WARNING, "infinite select succeeded??\n"); |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | LOG(ERROR, "Exiting WPILibRobot\n"); |
| 798 | |
| 799 | joystick_sender.Quit(); |
| 800 | joystick_thread.join(); |
| 801 | pdp_fetcher.Quit(); |
| 802 | pdp_fetcher_thread.join(); |
| 803 | reader.Quit(); |
| 804 | reader_thread.join(); |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 805 | imu.Quit(); |
| 806 | imu_thread.join(); |
| 807 | |
| 808 | drivetrain_writer.Quit(); |
| 809 | drivetrain_writer_thread.join(); |
| 810 | superstructure_writer.Quit(); |
| 811 | superstructure_writer_thread.join(); |
| 812 | |
| 813 | ::aos::Cleanup(); |
| 814 | } |
| 815 | }; |
| 816 | |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 817 | } // namespace |
Campbell Crowley | ae6e842 | 2017-02-05 12:38:50 -0800 | [diff] [blame] | 818 | } // namespace wpilib |
| 819 | } // namespace y2017 |
| 820 | |
| 821 | AOS_ROBOT_CLASS(::y2017::wpilib::WPILibRobot); |