blob: c7aedd6a0ffa45f24ab1a603945a88f1882eac74 [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/AnalogPotentiometer.h"
9
Austin Schuh1e69f942020-11-14 15:06:14 -080010#include "frc/Base.h"
Brian Silverman8fce7482020-01-05 13:18:21 -080011#include "frc/RobotController.h"
12#include "frc/smartdashboard/SendableBuilder.h"
13#include "frc/smartdashboard/SendableRegistry.h"
14
15using namespace frc;
16
17AnalogPotentiometer::AnalogPotentiometer(int channel, double fullRange,
18 double offset)
19 : AnalogPotentiometer(std::make_shared<AnalogInput>(channel), fullRange,
20 offset) {
21 SendableRegistry::GetInstance().AddChild(this, m_analog_input.get());
22}
23
24AnalogPotentiometer::AnalogPotentiometer(AnalogInput* input, double fullRange,
25 double offset)
26 : AnalogPotentiometer(
27 std::shared_ptr<AnalogInput>(input, NullDeleter<AnalogInput>()),
28 fullRange, offset) {}
29
30AnalogPotentiometer::AnalogPotentiometer(std::shared_ptr<AnalogInput> input,
31 double fullRange, double offset)
32 : m_analog_input(input), m_fullRange(fullRange), m_offset(offset) {
33 SendableRegistry::GetInstance().AddLW(this, "AnalogPotentiometer",
34 m_analog_input->GetChannel());
35}
36
37double AnalogPotentiometer::Get() const {
Austin Schuh1e69f942020-11-14 15:06:14 -080038 return (m_analog_input->GetAverageVoltage() /
39 RobotController::GetVoltage5V()) *
Brian Silverman8fce7482020-01-05 13:18:21 -080040 m_fullRange +
41 m_offset;
42}
43
44double AnalogPotentiometer::PIDGet() { return Get(); }
45
46void AnalogPotentiometer::InitSendable(SendableBuilder& builder) {
Austin Schuh1e69f942020-11-14 15:06:14 -080047 builder.SetSmartDashboardType("Analog Input");
48 builder.AddDoubleProperty(
49 "Value", [=]() { return Get(); }, nullptr);
Brian Silverman8fce7482020-01-05 13:18:21 -080050}