Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame^] | 2 | /* Copyright (c) 2016-2020 FIRST. All Rights Reserved. */ |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 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/SpeedControllerGroup.h" |
| 9 | |
| 10 | #include "frc/smartdashboard/SendableBuilder.h" |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame^] | 11 | #include "frc/smartdashboard/SendableRegistry.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 12 | |
| 13 | using namespace frc; |
| 14 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame^] | 15 | // Can't use a delegated constructor here because of an MSVC bug. |
| 16 | // https://developercommunity.visualstudio.com/content/problem/583/compiler-bug-with-delegating-a-constructor.html |
| 17 | |
| 18 | SpeedControllerGroup::SpeedControllerGroup( |
| 19 | std::vector<std::reference_wrapper<SpeedController>>&& speedControllers) |
| 20 | : m_speedControllers(std::move(speedControllers)) { |
| 21 | Initialize(); |
| 22 | } |
| 23 | |
| 24 | void SpeedControllerGroup::Initialize() { |
| 25 | for (auto& speedController : m_speedControllers) |
| 26 | SendableRegistry::GetInstance().AddChild(this, &speedController.get()); |
| 27 | static int instances = 0; |
| 28 | ++instances; |
| 29 | SendableRegistry::GetInstance().Add(this, "SpeedControllerGroup", instances); |
| 30 | } |
| 31 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 32 | void SpeedControllerGroup::Set(double speed) { |
| 33 | for (auto speedController : m_speedControllers) { |
| 34 | speedController.get().Set(m_isInverted ? -speed : speed); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | double SpeedControllerGroup::Get() const { |
| 39 | if (!m_speedControllers.empty()) { |
| 40 | return m_speedControllers.front().get().Get() * (m_isInverted ? -1 : 1); |
| 41 | } |
| 42 | return 0.0; |
| 43 | } |
| 44 | |
| 45 | void SpeedControllerGroup::SetInverted(bool isInverted) { |
| 46 | m_isInverted = isInverted; |
| 47 | } |
| 48 | |
| 49 | bool SpeedControllerGroup::GetInverted() const { return m_isInverted; } |
| 50 | |
| 51 | void SpeedControllerGroup::Disable() { |
| 52 | for (auto speedController : m_speedControllers) { |
| 53 | speedController.get().Disable(); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | void SpeedControllerGroup::StopMotor() { |
| 58 | for (auto speedController : m_speedControllers) { |
| 59 | speedController.get().StopMotor(); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | void SpeedControllerGroup::PIDWrite(double output) { Set(output); } |
| 64 | |
| 65 | void SpeedControllerGroup::InitSendable(SendableBuilder& builder) { |
| 66 | builder.SetSmartDashboardType("Speed Controller"); |
| 67 | builder.SetActuator(true); |
| 68 | builder.SetSafeState([=]() { StopMotor(); }); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame^] | 69 | builder.AddDoubleProperty( |
| 70 | "Value", [=]() { return Get(); }, [=](double value) { Set(value); }); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 71 | } |