mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-28 02:11:43 +00:00
SCRIPT Move java files
This commit is contained in:
committed by
Peter Johnson
parent
7ca1be9bae
commit
c350c5f112
@@ -0,0 +1,213 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
package edu.wpi.first.wpilibj;
|
||||
|
||||
import edu.wpi.first.hal.AnalogJNI;
|
||||
import edu.wpi.first.hal.HAL;
|
||||
import edu.wpi.first.hal.SimDevice;
|
||||
import edu.wpi.first.util.sendable.Sendable;
|
||||
import edu.wpi.first.util.sendable.SendableBuilder;
|
||||
import edu.wpi.first.util.sendable.SendableRegistry;
|
||||
|
||||
/**
|
||||
* Analog channel class.
|
||||
*
|
||||
* <p>Each analog channel is read from hardware as a 12-bit number representing 0V to 3.3V.
|
||||
*
|
||||
* <p>Connected to each analog channel is an averaging and oversampling engine. This engine
|
||||
* accumulates the specified ( by setAverageBits() and setOversampleBits() ) number of samples
|
||||
* before returning a new value. This is not a sliding window average. The only difference between
|
||||
* the oversampled samples and the averaged samples is that the oversampled samples are simply
|
||||
* accumulated effectively increasing the resolution, while the averaged samples are divided by the
|
||||
* number of samples to retain the resolution, but get more stable values.
|
||||
*/
|
||||
public class AnalogInput implements Sendable, AutoCloseable {
|
||||
int m_port; // explicit no modifier, private and package accessible.
|
||||
private int m_channel;
|
||||
|
||||
/**
|
||||
* Construct an analog channel.
|
||||
*
|
||||
* @param channel The channel number to represent. 0-3 are on-board 4-7 are on the MXP port.
|
||||
*/
|
||||
@SuppressWarnings("this-escape")
|
||||
public AnalogInput(final int channel) {
|
||||
AnalogJNI.checkAnalogInputChannel(channel);
|
||||
m_channel = channel;
|
||||
|
||||
m_port = AnalogJNI.initializeAnalogInputPort(channel);
|
||||
|
||||
HAL.reportUsage("IO", channel, "AnalogInput");
|
||||
SendableRegistry.add(this, "AnalogInput", channel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
SendableRegistry.remove(this);
|
||||
AnalogJNI.freeAnalogInputPort(m_port);
|
||||
m_port = 0;
|
||||
m_channel = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a sample straight from this channel. The sample is a 12-bit value representing the 0V to
|
||||
* 3.3V range of the A/D converter. The units are in A/D converter codes. Use GetVoltage() to get
|
||||
* the analog value in calibrated units.
|
||||
*
|
||||
* @return A sample straight from this channel.
|
||||
*/
|
||||
public int getValue() {
|
||||
return AnalogJNI.getAnalogValue(m_port);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a sample from the output of the oversample and average engine for this channel. The sample
|
||||
* is 12-bit + the bits configured in SetOversampleBits(). The value configured in
|
||||
* setAverageBits() will cause this value to be averaged 2^bits number of samples. This is not a
|
||||
* sliding window. The sample will not change until 2^(OversampleBits + AverageBits) samples have
|
||||
* been acquired from this channel. Use getAverageVoltage() to get the analog value in calibrated
|
||||
* units.
|
||||
*
|
||||
* @return A sample from the oversample and average engine for this channel.
|
||||
*/
|
||||
public int getAverageValue() {
|
||||
return AnalogJNI.getAnalogAverageValue(m_port);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a scaled sample straight from this channel. The value is scaled to units of Volts using the
|
||||
* calibrated scaling data from getLSBWeight() and getOffset().
|
||||
*
|
||||
* @return A scaled sample straight from this channel.
|
||||
*/
|
||||
public double getVoltage() {
|
||||
return AnalogJNI.getAnalogVoltage(m_port);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a scaled sample from the output of the oversample and average engine for this channel. The
|
||||
* value is scaled to units of Volts using the calibrated scaling data from getLSBWeight() and
|
||||
* getOffset(). Using oversampling will cause this value to be higher resolution, but it will
|
||||
* update more slowly. Using averaging will cause this value to be more stable, but it will update
|
||||
* more slowly.
|
||||
*
|
||||
* @return A scaled sample from the output of the oversample and average engine for this channel.
|
||||
*/
|
||||
public double getAverageVoltage() {
|
||||
return AnalogJNI.getAnalogAverageVoltage(m_port);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the factory scaling the least significant bit weight constant. The least significant bit
|
||||
* weight constant for the channel that was calibrated in manufacturing and stored in an eeprom.
|
||||
*
|
||||
* <p>Volts = ((LSB_Weight * 1e-9) * raw) - (Offset * 1e-9)
|
||||
*
|
||||
* @return Least significant bit weight.
|
||||
*/
|
||||
public long getLSBWeight() {
|
||||
return AnalogJNI.getAnalogLSBWeight(m_port);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the factory scaling offset constant. The offset constant for the channel that was
|
||||
* calibrated in manufacturing and stored in an eeprom.
|
||||
*
|
||||
* <p>Volts = ((LSB_Weight * 1e-9) * raw) - (Offset * 1e-9)
|
||||
*
|
||||
* @return Offset constant.
|
||||
*/
|
||||
public int getOffset() {
|
||||
return AnalogJNI.getAnalogOffset(m_port);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the channel number.
|
||||
*
|
||||
* @return The channel number.
|
||||
*/
|
||||
public int getChannel() {
|
||||
return m_channel;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the number of averaging bits. This sets the number of averaging bits. The actual number of
|
||||
* averaged samples is 2^bits. The averaging is done automatically in the FPGA.
|
||||
*
|
||||
* @param bits The number of averaging bits.
|
||||
*/
|
||||
public void setAverageBits(final int bits) {
|
||||
AnalogJNI.setAnalogAverageBits(m_port, bits);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of averaging bits. This gets the number of averaging bits from the FPGA. The
|
||||
* actual number of averaged samples is 2^bits. The averaging is done automatically in the FPGA.
|
||||
*
|
||||
* @return The number of averaging bits.
|
||||
*/
|
||||
public int getAverageBits() {
|
||||
return AnalogJNI.getAnalogAverageBits(m_port);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the number of oversample bits. This sets the number of oversample bits. The actual number
|
||||
* of oversampled values is 2^bits. The oversampling is done automatically in the FPGA.
|
||||
*
|
||||
* @param bits The number of oversample bits.
|
||||
*/
|
||||
public void setOversampleBits(final int bits) {
|
||||
AnalogJNI.setAnalogOversampleBits(m_port, bits);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of oversample bits. This gets the number of oversample bits from the FPGA. The
|
||||
* actual number of oversampled values is 2^bits. The oversampling is done automatically in the
|
||||
* FPGA.
|
||||
*
|
||||
* @return The number of oversample bits.
|
||||
*/
|
||||
public int getOversampleBits() {
|
||||
return AnalogJNI.getAnalogOversampleBits(m_port);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the sample rate per channel.
|
||||
*
|
||||
* <p>This is a global setting for all channels. The maximum rate is 500kS/s divided by the number
|
||||
* of channels in use. This is 62500 samples/s per channel if all 8 channels are used.
|
||||
*
|
||||
* @param samplesPerSecond The number of samples per second.
|
||||
*/
|
||||
public static void setGlobalSampleRate(final double samplesPerSecond) {
|
||||
AnalogJNI.setAnalogSampleRate(samplesPerSecond);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current sample rate.
|
||||
*
|
||||
* <p>This assumes one entry in the scan list. This is a global setting for all channels.
|
||||
*
|
||||
* @return Sample rate.
|
||||
*/
|
||||
public static double getGlobalSampleRate() {
|
||||
return AnalogJNI.getAnalogSampleRate();
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates this input is used by a simulated device.
|
||||
*
|
||||
* @param device simulated device handle
|
||||
*/
|
||||
public void setSimDevice(SimDevice device) {
|
||||
AnalogJNI.setAnalogInputSimDevice(m_port, device.getNativeHandle());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initSendable(SendableBuilder builder) {
|
||||
builder.setSmartDashboardType("Analog Input");
|
||||
builder.addDoubleProperty("Value", this::getAverageVoltage, null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
package edu.wpi.first.wpilibj;
|
||||
|
||||
/**
|
||||
* Interface for counting the number of ticks on a digital input channel. Encoders, Gear tooth
|
||||
* sensors, and counters should all subclass this in order to build more advanced classes for
|
||||
* control and driving.
|
||||
*
|
||||
* <p>All counters will immediately start counting - reset() them if you need them to be zeroed
|
||||
* before use.
|
||||
*/
|
||||
public interface CounterBase {
|
||||
/** The number of edges for the CounterBase to increment or decrement on. */
|
||||
enum EncodingType {
|
||||
/** Count only the rising edge. */
|
||||
k1X(0),
|
||||
/** Count both the rising and falling edge. */
|
||||
k2X(1),
|
||||
/** Count rising and falling on both channels. */
|
||||
k4X(2);
|
||||
|
||||
/** EncodingType value. */
|
||||
public final int value;
|
||||
|
||||
EncodingType(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the count.
|
||||
*
|
||||
* @return the count
|
||||
*/
|
||||
int get();
|
||||
|
||||
/** Reset the count to zero. */
|
||||
void reset();
|
||||
|
||||
/**
|
||||
* Get the time between the last two edges counted.
|
||||
*
|
||||
* @return the time between the last two ticks in seconds
|
||||
*/
|
||||
double getPeriod();
|
||||
|
||||
/**
|
||||
* Set the maximum time between edges to be considered stalled.
|
||||
*
|
||||
* @param maxPeriod the maximum period in seconds
|
||||
*/
|
||||
void setMaxPeriod(double maxPeriod);
|
||||
|
||||
/**
|
||||
* Determine if the counter is not moving.
|
||||
*
|
||||
* @return true if the counter has not changed for the max period
|
||||
*/
|
||||
boolean getStopped();
|
||||
|
||||
/**
|
||||
* Determine which direction the counter is going.
|
||||
*
|
||||
* @return true for one direction, false for the other
|
||||
*/
|
||||
boolean getDirection();
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
package edu.wpi.first.wpilibj;
|
||||
|
||||
import edu.wpi.first.hal.DIOJNI;
|
||||
import edu.wpi.first.hal.HAL;
|
||||
import edu.wpi.first.hal.SimDevice;
|
||||
import edu.wpi.first.util.sendable.Sendable;
|
||||
import edu.wpi.first.util.sendable.SendableBuilder;
|
||||
import edu.wpi.first.util.sendable.SendableRegistry;
|
||||
|
||||
/**
|
||||
* Class to read a digital input. This class will read digital inputs and return the current value
|
||||
* on the channel. Other devices such as encoders, gear tooth sensors, etc. that are implemented
|
||||
* elsewhere will automatically allocate digital inputs and outputs as required. This class is only
|
||||
* for devices like switches etc. that aren't implemented anywhere else.
|
||||
*/
|
||||
public class DigitalInput implements AutoCloseable, Sendable {
|
||||
private final int m_channel;
|
||||
private int m_handle;
|
||||
|
||||
/**
|
||||
* Create an instance of a Digital Input class. Creates a digital input given a channel.
|
||||
*
|
||||
* @param channel the DIO channel for the digital input 0-9 are on-board, 10-25 are on the MXP
|
||||
*/
|
||||
@SuppressWarnings("this-escape")
|
||||
public DigitalInput(int channel) {
|
||||
SensorUtil.checkDigitalChannel(channel);
|
||||
m_channel = channel;
|
||||
|
||||
m_handle = DIOJNI.initializeDIOPort(channel, true);
|
||||
|
||||
HAL.reportUsage("IO", channel, "DigitalInput");
|
||||
SendableRegistry.add(this, "DigitalInput", channel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
SendableRegistry.remove(this);
|
||||
DIOJNI.freeDIOPort(m_handle);
|
||||
m_handle = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value from a digital input channel. Retrieve the value of a single digital input
|
||||
* channel from the FPGA.
|
||||
*
|
||||
* @return the status of the digital input
|
||||
*/
|
||||
public boolean get() {
|
||||
return DIOJNI.getDIO(m_handle);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the channel of the digital input.
|
||||
*
|
||||
* @return The GPIO channel number that this object represents.
|
||||
*/
|
||||
public int getChannel() {
|
||||
return m_channel;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates this input is used by a simulated device.
|
||||
*
|
||||
* @param device simulated device handle
|
||||
*/
|
||||
public void setSimDevice(SimDevice device) {
|
||||
DIOJNI.setDIOSimDevice(m_handle, device.getNativeHandle());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initSendable(SendableBuilder builder) {
|
||||
builder.setSmartDashboardType("Digital Input");
|
||||
builder.addBooleanProperty("Value", this::get, null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,199 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
package edu.wpi.first.wpilibj;
|
||||
|
||||
import edu.wpi.first.hal.DIOJNI;
|
||||
import edu.wpi.first.hal.HAL;
|
||||
import edu.wpi.first.hal.SimDevice;
|
||||
import edu.wpi.first.util.sendable.Sendable;
|
||||
import edu.wpi.first.util.sendable.SendableBuilder;
|
||||
import edu.wpi.first.util.sendable.SendableRegistry;
|
||||
|
||||
/**
|
||||
* Class to write digital outputs. This class will write digital outputs. Other devices that are
|
||||
* implemented elsewhere will automatically allocate digital inputs and outputs as required.
|
||||
*/
|
||||
public class DigitalOutput implements AutoCloseable, Sendable {
|
||||
private static final int invalidPwmGenerator = 0;
|
||||
private int m_pwmGenerator = invalidPwmGenerator;
|
||||
|
||||
private final int m_channel;
|
||||
private int m_handle;
|
||||
|
||||
/**
|
||||
* Create an instance of a digital output. Create an instance of a digital output given a channel.
|
||||
*
|
||||
* @param channel the DIO channel to use for the digital output. 0-9 are on-board, 10-25 are on
|
||||
* the MXP
|
||||
*/
|
||||
@SuppressWarnings("this-escape")
|
||||
public DigitalOutput(int channel) {
|
||||
SensorUtil.checkDigitalChannel(channel);
|
||||
m_channel = channel;
|
||||
|
||||
m_handle = DIOJNI.initializeDIOPort(channel, false);
|
||||
|
||||
HAL.reportUsage("IO", channel, "DigitalOutput");
|
||||
SendableRegistry.add(this, "DigitalOutput", channel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
SendableRegistry.remove(this);
|
||||
// Disable the pwm only if we have allocated it
|
||||
if (m_pwmGenerator != invalidPwmGenerator) {
|
||||
disablePWM();
|
||||
}
|
||||
DIOJNI.freeDIOPort(m_handle);
|
||||
m_handle = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value of a digital output.
|
||||
*
|
||||
* @param value true is on, off is false
|
||||
*/
|
||||
public void set(boolean value) {
|
||||
DIOJNI.setDIO(m_handle, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value being output from the Digital Output.
|
||||
*
|
||||
* @return the state of the digital output.
|
||||
*/
|
||||
public boolean get() {
|
||||
return DIOJNI.getDIO(m_handle);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the GPIO channel number that this object represents.
|
||||
*
|
||||
* @return The GPIO channel number.
|
||||
*/
|
||||
public int getChannel() {
|
||||
return m_channel;
|
||||
}
|
||||
|
||||
/**
|
||||
* Output a single pulse on the digital output line.
|
||||
*
|
||||
* <p>Send a single pulse on the digital output line where the pulse duration is specified in
|
||||
* seconds. Maximum of 65535 microseconds.
|
||||
*
|
||||
* @param pulseLength The pulse length in seconds
|
||||
*/
|
||||
public void pulse(final double pulseLength) {
|
||||
DIOJNI.pulse(m_handle, pulseLength);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the pulse is still going. Determine if a previously started pulse is still going.
|
||||
*
|
||||
* @return true if pulsing
|
||||
*/
|
||||
public boolean isPulsing() {
|
||||
return DIOJNI.isPulsing(m_handle);
|
||||
}
|
||||
|
||||
/**
|
||||
* Change the PWM frequency of the PWM output on a Digital Output line.
|
||||
*
|
||||
* <p>The valid range is from 0.6 Hz to 19 kHz. The frequency resolution is logarithmic.
|
||||
*
|
||||
* <p>There is only one PWM frequency for all channels.
|
||||
*
|
||||
* @param rate The frequency to output all digital output PWM signals.
|
||||
*/
|
||||
public void setPWMRate(double rate) {
|
||||
DIOJNI.setDigitalPWMRate(rate);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable a PWM PPS (Pulse Per Second) Output on this line.
|
||||
*
|
||||
* <p>Allocate one of the 6 DO PWM generator resources.
|
||||
*
|
||||
* <p>Supply the duty-cycle to output.
|
||||
*
|
||||
* <p>The resolution of the duty cycle is 8-bit.
|
||||
*
|
||||
* @param dutyCycle The duty-cycle to start generating. [0..1]
|
||||
*/
|
||||
public void enablePPS(double dutyCycle) {
|
||||
if (m_pwmGenerator != invalidPwmGenerator) {
|
||||
return;
|
||||
}
|
||||
m_pwmGenerator = DIOJNI.allocateDigitalPWM();
|
||||
DIOJNI.setDigitalPWMPPS(m_pwmGenerator, dutyCycle);
|
||||
DIOJNI.setDigitalPWMOutputChannel(m_pwmGenerator, m_channel);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable a PWM Output on this line.
|
||||
*
|
||||
* <p>Allocate one of the 6 DO PWM generator resources.
|
||||
*
|
||||
* <p>Supply the initial duty-cycle to output in order to avoid a glitch when first starting.
|
||||
*
|
||||
* <p>The resolution of the duty cycle is 8-bit for low frequencies (1kHz or less) but is reduced
|
||||
* the higher the frequency of the PWM signal is.
|
||||
*
|
||||
* @param initialDutyCycle The duty-cycle to start generating. [0..1]
|
||||
*/
|
||||
public void enablePWM(double initialDutyCycle) {
|
||||
if (m_pwmGenerator != invalidPwmGenerator) {
|
||||
return;
|
||||
}
|
||||
m_pwmGenerator = DIOJNI.allocateDigitalPWM();
|
||||
DIOJNI.setDigitalPWMDutyCycle(m_pwmGenerator, initialDutyCycle);
|
||||
DIOJNI.setDigitalPWMOutputChannel(m_pwmGenerator, m_channel);
|
||||
}
|
||||
|
||||
/**
|
||||
* Change this line from a PWM output back to a static Digital Output line.
|
||||
*
|
||||
* <p>Free up one of the 6 DO PWM generator resources that were in use.
|
||||
*/
|
||||
public void disablePWM() {
|
||||
if (m_pwmGenerator == invalidPwmGenerator) {
|
||||
return;
|
||||
}
|
||||
// Disable the output by routing to a dead bit.
|
||||
DIOJNI.setDigitalPWMOutputChannel(m_pwmGenerator, SensorUtil.kDigitalChannels);
|
||||
DIOJNI.freeDigitalPWM(m_pwmGenerator);
|
||||
m_pwmGenerator = invalidPwmGenerator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Change the duty-cycle that is being generated on the line.
|
||||
*
|
||||
* <p>The resolution of the duty cycle is 8-bit for low frequencies (1kHz or less) but is reduced
|
||||
* the higher the frequency of the PWM signal is.
|
||||
*
|
||||
* @param dutyCycle The duty-cycle to change to. [0..1]
|
||||
*/
|
||||
public void updateDutyCycle(double dutyCycle) {
|
||||
if (m_pwmGenerator == invalidPwmGenerator) {
|
||||
return;
|
||||
}
|
||||
DIOJNI.setDigitalPWMDutyCycle(m_pwmGenerator, dutyCycle);
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates this input is used by a simulated device.
|
||||
*
|
||||
* @param device simulated device handle
|
||||
*/
|
||||
public void setSimDevice(SimDevice device) {
|
||||
DIOJNI.setDIOSimDevice(m_handle, device.getNativeHandle());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initSendable(SendableBuilder builder) {
|
||||
builder.setSmartDashboardType("Digital Output");
|
||||
builder.addBooleanProperty("Value", this::get, this::set);
|
||||
}
|
||||
}
|
||||
160
wpilibj/src/main/java/org/wpilib/hardware/discrete/PWM.java
Normal file
160
wpilibj/src/main/java/org/wpilib/hardware/discrete/PWM.java
Normal file
@@ -0,0 +1,160 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
package edu.wpi.first.wpilibj;
|
||||
|
||||
import edu.wpi.first.hal.HAL;
|
||||
import edu.wpi.first.hal.PWMJNI;
|
||||
import edu.wpi.first.hal.SimDevice;
|
||||
import edu.wpi.first.util.sendable.Sendable;
|
||||
import edu.wpi.first.util.sendable.SendableBuilder;
|
||||
import edu.wpi.first.util.sendable.SendableRegistry;
|
||||
|
||||
/**
|
||||
* Class implements the PWM generation in the FPGA.
|
||||
*
|
||||
* <p>The values supplied as arguments for PWM outputs range from -1.0 to 1.0. They are mapped to
|
||||
* the microseconds to keep the pulse high, with a range of 0 (off) to 4096. Changes are immediately
|
||||
* sent to the FPGA, and the update occurs at the next FPGA cycle (5.05ms). There is no delay.
|
||||
*/
|
||||
public class PWM implements Sendable, AutoCloseable {
|
||||
/** Represents the output period in microseconds. */
|
||||
public enum OutputPeriod {
|
||||
/** Pulse every 5ms. */
|
||||
k5Ms,
|
||||
/** Pulse every 10ms. */
|
||||
k10Ms,
|
||||
/** Pulse every 20ms. */
|
||||
k20Ms
|
||||
}
|
||||
|
||||
private final int m_channel;
|
||||
|
||||
private int m_handle;
|
||||
|
||||
/**
|
||||
* Allocate a PWM given a channel.
|
||||
*
|
||||
* <p>Checks channel value range and allocates the appropriate channel. The allocation is only
|
||||
* done to help users ensure that they don't double assign channels.
|
||||
*
|
||||
* <p>By default, adds itself to SendableRegistry.
|
||||
*
|
||||
* @param channel The PWM channel number. 0-9 are on-board, 10-19 are on the MXP port
|
||||
*/
|
||||
public PWM(final int channel) {
|
||||
this(channel, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Allocate a PWM given a channel.
|
||||
*
|
||||
* @param channel The PWM channel number. 0-9 are on-board, 10-19 are on the MXP port
|
||||
* @param registerSendable If true, adds this instance to SendableRegistry
|
||||
*/
|
||||
@SuppressWarnings("this-escape")
|
||||
public PWM(final int channel, final boolean registerSendable) {
|
||||
SensorUtil.checkPWMChannel(channel);
|
||||
m_channel = channel;
|
||||
|
||||
m_handle = PWMJNI.initializePWMPort(channel);
|
||||
|
||||
setDisabled();
|
||||
|
||||
HAL.reportUsage("IO", channel, "PWM");
|
||||
if (registerSendable) {
|
||||
SendableRegistry.add(this, "PWM", channel);
|
||||
}
|
||||
}
|
||||
|
||||
/** Free the resource associated with the PWM channel and set the value to 0. */
|
||||
@Override
|
||||
public void close() {
|
||||
SendableRegistry.remove(this);
|
||||
if (m_handle == 0) {
|
||||
return;
|
||||
}
|
||||
setDisabled();
|
||||
PWMJNI.freePWMPort(m_handle);
|
||||
m_handle = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the channel number associated with the PWM Object.
|
||||
*
|
||||
* @return The channel number.
|
||||
*/
|
||||
public int getChannel() {
|
||||
return m_channel;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the PWM value directly to the hardware.
|
||||
*
|
||||
* <p>Write a microsecond pulse value to a PWM channel.
|
||||
*
|
||||
* @param microsecondPulseTime Microsecond pulse PWM value. Range 0 - 4096.
|
||||
*/
|
||||
public void setPulseTimeMicroseconds(int microsecondPulseTime) {
|
||||
PWMJNI.setPulseTimeMicroseconds(m_handle, microsecondPulseTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the PWM value directly from the hardware.
|
||||
*
|
||||
* <p>Read a raw value from a PWM channel.
|
||||
*
|
||||
* @return Microsecond pulse PWM control value. Range: 0 - 4096.
|
||||
*/
|
||||
public int getPulseTimeMicroseconds() {
|
||||
return PWMJNI.getPulseTimeMicroseconds(m_handle);
|
||||
}
|
||||
|
||||
/** Temporarily disables the PWM output. The next set call will re-enable the output. */
|
||||
public final void setDisabled() {
|
||||
setPulseTimeMicroseconds(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the PWM output period.
|
||||
*
|
||||
* @param mult The output period to apply to this channel
|
||||
*/
|
||||
public void setOutputPeriod(OutputPeriod mult) {
|
||||
int scale =
|
||||
switch (mult) {
|
||||
case k20Ms -> 3;
|
||||
case k10Ms -> 1;
|
||||
case k5Ms -> 0;
|
||||
};
|
||||
|
||||
PWMJNI.setPWMOutputPeriod(m_handle, scale);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the underlying handle.
|
||||
*
|
||||
* @return Underlying PWM handle
|
||||
*/
|
||||
public int getHandle() {
|
||||
return m_handle;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates this input is used by a simulated device.
|
||||
*
|
||||
* @param device simulated device handle
|
||||
*/
|
||||
public void setSimDevice(SimDevice device) {
|
||||
PWMJNI.setPWMSimDevice(m_handle, device.getNativeHandle());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initSendable(SendableBuilder builder) {
|
||||
builder.setSmartDashboardType("PWM");
|
||||
builder.setActuator(true);
|
||||
builder.addDoubleProperty(
|
||||
"Value", this::getPulseTimeMicroseconds, value -> setPulseTimeMicroseconds((int) value));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user