Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 1 | // Copyright (c) FIRST and other WPILib contributors. |
| 2 | // Open Source Software; you can modify and/or share it under the terms of |
| 3 | // the WPILib BSD license file in the root directory of this project. |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 4 | |
| 5 | #include "frc/DutyCycle.h" |
| 6 | |
| 7 | #include <hal/DutyCycle.h> |
| 8 | #include <hal/FRCUsageReporting.h> |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 9 | #include <wpi/NullDeleter.h> |
| 10 | #include <wpi/sendable/SendableBuilder.h> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 11 | |
| 12 | #include "frc/DigitalSource.h" |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 13 | #include "frc/Errors.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 14 | |
| 15 | using namespace frc; |
| 16 | |
| 17 | DutyCycle::DutyCycle(DigitalSource* source) |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 18 | : m_source{source, wpi::NullDeleter<DigitalSource>()} { |
| 19 | if (!m_source) { |
| 20 | throw FRC_MakeError(err::NullParameter, "{}", "source"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 21 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 22 | InitDutyCycle(); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | DutyCycle::DutyCycle(DigitalSource& source) |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 26 | : m_source{&source, wpi::NullDeleter<DigitalSource>()} { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 27 | InitDutyCycle(); |
| 28 | } |
| 29 | |
| 30 | DutyCycle::DutyCycle(std::shared_ptr<DigitalSource> source) |
| 31 | : m_source{std::move(source)} { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 32 | if (!m_source) { |
| 33 | throw FRC_MakeError(err::NullParameter, "{}", "source"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 34 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 35 | InitDutyCycle(); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 36 | } |
| 37 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 38 | DutyCycle::~DutyCycle() { |
| 39 | HAL_FreeDutyCycle(m_handle); |
| 40 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 41 | |
| 42 | void DutyCycle::InitDutyCycle() { |
| 43 | int32_t status = 0; |
| 44 | m_handle = |
| 45 | HAL_InitializeDutyCycle(m_source->GetPortHandleForRouting(), |
| 46 | static_cast<HAL_AnalogTriggerType>( |
| 47 | m_source->GetAnalogTriggerTypeForRouting()), |
| 48 | &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 49 | FRC_CheckErrorStatus(status, "Channel {}", GetSourceChannel()); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 50 | int index = GetFPGAIndex(); |
| 51 | HAL_Report(HALUsageReporting::kResourceType_DutyCycle, index + 1); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 52 | wpi::SendableRegistry::AddLW(this, "Duty Cycle", index); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | int DutyCycle::GetFPGAIndex() const { |
| 56 | int32_t status = 0; |
| 57 | auto retVal = HAL_GetDutyCycleFPGAIndex(m_handle, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 58 | FRC_CheckErrorStatus(status, "Channel {}", GetSourceChannel()); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 59 | return retVal; |
| 60 | } |
| 61 | |
| 62 | int DutyCycle::GetFrequency() const { |
| 63 | int32_t status = 0; |
| 64 | auto retVal = HAL_GetDutyCycleFrequency(m_handle, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 65 | FRC_CheckErrorStatus(status, "Channel {}", GetSourceChannel()); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 66 | return retVal; |
| 67 | } |
| 68 | |
| 69 | double DutyCycle::GetOutput() const { |
| 70 | int32_t status = 0; |
| 71 | auto retVal = HAL_GetDutyCycleOutput(m_handle, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 72 | FRC_CheckErrorStatus(status, "Channel {}", GetSourceChannel()); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 73 | return retVal; |
| 74 | } |
| 75 | |
| 76 | unsigned int DutyCycle::GetOutputRaw() const { |
| 77 | int32_t status = 0; |
| 78 | auto retVal = HAL_GetDutyCycleOutputRaw(m_handle, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 79 | FRC_CheckErrorStatus(status, "Channel {}", GetSourceChannel()); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 80 | return retVal; |
| 81 | } |
| 82 | |
| 83 | unsigned int DutyCycle::GetOutputScaleFactor() const { |
| 84 | int32_t status = 0; |
| 85 | auto retVal = HAL_GetDutyCycleOutputScaleFactor(m_handle, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 86 | FRC_CheckErrorStatus(status, "Channel {}", GetSourceChannel()); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 87 | return retVal; |
| 88 | } |
| 89 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 90 | int DutyCycle::GetSourceChannel() const { |
| 91 | return m_source->GetChannel(); |
| 92 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 93 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 94 | void DutyCycle::InitSendable(wpi::SendableBuilder& builder) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 95 | builder.SetSmartDashboardType("Duty Cycle"); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 96 | builder.AddDoubleProperty( |
| 97 | "Frequency", [this] { return this->GetFrequency(); }, nullptr); |
| 98 | builder.AddDoubleProperty( |
| 99 | "Output", [this] { return this->GetOutput(); }, nullptr); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 100 | } |