mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-27 02:01:42 +00:00
Switches Java to use HAL Constants (#145)
This commit is contained in:
committed by
Peter Johnson
parent
4a3e3a6324
commit
be2647d44e
90
wpilibj/src/athena/java/edu/wpi/first/wpilibj/GyroBase.java
Normal file
90
wpilibj/src/athena/java/edu/wpi/first/wpilibj/GyroBase.java
Normal file
@@ -0,0 +1,90 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2008-2016. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
package edu.wpi.first.wpilibj;
|
||||
|
||||
import edu.wpi.first.wpilibj.interfaces.Gyro;
|
||||
import edu.wpi.first.wpilibj.livewindow.LiveWindowSendable;
|
||||
import edu.wpi.first.wpilibj.tables.ITable;
|
||||
|
||||
/**
|
||||
* GyroBase is the common base class for Gyro implementations such as AnalogGyro.
|
||||
*/
|
||||
public abstract class GyroBase extends SensorBase implements Gyro, PIDSource, LiveWindowSendable {
|
||||
private PIDSourceType m_pidSource = PIDSourceType.kDisplacement;
|
||||
|
||||
/**
|
||||
* Set which parameter of the gyro you are using as a process control variable. The Gyro class
|
||||
* supports the rate and displacement parameters
|
||||
*
|
||||
* @param pidSource An enum to select the parameter.
|
||||
*/
|
||||
@Override
|
||||
public void setPIDSourceType(PIDSourceType pidSource) {
|
||||
m_pidSource = pidSource;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PIDSourceType getPIDSourceType() {
|
||||
return m_pidSource;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the output of the gyro for use with PIDControllers. May be the angle or rate depending on
|
||||
* the set PIDSourceType
|
||||
*
|
||||
* @return the output according to the gyro
|
||||
*/
|
||||
@Override
|
||||
public double pidGet() {
|
||||
switch (m_pidSource) {
|
||||
case kRate:
|
||||
return getRate();
|
||||
case kDisplacement:
|
||||
return getAngle();
|
||||
default:
|
||||
return 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Live Window code, only does anything if live window is activated.
|
||||
*/
|
||||
@Override
|
||||
public String getSmartDashboardType() {
|
||||
return "Gyro";
|
||||
}
|
||||
|
||||
private ITable m_table;
|
||||
|
||||
|
||||
@Override
|
||||
public void initTable(ITable subtable) {
|
||||
m_table = subtable;
|
||||
updateTable();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ITable getTable() {
|
||||
return m_table;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateTable() {
|
||||
if (m_table != null) {
|
||||
m_table.putNumber("Value", getAngle());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startLiveWindowMode() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stopLiveWindowMode() {
|
||||
}
|
||||
}
|
||||
195
wpilibj/src/athena/java/edu/wpi/first/wpilibj/SensorBase.java
Normal file
195
wpilibj/src/athena/java/edu/wpi/first/wpilibj/SensorBase.java
Normal file
@@ -0,0 +1,195 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2008-2016. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
package edu.wpi.first.wpilibj;
|
||||
|
||||
import edu.wpi.first.wpilibj.hal.ConstantsJNI;
|
||||
import edu.wpi.first.wpilibj.hal.PortsJNI;
|
||||
|
||||
/**
|
||||
* Base class for all sensors. Stores most recent status information as well as containing utility
|
||||
* functions for checking channels and error processing.
|
||||
*/
|
||||
public abstract class SensorBase { // TODO: Refactor
|
||||
/**
|
||||
* Ticks per microsecond.
|
||||
*/
|
||||
public static final int kSystemClockTicksPerMicrosecond =
|
||||
ConstantsJNI.getSystemClockTicksPerMicrosecond();
|
||||
/**
|
||||
* Number of digital channels per roboRIO.
|
||||
*/
|
||||
public static final int kDigitalChannels = PortsJNI.getNumDigitalPins();
|
||||
/**
|
||||
* Number of analog input channels.
|
||||
*/
|
||||
public static final int kAnalogInputChannels = PortsJNI.getNumAnalogInputs();
|
||||
/**
|
||||
* Number of analog output channels.
|
||||
*/
|
||||
public static final int kAnalogOutputChannels = PortsJNI.getNumAnalogOutputs();
|
||||
/**
|
||||
* Number of solenoid channels per module.
|
||||
*/
|
||||
public static final int kSolenoidChannels = PortsJNI.getNumSolenoidPins();
|
||||
/**
|
||||
* Number of PWM channels per roboRIO.
|
||||
*/
|
||||
public static final int kPwmChannels = PortsJNI.getNumPWMPins();
|
||||
/**
|
||||
* Number of relay channels per roboRIO.
|
||||
*/
|
||||
public static final int kRelayChannels = PortsJNI.getNumRelayPins();
|
||||
/**
|
||||
* Number of power distribution channels.
|
||||
*/
|
||||
public static final int kPDPChannels = PortsJNI.getNumPDPChannels();
|
||||
/**
|
||||
* Number of power distribution modules.
|
||||
*/
|
||||
public static final int kPDPModules = PortsJNI.getNumPDPModules();
|
||||
/**
|
||||
* Number of PCM Modules.
|
||||
*/
|
||||
public static final int kPCMModules = PortsJNI.getNumPCMModules();
|
||||
|
||||
private static int m_defaultSolenoidModule = 0;
|
||||
|
||||
/**
|
||||
* Creates an instance of the sensor base and gets an FPGA handle.
|
||||
*/
|
||||
public SensorBase() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the default location for the Solenoid module.
|
||||
*
|
||||
* @param moduleNumber The number of the solenoid module to use.
|
||||
*/
|
||||
public static void setDefaultSolenoidModule(final int moduleNumber) {
|
||||
checkSolenoidModule(moduleNumber);
|
||||
SensorBase.m_defaultSolenoidModule = moduleNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that the solenoid module is correct.
|
||||
*
|
||||
* @param moduleNumber The solenoid module module number to check.
|
||||
*/
|
||||
protected static void checkSolenoidModule(final int moduleNumber) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that the digital channel number is valid. Verify that the channel number is one of the
|
||||
* legal channel numbers. Channel numbers are 1-based.
|
||||
*
|
||||
* @param channel The channel number to check.
|
||||
*/
|
||||
protected static void checkDigitalChannel(final int channel) {
|
||||
if (channel < 0 || channel >= kDigitalChannels) {
|
||||
throw new IndexOutOfBoundsException("Requested digital channel number is out of range.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that the digital channel number is valid. Verify that the channel number is one of the
|
||||
* legal channel numbers. Channel numbers are 1-based.
|
||||
*
|
||||
* @param channel The channel number to check.
|
||||
*/
|
||||
protected static void checkRelayChannel(final int channel) {
|
||||
if (channel < 0 || channel >= kRelayChannels) {
|
||||
throw new IndexOutOfBoundsException("Requested relay channel number is out of range.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that the digital channel number is valid. Verify that the channel number is one of the
|
||||
* legal channel numbers. Channel numbers are 1-based.
|
||||
*
|
||||
* @param channel The channel number to check.
|
||||
*/
|
||||
protected static void checkPWMChannel(final int channel) {
|
||||
if (channel < 0 || channel >= kPwmChannels) {
|
||||
throw new IndexOutOfBoundsException("Requested PWM channel number is out of range.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that the analog input number is value. Verify that the analog input number is one of the
|
||||
* legal channel numbers. Channel numbers are 0-based.
|
||||
*
|
||||
* @param channel The channel number to check.
|
||||
*/
|
||||
protected static void checkAnalogInputChannel(final int channel) {
|
||||
if (channel < 0 || channel >= kAnalogInputChannels) {
|
||||
throw new IndexOutOfBoundsException("Requested analog input channel number is out of range.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that the analog input number is value. Verify that the analog input number is one of the
|
||||
* legal channel numbers. Channel numbers are 0-based.
|
||||
*
|
||||
* @param channel The channel number to check.
|
||||
*/
|
||||
protected static void checkAnalogOutputChannel(final int channel) {
|
||||
if (channel < 0 || channel >= kAnalogOutputChannels) {
|
||||
throw new IndexOutOfBoundsException(
|
||||
"Requested analog output channel number is out of range.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that the solenoid channel number is within limits. Channel numbers are 1-based.
|
||||
*
|
||||
* @param channel The channel number to check.
|
||||
*/
|
||||
protected static void checkSolenoidChannel(final int channel) {
|
||||
if (channel < 0 || channel >= kSolenoidChannels) {
|
||||
throw new IndexOutOfBoundsException("Requested solenoid channel number is out of range.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that the power distribution channel number is within limits. Channel numbers are
|
||||
* 1-based.
|
||||
*
|
||||
* @param channel The channel number to check.
|
||||
*/
|
||||
protected static void checkPDPChannel(final int channel) {
|
||||
if (channel < 0 || channel >= kPDPChannels) {
|
||||
throw new IndexOutOfBoundsException("Requested PDP channel number is out of range.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that the PDP module number is within limits. module numbers are 0-based.
|
||||
*
|
||||
* @param module The module number to check.
|
||||
*/
|
||||
protected static void checkPDPModule(final int module) {
|
||||
if (module < 0 || module > kPDPModules) {
|
||||
throw new IndexOutOfBoundsException("Requested PDP module number is out of range.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of the default solenoid module.
|
||||
*
|
||||
* @return The number of the default solenoid module.
|
||||
*/
|
||||
public static int getDefaultSolenoidModule() {
|
||||
return SensorBase.m_defaultSolenoidModule;
|
||||
}
|
||||
|
||||
/**
|
||||
* Free the resources used by this object.
|
||||
*/
|
||||
public void free() {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2016. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
package edu.wpi.first.wpilibj.hal;
|
||||
|
||||
public class ConstantsJNI extends JNIWrapper {
|
||||
public static native int getSystemClockTicksPerMicrosecond();
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2016. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
package edu.wpi.first.wpilibj.hal;
|
||||
|
||||
public class PortsJNI extends JNIWrapper {
|
||||
public static native int getNumAccumulators();
|
||||
|
||||
public static native int getNumAnalogTriggers();
|
||||
|
||||
public static native int getNumAnalogInputs();
|
||||
|
||||
public static native int getNumAnalogOutputs();
|
||||
|
||||
public static native int getNumCounters();
|
||||
|
||||
public static native int getNumDigitalHeaders();
|
||||
|
||||
public static native int getNumPWMHeaders();
|
||||
|
||||
public static native int getNumDigitalPins();
|
||||
|
||||
public static native int getNumPWMPins();
|
||||
|
||||
public static native int getNumDigitalPWMOutputs();
|
||||
|
||||
public static native int getNumEncoders();
|
||||
|
||||
public static native int getNumInterrupts();
|
||||
|
||||
public static native int getNumRelayPins();
|
||||
|
||||
public static native int getNumRelayHeaders();
|
||||
|
||||
public static native int getNumPCMModules();
|
||||
|
||||
public static native int getNumSolenoidPins();
|
||||
|
||||
public static native int getNumPDPModules();
|
||||
|
||||
public static native int getNumPDPChannels();
|
||||
|
||||
public static native int getNumCanTalons();
|
||||
}
|
||||
Reference in New Issue
Block a user