blob: 846de6053b91e97ea7096c6a9ae0e73451a11785 [file] [log] [blame]
Brian Silverman425492b2015-12-30 15:23:55 -08001#include "frc971/wpilib/pdp_fetcher.h"
2
Austin Schuh8aec1ed2016-05-01 13:29:20 -07003#include <chrono>
4
Brian Silverman425492b2015-12-30 15:23:55 -08005#include "aos/common/logging/queue_logging.h"
6#include "aos/linux_code/init.h"
Brian Silverman25ff5052016-01-02 14:13:46 -08007#include "aos/common/util/phased_loop.h"
Brian Silverman39b339e2016-01-03 13:24:22 -08008#include "frc971/wpilib/pdp_values.q.h"
Brian Silverman425492b2015-12-30 15:23:55 -08009
10namespace frc971 {
11namespace wpilib {
12
Brian Silverman425492b2015-12-30 15:23:55 -080013void PDPFetcher::operator()() {
14 ::aos::SetCurrentThreadName("PDPFetcher");
Brian Silverman39b339e2016-01-03 13:24:22 -080015 ::std::unique_ptr<PowerDistributionPanel> pdp(new PowerDistributionPanel());
Brian Silverman25ff5052016-01-02 14:13:46 -080016
Austin Schuh8aec1ed2016-05-01 13:29:20 -070017 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
18 ::std::chrono::milliseconds(4));
Brian Silverman25ff5052016-01-02 14:13:46 -080019
Brian Silverman425492b2015-12-30 15:23:55 -080020 while (true) {
21 {
Brian Silverman25ff5052016-01-02 14:13:46 -080022 const int iterations = phased_loop.SleepUntilNext();
23 if (iterations != 1) {
24 LOG(DEBUG, "PDPFetcher skipped %d iterations\n", iterations - 1);
25 }
26 }
Brian Silverman39b339e2016-01-03 13:24:22 -080027 auto message = pdp_values.MakeMessage();
28 message->voltage = pdp->GetVoltage();
29 message->temperature = pdp->GetTemperature();
30 message->power = pdp->GetTotalPower();
Brian Silverman425492b2015-12-30 15:23:55 -080031 for (int i = 0; i < 16; ++i) {
Brian Silverman39b339e2016-01-03 13:24:22 -080032 message->currents[i] = pdp->GetCurrent(i);
Brian Silverman425492b2015-12-30 15:23:55 -080033 }
Brian Silverman39b339e2016-01-03 13:24:22 -080034 LOG_STRUCT(DEBUG, "got", *message);
35 if (!message.Send()) {
36 LOG(WARNING, "sending pdp values failed\n");
Brian Silverman425492b2015-12-30 15:23:55 -080037 }
38 }
39}
40
41} // namespace wpilib
42} // namespace frc971