001// Copyright (c) FIRST and other WPILib contributors.
002// Open Source Software; you can modify and/or share it under the terms of
003// the WPILib BSD license file in the root directory of this project.
004
005package edu.wpi.first.wpilibj;
006
007import edu.wpi.first.hal.HAL;
008import edu.wpi.first.hal.HALUtil;
009import edu.wpi.first.hal.LEDJNI;
010import edu.wpi.first.hal.PowerJNI;
011import edu.wpi.first.hal.can.CANJNI;
012import edu.wpi.first.hal.can.CANStatus;
013
014/** Contains functions for roboRIO functionality. */
015public final class RobotController {
016  private RobotController() {
017    throw new UnsupportedOperationException("This is a utility class!");
018  }
019
020  /**
021   * Return the FPGA Version number. For now, expect this to be the current year.
022   *
023   * @return FPGA Version number.
024   */
025  public static int getFPGAVersion() {
026    return HALUtil.getFPGAVersion();
027  }
028
029  /**
030   * Return the FPGA Revision number. The format of the revision is 3 numbers. The 12 most
031   * significant bits are the Major Revision. the next 8 bits are the Minor Revision. The 12 least
032   * significant bits are the Build Number.
033   *
034   * @return FPGA Revision number.
035   */
036  public static long getFPGARevision() {
037    return HALUtil.getFPGARevision();
038  }
039
040  /**
041   * Return the serial number of the roboRIO.
042   *
043   * @return The serial number of the roboRIO.
044   */
045  public static String getSerialNumber() {
046    return HALUtil.getSerialNumber();
047  }
048
049  /**
050   * Return the comments from the roboRIO web interface.
051   *
052   * <p>The comments string is cached after the first call to this function on the RoboRIO - restart
053   * the robot code to reload the comments string after changing it in the web interface.
054   *
055   * @return the comments from the roboRIO web interface.
056   */
057  public static String getComments() {
058    return HALUtil.getComments();
059  }
060
061  /**
062   * Returns the team number configured for the robot controller.
063   *
064   * @return team number, or 0 if not found.
065   */
066  public static int getTeamNumber() {
067    return HALUtil.getTeamNumber();
068  }
069
070  /**
071   * Read the microsecond timer from the FPGA.
072   *
073   * @return The current time in microseconds according to the FPGA.
074   */
075  public static long getFPGATime() {
076    return HALUtil.getFPGATime();
077  }
078
079  /**
080   * Get the state of the "USER" button on the roboRIO.
081   *
082   * <p>Warning: the User Button is used to stop user programs from automatically loading if it is
083   * held for more then 5 seconds. Because of this, it's not recommended to be used by teams for any
084   * other purpose.
085   *
086   * @return true if the button is currently pressed down
087   */
088  public static boolean getUserButton() {
089    return HALUtil.getFPGAButton();
090  }
091
092  /**
093   * Read the battery voltage.
094   *
095   * @return The battery voltage in Volts.
096   */
097  public static double getBatteryVoltage() {
098    return PowerJNI.getVinVoltage();
099  }
100
101  /**
102   * Gets a value indicating whether the FPGA outputs are enabled. The outputs may be disabled if
103   * the robot is disabled or e-stopped, the watchdog has expired, or if the roboRIO browns out.
104   *
105   * @return True if the FPGA outputs are enabled.
106   */
107  public static boolean isSysActive() {
108    return HAL.getSystemActive();
109  }
110
111  /**
112   * Check if the system is browned out.
113   *
114   * @return True if the system is browned out
115   */
116  public static boolean isBrownedOut() {
117    return HAL.getBrownedOut();
118  }
119
120  /**
121   * Gets the current state of the Robot Signal Light (RSL).
122   *
123   * @return The current state of the RSL- true if on, false if off
124   */
125  public static boolean getRSLState() {
126    return HAL.getRSLState();
127  }
128
129  /**
130   * Gets if the system time is valid.
131   *
132   * @return True if the system time is valid, false otherwise
133   */
134  public static boolean isSystemTimeValid() {
135    return HAL.getSystemTimeValid();
136  }
137
138  /**
139   * Get the input voltage to the robot controller.
140   *
141   * @return The controller input voltage value in Volts
142   */
143  public static double getInputVoltage() {
144    return PowerJNI.getVinVoltage();
145  }
146
147  /**
148   * Get the input current to the robot controller.
149   *
150   * @return The controller input current value in Amps
151   */
152  public static double getInputCurrent() {
153    return PowerJNI.getVinCurrent();
154  }
155
156  /**
157   * Get the voltage of the 3.3V rail.
158   *
159   * @return The controller 3.3V rail voltage value in Volts
160   */
161  public static double getVoltage3V3() {
162    return PowerJNI.getUserVoltage3V3();
163  }
164
165  /**
166   * Get the current output of the 3.3V rail.
167   *
168   * @return The controller 3.3V rail output current value in Amps
169   */
170  public static double getCurrent3V3() {
171    return PowerJNI.getUserCurrent3V3();
172  }
173
174  /**
175   * Enables or disables the 3.3V rail.
176   *
177   * @param enabled whether to enable the 3.3V rail.
178   */
179  public static void setEnabled3V3(boolean enabled) {
180    PowerJNI.setUserEnabled3V3(enabled);
181  }
182
183  /**
184   * Get the enabled state of the 3.3V rail. The rail may be disabled due to a controller brownout,
185   * a short circuit on the rail, or controller over-voltage.
186   *
187   * @return The controller 3.3V rail enabled value
188   */
189  public static boolean getEnabled3V3() {
190    return PowerJNI.getUserActive3V3();
191  }
192
193  /**
194   * Get the count of the total current faults on the 3.3V rail since the controller has booted.
195   *
196   * @return The number of faults
197   */
198  public static int getFaultCount3V3() {
199    return PowerJNI.getUserCurrentFaults3V3();
200  }
201
202  /**
203   * Get the voltage of the 5V rail.
204   *
205   * @return The controller 5V rail voltage value in Volts
206   */
207  public static double getVoltage5V() {
208    return PowerJNI.getUserVoltage5V();
209  }
210
211  /**
212   * Get the current output of the 5V rail.
213   *
214   * @return The controller 5V rail output current value in Amps
215   */
216  public static double getCurrent5V() {
217    return PowerJNI.getUserCurrent5V();
218  }
219
220  /**
221   * Enables or disables the 5V rail.
222   *
223   * @param enabled whether to enable the 5V rail.
224   */
225  public static void setEnabled5V(boolean enabled) {
226    PowerJNI.setUserEnabled5V(enabled);
227  }
228
229  /**
230   * Get the enabled state of the 5V rail. The rail may be disabled due to a controller brownout, a
231   * short circuit on the rail, or controller over-voltage.
232   *
233   * @return The controller 5V rail enabled value
234   */
235  public static boolean getEnabled5V() {
236    return PowerJNI.getUserActive5V();
237  }
238
239  /**
240   * Get the count of the total current faults on the 5V rail since the controller has booted.
241   *
242   * @return The number of faults
243   */
244  public static int getFaultCount5V() {
245    return PowerJNI.getUserCurrentFaults5V();
246  }
247
248  /**
249   * Get the voltage of the 6V rail.
250   *
251   * @return The controller 6V rail voltage value in Volts
252   */
253  public static double getVoltage6V() {
254    return PowerJNI.getUserVoltage6V();
255  }
256
257  /**
258   * Get the current output of the 6V rail.
259   *
260   * @return The controller 6V rail output current value in Amps
261   */
262  public static double getCurrent6V() {
263    return PowerJNI.getUserCurrent6V();
264  }
265
266  /**
267   * Enables or disables the 6V rail.
268   *
269   * @param enabled whether to enable the 6V rail.
270   */
271  public static void setEnabled6V(boolean enabled) {
272    PowerJNI.setUserEnabled6V(enabled);
273  }
274
275  /**
276   * Get the enabled state of the 6V rail. The rail may be disabled due to a controller brownout, a
277   * short circuit on the rail, or controller over-voltage.
278   *
279   * @return The controller 6V rail enabled value
280   */
281  public static boolean getEnabled6V() {
282    return PowerJNI.getUserActive6V();
283  }
284
285  /**
286   * Get the count of the total current faults on the 6V rail since the controller has booted.
287   *
288   * @return The number of faults
289   */
290  public static int getFaultCount6V() {
291    return PowerJNI.getUserCurrentFaults6V();
292  }
293
294  /**
295   * Get the current brownout voltage setting.
296   *
297   * @return The brownout voltage
298   */
299  public static double getBrownoutVoltage() {
300    return PowerJNI.getBrownoutVoltage();
301  }
302
303  /**
304   * Set the voltage the roboRIO will brownout and disable all outputs.
305   *
306   * <p>Note that this only does anything on the roboRIO 2. On the roboRIO it is a no-op.
307   *
308   * @param brownoutVoltage The brownout voltage
309   */
310  public static void setBrownoutVoltage(double brownoutVoltage) {
311    PowerJNI.setBrownoutVoltage(brownoutVoltage);
312  }
313
314  /**
315   * Get the current CPU temperature in degrees Celsius.
316   *
317   * @return current CPU temperature in degrees Celsius
318   */
319  public static double getCPUTemp() {
320    return PowerJNI.getCPUTemp();
321  }
322
323  /** State for the radio led. */
324  public enum RadioLEDState {
325    /** Off. */
326    kOff(LEDJNI.RADIO_LED_STATE_OFF),
327    /** Green. */
328    kGreen(LEDJNI.RADIO_LED_STATE_GREEN),
329    /** Red. */
330    kRed(LEDJNI.RADIO_LED_STATE_RED),
331    /** Orange. */
332    kOrange(LEDJNI.RADIO_LED_STATE_ORANGE);
333
334    /** The native value for this state. */
335    public final int value;
336
337    RadioLEDState(int value) {
338      this.value = value;
339    }
340
341    /**
342     * Gets a state from an int value.
343     *
344     * @param value int value
345     * @return state
346     */
347    public static RadioLEDState fromValue(int value) {
348      switch (value) {
349        case LEDJNI.RADIO_LED_STATE_OFF:
350          return RadioLEDState.kOff;
351        case LEDJNI.RADIO_LED_STATE_GREEN:
352          return RadioLEDState.kGreen;
353        case LEDJNI.RADIO_LED_STATE_RED:
354          return RadioLEDState.kRed;
355        case LEDJNI.RADIO_LED_STATE_ORANGE:
356          return RadioLEDState.kOrange;
357        default:
358          return RadioLEDState.kOff;
359      }
360    }
361  }
362
363  /**
364   * Set the state of the "Radio" LED. On the RoboRIO, this writes to sysfs, so this function should
365   * not be called multiple times per loop cycle to avoid overruns.
366   *
367   * @param state The state to set the LED to.
368   */
369  public static void setRadioLEDState(RadioLEDState state) {
370    LEDJNI.setRadioLEDState(state.value);
371  }
372
373  /**
374   * Get the state of the "Radio" LED. On the RoboRIO, this reads from sysfs, so this function
375   * should not be called multiple times per loop cycle to avoid overruns.
376   *
377   * @return The state of the LED.
378   */
379  public static RadioLEDState getRadioLEDState() {
380    return RadioLEDState.fromValue(LEDJNI.getRadioLEDState());
381  }
382
383  /**
384   * Get the current status of the CAN bus.
385   *
386   * @return The status of the CAN bus
387   */
388  public static CANStatus getCANStatus() {
389    CANStatus status = new CANStatus();
390    CANJNI.getCANStatus(status);
391    return status;
392  }
393}