Brian Silverman | 425492b | 2015-12-30 15:23:55 -0800 | [diff] [blame] | 1 | #include "frc971/wpilib/pdp_fetcher.h" |
| 2 | |
| 3 | #include "aos/common/logging/queue_logging.h" |
| 4 | #include "aos/linux_code/init.h" |
Brian Silverman | 25ff505 | 2016-01-02 14:13:46 -0800 | [diff] [blame^] | 5 | #include "aos/common/util/phased_loop.h" |
Brian Silverman | 425492b | 2015-12-30 15:23:55 -0800 | [diff] [blame] | 6 | |
| 7 | namespace frc971 { |
| 8 | namespace wpilib { |
| 9 | |
| 10 | PDPFetcher::PDPFetcher() : pdp_(new PowerDistributionPanel()) { |
| 11 | pdp_values_.Zero(); |
| 12 | } |
| 13 | |
| 14 | void PDPFetcher::GetValues(::aos::PDPValues *pdp_values) { |
| 15 | ::aos::MutexLocker locker(&values_lock_); |
| 16 | *pdp_values = pdp_values_; |
| 17 | } |
| 18 | |
| 19 | void PDPFetcher::operator()() { |
| 20 | ::aos::SetCurrentThreadName("PDPFetcher"); |
Brian Silverman | 25ff505 | 2016-01-02 14:13:46 -0800 | [diff] [blame^] | 21 | |
| 22 | ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(20), |
| 23 | ::aos::time::Time::InMS(4)); |
| 24 | |
Brian Silverman | 425492b | 2015-12-30 15:23:55 -0800 | [diff] [blame] | 25 | while (true) { |
| 26 | { |
Brian Silverman | 25ff505 | 2016-01-02 14:13:46 -0800 | [diff] [blame^] | 27 | const int iterations = phased_loop.SleepUntilNext(); |
| 28 | if (iterations != 1) { |
| 29 | LOG(DEBUG, "PDPFetcher skipped %d iterations\n", iterations - 1); |
| 30 | } |
| 31 | } |
| 32 | { |
Brian Silverman | 425492b | 2015-12-30 15:23:55 -0800 | [diff] [blame] | 33 | const double voltage = pdp_->GetVoltage(); |
| 34 | ::aos::MutexLocker locker(&values_lock_); |
| 35 | pdp_values_.voltage = voltage; |
| 36 | } |
| 37 | { |
| 38 | const double temperature = pdp_->GetTemperature(); |
| 39 | ::aos::MutexLocker locker(&values_lock_); |
| 40 | pdp_values_.temperature = temperature; |
| 41 | } |
| 42 | { |
| 43 | const double power = pdp_->GetTotalPower(); |
| 44 | ::aos::MutexLocker locker(&values_lock_); |
| 45 | pdp_values_.power = power; |
| 46 | } |
| 47 | for (int i = 0; i < 16; ++i) { |
| 48 | const double current = pdp_->GetCurrent(i); |
| 49 | ::aos::MutexLocker locker(&values_lock_); |
| 50 | pdp_values_.currents[i] = current; |
| 51 | } |
| 52 | { |
| 53 | ::aos::MutexLocker locker(&values_lock_); |
| 54 | LOG_STRUCT(DEBUG, "finished fetching", pdp_values_); |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | } // namespace wpilib |
| 60 | } // namespace frc971 |