Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */ |
| 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #include "frc/NidecBrushless.h" |
| 9 | |
| 10 | #include <hal/HAL.h> |
| 11 | |
| 12 | #include "frc/smartdashboard/SendableBuilder.h" |
| 13 | |
| 14 | using namespace frc; |
| 15 | |
| 16 | NidecBrushless::NidecBrushless(int pwmChannel, int dioChannel) |
| 17 | : m_dio(dioChannel), m_pwm(pwmChannel) { |
| 18 | AddChild(&m_dio); |
| 19 | AddChild(&m_pwm); |
| 20 | SetExpiration(0.0); |
| 21 | SetSafetyEnabled(false); |
| 22 | |
| 23 | // the dio controls the output (in PWM mode) |
| 24 | m_dio.SetPWMRate(15625); |
| 25 | m_dio.EnablePWM(0.5); |
| 26 | |
| 27 | HAL_Report(HALUsageReporting::kResourceType_NidecBrushless, pwmChannel); |
| 28 | SetName("Nidec Brushless", pwmChannel); |
| 29 | } |
| 30 | |
| 31 | void NidecBrushless::Set(double speed) { |
| 32 | if (!m_disabled) { |
| 33 | m_speed = speed; |
| 34 | m_dio.UpdateDutyCycle(0.5 + 0.5 * (m_isInverted ? -speed : speed)); |
| 35 | m_pwm.SetRaw(0xffff); |
| 36 | } |
| 37 | Feed(); |
| 38 | } |
| 39 | |
| 40 | double NidecBrushless::Get() const { return m_speed; } |
| 41 | |
| 42 | void NidecBrushless::SetInverted(bool isInverted) { m_isInverted = isInverted; } |
| 43 | |
| 44 | bool NidecBrushless::GetInverted() const { return m_isInverted; } |
| 45 | |
| 46 | void NidecBrushless::Disable() { |
| 47 | m_disabled = true; |
| 48 | m_dio.UpdateDutyCycle(0.5); |
| 49 | m_pwm.SetDisabled(); |
| 50 | } |
| 51 | |
| 52 | void NidecBrushless::Enable() { m_disabled = false; } |
| 53 | |
| 54 | void NidecBrushless::PIDWrite(double output) { Set(output); } |
| 55 | |
| 56 | void NidecBrushless::StopMotor() { |
| 57 | m_dio.UpdateDutyCycle(0.5); |
| 58 | m_pwm.SetDisabled(); |
| 59 | } |
| 60 | |
| 61 | void NidecBrushless::GetDescription(wpi::raw_ostream& desc) const { |
| 62 | desc << "Nidec " << GetChannel(); |
| 63 | } |
| 64 | |
| 65 | int NidecBrushless::GetChannel() const { return m_pwm.GetChannel(); } |
| 66 | |
| 67 | void NidecBrushless::InitSendable(SendableBuilder& builder) { |
| 68 | builder.SetSmartDashboardType("Nidec Brushless"); |
| 69 | builder.SetActuator(true); |
| 70 | builder.SetSafeState([=]() { StopMotor(); }); |
| 71 | builder.AddDoubleProperty("Value", [=]() { return Get(); }, |
| 72 | [=](double value) { Set(value); }); |
| 73 | } |