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  public static int getFPGAVersion() {
025    return HALUtil.getFPGAVersion();
026  }
027
028  /**
029   * Return the FPGA Revision number. The format of the revision is 3 numbers. The 12 most
030   * significant bits are the Major Revision. the next 8 bits are the Minor Revision. The 12 least
031   * significant bits are the Build Number.
032   *
033   * @return FPGA Revision number.
034   */
035  public static long getFPGARevision() {
036    return (long) HALUtil.getFPGARevision();
037  }
038
039  /**
040   * Return the serial number of the roboRIO.
041   *
042   * @return The serial number of the roboRIO.
043   */
044  public static String getSerialNumber() {
045    return HALUtil.getSerialNumber();
046  }
047
048  /**
049   * Return the comments from the roboRIO web interface.
050   *
051   * <p>The comments string is cached after the first call to this function on the RoboRIO - restart
052   * the robot code to reload the comments string after changing it in the web interface.
053   *
054   * @return the comments from the roboRIO web interface.
055   */
056  public static String getComments() {
057    return HALUtil.getComments();
058  }
059
060  /**
061   * Read the microsecond timer from the FPGA.
062   *
063   * @return The current time in microseconds according to the FPGA.
064   */
065  public static long getFPGATime() {
066    return HALUtil.getFPGATime();
067  }
068
069  /**
070   * Get the state of the "USER" button on the roboRIO.
071   *
072   * @return true if the button is currently pressed down
073   */
074  public static boolean getUserButton() {
075    return HALUtil.getFPGAButton();
076  }
077
078  /**
079   * Read the battery voltage.
080   *
081   * @return The battery voltage in Volts.
082   */
083  public static double getBatteryVoltage() {
084    return PowerJNI.getVinVoltage();
085  }
086
087  /**
088   * Gets a value indicating whether the FPGA outputs are enabled. The outputs may be disabled if
089   * the robot is disabled or e-stopped, the watchdog has expired, or if the roboRIO browns out.
090   *
091   * @return True if the FPGA outputs are enabled.
092   */
093  public static boolean isSysActive() {
094    return HAL.getSystemActive();
095  }
096
097  /**
098   * Check if the system is browned out.
099   *
100   * @return True if the system is browned out
101   */
102  public static boolean isBrownedOut() {
103    return HAL.getBrownedOut();
104  }
105
106  /**
107   * Get the input voltage to the robot controller.
108   *
109   * @return The controller input voltage value in Volts
110   */
111  public static double getInputVoltage() {
112    return PowerJNI.getVinVoltage();
113  }
114
115  /**
116   * Get the input current to the robot controller.
117   *
118   * @return The controller input current value in Amps
119   */
120  public static double getInputCurrent() {
121    return PowerJNI.getVinCurrent();
122  }
123
124  /**
125   * Get the voltage of the 3.3V rail.
126   *
127   * @return The controller 3.3V rail voltage value in Volts
128   */
129  public static double getVoltage3V3() {
130    return PowerJNI.getUserVoltage3V3();
131  }
132
133  /**
134   * Get the current output of the 3.3V rail.
135   *
136   * @return The controller 3.3V rail output current value in Amps
137   */
138  public static double getCurrent3V3() {
139    return PowerJNI.getUserCurrent3V3();
140  }
141
142  /**
143   * Get the enabled state of the 3.3V rail. The rail may be disabled due to a controller brownout,
144   * a short circuit on the rail, or controller over-voltage.
145   *
146   * @return The controller 3.3V rail enabled value
147   */
148  public static boolean getEnabled3V3() {
149    return PowerJNI.getUserActive3V3();
150  }
151
152  /**
153   * Get the count of the total current faults on the 3.3V rail since the controller has booted.
154   *
155   * @return The number of faults
156   */
157  public static int getFaultCount3V3() {
158    return PowerJNI.getUserCurrentFaults3V3();
159  }
160
161  /**
162   * Get the voltage of the 5V rail.
163   *
164   * @return The controller 5V rail voltage value in Volts
165   */
166  public static double getVoltage5V() {
167    return PowerJNI.getUserVoltage5V();
168  }
169
170  /**
171   * Get the current output of the 5V rail.
172   *
173   * @return The controller 5V rail output current value in Amps
174   */
175  public static double getCurrent5V() {
176    return PowerJNI.getUserCurrent5V();
177  }
178
179  /**
180   * Get the enabled state of the 5V rail. The rail may be disabled due to a controller brownout, a
181   * short circuit on the rail, or controller over-voltage.
182   *
183   * @return The controller 5V rail enabled value
184   */
185  public static boolean getEnabled5V() {
186    return PowerJNI.getUserActive5V();
187  }
188
189  /**
190   * Get the count of the total current faults on the 5V rail since the controller has booted.
191   *
192   * @return The number of faults
193   */
194  public static int getFaultCount5V() {
195    return PowerJNI.getUserCurrentFaults5V();
196  }
197
198  /**
199   * Get the voltage of the 6V rail.
200   *
201   * @return The controller 6V rail voltage value in Volts
202   */
203  public static double getVoltage6V() {
204    return PowerJNI.getUserVoltage6V();
205  }
206
207  /**
208   * Get the current output of the 6V rail.
209   *
210   * @return The controller 6V rail output current value in Amps
211   */
212  public static double getCurrent6V() {
213    return PowerJNI.getUserCurrent6V();
214  }
215
216  /**
217   * Get the enabled state of the 6V rail. The rail may be disabled due to a controller brownout, a
218   * short circuit on the rail, or controller over-voltage.
219   *
220   * @return The controller 6V rail enabled value
221   */
222  public static boolean getEnabled6V() {
223    return PowerJNI.getUserActive6V();
224  }
225
226  /**
227   * Get the count of the total current faults on the 6V rail since the controller has booted.
228   *
229   * @return The number of faults
230   */
231  public static int getFaultCount6V() {
232    return PowerJNI.getUserCurrentFaults6V();
233  }
234
235  /**
236   * Get the current brownout voltage setting.
237   *
238   * @return The brownout voltage
239   */
240  public static double getBrownoutVoltage() {
241    return PowerJNI.getBrownoutVoltage();
242  }
243
244  /**
245   * Set the voltage the roboRIO will brownout and disable all outputs.
246   *
247   * <p>Note that this only does anything on the roboRIO 2. On the roboRIO it is a no-op.
248   *
249   * @param brownoutVoltage The brownout voltage
250   */
251  public static void setBrownoutVoltage(double brownoutVoltage) {
252    PowerJNI.setBrownoutVoltage(brownoutVoltage);
253  }
254
255  /**
256   * Get the current status of the CAN bus.
257   *
258   * @return The status of the CAN bus
259   */
260  public static CANStatus getCANStatus() {
261    CANStatus status = new CANStatus();
262    CANJNI.getCANStatus(status);
263    return status;
264  }
265}