001/*----------------------------------------------------------------------------*/
002/* Copyright (c) FIRST 2008-2017. All Rights Reserved.                        */
003/* Open Source Software - may be modified and shared by FRC teams. The code   */
004/* must be accompanied by the FIRST BSD license file in the root directory of */
005/* the project.                                                               */
006/*----------------------------------------------------------------------------*/
007
008package edu.wpi.first.wpilibj;
009
010import edu.wpi.first.wpilibj.hal.AnalogJNI;
011import edu.wpi.first.wpilibj.hal.ConstantsJNI;
012import edu.wpi.first.wpilibj.hal.DIOJNI;
013import edu.wpi.first.wpilibj.hal.PDPJNI;
014import edu.wpi.first.wpilibj.hal.PWMJNI;
015import edu.wpi.first.wpilibj.hal.PortsJNI;
016import edu.wpi.first.wpilibj.hal.RelayJNI;
017import edu.wpi.first.wpilibj.hal.SolenoidJNI;
018
019/**
020 * Base class for all sensors. Stores most recent status information as well as containing utility
021 * functions for checking channels and error processing.
022 */
023public abstract class SensorBase {
024  /**
025   * Ticks per microsecond.
026   */
027  public static final int kSystemClockTicksPerMicrosecond =
028      ConstantsJNI.getSystemClockTicksPerMicrosecond();
029  /**
030   * Number of digital channels per roboRIO.
031   */
032  public static final int kDigitalChannels = PortsJNI.getNumDigitalChannels();
033  /**
034   * Number of analog input channels per roboRIO.
035   */
036  public static final int kAnalogInputChannels = PortsJNI.getNumAnalogInputs();
037  /**
038   * Number of analog output channels per roboRIO.
039   */
040  public static final int kAnalogOutputChannels = PortsJNI.getNumAnalogOutputs();
041  /**
042   * Number of solenoid channels per module.
043   */
044  public static final int kSolenoidChannels = PortsJNI.getNumSolenoidChannels();
045  /**
046   * Number of PWM channels per roboRIO.
047   */
048  public static final int kPwmChannels = PortsJNI.getNumPWMChannels();
049  /**
050   * Number of relay channels per roboRIO.
051   */
052  public static final int kRelayChannels = PortsJNI.getNumRelayHeaders();
053  /**
054   * Number of power distribution channels per PDP.
055   */
056  public static final int kPDPChannels = PortsJNI.getNumPDPChannels();
057  /**
058   * Number of power distribution modules per PDP.
059   */
060  public static final int kPDPModules = PortsJNI.getNumPDPModules();
061  /**
062   * Number of PCM Modules.
063   */
064  public static final int kPCMModules = PortsJNI.getNumPCMModules();
065
066  private static int m_defaultSolenoidModule = 0;
067
068  /**
069   * Creates an instance of the sensor base and gets an FPGA handle.
070   */
071  public SensorBase() {
072  }
073
074  /**
075   * Set the default location for the Solenoid module.
076   *
077   * @param moduleNumber The number of the solenoid module to use.
078   */
079  public static void setDefaultSolenoidModule(final int moduleNumber) {
080    checkSolenoidModule(moduleNumber);
081    SensorBase.m_defaultSolenoidModule = moduleNumber;
082  }
083
084  /**
085   * Verify that the solenoid module is correct.
086   *
087   * @param moduleNumber The solenoid module module number to check.
088   */
089  protected static void checkSolenoidModule(final int moduleNumber) {
090    if (!SolenoidJNI.checkSolenoidModule(moduleNumber)) {
091      StringBuilder buf = new StringBuilder();
092      buf.append("Requested solenoid module is out of range. Minimum: 0, Maximum: ")
093        .append(kPCMModules)
094        .append(", Requested: ")
095        .append(moduleNumber);
096      throw new IndexOutOfBoundsException(buf.toString());
097    }
098  }
099
100  /**
101   * Check that the digital channel number is valid. Verify that the channel number is one of the
102   * legal channel numbers. Channel numbers are 0-based.
103   *
104   * @param channel The channel number to check.
105   */
106  protected static void checkDigitalChannel(final int channel) {
107    if (!DIOJNI.checkDIOChannel(channel)) {
108      StringBuilder buf = new StringBuilder();
109      buf.append("Requested DIO channel is out of range. Minimum: 0, Maximum: ")
110        .append(kDigitalChannels)
111        .append(", Requested: ")
112        .append(channel);
113      throw new IndexOutOfBoundsException(buf.toString());
114    }
115  }
116
117  /**
118   * Check that the digital channel number is valid. Verify that the channel number is one of the
119   * legal channel numbers. Channel numbers are 0-based.
120   *
121   * @param channel The channel number to check.
122   */
123  protected static void checkRelayChannel(final int channel) {
124    if (!RelayJNI.checkRelayChannel(channel)) {
125      StringBuilder buf = new StringBuilder();
126      buf.append("Requested relay channel is out of range. Minimum: 0, Maximum: ")
127        .append(kRelayChannels)
128        .append(", Requested: ")
129        .append(channel);
130      throw new IndexOutOfBoundsException(buf.toString());
131    }
132  }
133
134  /**
135   * Check that the digital channel number is valid. Verify that the channel number is one of the
136   * legal channel numbers. Channel numbers are 0-based.
137   *
138   * @param channel The channel number to check.
139   */
140  protected static void checkPWMChannel(final int channel) {
141    if (!PWMJNI.checkPWMChannel(channel)) {
142      StringBuilder buf = new StringBuilder();
143      buf.append("Requested PWM channel is out of range. Minimum: 0, Maximum: ")
144        .append(kPwmChannels)
145        .append(", Requested: ")
146        .append(channel);
147      throw new IndexOutOfBoundsException(buf.toString());
148    }
149  }
150
151  /**
152   * Check that the analog input number is value. Verify that the analog input number is one of the
153   * legal channel numbers. Channel numbers are 0-based.
154   *
155   * @param channel The channel number to check.
156   */
157  protected static void checkAnalogInputChannel(final int channel) {
158    if (!AnalogJNI.checkAnalogInputChannel(channel)) {
159      StringBuilder buf = new StringBuilder();
160      buf.append("Requested analog input channel is out of range. Minimum: 0, Maximum: ")
161        .append(kAnalogInputChannels)
162        .append(", Requested: ")
163        .append(channel);
164      throw new IndexOutOfBoundsException(buf.toString());
165    }
166  }
167
168  /**
169   * Check that the analog input number is value. Verify that the analog input number is one of the
170   * legal channel numbers. Channel numbers are 0-based.
171   *
172   * @param channel The channel number to check.
173   */
174  protected static void checkAnalogOutputChannel(final int channel) {
175    if (!AnalogJNI.checkAnalogOutputChannel(channel)) {
176      StringBuilder buf = new StringBuilder();
177      buf.append("Requested analog output channel is out of range. Minimum: 0, Maximum: ")
178        .append(kAnalogOutputChannels)
179        .append(", Requested: ")
180        .append(channel);
181      throw new IndexOutOfBoundsException(buf.toString());
182    }
183  }
184
185  /**
186   * Verify that the solenoid channel number is within limits. Channel numbers are 0-based.
187   *
188   * @param channel The channel number to check.
189   */
190  protected static void checkSolenoidChannel(final int channel) {
191    if (!SolenoidJNI.checkSolenoidChannel(channel)) {
192      StringBuilder buf = new StringBuilder();
193      buf.append("Requested solenoid channel is out of range. Minimum: 0, Maximum: ")
194        .append(kSolenoidChannels)
195        .append(", Requested: ")
196        .append(channel);
197      throw new IndexOutOfBoundsException(buf.toString());
198    }
199  }
200
201  /**
202   * Verify that the power distribution channel number is within limits. Channel numbers are
203   * 0-based.
204   *
205   * @param channel The channel number to check.
206   */
207  protected static void checkPDPChannel(final int channel) {
208    if (!PDPJNI.checkPDPChannel(channel)) {
209      StringBuilder buf = new StringBuilder();
210      buf.append("Requested PDP channel is out of range. Minimum: 0, Maximum: ")
211        .append(kPDPChannels)
212        .append(", Requested: ")
213        .append(channel);
214      throw new IndexOutOfBoundsException(buf.toString());
215    }
216  }
217
218  /**
219   * Verify that the PDP module number is within limits. module numbers are 0-based.
220   *
221   * @param module The module number to check.
222   */
223  protected static void checkPDPModule(final int module) {
224    if (!PDPJNI.checkPDPModule(module)) {
225      StringBuilder buf = new StringBuilder();
226      buf.append("Requested PDP module is out of range. Minimum: 0, Maximum: ")
227        .append(kPDPModules)
228        .append(", Requested: ")
229        .append(module);
230      throw new IndexOutOfBoundsException(buf.toString());
231    }
232  }
233
234  /**
235   * Get the number of the default solenoid module.
236   *
237   * @return The number of the default solenoid module.
238   */
239  public static int getDefaultSolenoidModule() {
240    return SensorBase.m_defaultSolenoidModule;
241  }
242
243  /**
244   * Free the resources used by this object.
245   */
246  public void free() {
247  }
248}