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.PowerJNI;
010import edu.wpi.first.hal.can.CANJNI;
011import edu.wpi.first.hal.can.CANStatus;
012
013/** Contains functions for roboRIO functionality. */
014public final class RobotController {
015  private RobotController() {
016    throw new UnsupportedOperationException("This is a utility class!");
017  }
018
019  /**
020   * Return the FPGA Version number. For now, expect this to be the current year.
021   *
022   * @return FPGA Version number.
023   */
024  @SuppressWarnings("AbbreviationAsWordInName")
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  @SuppressWarnings("AbbreviationAsWordInName")
037  public static long getFPGARevision() {
038    return (long) HALUtil.getFPGARevision();
039  }
040
041  /**
042   * Read the microsecond timer from the FPGA.
043   *
044   * @return The current time in microseconds according to the FPGA.
045   */
046  public static long getFPGATime() {
047    return HALUtil.getFPGATime();
048  }
049
050  /**
051   * Get the state of the "USER" button on the roboRIO.
052   *
053   * @return true if the button is currently pressed down
054   */
055  public static boolean getUserButton() {
056    return HALUtil.getFPGAButton();
057  }
058
059  /**
060   * Read the battery voltage.
061   *
062   * @return The battery voltage in Volts.
063   */
064  public static double getBatteryVoltage() {
065    return PowerJNI.getVinVoltage();
066  }
067
068  /**
069   * Gets a value indicating whether the FPGA outputs are enabled. The outputs may be disabled if
070   * the robot is disabled or e-stopped, the watchdog has expired, or if the roboRIO browns out.
071   *
072   * @return True if the FPGA outputs are enabled.
073   */
074  public static boolean isSysActive() {
075    return HAL.getSystemActive();
076  }
077
078  /**
079   * Check if the system is browned out.
080   *
081   * @return True if the system is browned out
082   */
083  public static boolean isBrownedOut() {
084    return HAL.getBrownedOut();
085  }
086
087  /**
088   * Get the input voltage to the robot controller.
089   *
090   * @return The controller input voltage value in Volts
091   */
092  public static double getInputVoltage() {
093    return PowerJNI.getVinVoltage();
094  }
095
096  /**
097   * Get the input current to the robot controller.
098   *
099   * @return The controller input current value in Amps
100   */
101  public static double getInputCurrent() {
102    return PowerJNI.getVinCurrent();
103  }
104
105  /**
106   * Get the voltage of the 3.3V rail.
107   *
108   * @return The controller 3.3V rail voltage value in Volts
109   */
110  public static double getVoltage3V3() {
111    return PowerJNI.getUserVoltage3V3();
112  }
113
114  /**
115   * Get the current output of the 3.3V rail.
116   *
117   * @return The controller 3.3V rail output current value in Amps
118   */
119  public static double getCurrent3V3() {
120    return PowerJNI.getUserCurrent3V3();
121  }
122
123  /**
124   * Get the enabled state of the 3.3V rail. The rail may be disabled due to a controller brownout,
125   * a short circuit on the rail, or controller over-voltage.
126   *
127   * @return The controller 3.3V rail enabled value
128   */
129  public static boolean getEnabled3V3() {
130    return PowerJNI.getUserActive3V3();
131  }
132
133  /**
134   * Get the count of the total current faults on the 3.3V rail since the controller has booted.
135   *
136   * @return The number of faults
137   */
138  public static int getFaultCount3V3() {
139    return PowerJNI.getUserCurrentFaults3V3();
140  }
141
142  /**
143   * Get the voltage of the 5V rail.
144   *
145   * @return The controller 5V rail voltage value in Volts
146   */
147  public static double getVoltage5V() {
148    return PowerJNI.getUserVoltage5V();
149  }
150
151  /**
152   * Get the current output of the 5V rail.
153   *
154   * @return The controller 5V rail output current value in Amps
155   */
156  public static double getCurrent5V() {
157    return PowerJNI.getUserCurrent5V();
158  }
159
160  /**
161   * Get the enabled state of the 5V rail. The rail may be disabled due to a controller brownout, a
162   * short circuit on the rail, or controller over-voltage.
163   *
164   * @return The controller 5V rail enabled value
165   */
166  public static boolean getEnabled5V() {
167    return PowerJNI.getUserActive5V();
168  }
169
170  /**
171   * Get the count of the total current faults on the 5V rail since the controller has booted.
172   *
173   * @return The number of faults
174   */
175  public static int getFaultCount5V() {
176    return PowerJNI.getUserCurrentFaults5V();
177  }
178
179  /**
180   * Get the voltage of the 6V rail.
181   *
182   * @return The controller 6V rail voltage value in Volts
183   */
184  public static double getVoltage6V() {
185    return PowerJNI.getUserVoltage6V();
186  }
187
188  /**
189   * Get the current output of the 6V rail.
190   *
191   * @return The controller 6V rail output current value in Amps
192   */
193  public static double getCurrent6V() {
194    return PowerJNI.getUserCurrent6V();
195  }
196
197  /**
198   * Get the enabled state of the 6V rail. The rail may be disabled due to a controller brownout, a
199   * short circuit on the rail, or controller over-voltage.
200   *
201   * @return The controller 6V rail enabled value
202   */
203  public static boolean getEnabled6V() {
204    return PowerJNI.getUserActive6V();
205  }
206
207  /**
208   * Get the count of the total current faults on the 6V rail since the controller has booted.
209   *
210   * @return The number of faults
211   */
212  public static int getFaultCount6V() {
213    return PowerJNI.getUserCurrentFaults6V();
214  }
215
216  /**
217   * Get the current brownout voltage setting.
218   *
219   * @return The brownout voltage
220   */
221  public static double getBrownoutVoltage() {
222    return PowerJNI.getBrownoutVoltage();
223  }
224
225  /**
226   * Set the voltage the roboRIO will brownout and disable all outputs.
227   *
228   * <p>Note that this only does anything on the roboRIO 2. On the roboRIO it is a no-op.
229   *
230   * @param brownoutVoltage The brownout voltage
231   */
232  public static void setBrownoutVoltage(double brownoutVoltage) {
233    PowerJNI.setBrownoutVoltage(brownoutVoltage);
234  }
235
236  /**
237   * Get the current status of the CAN bus.
238   *
239   * @return The status of the CAN bus
240   */
241  public static CANStatus getCANStatus() {
242    CANStatus status = new CANStatus();
243    CANJNI.getCANStatus(status);
244    return status;
245  }
246}