blob: 735036e39bb23cef22436ec87b93cc76c2c1fb37 [file] [log] [blame]
Brian Silverman425492b2015-12-30 15:23:55 -08001#include "frc971/wpilib/pdp_fetcher.h"
2
3#include "aos/common/logging/queue_logging.h"
4#include "aos/linux_code/init.h"
Brian Silverman25ff5052016-01-02 14:13:46 -08005#include "aos/common/util/phased_loop.h"
Brian Silverman425492b2015-12-30 15:23:55 -08006
7namespace frc971 {
8namespace wpilib {
9
10PDPFetcher::PDPFetcher() : pdp_(new PowerDistributionPanel()) {
11 pdp_values_.Zero();
12}
13
14void PDPFetcher::GetValues(::aos::PDPValues *pdp_values) {
15 ::aos::MutexLocker locker(&values_lock_);
16 *pdp_values = pdp_values_;
17}
18
19void PDPFetcher::operator()() {
20 ::aos::SetCurrentThreadName("PDPFetcher");
Brian Silverman25ff5052016-01-02 14:13:46 -080021
22 ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(20),
23 ::aos::time::Time::InMS(4));
24
Brian Silverman425492b2015-12-30 15:23:55 -080025 while (true) {
26 {
Brian Silverman25ff5052016-01-02 14:13:46 -080027 const int iterations = phased_loop.SleepUntilNext();
28 if (iterations != 1) {
29 LOG(DEBUG, "PDPFetcher skipped %d iterations\n", iterations - 1);
30 }
31 }
32 {
Brian Silverman425492b2015-12-30 15:23:55 -080033 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