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