blob: ba94613b11e2744369f6d83d90110ed8f9ccec8d [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/AnalogPotentiometer.h"
6
Austin Schuh812d0d12021-11-04 20:16:48 -07007#include <utility>
8
9#include <wpi/NullDeleter.h>
10#include <wpi/sendable/SendableBuilder.h>
11#include <wpi/sendable/SendableRegistry.h>
12
Brian Silverman8fce7482020-01-05 13:18:21 -080013#include "frc/RobotController.h"
Brian Silverman8fce7482020-01-05 13:18:21 -080014
15using namespace frc;
16
17AnalogPotentiometer::AnalogPotentiometer(int channel, double fullRange,
18 double offset)
19 : AnalogPotentiometer(std::make_shared<AnalogInput>(channel), fullRange,
20 offset) {
Austin Schuh812d0d12021-11-04 20:16:48 -070021 wpi::SendableRegistry::AddChild(this, m_analog_input.get());
Brian Silverman8fce7482020-01-05 13:18:21 -080022}
23
24AnalogPotentiometer::AnalogPotentiometer(AnalogInput* input, double fullRange,
25 double offset)
26 : AnalogPotentiometer(
Austin Schuh812d0d12021-11-04 20:16:48 -070027 std::shared_ptr<AnalogInput>(input, wpi::NullDeleter<AnalogInput>()),
Brian Silverman8fce7482020-01-05 13:18:21 -080028 fullRange, offset) {}
29
30AnalogPotentiometer::AnalogPotentiometer(std::shared_ptr<AnalogInput> input,
31 double fullRange, double offset)
Austin Schuh812d0d12021-11-04 20:16:48 -070032 : m_analog_input(std::move(input)),
33 m_fullRange(fullRange),
34 m_offset(offset) {
35 wpi::SendableRegistry::AddLW(this, "AnalogPotentiometer",
36 m_analog_input->GetChannel());
Brian Silverman8fce7482020-01-05 13:18:21 -080037}
38
39double AnalogPotentiometer::Get() const {
Austin Schuh1e69f942020-11-14 15:06:14 -080040 return (m_analog_input->GetAverageVoltage() /
41 RobotController::GetVoltage5V()) *
Brian Silverman8fce7482020-01-05 13:18:21 -080042 m_fullRange +
43 m_offset;
44}
45
Austin Schuh812d0d12021-11-04 20:16:48 -070046void AnalogPotentiometer::InitSendable(wpi::SendableBuilder& builder) {
Austin Schuh1e69f942020-11-14 15:06:14 -080047 builder.SetSmartDashboardType("Analog Input");
48 builder.AddDoubleProperty(
Austin Schuh812d0d12021-11-04 20:16:48 -070049 "Value", [=] { return Get(); }, nullptr);
Brian Silverman8fce7482020-01-05 13:18:21 -080050}