Files
allwpilib/wpilibc/src/main/native/cpp/hardware/discrete/PWM.cpp

108 lines
3.0 KiB
C++
Raw Normal View History

// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
2025-11-07 19:56:21 -05:00
#include "wpi/hardware/discrete/PWM.hpp"
#include <utility>
2025-11-07 19:56:21 -05:00
#include "wpi/hal/HALBase.h"
#include "wpi/hal/PWM.h"
2025-11-07 19:56:21 -05:00
#include "wpi/hal/Ports.h"
#include "wpi/hal/UsageReporting.h"
2025-11-07 19:57:55 -05:00
#include "wpi/system/Errors.hpp"
#include "wpi/util/SensorUtil.hpp"
2025-11-07 19:56:21 -05:00
#include "wpi/util/StackTrace.hpp"
#include "wpi/util/sendable/SendableBuilder.hpp"
#include "wpi/util/sendable/SendableRegistry.hpp"
2025-11-07 20:00:05 -05:00
using namespace wpi;
PWM::PWM(int channel, bool registerSendable) {
if (!SensorUtil::CheckPWMChannel(channel)) {
2025-11-07 20:00:43 -05:00
throw WPILIB_MakeError(err::ChannelIndexOutOfRange, "Channel {}", channel);
}
2025-11-07 20:00:05 -05:00
auto stack = wpi::util::GetStackTrace(1);
int32_t status = 0;
2025-01-30 18:59:34 -08:00
m_handle = HAL_InitializePWMPort(channel, stack.c_str(), &status);
2025-11-07 20:00:43 -05:00
WPILIB_CheckErrorStatus(status, "Channel {}", channel);
m_channel = channel;
SetDisabled();
HAL_ReportUsage("IO", channel, "PWM");
if (registerSendable) {
2025-11-07 20:00:05 -05:00
wpi::util::SendableRegistry::Add(this, "PWM", channel);
}
}
PWM::~PWM() {
if (m_handle != HAL_kInvalidHandle) {
SetDisabled();
}
}
2025-11-07 20:00:05 -05:00
void PWM::SetPulseTime(wpi::units::microsecond_t time) {
int32_t status = 0;
HAL_SetPWMPulseTimeMicroseconds(m_handle, time.value(), &status);
2025-11-07 20:00:43 -05:00
WPILIB_CheckErrorStatus(status, "Channel {}", m_channel);
}
2025-11-07 20:00:05 -05:00
wpi::units::microsecond_t PWM::GetPulseTime() const {
int32_t status = 0;
double value = HAL_GetPWMPulseTimeMicroseconds(m_handle, &status);
2025-11-07 20:00:43 -05:00
WPILIB_CheckErrorStatus(status, "Channel {}", m_channel);
2025-11-07 20:00:05 -05:00
return wpi::units::microsecond_t{value};
}
void PWM::SetDisabled() {
int32_t status = 0;
HAL_SetPWMPulseTimeMicroseconds(m_handle, 0, &status);
2025-11-07 20:00:43 -05:00
WPILIB_CheckErrorStatus(status, "Channel {}", m_channel);
}
void PWM::SetOutputPeriod(OutputPeriod mult) {
int32_t status = 0;
switch (mult) {
case kOutputPeriod_20Ms:
HAL_SetPWMOutputPeriod(m_handle, 3,
&status); // Squelch 3 out of 4 outputs
break;
case kOutputPeriod_10Ms:
HAL_SetPWMOutputPeriod(m_handle, 1,
&status); // Squelch 1 out of 2 outputs
break;
case kOutputPeriod_5Ms:
HAL_SetPWMOutputPeriod(m_handle, 0,
&status); // Don't squelch any outputs
break;
default:
2025-11-07 20:00:43 -05:00
throw WPILIB_MakeError(err::InvalidParameter, "OutputPeriod value {}",
2025-11-07 20:01:58 -05:00
static_cast<int>(mult));
}
2025-11-07 20:00:43 -05:00
WPILIB_CheckErrorStatus(status, "Channel {}", m_channel);
}
int PWM::GetChannel() const {
return m_channel;
}
void PWM::SetSimDevice(HAL_SimDeviceHandle device) {
HAL_SetPWMSimDevice(m_handle, device);
}
2025-11-07 20:00:05 -05:00
void PWM::InitSendable(wpi::util::SendableBuilder& builder) {
builder.SetSmartDashboardType("PWM");
builder.SetActuator(true);
builder.AddDoubleProperty(
"Value", [=, this] { return GetPulseTime().value(); },
2025-11-07 20:01:58 -05:00
[=, this](double value) {
SetPulseTime(wpi::units::millisecond_t{value});
});
}