2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2018-01-02 09:20:21 -08:00
|
|
|
/* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
2016-01-02 03:02:34 -08:00
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the 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>
|
|
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include <hal/HAL.h>
|
|
|
|
|
#include <hal/PWM.h>
|
|
|
|
|
#include <hal/Ports.h>
|
2017-08-27 00:11:52 -07:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/SensorUtil.h"
|
|
|
|
|
#include "frc/Utility.h"
|
|
|
|
|
#include "frc/WPIErrors.h"
|
|
|
|
|
#include "frc/smartdashboard/SendableBuilder.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
PWM::PWM(int channel) {
|
2018-05-23 20:22:30 -07:00
|
|
|
if (!SensorUtil::CheckPWMChannel(channel)) {
|
2017-12-01 21:50:24 -08:00
|
|
|
wpi_setWPIErrorWithContext(ChannelIndexOutOfRange,
|
2018-04-29 23:33:19 -07:00
|
|
|
"PWM Channel " + wpi::Twine(channel));
|
2015-06-25 15:07:55 -04:00
|
|
|
return;
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
m_handle = HAL_InitializePWMPort(HAL_GetPort(channel), &status);
|
2016-06-30 21:39:09 -07:00
|
|
|
if (status != 0) {
|
2016-08-12 13:45:28 -07:00
|
|
|
wpi_setErrorWithContextRange(status, 0, HAL_GetNumPWMChannels(), channel,
|
2016-07-13 20:29:28 -07:00
|
|
|
HAL_GetErrorMessage(status));
|
2016-09-06 00:01:45 -07:00
|
|
|
m_channel = std::numeric_limits<int>::max();
|
2016-07-09 00:24:26 -07:00
|
|
|
m_handle = HAL_kInvalidHandle;
|
2016-07-08 21:29:29 -07:00
|
|
|
return;
|
2016-06-30 21:39:09 -07:00
|
|
|
}
|
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);
|
|
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2016-07-08 21:29:29 -07:00
|
|
|
status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SetPWMEliminateDeadband(m_handle, false, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_Report(HALUsageReporting::kResourceType_PWM, channel);
|
2017-12-04 23:28:33 -08:00
|
|
|
SetName("PWM", channel);
|
2018-11-22 21:15:26 -08:00
|
|
|
|
|
|
|
|
SetSafetyEnabled(false);
|
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);
|
|
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2014-07-21 16:32:36 -04:00
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_FreePWMPort(m_handle, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-09-24 00:08:25 -07:00
|
|
|
PWM::PWM(PWM&& rhs)
|
2018-11-22 21:15:26 -08:00
|
|
|
: MotorSafety(std::move(rhs)),
|
2018-09-24 00:08:25 -07:00
|
|
|
SendableBase(std::move(rhs)),
|
|
|
|
|
m_channel(std::move(rhs.m_channel)) {
|
|
|
|
|
std::swap(m_handle, rhs.m_handle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PWM& PWM::operator=(PWM&& rhs) {
|
|
|
|
|
ErrorBase::operator=(std::move(rhs));
|
|
|
|
|
SendableBase::operator=(std::move(rhs));
|
|
|
|
|
|
|
|
|
|
m_channel = std::move(rhs.m_channel);
|
|
|
|
|
std::swap(m_handle, rhs.m_handle);
|
|
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-22 21:15:26 -08:00
|
|
|
void PWM::StopMotor() { SetDisabled(); }
|
|
|
|
|
|
|
|
|
|
void PWM::GetDescription(wpi::raw_ostream& desc) const {
|
|
|
|
|
desc << "PWM " << GetChannel();
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
void PWM::SetRaw(uint16_t value) {
|
2015-06-25 15:07:55 -04:00
|
|
|
if (StatusIsFatal()) return;
|
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);
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
uint16_t PWM::GetRaw() const {
|
|
|
|
|
if (StatusIsFatal()) return 0;
|
2013-12-15 18:30:16 -05:00
|
|
|
|
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);
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
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) {
|
2015-06-25 15:07:55 -04:00
|
|
|
if (StatusIsFatal()) return;
|
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);
|
|
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
double PWM::GetPosition() const {
|
2015-06-25 15:07:55 -04:00
|
|
|
if (StatusIsFatal()) return 0.0;
|
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);
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
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) {
|
2015-06-25 15:07:55 -04:00
|
|
|
if (StatusIsFatal()) return;
|
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);
|
|
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2018-11-22 21:15:26 -08:00
|
|
|
|
|
|
|
|
Feed();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
double PWM::GetSpeed() const {
|
2015-06-25 15:07:55 -04:00
|
|
|
if (StatusIsFatal()) return 0.0;
|
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);
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
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
|
|
|
if (StatusIsFatal()) return;
|
2014-07-21 16:32:36 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
HAL_SetPWMDisabled(m_handle, &status);
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void PWM::SetPeriodMultiplier(PeriodMultiplier mult) {
|
|
|
|
|
if (StatusIsFatal()) return;
|
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
wpi_assert(false);
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
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
|
|
|
if (StatusIsFatal()) return;
|
|
|
|
|
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
HAL_LatchPWMZero(m_handle, &status);
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
void PWM::EnableDeadbandElimination(bool eliminateDeadband) {
|
2015-06-25 15:07:55 -04:00
|
|
|
if (StatusIsFatal()) return;
|
2018-05-31 20:47:15 -07:00
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_SetPWMEliminateDeadband(m_handle, eliminateDeadband, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
|
|
|
|
}
|
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) {
|
|
|
|
|
if (StatusIsFatal()) return;
|
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);
|
|
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
|
|
|
|
}
|
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) {
|
|
|
|
|
if (StatusIsFatal()) return;
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_SetPWMConfigRaw(m_handle, max, deadbandMax, center, deadbandMin, min,
|
|
|
|
|
&status);
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
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);
|
|
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int PWM::GetChannel() const { return m_channel; }
|
|
|
|
|
|
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);
|
2017-12-04 23:28:33 -08:00
|
|
|
builder.SetSafeState([=]() { SetDisabled(); });
|
2018-03-03 21:36:25 -08:00
|
|
|
builder.AddDoubleProperty("Value", [=]() { return GetRaw(); },
|
|
|
|
|
[=](double value) { SetRaw(value); });
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|