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.
|
2017-11-08 23:40:01 -08:00
|
|
|
|
2021-04-17 11:27:16 -07:00
|
|
|
#include "frc/motorcontrol/NidecBrushless.h"
|
2017-11-08 23:40:01 -08:00
|
|
|
|
2019-11-08 22:53:20 -08:00
|
|
|
#include <hal/FRCUsageReporting.h>
|
2021-03-21 11:12:49 -07:00
|
|
|
#include <wpi/raw_ostream.h>
|
2017-11-08 23:40:01 -08:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/smartdashboard/SendableBuilder.h"
|
2019-09-14 15:22:54 -05:00
|
|
|
#include "frc/smartdashboard/SendableRegistry.h"
|
2017-11-08 23:40:01 -08:00
|
|
|
|
|
|
|
|
using namespace frc;
|
|
|
|
|
|
|
|
|
|
NidecBrushless::NidecBrushless(int pwmChannel, int dioChannel)
|
2018-11-22 21:15:26 -08:00
|
|
|
: m_dio(dioChannel), m_pwm(pwmChannel) {
|
2019-09-14 15:22:54 -05:00
|
|
|
auto& registry = SendableRegistry::GetInstance();
|
|
|
|
|
registry.AddChild(this, &m_dio);
|
|
|
|
|
registry.AddChild(this, &m_pwm);
|
2018-11-22 21:15:26 -08:00
|
|
|
SetExpiration(0.0);
|
|
|
|
|
SetSafetyEnabled(false);
|
2017-11-08 23:40:01 -08:00
|
|
|
|
|
|
|
|
// the dio controls the output (in PWM mode)
|
|
|
|
|
m_dio.SetPWMRate(15625);
|
|
|
|
|
m_dio.EnablePWM(0.5);
|
|
|
|
|
|
2019-10-29 21:34:10 -07:00
|
|
|
HAL_Report(HALUsageReporting::kResourceType_NidecBrushless, pwmChannel + 1);
|
2019-09-14 15:22:54 -05:00
|
|
|
registry.AddLW(this, "Nidec Brushless", pwmChannel);
|
2017-11-08 23:40:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NidecBrushless::Set(double speed) {
|
2017-12-01 00:43:11 -08:00
|
|
|
if (!m_disabled) {
|
|
|
|
|
m_speed = speed;
|
|
|
|
|
m_dio.UpdateDutyCycle(0.5 + 0.5 * (m_isInverted ? -speed : speed));
|
|
|
|
|
m_pwm.SetRaw(0xffff);
|
|
|
|
|
}
|
2018-11-22 21:15:26 -08:00
|
|
|
Feed();
|
2017-11-08 23:40:01 -08:00
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
double NidecBrushless::Get() const {
|
|
|
|
|
return m_speed;
|
|
|
|
|
}
|
2017-11-08 23:40:01 -08:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
void NidecBrushless::SetInverted(bool isInverted) {
|
|
|
|
|
m_isInverted = isInverted;
|
|
|
|
|
}
|
2017-11-08 23:40:01 -08:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
bool NidecBrushless::GetInverted() const {
|
|
|
|
|
return m_isInverted;
|
|
|
|
|
}
|
2017-11-08 23:40:01 -08:00
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
void NidecBrushless::Disable() {
|
|
|
|
|
m_disabled = true;
|
|
|
|
|
m_dio.UpdateDutyCycle(0.5);
|
|
|
|
|
m_pwm.SetDisabled();
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
void NidecBrushless::Enable() {
|
|
|
|
|
m_disabled = false;
|
|
|
|
|
}
|
2018-05-31 20:47:15 -07:00
|
|
|
|
2018-11-22 21:15:26 -08:00
|
|
|
void NidecBrushless::StopMotor() {
|
|
|
|
|
m_dio.UpdateDutyCycle(0.5);
|
|
|
|
|
m_pwm.SetDisabled();
|
2017-11-08 23:40:01 -08:00
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
void NidecBrushless::GetDescription(wpi::raw_ostream& desc) const {
|
2017-11-08 23:40:01 -08:00
|
|
|
desc << "Nidec " << GetChannel();
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
int NidecBrushless::GetChannel() const {
|
|
|
|
|
return m_pwm.GetChannel();
|
|
|
|
|
}
|
2017-11-08 23:40:01 -08:00
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
void NidecBrushless::InitSendable(SendableBuilder& builder) {
|
|
|
|
|
builder.SetSmartDashboardType("Nidec Brushless");
|
2018-07-28 14:04:46 -07:00
|
|
|
builder.SetActuator(true);
|
2017-12-04 23:28:33 -08:00
|
|
|
builder.SetSafeState([=]() { StopMotor(); });
|
2020-06-27 20:39:00 -07:00
|
|
|
builder.AddDoubleProperty(
|
|
|
|
|
"Value", [=]() { return Get(); }, [=](double value) { Set(value); });
|
2017-11-08 23:40:01 -08:00
|
|
|
}
|