blob: eff53e7f15e97dad9f85cf542d862b2afbe51380 [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
Austin Schuh0b545432019-05-12 15:46:12 -07005#include "aos/events/event-loop.h"
John Park398c74a2018-10-20 21:17:39 -07006#include "aos/init.h"
Austin Schuh0b545432019-05-12 15:46:12 -07007#include "aos/logging/queue_logging.h"
John Park33858a32018-09-28 23:05:48 -07008#include "aos/util/phased_loop.h"
Parker Schuhd3b7a8872018-02-19 16:42:27 -08009#include "frc971/wpilib/ahal/PowerDistributionPanel.h"
Brian Silverman39b339e2016-01-03 13:24:22 -080010#include "frc971/wpilib/pdp_values.q.h"
Brian Silverman425492b2015-12-30 15:23:55 -080011
12namespace frc971 {
13namespace wpilib {
14
Brian Silverman425492b2015-12-30 15:23:55 -080015void PDPFetcher::operator()() {
16 ::aos::SetCurrentThreadName("PDPFetcher");
Parker Schuhd3b7a8872018-02-19 16:42:27 -080017 ::std::unique_ptr<frc::PowerDistributionPanel> pdp(
18 new frc::PowerDistributionPanel());
Brian Silverman25ff5052016-01-02 14:13:46 -080019
Austin Schuh8aec1ed2016-05-01 13:29:20 -070020 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
21 ::std::chrono::milliseconds(4));
Brian Silverman25ff5052016-01-02 14:13:46 -080022
Austin Schuh0b545432019-05-12 15:46:12 -070023 // TODO(austin): Event loop instead of while loop.
Brian Silverman425492b2015-12-30 15:23:55 -080024 while (true) {
25 {
Brian Silverman25ff5052016-01-02 14:13:46 -080026 const int iterations = phased_loop.SleepUntilNext();
27 if (iterations != 1) {
28 LOG(DEBUG, "PDPFetcher skipped %d iterations\n", iterations - 1);
29 }
30 }
Austin Schuh0b545432019-05-12 15:46:12 -070031 auto message = pdp_values_sender_.MakeMessage();
Brian Silverman39b339e2016-01-03 13:24:22 -080032 message->voltage = pdp->GetVoltage();
33 message->temperature = pdp->GetTemperature();
34 message->power = pdp->GetTotalPower();
Brian Silverman425492b2015-12-30 15:23:55 -080035 for (int i = 0; i < 16; ++i) {
Brian Silverman39b339e2016-01-03 13:24:22 -080036 message->currents[i] = pdp->GetCurrent(i);
Brian Silverman425492b2015-12-30 15:23:55 -080037 }
Brian Silverman39b339e2016-01-03 13:24:22 -080038 LOG_STRUCT(DEBUG, "got", *message);
39 if (!message.Send()) {
40 LOG(WARNING, "sending pdp values failed\n");
Brian Silverman425492b2015-12-30 15:23:55 -080041 }
42 }
43}
44
45} // namespace wpilib
46} // namespace frc971