001 package edu.wpi.first.wpilibj; 002 import edu.wpi.first.wpilibj.interfaces.Potentiometer; 003 import edu.wpi.first.wpilibj.livewindow.LiveWindowSendable; 004 import edu.wpi.first.wpilibj.tables.ITable; 005 006 /** 007 * Class for reading analog potentiometers. Analog potentiometers read 008 * in an analog voltage that corresponds to a position. Usually the 009 * position is either degrees or meters. However, if no conversion is 010 * given it remains volts. 011 * 012 * @author Alex Henning 013 */ 014 public class AnalogPotentiometer implements Potentiometer, LiveWindowSendable { 015 private int m_module, m_channel; 016 private double m_scale, m_offset; 017 private AnalogChannel m_analog_channel; 018 019 /** 020 * Common initialization code called by all constructors. 021 */ 022 private void initPot(final int slot, final int channel, double scale, double offset) { 023 this.m_module = slot; 024 this.m_channel = channel; 025 this.m_scale = scale; 026 this.m_offset = offset; 027 m_analog_channel = new AnalogChannel(slot, channel); 028 } 029 030 /** 031 * AnalogPotentiometer constructor. 032 * 033 * Use the scaling and offset values so that the output produces 034 * meaningful values. I.E: you have a 270 degree potentiometer and 035 * you want the output to be degrees with the halfway point as 0 036 * degrees. The scale value is 270.0(degrees)/5.0(volts) and the 037 * offset is -135.0 since the halfway point after scaling is 135 038 * degrees. 039 * 040 * @param slot The analog module this potentiometer is plugged into. 041 * @param channel The analog channel this potentiometer is plugged into. 042 * @param scale The scaling to multiply the voltage by to get a meaningful unit. 043 * @param offset The offset to add to the scaled value for controlling the zero value 044 */ 045 public AnalogPotentiometer(final int slot, final int channel, double scale, double offset) { 046 initPot(slot, channel, scale, offset); 047 } 048 049 /** 050 * AnalogPotentiometer constructor. 051 * 052 * Use the scaling and offset values so that the output produces 053 * meaningful values. I.E: you have a 270 degree potentiometer and 054 * you want the output to be degrees with the halfway point as 0 055 * degrees. The scale value is 270.0(degrees)/5.0(volts) and the 056 * offset is -135.0 since the halfway point after scaling is 135 057 * degrees. 058 * 059 * @param channel The analog channel this potentiometer is plugged into. 060 * @param scale The scaling to multiply the voltage by to get a meaningful unit. 061 * @param offset The offset to add to the scaled value for controlling the zero value 062 */ 063 public AnalogPotentiometer(final int channel, double scale, double offset) { 064 initPot(1, channel, scale, offset); 065 } 066 067 /** 068 * AnalogPotentiometer constructor. 069 * 070 * Use the scaling and offset values so that the output produces 071 * meaningful values. I.E: you have a 270 degree potentiometer and 072 * you want the output to be degrees with the halfway point as 0 073 * degrees. The scale value is 270.0(degrees)/5.0(volts) and the 074 * offset is -135.0 since the halfway point after scaling is 135 075 * degrees. 076 * 077 * @param channel The analog channel this potentiometer is plugged into. 078 * @param scale The scaling to multiply the voltage by to get a meaningful unit. 079 */ 080 public AnalogPotentiometer(final int channel, double scale) { 081 initPot(1, channel, scale, 0); 082 } 083 084 /** 085 * AnalogPotentiometer constructor. 086 * 087 * @param channel The analog channel this potentiometer is plugged into. 088 */ 089 public AnalogPotentiometer(final int channel) { 090 initPot(1, channel, 1, 0); 091 } 092 093 /** 094 * Get the current reading of the potentiomere. 095 * 096 * @return The current position of the potentiometer. 097 */ 098 public double get() { 099 return m_analog_channel.getVoltage() * m_scale + m_offset; 100 } 101 102 103 /** 104 * Implement the PIDSource interface. 105 * 106 * @return The current reading. 107 */ 108 public double pidGet() { 109 return get(); 110 } 111 112 /* 113 * Live Window code, only does anything if live window is activated. 114 */ 115 public String getSmartDashboardType(){ 116 return "Analog Input"; 117 } 118 private ITable m_table; 119 120 /** 121 * {@inheritDoc} 122 */ 123 public void initTable(ITable subtable) { 124 m_table = subtable; 125 updateTable(); 126 } 127 128 /** 129 * {@inheritDoc} 130 */ 131 public void updateTable() { 132 if (m_table != null) { 133 m_table.putNumber("Value", get()); 134 } 135 } 136 137 /** 138 * {@inheritDoc} 139 */ 140 public ITable getTable(){ 141 return m_table; 142 } 143 144 /** 145 * Analog Channels don't have to do anything special when entering the LiveWindow. 146 * {@inheritDoc} 147 */ 148 public void startLiveWindowMode() {} 149 150 /** 151 * Analog Channels don't have to do anything special when exiting the LiveWindow. 152 * {@inheritDoc} 153 */ 154 public void stopLiveWindowMode() {} 155 }