blob: 8fea1b24f8308902811b5b040d76150f86aae2e1 [file] [log] [blame]
Brian Silverman8fce7482020-01-05 13:18:21 -08001/*----------------------------------------------------------------------------*/
Austin Schuh1e69f942020-11-14 15:06:14 -08002/* Copyright (c) 2016-2020 FIRST. All Rights Reserved. */
Brian Silverman8fce7482020-01-05 13:18:21 -08003/* 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 Schuh1e69f942020-11-14 15:06:14 -080011#include "frc/smartdashboard/SendableRegistry.h"
Brian Silverman8fce7482020-01-05 13:18:21 -080012
13using namespace frc;
14
Austin Schuh1e69f942020-11-14 15:06:14 -080015// 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
18SpeedControllerGroup::SpeedControllerGroup(
19 std::vector<std::reference_wrapper<SpeedController>>&& speedControllers)
20 : m_speedControllers(std::move(speedControllers)) {
21 Initialize();
22}
23
24void 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 Silverman8fce7482020-01-05 13:18:21 -080032void SpeedControllerGroup::Set(double speed) {
33 for (auto speedController : m_speedControllers) {
34 speedController.get().Set(m_isInverted ? -speed : speed);
35 }
36}
37
38double 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
45void SpeedControllerGroup::SetInverted(bool isInverted) {
46 m_isInverted = isInverted;
47}
48
49bool SpeedControllerGroup::GetInverted() const { return m_isInverted; }
50
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
63void SpeedControllerGroup::PIDWrite(double output) { Set(output); }
64
65void SpeedControllerGroup::InitSendable(SendableBuilder& builder) {
66 builder.SetSmartDashboardType("Speed Controller");
67 builder.SetActuator(true);
68 builder.SetSafeState([=]() { StopMotor(); });
Austin Schuh1e69f942020-11-14 15:06:14 -080069 builder.AddDoubleProperty(
70 "Value", [=]() { return Get(); }, [=](double value) { Set(value); });
Brian Silverman8fce7482020-01-05 13:18:21 -080071}