blob: 0ccc7c78144a71e6dd1670c7cdcf075f3c4f2c7c [file] [log] [blame]
Brian Silverman41cdd3e2019-01-19 19:48:58 -08001/*----------------------------------------------------------------------------*/
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
14using namespace frc;
15
16NidecBrushless::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
31void 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
40double NidecBrushless::Get() const { return m_speed; }
41
42void NidecBrushless::SetInverted(bool isInverted) { m_isInverted = isInverted; }
43
44bool NidecBrushless::GetInverted() const { return m_isInverted; }
45
46void NidecBrushless::Disable() {
47 m_disabled = true;
48 m_dio.UpdateDutyCycle(0.5);
49 m_pwm.SetDisabled();
50}
51
52void NidecBrushless::Enable() { m_disabled = false; }
53
54void NidecBrushless::PIDWrite(double output) { Set(output); }
55
56void NidecBrushless::StopMotor() {
57 m_dio.UpdateDutyCycle(0.5);
58 m_pwm.SetDisabled();
59}
60
61void NidecBrushless::GetDescription(wpi::raw_ostream& desc) const {
62 desc << "Nidec " << GetChannel();
63}
64
65int NidecBrushless::GetChannel() const { return m_pwm.GetChannel(); }
66
67void 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}