Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 1 | // Copyright (c) FIRST and other WPILib contributors. |
| 2 | // Open Source Software; you can modify and/or share it under the terms of |
| 3 | // the WPILib BSD license file in the root directory of this project. |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 4 | |
| 5 | #include "frc/SpeedControllerGroup.h" |
| 6 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 7 | #include <wpi/sendable/SendableBuilder.h> |
| 8 | #include <wpi/sendable/SendableRegistry.h> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 9 | |
| 10 | using namespace frc; |
| 11 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 12 | // Can't use a delegated constructor here because of an MSVC bug. |
| 13 | // https://developercommunity.visualstudio.com/content/problem/583/compiler-bug-with-delegating-a-constructor.html |
| 14 | |
| 15 | SpeedControllerGroup::SpeedControllerGroup( |
| 16 | std::vector<std::reference_wrapper<SpeedController>>&& speedControllers) |
| 17 | : m_speedControllers(std::move(speedControllers)) { |
| 18 | Initialize(); |
| 19 | } |
| 20 | |
| 21 | void SpeedControllerGroup::Initialize() { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 22 | for (auto& speedController : m_speedControllers) { |
| 23 | wpi::SendableRegistry::AddChild(this, &speedController.get()); |
| 24 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 25 | static int instances = 0; |
| 26 | ++instances; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 27 | wpi::SendableRegistry::Add(this, "SpeedControllerGroup", instances); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 28 | } |
| 29 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 30 | void SpeedControllerGroup::Set(double speed) { |
| 31 | for (auto speedController : m_speedControllers) { |
| 32 | speedController.get().Set(m_isInverted ? -speed : speed); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | double SpeedControllerGroup::Get() const { |
| 37 | if (!m_speedControllers.empty()) { |
| 38 | return m_speedControllers.front().get().Get() * (m_isInverted ? -1 : 1); |
| 39 | } |
| 40 | return 0.0; |
| 41 | } |
| 42 | |
| 43 | void SpeedControllerGroup::SetInverted(bool isInverted) { |
| 44 | m_isInverted = isInverted; |
| 45 | } |
| 46 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 47 | bool SpeedControllerGroup::GetInverted() const { |
| 48 | return m_isInverted; |
| 49 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 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 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 63 | void SpeedControllerGroup::InitSendable(wpi::SendableBuilder& builder) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 64 | builder.SetSmartDashboardType("Speed Controller"); |
| 65 | builder.SetActuator(true); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 66 | builder.SetSafeState([=] { StopMotor(); }); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 67 | builder.AddDoubleProperty( |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 68 | "Value", [=] { return Get(); }, [=](double value) { Set(value); }); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 69 | } |