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

107 lines
2.9 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"
#include "wpi/hal/Ports.h"
#include "wpi/hal/UsageReporting.h"
#include "wpi/util/StackTrace.hpp"
#include "wpi/util/sendable/SendableBuilder.hpp"
#include "wpi/util/sendable/SendableRegistry.hpp"
2025-11-07 19:56:21 -05:00
#include "wpi/system/Errors.hpp"
#include "wpi/util/SensorUtil.hpp"
using namespace frc;
PWM::PWM(int channel, bool registerSendable) {
if (!SensorUtil::CheckPWMChannel(channel)) {
2021-05-23 19:33:33 -07:00
throw FRC_MakeError(err::ChannelIndexOutOfRange, "Channel {}", channel);
}
auto stack = wpi::GetStackTrace(1);
int32_t status = 0;
2025-01-30 18:59:34 -08:00
m_handle = HAL_InitializePWMPort(channel, stack.c_str(), &status);
2021-05-23 19:33:33 -07:00
FRC_CheckErrorStatus(status, "Channel {}", channel);
m_channel = channel;
SetDisabled();
HAL_ReportUsage("IO", channel, "PWM");
if (registerSendable) {
wpi::SendableRegistry::Add(this, "PWM", channel);
}
}
PWM::~PWM() {
if (m_handle != HAL_kInvalidHandle) {
SetDisabled();
}
}
void PWM::SetPulseTime(units::microsecond_t time) {
int32_t status = 0;
HAL_SetPWMPulseTimeMicroseconds(m_handle, time.value(), &status);
2021-05-23 19:33:33 -07:00
FRC_CheckErrorStatus(status, "Channel {}", m_channel);
}
units::microsecond_t PWM::GetPulseTime() const {
int32_t status = 0;
double value = HAL_GetPWMPulseTimeMicroseconds(m_handle, &status);
2021-05-23 19:33:33 -07:00
FRC_CheckErrorStatus(status, "Channel {}", m_channel);
return units::microsecond_t{value};
}
void PWM::SetDisabled() {
int32_t status = 0;
HAL_SetPWMPulseTimeMicroseconds(m_handle, 0, &status);
2021-05-23 19:33:33 -07:00
FRC_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:
throw FRC_MakeError(err::InvalidParameter, "OutputPeriod value {}",
static_cast<int>(mult));
}
2021-05-23 19:33:33 -07:00
FRC_CheckErrorStatus(status, "Channel {}", m_channel);
}
int PWM::GetChannel() const {
return m_channel;
}
void PWM::SetSimDevice(HAL_SimDeviceHandle device) {
HAL_SetPWMSimDevice(m_handle, device);
}
void PWM::InitSendable(wpi::SendableBuilder& builder) {
builder.SetSmartDashboardType("PWM");
builder.SetActuator(true);
builder.AddDoubleProperty(
"Value", [=, this] { return GetPulseTime().value(); },
[=, this](double value) { SetPulseTime(units::millisecond_t{value}); });
}