blob: c3704dfa4ef2b859b6e35641433b9f4cb17585cf [file] [log] [blame]
Austin Schuh812d0d12021-11-04 20:16:48 -07001// 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 Silverman8fce7482020-01-05 13:18:21 -08004
5#include "frc/SpeedControllerGroup.h"
6
Austin Schuh812d0d12021-11-04 20:16:48 -07007#include <wpi/sendable/SendableBuilder.h>
8#include <wpi/sendable/SendableRegistry.h>
Brian Silverman8fce7482020-01-05 13:18:21 -08009
10using namespace frc;
11
Austin Schuh1e69f942020-11-14 15:06:14 -080012// 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
15SpeedControllerGroup::SpeedControllerGroup(
16 std::vector<std::reference_wrapper<SpeedController>>&& speedControllers)
17 : m_speedControllers(std::move(speedControllers)) {
18 Initialize();
19}
20
21void SpeedControllerGroup::Initialize() {
Austin Schuh812d0d12021-11-04 20:16:48 -070022 for (auto& speedController : m_speedControllers) {
23 wpi::SendableRegistry::AddChild(this, &speedController.get());
24 }
Austin Schuh1e69f942020-11-14 15:06:14 -080025 static int instances = 0;
26 ++instances;
Austin Schuh812d0d12021-11-04 20:16:48 -070027 wpi::SendableRegistry::Add(this, "SpeedControllerGroup", instances);
Austin Schuh1e69f942020-11-14 15:06:14 -080028}
29
Brian Silverman8fce7482020-01-05 13:18:21 -080030void SpeedControllerGroup::Set(double speed) {
31 for (auto speedController : m_speedControllers) {
32 speedController.get().Set(m_isInverted ? -speed : speed);
33 }
34}
35
36double 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
43void SpeedControllerGroup::SetInverted(bool isInverted) {
44 m_isInverted = isInverted;
45}
46
Austin Schuh812d0d12021-11-04 20:16:48 -070047bool SpeedControllerGroup::GetInverted() const {
48 return m_isInverted;
49}
Brian Silverman8fce7482020-01-05 13:18:21 -080050
51void SpeedControllerGroup::Disable() {
52 for (auto speedController : m_speedControllers) {
53 speedController.get().Disable();
54 }
55}
56
57void SpeedControllerGroup::StopMotor() {
58 for (auto speedController : m_speedControllers) {
59 speedController.get().StopMotor();
60 }
61}
62
Austin Schuh812d0d12021-11-04 20:16:48 -070063void SpeedControllerGroup::InitSendable(wpi::SendableBuilder& builder) {
Brian Silverman8fce7482020-01-05 13:18:21 -080064 builder.SetSmartDashboardType("Speed Controller");
65 builder.SetActuator(true);
Austin Schuh812d0d12021-11-04 20:16:48 -070066 builder.SetSafeState([=] { StopMotor(); });
Austin Schuh1e69f942020-11-14 15:06:14 -080067 builder.AddDoubleProperty(
Austin Schuh812d0d12021-11-04 20:16:48 -070068 "Value", [=] { return Get(); }, [=](double value) { Set(value); });
Brian Silverman8fce7482020-01-05 13:18:21 -080069}