mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-27 02:01:42 +00:00
Switches Solenoids to Handles (#126)
This commit is contained in:
committed by
Peter Johnson
parent
62c217cd01
commit
8c4ad62422
@@ -9,6 +9,7 @@ package edu.wpi.first.wpilibj;
|
||||
|
||||
import edu.wpi.first.wpilibj.communication.FRCNetworkCommunicationsLibrary.tResourceType;
|
||||
import edu.wpi.first.wpilibj.communication.UsageReporting;
|
||||
import edu.wpi.first.wpilibj.hal.SolenoidJNI;
|
||||
import edu.wpi.first.wpilibj.livewindow.LiveWindow;
|
||||
import edu.wpi.first.wpilibj.livewindow.LiveWindowSendable;
|
||||
import edu.wpi.first.wpilibj.tables.ITable;
|
||||
@@ -37,6 +38,8 @@ public class DoubleSolenoid extends SolenoidBase implements LiveWindowSendable {
|
||||
private int m_reverseChannel; // /< The reverse channel on the module to control.
|
||||
private byte m_forwardMask; // /< The mask for the forward channel.
|
||||
private byte m_reverseMask; // /< The mask for the reverse channel.
|
||||
private int m_forwardHandle = 0;
|
||||
private int m_reverseHandle = 0;
|
||||
|
||||
/**
|
||||
* Constructor. Uses the default PCM ID of 0.
|
||||
@@ -65,18 +68,20 @@ public class DoubleSolenoid extends SolenoidBase implements LiveWindowSendable {
|
||||
checkSolenoidChannel(m_forwardChannel);
|
||||
checkSolenoidChannel(m_reverseChannel);
|
||||
|
||||
int portHandle = SolenoidJNI.getPortWithModule((byte) m_moduleNumber, (byte) m_forwardChannel);
|
||||
m_forwardHandle = SolenoidJNI.initializeSolenoidPort(portHandle);
|
||||
|
||||
try {
|
||||
allocated.allocate(m_moduleNumber * kSolenoidChannels + m_forwardChannel);
|
||||
} catch (CheckedAllocationException exception) {
|
||||
throw new AllocationException("Solenoid channel " + m_forwardChannel + " on module "
|
||||
+ m_moduleNumber + " is already allocated");
|
||||
}
|
||||
try {
|
||||
allocated.allocate(m_moduleNumber * kSolenoidChannels + m_reverseChannel);
|
||||
} catch (CheckedAllocationException exception) {
|
||||
throw new AllocationException("Solenoid channel " + m_reverseChannel + " on module "
|
||||
+ m_moduleNumber + " is already allocated");
|
||||
portHandle = SolenoidJNI.getPortWithModule((byte) m_moduleNumber, (byte) m_reverseChannel);
|
||||
m_reverseHandle = SolenoidJNI.initializeSolenoidPort(portHandle);
|
||||
} catch (RuntimeException ex) {
|
||||
// free the forward handle on exception, then rethrow
|
||||
SolenoidJNI.freeSolenoidPort(m_forwardHandle);
|
||||
m_forwardHandle = 0;
|
||||
m_reverseHandle = 0;
|
||||
throw ex;
|
||||
}
|
||||
|
||||
m_forwardMask = (byte) (1 << m_forwardChannel);
|
||||
m_reverseMask = (byte) (1 << m_reverseChannel);
|
||||
|
||||
@@ -89,8 +94,8 @@ public class DoubleSolenoid extends SolenoidBase implements LiveWindowSendable {
|
||||
* Destructor.
|
||||
*/
|
||||
public synchronized void free() {
|
||||
allocated.free(m_moduleNumber * kSolenoidChannels + m_forwardChannel);
|
||||
allocated.free(m_moduleNumber * kSolenoidChannels + m_reverseChannel);
|
||||
SolenoidJNI.freeSolenoidPort(m_forwardHandle);
|
||||
SolenoidJNI.freeSolenoidPort(m_reverseChannel);
|
||||
super.free();
|
||||
}
|
||||
|
||||
@@ -100,24 +105,29 @@ public class DoubleSolenoid extends SolenoidBase implements LiveWindowSendable {
|
||||
* @param value The value to set (Off, Forward, Reverse)
|
||||
*/
|
||||
public void set(final Value value) {
|
||||
final byte rawValue;
|
||||
|
||||
boolean forward = false;
|
||||
boolean reverse = false;
|
||||
|
||||
switch (value) {
|
||||
case kOff:
|
||||
rawValue = 0x00;
|
||||
forward = false;
|
||||
reverse = false;
|
||||
break;
|
||||
case kForward:
|
||||
rawValue = m_forwardMask;
|
||||
forward = true;
|
||||
reverse = false;
|
||||
break;
|
||||
case kReverse:
|
||||
rawValue = m_reverseMask;
|
||||
forward = false;
|
||||
reverse = true;
|
||||
break;
|
||||
default:
|
||||
throw new AssertionError("Illegal value: " + value);
|
||||
|
||||
}
|
||||
|
||||
set(rawValue, m_forwardMask | m_reverseMask);
|
||||
SolenoidJNI.setSolenoid(m_forwardHandle, forward);
|
||||
SolenoidJNI.setSolenoid(m_reverseHandle, reverse);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -126,12 +136,13 @@ public class DoubleSolenoid extends SolenoidBase implements LiveWindowSendable {
|
||||
* @return The current value of the solenoid.
|
||||
*/
|
||||
public Value get() {
|
||||
byte value = getAll();
|
||||
|
||||
if ((value & m_forwardMask) != 0) {
|
||||
boolean valueForward = SolenoidJNI.getSolenoid(m_forwardHandle);
|
||||
boolean valueReverse = SolenoidJNI.getSolenoid(m_reverseHandle);
|
||||
|
||||
if (valueForward) {
|
||||
return Value.kForward;
|
||||
}
|
||||
if ((value & m_reverseMask) != 0) {
|
||||
if (valueReverse) {
|
||||
return Value.kReverse;
|
||||
}
|
||||
return Value.kOff;
|
||||
|
||||
@@ -26,7 +26,7 @@ import edu.wpi.first.wpilibj.util.CheckedAllocationException;
|
||||
public class Solenoid extends SolenoidBase implements LiveWindowSendable {
|
||||
|
||||
private final int m_channel; // /< The channel to control.
|
||||
private long m_solenoidPort;
|
||||
private int m_solenoidHandle;
|
||||
|
||||
/**
|
||||
* Constructor using the default PCM ID (0)
|
||||
@@ -50,15 +50,8 @@ public class Solenoid extends SolenoidBase implements LiveWindowSendable {
|
||||
checkSolenoidModule(m_moduleNumber);
|
||||
checkSolenoidChannel(m_channel);
|
||||
|
||||
try {
|
||||
allocated.allocate(m_moduleNumber * kSolenoidChannels + m_channel);
|
||||
} catch (CheckedAllocationException ex) {
|
||||
throw new AllocationException("Solenoid channel " + m_channel + " on module "
|
||||
+ m_moduleNumber + " is already allocated");
|
||||
}
|
||||
|
||||
int portHandle = SolenoidJNI.getPortWithModule((byte) m_moduleNumber, (byte) m_channel);
|
||||
m_solenoidPort = SolenoidJNI.initializeSolenoidPort(portHandle);
|
||||
m_solenoidHandle = SolenoidJNI.initializeSolenoidPort(portHandle);
|
||||
|
||||
LiveWindow.addActuator("Solenoid", m_moduleNumber, m_channel, this);
|
||||
UsageReporting.report(tResourceType.kResourceType_Solenoid, m_channel, m_moduleNumber);
|
||||
@@ -68,9 +61,11 @@ public class Solenoid extends SolenoidBase implements LiveWindowSendable {
|
||||
* Destructor.
|
||||
*/
|
||||
public synchronized void free() {
|
||||
allocated.free(m_moduleNumber * kSolenoidChannels + m_channel);
|
||||
SolenoidJNI.freeSolenoidPort(m_solenoidPort);
|
||||
m_solenoidPort = 0;
|
||||
SolenoidJNI.freeSolenoidPort(m_solenoidHandle);
|
||||
m_solenoidHandle = 0;
|
||||
if (m_table != null) {
|
||||
m_table.removeTableListener(m_tableListener);
|
||||
}
|
||||
super.free();
|
||||
}
|
||||
|
||||
@@ -80,10 +75,7 @@ public class Solenoid extends SolenoidBase implements LiveWindowSendable {
|
||||
* @param on Turn the solenoid output off or on.
|
||||
*/
|
||||
public void set(boolean on) {
|
||||
byte value = (byte) (on ? 0xFF : 0x00);
|
||||
byte mask = (byte) (1 << m_channel);
|
||||
|
||||
set(value, mask);
|
||||
SolenoidJNI.setSolenoid(m_solenoidHandle, on);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -92,8 +84,7 @@ public class Solenoid extends SolenoidBase implements LiveWindowSendable {
|
||||
* @return The current value of the solenoid.
|
||||
*/
|
||||
public boolean get() {
|
||||
int value = getAll() & (1 << m_channel);
|
||||
return (value != 0);
|
||||
return SolenoidJNI.getSolenoid(m_solenoidHandle);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -13,11 +13,8 @@ import edu.wpi.first.wpilibj.hal.SolenoidJNI;
|
||||
* SolenoidBase class is the common base class for the Solenoid and DoubleSolenoid classes.
|
||||
*/
|
||||
public abstract class SolenoidBase extends SensorBase {
|
||||
|
||||
private final long[] m_ports;
|
||||
protected final int m_moduleNumber; // /< The number of the solenoid module being
|
||||
protected final byte m_moduleNumber; // /< The number of the solenoid module being
|
||||
// used.
|
||||
protected static final Resource allocated = new Resource(63 * SensorBase.kSolenoidChannels);
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
@@ -25,38 +22,7 @@ public abstract class SolenoidBase extends SensorBase {
|
||||
* @param moduleNumber The PCM CAN ID
|
||||
*/
|
||||
public SolenoidBase(final int moduleNumber) {
|
||||
m_moduleNumber = moduleNumber;
|
||||
m_ports = new long[SensorBase.kSolenoidChannels];
|
||||
for (int i = 0; i < SensorBase.kSolenoidChannels; i++) {
|
||||
int portHandle = SolenoidJNI.getPortWithModule((byte) moduleNumber, (byte) i);
|
||||
m_ports[i] = SolenoidJNI.initializeSolenoidPort(portHandle);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Free the resources associated with by the solenoid base.
|
||||
*/
|
||||
@Override
|
||||
public void free() {
|
||||
for (int i = 0; i < m_ports.length; i++) {
|
||||
SolenoidJNI.freeSolenoidPort(m_ports[i]);
|
||||
m_ports[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value of a solenoid.
|
||||
*
|
||||
* @param value The value you want to set on the module.
|
||||
* @param mask The channels you want to be affected.
|
||||
*/
|
||||
protected synchronized void set(int value, int mask) {
|
||||
for (int i = 0; i < SensorBase.kSolenoidChannels; i++) {
|
||||
int localMask = 1 << i;
|
||||
if ((mask & localMask) != 0) {
|
||||
SolenoidJNI.setSolenoid(m_ports[i], (value & localMask) != 0);
|
||||
}
|
||||
}
|
||||
m_moduleNumber = (byte)moduleNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -65,7 +31,7 @@ public abstract class SolenoidBase extends SensorBase {
|
||||
* @return The current value of all 8 solenoids on this module.
|
||||
*/
|
||||
public byte getAll() {
|
||||
return SolenoidJNI.getAllSolenoids(m_ports[0]);
|
||||
return SolenoidJNI.getAllSolenoids(m_moduleNumber);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -77,7 +43,7 @@ public abstract class SolenoidBase extends SensorBase {
|
||||
* @see #clearAllPCMStickyFaults()
|
||||
*/
|
||||
public byte getPCMSolenoidBlackList() {
|
||||
return (byte) SolenoidJNI.getPCMSolenoidBlackList(m_ports[0]);
|
||||
return (byte) SolenoidJNI.getPCMSolenoidBlackList(m_moduleNumber);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -87,7 +53,7 @@ public abstract class SolenoidBase extends SensorBase {
|
||||
* @return true if PCM sticky fault is set
|
||||
*/
|
||||
public boolean getPCMSolenoidVoltageStickyFault() {
|
||||
return SolenoidJNI.getPCMSolenoidVoltageStickyFault(m_ports[0]);
|
||||
return SolenoidJNI.getPCMSolenoidVoltageStickyFault(m_moduleNumber);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -97,7 +63,7 @@ public abstract class SolenoidBase extends SensorBase {
|
||||
* @return true if PCM is in fault state.
|
||||
*/
|
||||
public boolean getPCMSolenoidVoltageFault() {
|
||||
return SolenoidJNI.getPCMSolenoidVoltageFault(m_ports[0]);
|
||||
return SolenoidJNI.getPCMSolenoidVoltageFault(m_moduleNumber);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -110,6 +76,6 @@ public abstract class SolenoidBase extends SensorBase {
|
||||
* <p>If no sticky faults are set then this call will have no effect.
|
||||
*/
|
||||
public void clearAllPCMStickyFaults() {
|
||||
SolenoidJNI.clearAllPCMStickyFaults(m_ports[0]);
|
||||
SolenoidJNI.clearAllPCMStickyFaults(m_moduleNumber);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,21 +8,21 @@
|
||||
package edu.wpi.first.wpilibj.hal;
|
||||
|
||||
public class SolenoidJNI extends JNIWrapper {
|
||||
public static native long initializeSolenoidPort(int halPortHandle);
|
||||
public static native int initializeSolenoidPort(int halPortHandle);
|
||||
|
||||
public static native void freeSolenoidPort(long portPointer);
|
||||
public static native void freeSolenoidPort(int portHandle);
|
||||
|
||||
public static native void setSolenoid(long port, boolean on);
|
||||
public static native void setSolenoid(int portHandle, boolean on);
|
||||
|
||||
public static native boolean getSolenoid(long port);
|
||||
public static native boolean getSolenoid(int portHandle);
|
||||
|
||||
public static native byte getAllSolenoids(long port);
|
||||
public static native byte getAllSolenoids(byte module);
|
||||
|
||||
public static native int getPCMSolenoidBlackList(long pcmPointer);
|
||||
public static native int getPCMSolenoidBlackList(byte module);
|
||||
|
||||
public static native boolean getPCMSolenoidVoltageStickyFault(long pcmPointer);
|
||||
public static native boolean getPCMSolenoidVoltageStickyFault(byte module);
|
||||
|
||||
public static native boolean getPCMSolenoidVoltageFault(long pcmPointer);
|
||||
public static native boolean getPCMSolenoidVoltageFault(byte module);
|
||||
|
||||
public static native void clearAllPCMStickyFaults(long pcmPointer);
|
||||
public static native void clearAllPCMStickyFaults(byte module);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user