001/*----------------------------------------------------------------------------*/ 002/* Copyright (c) 2008-2018 FIRST. 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 extends SendableBase { 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 * Set the default location for the Solenoid module. 070 * 071 * @param moduleNumber The number of the solenoid module to use. 072 */ 073 public static void setDefaultSolenoidModule(final int moduleNumber) { 074 checkSolenoidModule(moduleNumber); 075 SensorBase.m_defaultSolenoidModule = moduleNumber; 076 } 077 078 /** 079 * Verify that the solenoid module is correct. 080 * 081 * @param moduleNumber The solenoid module module number to check. 082 */ 083 public static void checkSolenoidModule(final int moduleNumber) { 084 if (!SolenoidJNI.checkSolenoidModule(moduleNumber)) { 085 StringBuilder buf = new StringBuilder(); 086 buf.append("Requested solenoid module is out of range. Minimum: 0, Maximum: ") 087 .append(kPCMModules) 088 .append(", Requested: ") 089 .append(moduleNumber); 090 throw new IndexOutOfBoundsException(buf.toString()); 091 } 092 } 093 094 /** 095 * Check that the digital channel number is valid. Verify that the channel number is one of the 096 * legal channel numbers. Channel numbers are 0-based. 097 * 098 * @param channel The channel number to check. 099 */ 100 public static void checkDigitalChannel(final int channel) { 101 if (!DIOJNI.checkDIOChannel(channel)) { 102 StringBuilder buf = new StringBuilder(); 103 buf.append("Requested DIO channel is out of range. Minimum: 0, Maximum: ") 104 .append(kDigitalChannels) 105 .append(", Requested: ") 106 .append(channel); 107 throw new IndexOutOfBoundsException(buf.toString()); 108 } 109 } 110 111 /** 112 * Check that the digital channel number is valid. Verify that the channel number is one of the 113 * legal channel numbers. Channel numbers are 0-based. 114 * 115 * @param channel The channel number to check. 116 */ 117 public static void checkRelayChannel(final int channel) { 118 if (!RelayJNI.checkRelayChannel(channel)) { 119 StringBuilder buf = new StringBuilder(); 120 buf.append("Requested relay channel is out of range. Minimum: 0, Maximum: ") 121 .append(kRelayChannels) 122 .append(", Requested: ") 123 .append(channel); 124 throw new IndexOutOfBoundsException(buf.toString()); 125 } 126 } 127 128 /** 129 * Check that the digital channel number is valid. Verify that the channel number is one of the 130 * legal channel numbers. Channel numbers are 0-based. 131 * 132 * @param channel The channel number to check. 133 */ 134 public static void checkPWMChannel(final int channel) { 135 if (!PWMJNI.checkPWMChannel(channel)) { 136 StringBuilder buf = new StringBuilder(); 137 buf.append("Requested PWM channel is out of range. Minimum: 0, Maximum: ") 138 .append(kPwmChannels) 139 .append(", Requested: ") 140 .append(channel); 141 throw new IndexOutOfBoundsException(buf.toString()); 142 } 143 } 144 145 /** 146 * Check that the analog input number is value. Verify that the analog input number is one of the 147 * legal channel numbers. Channel numbers are 0-based. 148 * 149 * @param channel The channel number to check. 150 */ 151 public static void checkAnalogInputChannel(final int channel) { 152 if (!AnalogJNI.checkAnalogInputChannel(channel)) { 153 StringBuilder buf = new StringBuilder(); 154 buf.append("Requested analog input channel is out of range. Minimum: 0, Maximum: ") 155 .append(kAnalogInputChannels) 156 .append(", Requested: ") 157 .append(channel); 158 throw new IndexOutOfBoundsException(buf.toString()); 159 } 160 } 161 162 /** 163 * Check that the analog input number is value. Verify that the analog input number is one of the 164 * legal channel numbers. Channel numbers are 0-based. 165 * 166 * @param channel The channel number to check. 167 */ 168 public static void checkAnalogOutputChannel(final int channel) { 169 if (!AnalogJNI.checkAnalogOutputChannel(channel)) { 170 StringBuilder buf = new StringBuilder(); 171 buf.append("Requested analog output channel is out of range. Minimum: 0, Maximum: ") 172 .append(kAnalogOutputChannels) 173 .append(", Requested: ") 174 .append(channel); 175 throw new IndexOutOfBoundsException(buf.toString()); 176 } 177 } 178 179 /** 180 * Verify that the solenoid channel number is within limits. Channel numbers are 0-based. 181 * 182 * @param channel The channel number to check. 183 */ 184 public static void checkSolenoidChannel(final int channel) { 185 if (!SolenoidJNI.checkSolenoidChannel(channel)) { 186 StringBuilder buf = new StringBuilder(); 187 buf.append("Requested solenoid channel is out of range. Minimum: 0, Maximum: ") 188 .append(kSolenoidChannels) 189 .append(", Requested: ") 190 .append(channel); 191 throw new IndexOutOfBoundsException(buf.toString()); 192 } 193 } 194 195 /** 196 * Verify that the power distribution channel number is within limits. Channel numbers are 197 * 0-based. 198 * 199 * @param channel The channel number to check. 200 */ 201 public static void checkPDPChannel(final int channel) { 202 if (!PDPJNI.checkPDPChannel(channel)) { 203 StringBuilder buf = new StringBuilder(); 204 buf.append("Requested PDP channel is out of range. Minimum: 0, Maximum: ") 205 .append(kPDPChannels) 206 .append(", Requested: ") 207 .append(channel); 208 throw new IndexOutOfBoundsException(buf.toString()); 209 } 210 } 211 212 /** 213 * Verify that the PDP module number is within limits. module numbers are 0-based. 214 * 215 * @param module The module number to check. 216 */ 217 public static void checkPDPModule(final int module) { 218 if (!PDPJNI.checkPDPModule(module)) { 219 StringBuilder buf = new StringBuilder(); 220 buf.append("Requested PDP module is out of range. Minimum: 0, Maximum: ") 221 .append(kPDPModules) 222 .append(", Requested: ") 223 .append(module); 224 throw new IndexOutOfBoundsException(buf.toString()); 225 } 226 } 227 228 /** 229 * Get the number of the default solenoid module. 230 * 231 * @return The number of the default solenoid module. 232 */ 233 public static int getDefaultSolenoidModule() { 234 return SensorBase.m_defaultSolenoidModule; 235 } 236}