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
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/PWM.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2018-09-24 00:08:25 -07:00
|
|
|
#include <utility>
|
|
|
|
|
|
2019-11-08 22:53:20 -08:00
|
|
|
#include <hal/FRCUsageReporting.h>
|
|
|
|
|
#include <hal/HALBase.h>
|
2018-07-20 00:03:45 -07:00
|
|
|
#include <hal/PWM.h>
|
|
|
|
|
#include <hal/Ports.h>
|
2021-05-01 10:28:30 -07:00
|
|
|
#include <wpi/StackTrace.h>
|
2017-08-27 00:11:52 -07:00
|
|
|
|
2021-04-18 20:35:29 -07:00
|
|
|
#include "frc/Errors.h"
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/SensorUtil.h"
|
|
|
|
|
#include "frc/smartdashboard/SendableBuilder.h"
|
2019-09-14 15:22:54 -05:00
|
|
|
#include "frc/smartdashboard/SendableRegistry.h"
|
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-04-18 20:35:29 -07:00
|
|
|
throw FRC_MakeError(err::ChannelIndexOutOfRange,
|
|
|
|
|
"PWM Channel " + wpi::Twine{channel});
|
2015-06-25 15:07:55 -04:00
|
|
|
return;
|
|
|
|
|
}
|
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;
|
2021-05-01 10:28:30 -07:00
|
|
|
m_handle =
|
|
|
|
|
HAL_InitializePWMPort(HAL_GetPort(channel), stack.c_str(), &status);
|
2021-04-18 20:35:29 -07:00
|
|
|
FRC_CheckErrorStatus(status, "PWM Channel " + wpi::Twine{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
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SetPWMDisabled(m_handle, &status);
|
2021-04-18 20:35:29 -07:00
|
|
|
FRC_CheckErrorStatus(status, "SetPWMDisabled");
|
2016-07-08 21:29:29 -07:00
|
|
|
status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SetPWMEliminateDeadband(m_handle, false, &status);
|
2021-04-18 20:35:29 -07:00
|
|
|
FRC_CheckErrorStatus(status, "SetPWMEliminateDeadband");
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2019-10-29 21:34:10 -07:00
|
|
|
HAL_Report(HALUsageReporting::kResourceType_PWM, channel + 1);
|
2021-03-21 11:12:49 -07:00
|
|
|
if (registerSendable) {
|
|
|
|
|
SendableRegistry::GetInstance().AddLW(this, "PWM", channel);
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
PWM::~PWM() {
|
|
|
|
|
int32_t status = 0;
|
2014-08-05 17:27:43 -04:00
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SetPWMDisabled(m_handle, &status);
|
2021-04-18 20:35:29 -07:00
|
|
|
FRC_ReportError(status, "SetPWMDisabled");
|
2014-07-21 16:32:36 -04:00
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_FreePWMPort(m_handle, &status);
|
2021-04-18 20:35:29 -07:00
|
|
|
FRC_ReportError(status, "FreePWM");
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
void PWM::SetRaw(uint16_t value) {
|
2016-07-08 21:29:29 -07:00
|
|
|
int32_t status = 0;
|
2018-05-31 20:47:15 -07:00
|
|
|
HAL_SetPWMRaw(m_handle, value, &status);
|
2021-04-18 20:35:29 -07:00
|
|
|
FRC_CheckErrorStatus(status, "SetRaw");
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
uint16_t PWM::GetRaw() const {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2018-05-31 20:47:15 -07:00
|
|
|
uint16_t value = HAL_GetPWMRaw(m_handle, &status);
|
2021-04-18 20:35:29 -07:00
|
|
|
FRC_CheckErrorStatus(status, "GetRaw");
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
return value;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
void PWM::SetPosition(double pos) {
|
2016-07-08 21:29:29 -07:00
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SetPWMPosition(m_handle, pos, &status);
|
2021-04-18 20:35:29 -07:00
|
|
|
FRC_CheckErrorStatus(status, "SetPosition");
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
double PWM::GetPosition() const {
|
2016-07-08 21:29:29 -07:00
|
|
|
int32_t status = 0;
|
2016-11-20 07:25:03 -08:00
|
|
|
double position = HAL_GetPWMPosition(m_handle, &status);
|
2021-04-18 20:35:29 -07:00
|
|
|
FRC_CheckErrorStatus(status, "GetPosition");
|
2016-07-08 21:29:29 -07:00
|
|
|
return position;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
void PWM::SetSpeed(double speed) {
|
2016-07-08 21:29:29 -07:00
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SetPWMSpeed(m_handle, speed, &status);
|
2021-04-18 20:35:29 -07:00
|
|
|
FRC_CheckErrorStatus(status, "SetSpeed");
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
double PWM::GetSpeed() const {
|
2016-07-08 21:29:29 -07:00
|
|
|
int32_t status = 0;
|
2016-11-20 07:25:03 -08:00
|
|
|
double speed = HAL_GetPWMSpeed(m_handle, &status);
|
2021-04-18 20:35:29 -07:00
|
|
|
FRC_CheckErrorStatus(status, "GetSpeed");
|
2016-07-08 21:29:29 -07:00
|
|
|
return speed;
|
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;
|
2018-05-31 20:47:15 -07:00
|
|
|
HAL_SetPWMDisabled(m_handle, &status);
|
2021-04-18 20:35:29 -07:00
|
|
|
FRC_CheckErrorStatus(status, "SetDisabled");
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void PWM::SetPeriodMultiplier(PeriodMultiplier mult) {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
|
|
|
|
|
switch (mult) {
|
|
|
|
|
case kPeriodMultiplier_4X:
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SetPWMPeriodScale(m_handle, 3,
|
|
|
|
|
&status); // Squelch 3 out of 4 outputs
|
2015-06-25 15:07:55 -04:00
|
|
|
break;
|
|
|
|
|
case kPeriodMultiplier_2X:
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SetPWMPeriodScale(m_handle, 1,
|
|
|
|
|
&status); // Squelch 1 out of 2 outputs
|
2015-06-25 15:07:55 -04:00
|
|
|
break;
|
|
|
|
|
case kPeriodMultiplier_1X:
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SetPWMPeriodScale(m_handle, 0, &status); // Don't squelch any outputs
|
2015-06-25 15:07:55 -04:00
|
|
|
break;
|
|
|
|
|
default:
|
2021-05-23 15:25:08 -07:00
|
|
|
throw FRC_MakeError(err::InvalidParameter, "PeriodMultiplier value");
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
2021-04-18 20:35:29 -07:00
|
|
|
FRC_CheckErrorStatus(status, "SetPeriodMultiplier");
|
2016-07-08 21:29:29 -07:00
|
|
|
}
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
void PWM::SetZeroLatch() {
|
2016-07-08 21:29:29 -07:00
|
|
|
int32_t status = 0;
|
2018-05-31 20:47:15 -07:00
|
|
|
HAL_LatchPWMZero(m_handle, &status);
|
2021-04-18 20:35:29 -07:00
|
|
|
FRC_CheckErrorStatus(status, "SetZeroLatch");
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
void PWM::EnableDeadbandElimination(bool eliminateDeadband) {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_SetPWMEliminateDeadband(m_handle, eliminateDeadband, &status);
|
2021-04-18 20:35:29 -07:00
|
|
|
FRC_CheckErrorStatus(status, "EnableDeadbandElimination");
|
2018-05-31 20:47:15 -07:00
|
|
|
}
|
2014-09-26 17:20:57 -04:00
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
void PWM::SetBounds(double max, double deadbandMax, double center,
|
|
|
|
|
double deadbandMin, double min) {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2018-05-31 20:47:15 -07:00
|
|
|
HAL_SetPWMConfig(m_handle, max, deadbandMax, center, deadbandMin, min,
|
|
|
|
|
&status);
|
2021-04-18 20:35:29 -07:00
|
|
|
FRC_CheckErrorStatus(status, "SetBounds");
|
2018-05-31 20:47:15 -07:00
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
void PWM::SetRawBounds(int max, int deadbandMax, int center, int deadbandMin,
|
|
|
|
|
int min) {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_SetPWMConfigRaw(m_handle, max, deadbandMax, center, deadbandMin, min,
|
|
|
|
|
&status);
|
2021-04-18 20:35:29 -07:00
|
|
|
FRC_CheckErrorStatus(status, "SetRawBounds");
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
void PWM::GetRawBounds(int* max, int* deadbandMax, int* center,
|
|
|
|
|
int* deadbandMin, int* min) {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_GetPWMConfigRaw(m_handle, max, deadbandMax, center, deadbandMin, min,
|
|
|
|
|
&status);
|
2021-04-18 20:35:29 -07:00
|
|
|
FRC_CheckErrorStatus(status, "GetRawBounds");
|
2018-05-31 20:47:15 -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
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
void PWM::InitSendable(SendableBuilder& builder) {
|
2018-03-03 21:36:25 -08:00
|
|
|
builder.SetSmartDashboardType("PWM");
|
2018-07-28 14:04:46 -07:00
|
|
|
builder.SetActuator(true);
|
2021-03-21 11:12:49 -07:00
|
|
|
builder.SetSafeState([=] { SetDisabled(); });
|
2020-06-27 20:39:00 -07:00
|
|
|
builder.AddDoubleProperty(
|
2021-03-21 11:12:49 -07:00
|
|
|
"Value", [=] { return GetRaw(); }, [=](double value) { SetRaw(value); });
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|