2017-11-08 23:40:01 -08:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2018-01-02 09:20:21 -08:00
|
|
|
/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
|
2017-11-08 23:40:01 -08:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#include "NidecBrushless.h"
|
|
|
|
|
|
|
|
|
|
#include <HAL/HAL.h>
|
|
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
#include "SmartDashboard/SendableBuilder.h"
|
2017-11-08 23:40:01 -08:00
|
|
|
|
|
|
|
|
using namespace frc;
|
|
|
|
|
|
|
|
|
|
NidecBrushless::NidecBrushless(int pwmChannel, int dioChannel)
|
|
|
|
|
: m_safetyHelper(this), m_dio(dioChannel), m_pwm(pwmChannel) {
|
2017-12-04 23:28:33 -08:00
|
|
|
AddChild(&m_dio);
|
|
|
|
|
AddChild(&m_pwm);
|
2017-11-08 23:40:01 -08:00
|
|
|
m_safetyHelper.SetExpiration(0.0);
|
|
|
|
|
m_safetyHelper.SetSafetyEnabled(false);
|
|
|
|
|
|
|
|
|
|
// the dio controls the output (in PWM mode)
|
|
|
|
|
m_dio.SetPWMRate(15625);
|
|
|
|
|
m_dio.EnablePWM(0.5);
|
|
|
|
|
|
|
|
|
|
HAL_Report(HALUsageReporting::kResourceType_NidecBrushless, pwmChannel);
|
2017-12-04 23:28:33 -08:00
|
|
|
SetName("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);
|
|
|
|
|
}
|
2017-11-08 23:40:01 -08:00
|
|
|
m_safetyHelper.Feed();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double NidecBrushless::Get() const { return m_speed; }
|
|
|
|
|
|
|
|
|
|
void NidecBrushless::SetInverted(bool isInverted) { m_isInverted = isInverted; }
|
|
|
|
|
|
|
|
|
|
bool NidecBrushless::GetInverted() const { return m_isInverted; }
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
void NidecBrushless::Disable() {
|
|
|
|
|
m_disabled = true;
|
|
|
|
|
m_dio.UpdateDutyCycle(0.5);
|
|
|
|
|
m_pwm.SetDisabled();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NidecBrushless::StopMotor() {
|
|
|
|
|
m_dio.UpdateDutyCycle(0.5);
|
|
|
|
|
m_pwm.SetDisabled();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NidecBrushless::Enable() { m_disabled = false; }
|
|
|
|
|
|
2017-11-08 23:40:01 -08:00
|
|
|
void NidecBrushless::PIDWrite(double output) { Set(output); }
|
|
|
|
|
|
|
|
|
|
void NidecBrushless::SetExpiration(double timeout) {
|
|
|
|
|
m_safetyHelper.SetExpiration(timeout);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double NidecBrushless::GetExpiration() const {
|
|
|
|
|
return m_safetyHelper.GetExpiration();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool NidecBrushless::IsAlive() const { return m_safetyHelper.IsAlive(); }
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
void NidecBrushless::SetSafetyEnabled(bool enabled) {
|
|
|
|
|
m_safetyHelper.SetSafetyEnabled(enabled);
|
2017-12-01 00:43:11 -08:00
|
|
|
}
|
2017-11-08 23:40:01 -08:00
|
|
|
|
|
|
|
|
bool NidecBrushless::IsSafetyEnabled() const {
|
|
|
|
|
return m_safetyHelper.IsSafetyEnabled();
|
|
|
|
|
}
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int NidecBrushless::GetChannel() const { return m_pwm.GetChannel(); }
|
|
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
void NidecBrushless::InitSendable(SendableBuilder& builder) {
|
|
|
|
|
builder.SetSmartDashboardType("Nidec Brushless");
|
|
|
|
|
builder.SetSafeState([=]() { StopMotor(); });
|
|
|
|
|
builder.AddDoubleProperty("Value", [=]() { return Get(); },
|
|
|
|
|
[=](double value) { Set(value); });
|
2017-11-08 23:40:01 -08:00
|
|
|
}
|