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