2020-12-26 14:12:05 -08:00
|
|
|
// 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.
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/hardware/discrete/PWM.hpp"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2018-09-24 00:08:25 -07:00
|
|
|
#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"
|
2017-08-27 00:11:52 -07:00
|
|
|
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/system/Errors.hpp"
|
|
|
|
|
#include "wpi/util/SensorUtil.hpp"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2021-03-21 11:12:49 -07:00
|
|
|
PWM::PWM(int channel, bool registerSendable) {
|
2018-05-23 20:22:30 -07:00
|
|
|
if (!SensorUtil::CheckPWMChannel(channel)) {
|
2021-05-23 19:33:33 -07:00
|
|
|
throw FRC_MakeError(err::ChannelIndexOutOfRange, "Channel {}", channel);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2021-05-01 10:28:30 -07:00
|
|
|
auto stack = wpi::GetStackTrace(1);
|
2015-06-25 15:07:55 -04:00
|
|
|
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);
|
2014-07-21 16:32:36 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
m_channel = channel;
|
2014-07-21 16:32:36 -04:00
|
|
|
|
2025-03-20 19:23:22 -07:00
|
|
|
SetDisabled();
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2025-02-07 12:37:23 -08:00
|
|
|
HAL_ReportUsage("IO", channel, "PWM");
|
2021-03-21 11:12:49 -07:00
|
|
|
if (registerSendable) {
|
2025-01-25 10:52:19 -08:00
|
|
|
wpi::SendableRegistry::Add(this, "PWM", channel);
|
2021-03-21 11:12:49 -07:00
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
PWM::~PWM() {
|
2024-10-15 22:56:13 -04:00
|
|
|
if (m_handle != HAL_kInvalidHandle) {
|
2025-03-20 19:23:22 -07:00
|
|
|
SetDisabled();
|
2024-10-15 22:56:13 -04:00
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2023-06-22 19:43:16 -07:00
|
|
|
void PWM::SetPulseTime(units::microsecond_t time) {
|
2016-07-08 21:29:29 -07:00
|
|
|
int32_t status = 0;
|
2023-06-22 19:43:16 -07:00
|
|
|
HAL_SetPWMPulseTimeMicroseconds(m_handle, time.value(), &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Channel {}", m_channel);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2023-06-22 19:43:16 -07:00
|
|
|
units::microsecond_t PWM::GetPulseTime() const {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2023-06-22 19:43:16 -07:00
|
|
|
double value = HAL_GetPWMPulseTimeMicroseconds(m_handle, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Channel {}", m_channel);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
2023-06-22 19:43:16 -07:00
|
|
|
return units::microsecond_t{value};
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
void PWM::SetDisabled() {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2025-03-20 19:23:22 -07:00
|
|
|
HAL_SetPWMPulseTimeMicroseconds(m_handle, 0, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Channel {}", m_channel);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2025-03-20 19:23:22 -07:00
|
|
|
void PWM::SetOutputPeriod(OutputPeriod mult) {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
|
|
|
|
|
|
|
|
|
switch (mult) {
|
2025-03-20 19:23:22 -07:00
|
|
|
case kOutputPeriod_20Ms:
|
|
|
|
|
HAL_SetPWMOutputPeriod(m_handle, 3,
|
|
|
|
|
&status); // Squelch 3 out of 4 outputs
|
2015-06-25 15:07:55 -04:00
|
|
|
break;
|
2025-03-20 19:23:22 -07:00
|
|
|
case kOutputPeriod_10Ms:
|
|
|
|
|
HAL_SetPWMOutputPeriod(m_handle, 1,
|
|
|
|
|
&status); // Squelch 1 out of 2 outputs
|
2015-06-25 15:07:55 -04:00
|
|
|
break;
|
2025-03-20 19:23:22 -07:00
|
|
|
case kOutputPeriod_5Ms:
|
|
|
|
|
HAL_SetPWMOutputPeriod(m_handle, 0,
|
|
|
|
|
&status); // Don't squelch any outputs
|
2015-06-25 15:07:55 -04:00
|
|
|
break;
|
|
|
|
|
default:
|
2025-03-20 19:23:22 -07:00
|
|
|
throw FRC_MakeError(err::InvalidParameter, "OutputPeriod value {}",
|
2022-08-16 15:35:26 -07:00
|
|
|
static_cast<int>(mult));
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Channel {}", m_channel);
|
2016-07-08 21:29:29 -07:00
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
int PWM::GetChannel() const {
|
|
|
|
|
return m_channel;
|
|
|
|
|
}
|
2018-05-31 20:47:15 -07:00
|
|
|
|
2025-03-20 19:23:22 -07:00
|
|
|
void PWM::SetSimDevice(HAL_SimDeviceHandle device) {
|
|
|
|
|
HAL_SetPWMSimDevice(m_handle, device);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 16:38:05 -07:00
|
|
|
void PWM::InitSendable(wpi::SendableBuilder& builder) {
|
2018-03-03 21:36:25 -08:00
|
|
|
builder.SetSmartDashboardType("PWM");
|
2018-07-28 14:04:46 -07:00
|
|
|
builder.SetActuator(true);
|
2020-06-27 20:39:00 -07:00
|
|
|
builder.AddDoubleProperty(
|
2023-06-22 19:43:16 -07:00
|
|
|
"Value", [=, this] { return GetPulseTime().value(); },
|
|
|
|
|
[=, this](double value) { SetPulseTime(units::millisecond_t{value}); });
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|