001 /*----------------------------------------------------------------------------*/
002 /* Copyright (c) FIRST 2008-2012. 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
008 package edu.wpi.first.wpilibj;
009
010 import edu.wpi.first.wpilibj.communication.ModulePresence;
011 import edu.wpi.first.wpilibj.tables.ITable;
012
013 /**
014 * Base class for all sensors.
015 * Stores most recent status information as well as containing utility functions for checking
016 * channels and error processing.
017 */
018 public abstract class SensorBase { // TODO: Refactor
019
020 /**
021 * Ticks per microsecond
022 */
023 public static final int kSystemClockTicksPerMicrosecond = 40;
024 /**
025 * Number of digital channels per digital sidecar
026 */
027 public static final int kDigitalChannels = 14;
028 /**
029 * Number of analog channels per module
030 */
031 public static final int kAnalogChannels = 8;
032 /**
033 * Number of analog modules
034 */
035 public static final int kAnalogModules = 2;
036 /**
037 * Number of solenoid channels per module
038 */
039 public static final int kSolenoidChannels = 8;
040 /**
041 * Number of analog modules
042 */
043 public static final int kSolenoidModules = 2;
044 /**
045 * Number of PWM channels per sidecar
046 */
047 public static final int kPwmChannels = 10;
048 /**
049 * Number of relay channels per sidecar
050 */
051 public static final int kRelayChannels = 8;
052
053 private static int m_defaultAnalogModule = 1;
054 private static int m_defaultDigitalModule = 1;
055 private static int m_defaultSolenoidModule = 1;
056
057 /**
058 * Creates an instance of the sensor base and gets an FPGA handle
059 */
060 public SensorBase() {
061 }
062
063 /**
064 * Sets the default Digital Module.
065 * This sets the default digital module to use for objects that are created without
066 * specifying the digital module in the constructor. The default module is initialized
067 * to the first module in the chassis.
068 *
069 * @param moduleNumber The number of the digital module to use.
070 */
071 public static void setDefaultDigitalModule(final int moduleNumber) {
072 checkDigitalModule(moduleNumber);
073 SensorBase.m_defaultDigitalModule = moduleNumber;
074 }
075
076 /**
077 * Sets the default Analog module.
078 * This sets the default analog module to use for objects that are created without
079 * specifying the analog module in the constructor. The default module is initialized
080 * to the first module in the chassis.
081 *
082 * @param moduleNumber The number of the analog module to use.
083 */
084 public static void setDefaultAnalogModule(final int moduleNumber) {
085 checkAnalogModule(moduleNumber);
086 SensorBase.m_defaultAnalogModule = moduleNumber;
087 }
088
089 /**
090 * Set the default location for the Solenoid (9472) module.
091 *
092 * @param moduleNumber The number of the solenoid module to use.
093 */
094 public static void setDefaultSolenoidModule(final int moduleNumber) {
095 checkSolenoidModule(moduleNumber);
096 SensorBase.m_defaultSolenoidModule = moduleNumber;
097 }
098
099 /**
100 * Check that the digital module number is valid.
101 * Module numbers are 1 or 2 (they are no longer real cRIO slots).
102 *
103 * @param moduleNumber The digital module module number to check.
104 */
105 protected static void checkDigitalModule(final int moduleNumber) {
106 if(!ModulePresence.getModulePresence(ModulePresence.ModuleType.kDigital, moduleNumber - 1))
107 System.err.println("Digital module " + moduleNumber + " is not present.");
108 }
109
110 /**
111 * Check that the digital module number is valid.
112 * Module numbers are 1 or 2 (they are no longer real cRIO slots).
113 *
114 * @param moduleNumber The digital module module number to check.
115 */
116 protected static void checkRelayModule(final int moduleNumber) {
117 checkDigitalModule(moduleNumber);
118 }
119
120 /**
121 * Check that the digital module number is valid.
122 * Module numbers are 1 or 2 (they are no longer real cRIO slots).
123 *
124 * @param moduleNumber The digital module module number to check.
125 */
126 protected static void checkPWMModule(final int moduleNumber) {
127 checkDigitalModule(moduleNumber);
128 }
129
130 /**
131 * Check that the analog module number is valid.
132 * Module numbers are 1 or 2 (they are no longer real cRIO slots).
133 *
134 * @param moduleNumber The analog module module number to check.
135 */
136 protected static void checkAnalogModule(final int moduleNumber) {
137 if(!ModulePresence.getModulePresence(ModulePresence.ModuleType.kAnalog, moduleNumber - 1)) {
138 System.err.println("Analog module " + moduleNumber + " is not present.");
139 }
140 }
141
142 /**
143 * Verify that the solenoid module is correct.
144 * Module numbers are 1 or 2 (they are no longer real cRIO slots).
145 *
146 * @param moduleNumber The solenoid module module number to check.
147 */
148 protected static void checkSolenoidModule(final int moduleNumber) {
149 if(!ModulePresence.getModulePresence(ModulePresence.ModuleType.kSolenoid, moduleNumber - 1)) {
150 System.err.println("Solenoid module " + moduleNumber + " is not present.");
151 }
152 }
153
154 /**
155 * Check that the digital channel number is valid.
156 * Verify that the channel number is one of the legal channel numbers. Channel numbers are
157 * 1-based.
158 *
159 * @param channel The channel number to check.
160 */
161 protected static void checkDigitalChannel(final int channel) {
162 if (channel <= 0 || channel > kDigitalChannels) {
163 System.err.println("Requested digital channel number is out of range.");
164 }
165 }
166
167 /**
168 * Check that the digital channel number is valid.
169 * Verify that the channel number is one of the legal channel numbers. Channel numbers are
170 * 1-based.
171 *
172 * @param channel The channel number to check.
173 */
174 protected static void checkRelayChannel(final int channel) {
175 if (channel <= 0 || channel > kRelayChannels) {
176 System.err.println("Requested relay channel number is out of range.");
177 throw new IndexOutOfBoundsException("Requested relay channel number is out of range.");
178 }
179 }
180
181 /**
182 * Check that the digital channel number is valid.
183 * Verify that the channel number is one of the legal channel numbers. Channel numbers are
184 * 1-based.
185 *
186 * @param channel The channel number to check.
187 */
188 protected static void checkPWMChannel(final int channel) {
189 if (channel <= 0 || channel > kPwmChannels) {
190 System.err.println("Requested PWM channel number is out of range.");
191 throw new IndexOutOfBoundsException("Requested PWM channel number is out of range.");
192 }
193 }
194
195 /**
196 * Check that the analog channel number is value.
197 * Verify that the analog channel number is one of the legal channel numbers. Channel numbers
198 * are 1-based.
199 *
200 * @param channel The channel number to check.
201 */
202 protected static void checkAnalogChannel(final int channel) {
203 if (channel <= 0 || channel > kAnalogChannels) {
204 System.err.println("Requested analog channel number is out of range.");
205 }
206 }
207
208 /**
209 * Verify that the solenoid channel number is within limits. Channel numbers
210 * are 1-based.
211 *
212 * @param channel The channel number to check.
213 */
214 protected static void checkSolenoidChannel(final int channel) {
215 if (channel <= 0 || channel > kSolenoidChannels) {
216 System.err.println("Requested solenoid channel number is out of range.");
217 }
218 }
219
220 /**
221 * Get the number of the default analog module.
222 *
223 * @return The number of the default analog module.
224 */
225 public static int getDefaultAnalogModule() {
226 return SensorBase.m_defaultAnalogModule;
227 }
228
229 /**
230 * Get the number of the default analog module.
231 *
232 * @return The number of the default analog module.
233 */
234 public static int getDefaultDigitalModule() {
235 return SensorBase.m_defaultDigitalModule;
236 }
237
238 /**
239 * Get the number of the default analog module.
240 *
241 * @return The number of the default analog module.
242 */
243 public static int getDefaultSolenoidModule() {
244 return SensorBase.m_defaultSolenoidModule;
245 }
246
247 /**
248 * Free the resources used by this object
249 */
250 public void free() {}
251 }