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.AccelerometerJNI;
008import edu.wpi.first.hal.FRCNetComm.tResourceType;
009import edu.wpi.first.hal.HAL;
010import edu.wpi.first.util.sendable.Sendable;
011import edu.wpi.first.util.sendable.SendableBuilder;
012import edu.wpi.first.util.sendable.SendableRegistry;
013import edu.wpi.first.wpilibj.interfaces.Accelerometer;
014
015/**
016 * Built-in accelerometer.
017 *
018 * <p>This class allows access to the roboRIO's internal accelerometer.
019 */
020public class BuiltInAccelerometer implements Accelerometer, Sendable, AutoCloseable {
021  /**
022   * Constructor.
023   *
024   * @param range The range the accelerometer will measure
025   */
026  public BuiltInAccelerometer(Range range) {
027    setRange(range);
028    HAL.report(tResourceType.kResourceType_Accelerometer, 0, 0, "Built-in accelerometer");
029    SendableRegistry.addLW(this, "BuiltInAccel", 0);
030  }
031
032  /** Constructor. The accelerometer will measure +/-8 g-forces */
033  public BuiltInAccelerometer() {
034    this(Range.k8G);
035  }
036
037  @Override
038  public void close() {
039    SendableRegistry.remove(this);
040  }
041
042  @Override
043  public void setRange(Range range) {
044    AccelerometerJNI.setAccelerometerActive(false);
045
046    switch (range) {
047      case k2G:
048        AccelerometerJNI.setAccelerometerRange(0);
049        break;
050      case k4G:
051        AccelerometerJNI.setAccelerometerRange(1);
052        break;
053      case k8G:
054        AccelerometerJNI.setAccelerometerRange(2);
055        break;
056      case k16G:
057      default:
058        throw new IllegalArgumentException(range + " range not supported (use k2G, k4G, or k8G)");
059    }
060
061    AccelerometerJNI.setAccelerometerActive(true);
062  }
063
064  /**
065   * The acceleration in the X axis.
066   *
067   * @return The acceleration of the roboRIO along the X axis in g-forces
068   */
069  @Override
070  public double getX() {
071    return AccelerometerJNI.getAccelerometerX();
072  }
073
074  /**
075   * The acceleration in the Y axis.
076   *
077   * @return The acceleration of the roboRIO along the Y axis in g-forces
078   */
079  @Override
080  public double getY() {
081    return AccelerometerJNI.getAccelerometerY();
082  }
083
084  /**
085   * The acceleration in the Z axis.
086   *
087   * @return The acceleration of the roboRIO along the Z axis in g-forces
088   */
089  @Override
090  public double getZ() {
091    return AccelerometerJNI.getAccelerometerZ();
092  }
093
094  @Override
095  public void initSendable(SendableBuilder builder) {
096    builder.setSmartDashboardType("3AxisAccelerometer");
097    builder.addDoubleProperty("X", this::getX, null);
098    builder.addDoubleProperty("Y", this::getY, null);
099    builder.addDoubleProperty("Z", this::getZ, null);
100  }
101}