Brian Silverman | 425492b | 2015-12-30 15:23:55 -0800 | [diff] [blame] | 1 | #include "frc971/wpilib/pdp_fetcher.h" |
| 2 | |
Austin Schuh | 8aec1ed | 2016-05-01 13:29:20 -0700 | [diff] [blame] | 3 | #include <chrono> |
| 4 | |
Austin Schuh | 0b54543 | 2019-05-12 15:46:12 -0700 | [diff] [blame] | 5 | #include "aos/events/event-loop.h" |
John Park | 398c74a | 2018-10-20 21:17:39 -0700 | [diff] [blame] | 6 | #include "aos/init.h" |
Austin Schuh | 0b54543 | 2019-05-12 15:46:12 -0700 | [diff] [blame] | 7 | #include "aos/logging/queue_logging.h" |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 8 | #include "frc971/wpilib/ahal/PowerDistributionPanel.h" |
Brian Silverman | 39b339e | 2016-01-03 13:24:22 -0800 | [diff] [blame] | 9 | #include "frc971/wpilib/pdp_values.q.h" |
Brian Silverman | 425492b | 2015-12-30 15:23:55 -0800 | [diff] [blame] | 10 | |
| 11 | namespace frc971 { |
| 12 | namespace wpilib { |
| 13 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 14 | namespace chrono = ::std::chrono; |
Brian Silverman | 25ff505 | 2016-01-02 14:13:46 -0800 | [diff] [blame] | 15 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 16 | PDPFetcher::PDPFetcher(::aos::EventLoop *event_loop) |
| 17 | : event_loop_(event_loop), |
| 18 | pdp_values_sender_( |
| 19 | event_loop_->MakeSender<::frc971::PDPValues>(".frc971.pdp_values")), |
| 20 | pdp_(new frc::PowerDistributionPanel()) { |
| 21 | event_loop_->set_name("PDPFetcher"); |
Brian Silverman | 25ff505 | 2016-01-02 14:13:46 -0800 | [diff] [blame] | 22 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 23 | // SCHED_OTHER on purpose. |
| 24 | event_loop_->AddPhasedLoop([this](int iterations) { Loop(iterations); }, |
| 25 | chrono::milliseconds(20), chrono::milliseconds(4)); |
| 26 | } |
| 27 | |
| 28 | PDPFetcher::~PDPFetcher() {} |
| 29 | |
| 30 | void PDPFetcher::Loop(int iterations) { |
| 31 | if (iterations != 1) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame^] | 32 | AOS_LOG(DEBUG, "PDPFetcher skipped %d iterations\n", iterations - 1); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 33 | } |
| 34 | auto message = pdp_values_sender_.MakeMessage(); |
| 35 | message->voltage = pdp_->GetVoltage(); |
| 36 | message->temperature = pdp_->GetTemperature(); |
| 37 | message->power = pdp_->GetTotalPower(); |
| 38 | for (int i = 0; i < 16; ++i) { |
| 39 | message->currents[i] = pdp_->GetCurrent(i); |
| 40 | } |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame^] | 41 | AOS_LOG_STRUCT(DEBUG, "got", *message); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 42 | if (!message.Send()) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame^] | 43 | AOS_LOG(WARNING, "sending pdp values failed\n"); |
Brian Silverman | 425492b | 2015-12-30 15:23:55 -0800 | [diff] [blame] | 44 | } |
| 45 | } |
| 46 | |
| 47 | } // namespace wpilib |
| 48 | } // namespace frc971 |