blob: 81f9fcfeaba2098b091e526dcb6c387c8af5693b [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
John Park33858a32018-09-28 23:05:48 -07005#include "aos/logging/queue_logging.h"
John Park398c74a2018-10-20 21:17:39 -07006#include "aos/init.h"
John Park33858a32018-09-28 23:05:48 -07007#include "aos/util/phased_loop.h"
Parker Schuhd3b7a8872018-02-19 16:42:27 -08008#include "frc971/wpilib/ahal/PowerDistributionPanel.h"
Brian Silverman39b339e2016-01-03 13:24:22 -08009#include "frc971/wpilib/pdp_values.q.h"
Brian Silverman425492b2015-12-30 15:23:55 -080010
11namespace frc971 {
12namespace wpilib {
13
Brian Silverman425492b2015-12-30 15:23:55 -080014void PDPFetcher::operator()() {
15 ::aos::SetCurrentThreadName("PDPFetcher");
Parker Schuhd3b7a8872018-02-19 16:42:27 -080016 ::std::unique_ptr<frc::PowerDistributionPanel> pdp(
17 new frc::PowerDistributionPanel());
Brian Silverman25ff5052016-01-02 14:13:46 -080018
Austin Schuh8aec1ed2016-05-01 13:29:20 -070019 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
20 ::std::chrono::milliseconds(4));
Brian Silverman25ff5052016-01-02 14:13:46 -080021
Brian Silverman425492b2015-12-30 15:23:55 -080022 while (true) {
23 {
Brian Silverman25ff5052016-01-02 14:13:46 -080024 const int iterations = phased_loop.SleepUntilNext();
25 if (iterations != 1) {
26 LOG(DEBUG, "PDPFetcher skipped %d iterations\n", iterations - 1);
27 }
28 }
Brian Silverman39b339e2016-01-03 13:24:22 -080029 auto message = pdp_values.MakeMessage();
30 message->voltage = pdp->GetVoltage();
31 message->temperature = pdp->GetTemperature();
32 message->power = pdp->GetTotalPower();
Brian Silverman425492b2015-12-30 15:23:55 -080033 for (int i = 0; i < 16; ++i) {
Brian Silverman39b339e2016-01-03 13:24:22 -080034 message->currents[i] = pdp->GetCurrent(i);
Brian Silverman425492b2015-12-30 15:23:55 -080035 }
Brian Silverman39b339e2016-01-03 13:24:22 -080036 LOG_STRUCT(DEBUG, "got", *message);
37 if (!message.Send()) {
38 LOG(WARNING, "sending pdp values failed\n");
Brian Silverman425492b2015-12-30 15:23:55 -080039 }
40 }
41}
42
43} // namespace wpilib
44} // namespace frc971