mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-21 01:01:43 +00:00
Make DoubleSolenoid work in Java [artf3457]
Make DoubleSolenoid and Solenoid use SolenoidBase, like in C++. Add an integration tests for double solenoids. Change-Id: I9a7ff562d65a564c5adabfa73f85f23ad466215b
This commit is contained in:
@@ -50,28 +50,28 @@ public class DoubleSolenoid extends SolenoidBase implements LiveWindowSendable {
|
||||
* Common function to implement constructor behavior.
|
||||
*/
|
||||
private synchronized void initSolenoid() {
|
||||
// checkSolenoidModule(m_moduleNumber);
|
||||
// checkSolenoidChannel(m_forwardChannel);
|
||||
// checkSolenoidChannel(m_reverseChannel);
|
||||
//
|
||||
// try {
|
||||
// m_allocated.allocate((m_moduleNumber - 1) * kSolenoidChannels + m_forwardChannel - 1);
|
||||
// } catch (CheckedAllocationException e) {
|
||||
// throw new AllocationException(
|
||||
// "Solenoid channel " + m_forwardChannel + " on module " + m_moduleNumber + " is already allocated");
|
||||
// }
|
||||
// try {
|
||||
// m_allocated.allocate((m_moduleNumber - 1) * kSolenoidChannels + m_reverseChannel - 1);
|
||||
// } catch (CheckedAllocationException e) {
|
||||
// throw new AllocationException(
|
||||
// "Solenoid channel " + m_reverseChannel + " on module " + m_moduleNumber + " is already allocated");
|
||||
// }
|
||||
// m_forwardMask = (byte) (1 << (m_forwardChannel - 1));
|
||||
// m_reverseMask = (byte) (1 << (m_reverseChannel - 1));
|
||||
//
|
||||
// UsageReporting.report(tResourceType.kResourceType_Solenoid, m_forwardChannel, m_moduleNumber-1);
|
||||
// UsageReporting.report(tResourceType.kResourceType_Solenoid, m_reverseChannel, m_moduleNumber-1);
|
||||
// LiveWindow.addActuator("DoubleSolenoid", m_moduleNumber, m_forwardChannel, this);
|
||||
checkSolenoidModule(m_moduleNumber);
|
||||
checkSolenoidChannel(m_forwardChannel);
|
||||
checkSolenoidChannel(m_reverseChannel);
|
||||
|
||||
try {
|
||||
m_allocated.allocate(m_moduleNumber * kSolenoidChannels + m_forwardChannel);
|
||||
} catch (CheckedAllocationException e) {
|
||||
throw new AllocationException(
|
||||
"Solenoid channel " + m_forwardChannel + " on module " + m_moduleNumber + " is already allocated");
|
||||
}
|
||||
try {
|
||||
m_allocated.allocate(m_moduleNumber * kSolenoidChannels + m_reverseChannel);
|
||||
} catch (CheckedAllocationException e) {
|
||||
throw new AllocationException(
|
||||
"Solenoid channel " + m_reverseChannel + " on module " + m_moduleNumber + " is already allocated");
|
||||
}
|
||||
m_forwardMask = (byte) (1 << m_forwardChannel);
|
||||
m_reverseMask = (byte) (1 << m_reverseChannel);
|
||||
|
||||
UsageReporting.report(tResourceType.kResourceType_Solenoid, m_forwardChannel, m_moduleNumber);
|
||||
UsageReporting.report(tResourceType.kResourceType_Solenoid, m_reverseChannel, m_moduleNumber);
|
||||
LiveWindow.addActuator("DoubleSolenoid", m_moduleNumber, m_forwardChannel, this);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -105,8 +105,8 @@ public class DoubleSolenoid extends SolenoidBase implements LiveWindowSendable {
|
||||
* Destructor.
|
||||
*/
|
||||
public synchronized void free() {
|
||||
// m_allocated.free((m_moduleNumber - 1) * kSolenoidChannels + m_forwardChannel - 1);
|
||||
// m_allocated.free((m_moduleNumber - 1) * kSolenoidChannels + m_reverseChannel - 1);
|
||||
m_allocated.free(m_moduleNumber * kSolenoidChannels + m_forwardChannel);
|
||||
m_allocated.free(m_moduleNumber * kSolenoidChannels + m_reverseChannel);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -86,10 +86,10 @@ public class Solenoid extends SolenoidBase implements LiveWindowSendable {
|
||||
* @param on Turn the solenoid output off or on.
|
||||
*/
|
||||
public void set(boolean on) {
|
||||
ByteBuffer status = ByteBuffer.allocateDirect(4);
|
||||
status.order(ByteOrder.LITTLE_ENDIAN);
|
||||
SolenoidJNI.setSolenoid(m_solenoid_port, (byte) (on ? 1 : 0), status.asIntBuffer());
|
||||
HALUtil.checkStatus(status.asIntBuffer());
|
||||
byte value = (byte) (on ? 0xFF : 0x00);
|
||||
byte mask = (byte) (1 << m_channel);
|
||||
|
||||
set(value, mask);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -98,11 +98,8 @@ public class Solenoid extends SolenoidBase implements LiveWindowSendable {
|
||||
* @return The current value of the solenoid.
|
||||
*/
|
||||
public boolean get() {
|
||||
ByteBuffer status = ByteBuffer.allocateDirect(4);
|
||||
status.order(ByteOrder.LITTLE_ENDIAN);
|
||||
boolean value = SolenoidJNI.getSolenoid(m_solenoid_port, status.asIntBuffer()) != 0;
|
||||
HALUtil.checkStatus(status.asIntBuffer());
|
||||
return value;
|
||||
int value = getAll() & ( 1 << m_channel);
|
||||
return (value != 0);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -11,6 +11,7 @@ import java.nio.IntBuffer;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import edu.wpi.first.wpilibj.hal.HALUtil;
|
||||
import edu.wpi.first.wpilibj.hal.SolenoidJNI;
|
||||
|
||||
/**
|
||||
* SolenoidBase class is the common base class for the Solenoid and
|
||||
@@ -20,8 +21,7 @@ public abstract class SolenoidBase extends SensorBase {
|
||||
|
||||
private ByteBuffer[] m_ports;
|
||||
protected int m_moduleNumber; ///< The number of the solenoid module being used.
|
||||
// XXX: Move this to be both HAL calls
|
||||
//protected Resource m_allocated = new Resource(SolenoidJNI.getModuleCount() * SensorBase.kSolenoidChannels);
|
||||
protected Resource m_allocated = new Resource(63* SensorBase.kSolenoidChannels);
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
@@ -30,13 +30,13 @@ public abstract class SolenoidBase extends SensorBase {
|
||||
*/
|
||||
public SolenoidBase(final int moduleNumber) {
|
||||
m_moduleNumber = moduleNumber;
|
||||
// m_ports = new ByteBuffer[SensorBase.kSolenoidChannels];
|
||||
// for (int i = 0; i < SensorBase.kSolenoidChannels; i++) {
|
||||
// ByteBuffer port = SolenoidJNI.getPortWithModule((byte) moduleNumber, (byte) (i+1));
|
||||
// IntBuffer status = IntBuffer.allocate(1);
|
||||
// m_ports[i] = SolenoidJNI.initializeSolenoidPort(port, status);
|
||||
// HALUtil.checkStatus(status);
|
||||
// }
|
||||
m_ports = new ByteBuffer[SensorBase.kSolenoidChannels];
|
||||
for (int i = 0; i < SensorBase.kSolenoidChannels; i++) {
|
||||
ByteBuffer port = SolenoidJNI.getPortWithModule((byte) moduleNumber, (byte) i);
|
||||
IntBuffer status = IntBuffer.allocate(1);
|
||||
m_ports[i] = SolenoidJNI.initializeSolenoidPort(port, status);
|
||||
HALUtil.checkStatus(status);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -46,13 +46,13 @@ public abstract class SolenoidBase extends SensorBase {
|
||||
* @param mask The channels you want to be affected.
|
||||
*/
|
||||
protected synchronized void set(int value, int mask) {
|
||||
// IntBuffer status = IntBuffer.allocate(1);
|
||||
// for (int i = 0; i < SensorBase.kSolenoidChannels; i++) {
|
||||
// int local_mask = 1 << i;
|
||||
// if ((mask & local_mask) != 0)
|
||||
// SolenoidJNI.setSolenoid(m_ports[i], (byte) (value & local_mask), status);
|
||||
// }
|
||||
// HALUtil.checkStatus(status);
|
||||
IntBuffer status = IntBuffer.allocate(1);
|
||||
for (int i = 0; i < SensorBase.kSolenoidChannels; i++) {
|
||||
int local_mask = 1 << i;
|
||||
if ((mask & local_mask) != 0)
|
||||
SolenoidJNI.setSolenoid(m_ports[i], (byte) (value & local_mask), status);
|
||||
}
|
||||
HALUtil.checkStatus(status);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -62,32 +62,11 @@ public abstract class SolenoidBase extends SensorBase {
|
||||
*/
|
||||
public byte getAll() {
|
||||
byte value = 0;
|
||||
// IntBuffer status = IntBuffer.allocate(1);
|
||||
// for (int i = 0; i < SensorBase.kSolenoidChannels; i++) {
|
||||
// value |= SolenoidJNI.getSolenoid(m_ports[i], status) << i;
|
||||
// }
|
||||
// HALUtil.checkStatus(status);
|
||||
IntBuffer status = IntBuffer.allocate(1);
|
||||
for (int i = 0; i < SensorBase.kSolenoidChannels; i++) {
|
||||
value |= SolenoidJNI.getSolenoid(m_ports[i], status) << i;
|
||||
}
|
||||
HALUtil.checkStatus(status);
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read all 8 solenoids in the default solenoid module as a single byte
|
||||
*
|
||||
* @return The current value of all 8 solenoids on the default module.
|
||||
*/
|
||||
public static byte getAllFromDefaultModule() {
|
||||
return getAllFromModule(getDefaultSolenoidModule());
|
||||
}
|
||||
|
||||
/**
|
||||
* Read all 8 solenoids in the specified solenoid module as a single byte
|
||||
*
|
||||
* @return The current value of all 8 solenoids on the specified module.
|
||||
*/
|
||||
public static byte getAllFromModule(int moduleNumber) {
|
||||
byte value = 0;
|
||||
// checkSolenoidModule(moduleNumber);
|
||||
// throw new RuntimeException("Not supported right now.");
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user