Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <string.h> |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 3 | #include <unistd.h> |
| 4 | #include <inttypes.h> |
| 5 | |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 6 | #include <thread> |
| 7 | #include <mutex> |
| 8 | #include <functional> |
| 9 | |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 10 | #include "aos/common/controls/output_check.q.h" |
| 11 | #include "aos/common/controls/sensor_generation.q.h" |
| 12 | #include "aos/common/logging/logging.h" |
| 13 | #include "aos/common/logging/queue_logging.h" |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 14 | #include "aos/common/messages/robot_state.q.h" |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 15 | #include "aos/common/time.h" |
| 16 | #include "aos/common/util/log_interval.h" |
| 17 | #include "aos/common/util/phased_loop.h" |
| 18 | #include "aos/common/util/wrapping_counter.h" |
Brian Silverman | b073f24 | 2014-09-08 16:29:57 -0400 | [diff] [blame] | 19 | #include "aos/common/stl_mutex.h" |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 20 | #include "aos/linux_code/init.h" |
| 21 | |
| 22 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 23 | #include "frc971/constants.h" |
Daniel Petti | aece37f | 2014-10-25 17:13:44 -0700 | [diff] [blame] | 24 | #include "frc971/shifter_hall_effect.h" |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 25 | |
Brian Silverman | da45b6c | 2014-12-28 11:36:50 -0800 | [diff] [blame] | 26 | #include "frc971/wpilib/hall_effect.h" |
| 27 | #include "frc971/wpilib/joystick_sender.h" |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 28 | #include "frc971/wpilib/loop_output_handler.h" |
| 29 | #include "frc971/wpilib/buffered_solenoid.h" |
| 30 | #include "frc971/wpilib/buffered_pcm.h" |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 31 | #include "frc971/wpilib/gyro_sender.h" |
Brian Silverman | da45b6c | 2014-12-28 11:36:50 -0800 | [diff] [blame] | 32 | |
Brian Silverman | cb77f23 | 2014-12-19 21:48:36 -0800 | [diff] [blame] | 33 | #include "Encoder.h" |
Brian Silverman | cb77f23 | 2014-12-19 21:48:36 -0800 | [diff] [blame] | 34 | #include "Talon.h" |
| 35 | #include "DriverStation.h" |
| 36 | #include "AnalogInput.h" |
Brian Silverman | cb77f23 | 2014-12-19 21:48:36 -0800 | [diff] [blame] | 37 | #include "Compressor.h" |
| 38 | #include "RobotBase.h" |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 39 | |
| 40 | #ifndef M_PI |
| 41 | #define M_PI 3.14159265358979323846 |
| 42 | #endif |
| 43 | |
| 44 | using ::aos::util::SimpleLogInterval; |
| 45 | using ::frc971::control_loops::drivetrain; |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 46 | using ::aos::util::WrappingCounter; |
| 47 | |
| 48 | namespace frc971 { |
Brian Silverman | da45b6c | 2014-12-28 11:36:50 -0800 | [diff] [blame] | 49 | namespace wpilib { |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 50 | |
Brian Silverman | da45b6c | 2014-12-28 11:36:50 -0800 | [diff] [blame] | 51 | // TODO(brian): Split this out into a separate file once DMA is in. |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 52 | class EdgeCounter { |
| 53 | public: |
| 54 | EdgeCounter(int priority, Encoder *encoder, HallEffect *input, |
Brian Silverman | b073f24 | 2014-09-08 16:29:57 -0400 | [diff] [blame] | 55 | ::aos::stl_mutex *mutex) |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 56 | : priority_(priority), |
| 57 | encoder_(encoder), |
| 58 | input_(input), |
| 59 | mutex_(mutex), |
| 60 | run_(true), |
| 61 | any_interrupt_count_(0) { |
| 62 | thread_.reset(new ::std::thread(::std::ref(*this))); |
| 63 | } |
| 64 | |
| 65 | // Waits for interrupts, locks the mutex, and updates the internal state. |
| 66 | // Updates the any_interrupt_count count when the interrupt comes in without |
| 67 | // the lock. |
Austin Schuh | db51603 | 2014-12-28 00:12:38 -0800 | [diff] [blame] | 68 | void operator()() { |
Brian Silverman | 2fe007c | 2014-12-28 12:20:01 -0800 | [diff] [blame] | 69 | ::aos::SetCurrentThreadName("EdgeCounter_" + |
| 70 | ::std::to_string(input_->GetChannel())); |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 71 | |
| 72 | input_->RequestInterrupts(); |
| 73 | input_->SetUpSourceEdge(true, true); |
| 74 | |
| 75 | { |
Brian Silverman | b073f24 | 2014-09-08 16:29:57 -0400 | [diff] [blame] | 76 | ::std::unique_lock<::aos::stl_mutex> mutex_guard(*mutex_); |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 77 | current_value_ = input_->GetHall(); |
| 78 | } |
| 79 | |
Brian Silverman | 2fe007c | 2014-12-28 12:20:01 -0800 | [diff] [blame] | 80 | ::aos::SetCurrentThreadRealtimePriority(priority_); |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 81 | InterruptableSensorBase::WaitResult result = InterruptableSensorBase::kBoth; |
| 82 | while (run_) { |
| 83 | result = input_->WaitForInterrupt( |
| 84 | 0.1, result != InterruptableSensorBase::kTimeout); |
| 85 | if (result == InterruptableSensorBase::kTimeout) { |
| 86 | continue; |
| 87 | } |
| 88 | ++any_interrupt_count_; |
| 89 | |
Brian Silverman | b073f24 | 2014-09-08 16:29:57 -0400 | [diff] [blame] | 90 | ::std::unique_lock<::aos::stl_mutex> mutex_guard(*mutex_); |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 91 | int32_t encoder_value = encoder_->GetRaw(); |
| 92 | bool hall_value = input_->GetHall(); |
| 93 | if (current_value_ != hall_value) { |
| 94 | if (hall_value) { |
| 95 | ++positive_interrupt_count_; |
| 96 | last_positive_encoder_value_ = encoder_value; |
| 97 | } else { |
| 98 | ++negative_interrupt_count_; |
| 99 | last_negative_encoder_value_ = encoder_value; |
| 100 | } |
| 101 | } else { |
| 102 | LOG(WARNING, "Detected spurious edge on %d. Dropping it.\n", |
| 103 | input_->GetChannel()); |
| 104 | } |
| 105 | |
| 106 | current_value_ = hall_value; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // Updates the internal hall effect value given this new observation. |
| 111 | // The mutex provided at construction time must be held during this operation. |
| 112 | void set_polled_value(bool value) { |
| 113 | polled_value_ = value; |
| 114 | bool miss_match = (value != current_value_); |
| 115 | if (miss_match && last_miss_match_) { |
| 116 | current_value_ = value; |
| 117 | last_miss_match_ = false; |
| 118 | } else { |
| 119 | last_miss_match_ = miss_match; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | // Signals the thread to quit next time it gets an interrupt. |
| 124 | void Quit() { |
| 125 | run_ = false; |
| 126 | thread_->join(); |
| 127 | } |
| 128 | |
| 129 | // Returns the total number of interrupts since construction time. This |
| 130 | // should be done without the mutex held. |
| 131 | int any_interrupt_count() const { return any_interrupt_count_; } |
| 132 | // Returns the current interrupt edge counts and encoder values. |
| 133 | // The mutex provided at construction time must be held during this operation. |
| 134 | int positive_interrupt_count() const { return positive_interrupt_count_; } |
| 135 | int negative_interrupt_count() const { return negative_interrupt_count_; } |
| 136 | int32_t last_positive_encoder_value() const { |
| 137 | return last_positive_encoder_value_; |
| 138 | } |
| 139 | int32_t last_negative_encoder_value() const { |
| 140 | return last_negative_encoder_value_; |
| 141 | } |
| 142 | // Returns the current polled value. |
| 143 | bool polled_value() const { return polled_value_; } |
| 144 | |
| 145 | private: |
| 146 | int priority_; |
| 147 | Encoder *encoder_; |
| 148 | HallEffect *input_; |
Brian Silverman | b073f24 | 2014-09-08 16:29:57 -0400 | [diff] [blame] | 149 | ::aos::stl_mutex *mutex_; |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 150 | ::std::atomic<bool> run_; |
| 151 | |
| 152 | ::std::atomic<int> any_interrupt_count_; |
| 153 | |
| 154 | // The following variables represent the current state. They must be |
| 155 | // synchronized by mutex_; |
| 156 | bool current_value_ = false; |
| 157 | bool polled_value_ = false; |
| 158 | bool last_miss_match_ = true; |
| 159 | int positive_interrupt_count_ = 0; |
| 160 | int negative_interrupt_count_ = 0; |
| 161 | int32_t last_positive_encoder_value_ = 0; |
| 162 | int32_t last_negative_encoder_value_ = 0; |
| 163 | |
| 164 | ::std::unique_ptr<::std::thread> thread_; |
| 165 | }; |
| 166 | |
Brian Silverman | da45b6c | 2014-12-28 11:36:50 -0800 | [diff] [blame] | 167 | // This class will synchronize sampling edges on a bunch of HallEffects with |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 168 | // the periodic poll. |
| 169 | // |
| 170 | // The data is provided to subclasses by calling SaveState when the state is |
| 171 | // consistent and ready. |
Brian Silverman | da45b6c | 2014-12-28 11:36:50 -0800 | [diff] [blame] | 172 | // |
| 173 | // TODO(brian): Split this out into a separate file once DMA is in. |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 174 | template <int num_sensors> |
| 175 | class PeriodicHallSynchronizer { |
| 176 | public: |
| 177 | PeriodicHallSynchronizer( |
| 178 | const char *name, int priority, int interrupt_priority, |
| 179 | ::std::unique_ptr<Encoder> encoder, |
| 180 | ::std::array<::std::unique_ptr<HallEffect>, num_sensors> *sensors) |
| 181 | : name_(name), |
| 182 | priority_(priority), |
| 183 | encoder_(::std::move(encoder)), |
| 184 | run_(true) { |
| 185 | for (int i = 0; i < num_sensors; ++i) { |
| 186 | sensors_[i] = ::std::move((*sensors)[i]); |
| 187 | edge_counters_[i] = ::std::unique_ptr<EdgeCounter>(new EdgeCounter( |
| 188 | interrupt_priority, encoder_.get(), sensors_[i].get(), &mutex_)); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | const char *name() const { return name_.c_str(); } |
| 193 | |
| 194 | void StartThread() { thread_.reset(new ::std::thread(::std::ref(*this))); } |
| 195 | |
| 196 | // Called when the state is consistent and up to date. |
| 197 | virtual void SaveState() = 0; |
| 198 | |
| 199 | // Starts a sampling iteration. See RunIteration for usage. |
| 200 | void StartIteration() { |
| 201 | // Start by capturing the current interrupt counts. |
| 202 | for (int i = 0; i < num_sensors; ++i) { |
| 203 | interrupt_counts_[i] = edge_counters_[i]->any_interrupt_count(); |
| 204 | } |
| 205 | |
| 206 | { |
| 207 | // Now, update the encoder and sensor values. |
Brian Silverman | b073f24 | 2014-09-08 16:29:57 -0400 | [diff] [blame] | 208 | ::std::unique_lock<::aos::stl_mutex> mutex_guard(mutex_); |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 209 | encoder_value_ = encoder_->GetRaw(); |
| 210 | for (int i = 0; i < num_sensors; ++i) { |
| 211 | edge_counters_[i]->set_polled_value(sensors_[i]->GetHall()); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | // Attempts to finish a sampling iteration. See RunIteration for usage. |
| 217 | // Returns true if the iteration succeeded, and false otherwise. |
| 218 | bool TryFinishingIteration() { |
| 219 | // Make sure no interrupts have occurred while we were waiting. If they |
| 220 | // have, we are in an inconsistent state and need to try again. |
Brian Silverman | b073f24 | 2014-09-08 16:29:57 -0400 | [diff] [blame] | 221 | ::std::unique_lock<::aos::stl_mutex> mutex_guard(mutex_); |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 222 | bool retry = false; |
| 223 | for (int i = 0; i < num_sensors; ++i) { |
| 224 | retry = retry || (interrupt_counts_[i] != |
| 225 | edge_counters_[i]->any_interrupt_count()); |
| 226 | } |
| 227 | if (!retry) { |
| 228 | SaveState(); |
| 229 | return true; |
| 230 | } |
| 231 | LOG(WARNING, "Got an interrupt while sampling encoder %s, retrying\n", |
| 232 | name()); |
| 233 | return false; |
| 234 | } |
| 235 | |
| 236 | void RunIteration() { |
| 237 | while (true) { |
| 238 | StartIteration(); |
| 239 | |
| 240 | // Wait more than the amount of time it takes for a digital input change |
| 241 | // to go from visible to software to having triggered an interrupt. |
| 242 | ::aos::time::SleepFor(::aos::time::Time::InUS(120)); |
| 243 | |
| 244 | if (TryFinishingIteration()) { |
| 245 | return; |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | void operator()() { |
Brian Silverman | 2fe007c | 2014-12-28 12:20:01 -0800 | [diff] [blame] | 251 | ::aos::SetCurrentThreadName("HallSync" + ::std::to_string(num_sensors)); |
Brian Silverman | da45b6c | 2014-12-28 11:36:50 -0800 | [diff] [blame] | 252 | ::aos::SetCurrentThreadRealtimePriority(priority_); |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 253 | while (run_) { |
| 254 | ::aos::time::PhasedLoopXMS(10, 9000); |
| 255 | RunIteration(); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | void Quit() { |
| 260 | run_ = false; |
| 261 | for (int i = 0; i < num_sensors; ++i) { |
| 262 | edge_counters_[i]->Quit(); |
| 263 | } |
| 264 | if (thread_) { |
| 265 | thread_->join(); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | protected: |
| 270 | // These values are only safe to fetch from inside SaveState() |
| 271 | int32_t encoder_value() const { return encoder_value_; } |
| 272 | ::std::array<::std::unique_ptr<EdgeCounter>, num_sensors> &edge_counters() { |
| 273 | return edge_counters_; |
| 274 | } |
| 275 | |
| 276 | private: |
| 277 | // A descriptive name for error messages. |
| 278 | ::std::string name_; |
| 279 | // The priority of the polling thread. |
| 280 | int priority_; |
| 281 | // The Encoder to sample. |
| 282 | ::std::unique_ptr<Encoder> encoder_; |
| 283 | // A list of all the digital inputs. |
| 284 | ::std::array<::std::unique_ptr<HallEffect>, num_sensors> sensors_; |
| 285 | // The mutex used to synchronize all the state. |
Brian Silverman | b073f24 | 2014-09-08 16:29:57 -0400 | [diff] [blame] | 286 | ::aos::stl_mutex mutex_; |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 287 | ::std::atomic<bool> run_; |
| 288 | |
| 289 | // The state. |
| 290 | // The current encoder value. |
| 291 | int32_t encoder_value_ = 0; |
| 292 | // The current edge counters. |
| 293 | ::std::array<::std::unique_ptr<EdgeCounter>, num_sensors> edge_counters_; |
| 294 | |
| 295 | ::std::unique_ptr<::std::thread> thread_; |
| 296 | ::std::array<int, num_sensors> interrupt_counts_; |
| 297 | }; |
| 298 | |
| 299 | double drivetrain_translate(int32_t in) { |
Austin Schuh | db51603 | 2014-12-28 00:12:38 -0800 | [diff] [blame] | 300 | return static_cast<double>(in) / |
| 301 | (256.0 /*cpr*/ * 2.0 /*2x. Stupid WPILib*/) * |
| 302 | (18.0 / 50.0 /*output stage*/) * (56.0 / 30.0 /*encoder gears*/) |
| 303 | // * constants::GetValues().drivetrain_encoder_ratio |
| 304 | * |
| 305 | (3.5 /*wheel diameter*/ * 2.54 / 100.0 * M_PI); |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 308 | class SensorReader { |
| 309 | public: |
| 310 | SensorReader() |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 311 | : left_encoder_(new Encoder(11, 10, false, Encoder::k2X)), // E0 |
Brian Silverman | cb77f23 | 2014-12-19 21:48:36 -0800 | [diff] [blame] | 312 | right_encoder_(new Encoder(13, 12, false, Encoder::k2X)), // E1 |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 313 | run_(true) { |
| 314 | filter_.SetPeriodNanoSeconds(100000); |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | void operator()() { |
Brian Silverman | 2fe007c | 2014-12-28 12:20:01 -0800 | [diff] [blame] | 318 | ::aos::SetCurrentThreadName("SensorReader"); |
| 319 | |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 320 | const int kPriority = 30; |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 321 | //const int kInterruptPriority = 55; |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 322 | |
Brian Silverman | 2fe007c | 2014-12-28 12:20:01 -0800 | [diff] [blame] | 323 | ::aos::SetCurrentThreadRealtimePriority(kPriority); |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 324 | while (run_) { |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 325 | ::aos::time::PhasedLoopXMS(5, 9000); |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 326 | RunIteration(); |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 327 | } |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | void RunIteration() { |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 331 | DriverStation *ds = DriverStation::GetInstance(); |
| 332 | |
Austin Schuh | db51603 | 2014-12-28 00:12:38 -0800 | [diff] [blame] | 333 | if (ds->IsSysActive()) { |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 334 | auto message = ::aos::controls::output_check_received.MakeMessage(); |
| 335 | // TODO(brians): Actually read a pulse value from the roboRIO. |
| 336 | message->pwm_value = 0; |
| 337 | message->pulse_length = -1; |
| 338 | LOG_STRUCT(DEBUG, "received", *message); |
| 339 | message.Send(); |
| 340 | } |
| 341 | |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 342 | // TODO(austin): Calibrate the shifter constants again. |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 343 | // TODO(sensors): Hook up the new dog position sensors. |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 344 | drivetrain.position.MakeWithBuilder() |
| 345 | .right_encoder(drivetrain_translate(right_encoder_->GetRaw())) |
| 346 | .left_encoder(-drivetrain_translate(left_encoder_->GetRaw())) |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 347 | .left_shifter_position(0) |
| 348 | .right_shifter_position(0) |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 349 | .battery_voltage(ds->GetBatteryVoltage()) |
| 350 | .Send(); |
| 351 | |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 352 | // Signal that we are alive. |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 353 | ::aos::controls::sensor_generation.MakeWithBuilder() |
| 354 | .reader_pid(getpid()) |
| 355 | .cape_resets(0) |
| 356 | .Send(); |
| 357 | } |
| 358 | |
| 359 | void Quit() { run_ = false; } |
| 360 | |
| 361 | private: |
| 362 | ::std::unique_ptr<AnalogInput> auto_selector_analog_; |
| 363 | |
| 364 | ::std::unique_ptr<Encoder> left_encoder_; |
| 365 | ::std::unique_ptr<Encoder> right_encoder_; |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 366 | |
| 367 | ::std::atomic<bool> run_; |
| 368 | DigitalGlitchFilter filter_; |
| 369 | }; |
| 370 | |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 371 | class SolenoidWriter { |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 372 | public: |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 373 | SolenoidWriter(const ::std::unique_ptr<BufferedPcm> &pcm) |
Brian Silverman | 20141f9 | 2015-01-05 17:39:01 -0800 | [diff] [blame] | 374 | : pcm_(pcm), drivetrain_(".frc971.control_loops.drivetrain.output") {} |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 375 | |
| 376 | void set_drivetrain_left(::std::unique_ptr<BufferedSolenoid> s) { |
| 377 | drivetrain_left_ = ::std::move(s); |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 380 | void set_drivetrain_right(::std::unique_ptr<BufferedSolenoid> s) { |
| 381 | drivetrain_right_ = ::std::move(s); |
| 382 | } |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 383 | |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 384 | void operator()() { |
| 385 | ::aos::SetCurrentThreadName("Solenoids"); |
| 386 | ::aos::SetCurrentThreadRealtimePriority(30); |
| 387 | |
| 388 | while (run_) { |
| 389 | ::aos::time::PhasedLoopXMS(20, 1000); |
| 390 | |
| 391 | { |
| 392 | drivetrain_.FetchLatest(); |
| 393 | if (drivetrain_.get()) { |
| 394 | LOG_STRUCT(DEBUG, "solenoids", *drivetrain_); |
| 395 | drivetrain_left_->Set(drivetrain_->left_high); |
| 396 | drivetrain_right_->Set(drivetrain_->right_high); |
| 397 | } |
| 398 | } |
| 399 | |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 400 | pcm_->Flush(); |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 404 | void Quit() { run_ = false; } |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 405 | |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 406 | private: |
| 407 | const ::std::unique_ptr<BufferedPcm> &pcm_; |
| 408 | ::std::unique_ptr<BufferedSolenoid> drivetrain_left_; |
| 409 | ::std::unique_ptr<BufferedSolenoid> drivetrain_right_; |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 410 | |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 411 | ::aos::Queue<::frc971::control_loops::Drivetrain::Output> drivetrain_; |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 412 | |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 413 | ::std::atomic<bool> run_{true}; |
| 414 | }; |
| 415 | |
| 416 | class DrivetrainWriter : public LoopOutputHandler { |
| 417 | public: |
| 418 | void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) { |
| 419 | left_drivetrain_talon_ = ::std::move(t); |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 422 | void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) { |
| 423 | right_drivetrain_talon_ = ::std::move(t); |
| 424 | } |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 425 | |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 426 | private: |
| 427 | virtual void Read() override { |
| 428 | ::frc971::control_loops::drivetrain.output.FetchAnother(); |
| 429 | } |
| 430 | |
| 431 | virtual void Write() override { |
| 432 | auto &queue = ::frc971::control_loops::drivetrain.output; |
| 433 | LOG_STRUCT(DEBUG, "will output", *queue); |
| 434 | left_drivetrain_talon_->Set(-queue->left_voltage / 12.0); |
| 435 | right_drivetrain_talon_->Set(queue->right_voltage / 12.0); |
| 436 | } |
| 437 | |
| 438 | virtual void Stop() override { |
| 439 | LOG(WARNING, "drivetrain output too old\n"); |
| 440 | left_drivetrain_talon_->Disable(); |
| 441 | right_drivetrain_talon_->Disable(); |
| 442 | } |
| 443 | |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 444 | ::std::unique_ptr<Talon> left_drivetrain_talon_; |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 445 | ::std::unique_ptr<Talon> right_drivetrain_talon_; |
| 446 | }; |
| 447 | |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 448 | class WPILibRobot : public RobotBase { |
| 449 | public: |
| 450 | virtual void StartCompetition() { |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 451 | ::aos::InitNRT(); |
Brian Silverman | 2fe007c | 2014-12-28 12:20:01 -0800 | [diff] [blame] | 452 | ::aos::SetCurrentThreadName("StartCompetition"); |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 453 | |
Brian Silverman | 98f6ee2 | 2015-01-26 17:50:12 -0500 | [diff] [blame^] | 454 | JoystickSender joystick_sender; |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 455 | ::std::thread joystick_thread(::std::ref(joystick_sender)); |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 456 | ::std::unique_ptr<Compressor> compressor(new Compressor()); |
| 457 | compressor->SetClosedLoopControl(true); |
| 458 | |
Brian Silverman | 98f6ee2 | 2015-01-26 17:50:12 -0500 | [diff] [blame^] | 459 | SensorReader reader; |
| 460 | ::std::thread reader_thread(::std::ref(reader)); |
| 461 | GyroSender gyro_sender; |
| 462 | ::std::thread gyro_thread(::std::ref(gyro_sender)); |
| 463 | |
| 464 | DrivetrainWriter drivetrain_writer; |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 465 | drivetrain_writer.set_left_drivetrain_talon( |
| 466 | ::std::unique_ptr<Talon>(new Talon(5))); |
| 467 | drivetrain_writer.set_right_drivetrain_talon( |
| 468 | ::std::unique_ptr<Talon>(new Talon(2))); |
| 469 | ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer)); |
| 470 | |
Brian Silverman | 98f6ee2 | 2015-01-26 17:50:12 -0500 | [diff] [blame^] | 471 | ::std::unique_ptr<BufferedPcm> pcm(new BufferedPcm()); |
| 472 | SolenoidWriter solenoid_writer(pcm); |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 473 | solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(6)); |
| 474 | solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(7)); |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 475 | ::std::thread solenoid_thread(::std::ref(solenoid_writer)); |
| 476 | |
| 477 | // Wait forever. Not much else to do... |
| 478 | PCHECK(select(0, nullptr, nullptr, nullptr, nullptr)); |
| 479 | |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 480 | LOG(ERROR, "Exiting WPILibRobot\n"); |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 481 | |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 482 | joystick_sender.Quit(); |
| 483 | joystick_thread.join(); |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 484 | reader.Quit(); |
| 485 | reader_thread.join(); |
Brian Silverman | 07ec88e | 2014-12-28 00:13:08 -0800 | [diff] [blame] | 486 | gyro_sender.Quit(); |
| 487 | gyro_thread.join(); |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 488 | |
| 489 | drivetrain_writer.Quit(); |
| 490 | drivetrain_writer_thread.join(); |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 491 | solenoid_writer.Quit(); |
| 492 | solenoid_thread.join(); |
| 493 | |
Austin Schuh | 010eb81 | 2014-10-25 18:06:49 -0700 | [diff] [blame] | 494 | ::aos::Cleanup(); |
| 495 | } |
| 496 | }; |
| 497 | |
Brian Silverman | 98f6ee2 | 2015-01-26 17:50:12 -0500 | [diff] [blame^] | 498 | } // namespace wpilib |
| 499 | } // namespace frc971 |
Austin Schuh | db51603 | 2014-12-28 00:12:38 -0800 | [diff] [blame] | 500 | |
Brian Silverman | 98f6ee2 | 2015-01-26 17:50:12 -0500 | [diff] [blame^] | 501 | |
| 502 | START_ROBOT_CLASS(::frc971::wpilib::WPILibRobot); |