mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-24 01:31:46 +00:00
Support for the CAN pneumatics module in C++ and Java
Change-Id: I2ccb1d13d1c5da00a99329c761b75a6c2b3ea56d
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
package edu.wpi.first.wpilibj;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
|
||||
import edu.wpi.first.wpilibj.SensorBase;
|
||||
import edu.wpi.first.wpilibj.hal.CompressorJNI;
|
||||
import edu.wpi.first.wpilibj.hal.HALUtil;
|
||||
import edu.wpi.first.wpilibj.livewindow.LiveWindowSendable;
|
||||
import edu.wpi.first.wpilibj.parsing.IDevice;
|
||||
import edu.wpi.first.wpilibj.tables.ITable;
|
||||
|
||||
public class PCMCompressor extends SensorBase implements IDevice, LiveWindowSendable {
|
||||
private ByteBuffer m_pcm;
|
||||
|
||||
public PCMCompressor(int module) {
|
||||
initCompressor(module);
|
||||
}
|
||||
|
||||
public PCMCompressor() {
|
||||
initCompressor(getDefaultSolenoidModule());
|
||||
}
|
||||
|
||||
private void initCompressor(int module) {
|
||||
m_table = null;
|
||||
|
||||
m_pcm = CompressorJNI.initializeCompressor((byte)module);
|
||||
}
|
||||
|
||||
void start() {
|
||||
setClosedLoopControl(false);
|
||||
setCompressor(true);
|
||||
}
|
||||
|
||||
void stop() {
|
||||
setClosedLoopControl(false);
|
||||
setCompressor(false);
|
||||
}
|
||||
|
||||
boolean enabled() {
|
||||
ByteBuffer status = ByteBuffer.allocateDirect(4);
|
||||
status.order(ByteOrder.LITTLE_ENDIAN);
|
||||
|
||||
boolean on = CompressorJNI.getCompressor(m_pcm, status.asIntBuffer());
|
||||
HALUtil.checkStatus(status.asIntBuffer());
|
||||
|
||||
return on;
|
||||
}
|
||||
|
||||
boolean getPressureSwitchValue() {
|
||||
ByteBuffer status = ByteBuffer.allocateDirect(4);
|
||||
status.order(ByteOrder.LITTLE_ENDIAN);
|
||||
|
||||
boolean on = CompressorJNI.getPressureSwitch(m_pcm, status.asIntBuffer());
|
||||
HALUtil.checkStatus(status.asIntBuffer());
|
||||
|
||||
return on;
|
||||
}
|
||||
|
||||
float getCompressorCurrent() {
|
||||
ByteBuffer status = ByteBuffer.allocateDirect(4);
|
||||
status.order(ByteOrder.LITTLE_ENDIAN);
|
||||
|
||||
float current = CompressorJNI.getCompressorCurrent(m_pcm, status.asIntBuffer());
|
||||
HALUtil.checkStatus(status.asIntBuffer());
|
||||
|
||||
return current;
|
||||
}
|
||||
|
||||
void setClosedLoopControl(boolean on) {
|
||||
ByteBuffer status = ByteBuffer.allocateDirect(4);
|
||||
status.order(ByteOrder.LITTLE_ENDIAN);
|
||||
|
||||
CompressorJNI.setClosedLoopControl(m_pcm, on, status.asIntBuffer());
|
||||
HALUtil.checkStatus(status.asIntBuffer());
|
||||
}
|
||||
|
||||
boolean getClosedLoopControl() {
|
||||
ByteBuffer status = ByteBuffer.allocateDirect(4);
|
||||
status.order(ByteOrder.LITTLE_ENDIAN);
|
||||
|
||||
boolean on = CompressorJNI.getClosedLoopControl(m_pcm, status.asIntBuffer());
|
||||
HALUtil.checkStatus(status.asIntBuffer());
|
||||
|
||||
return on;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startLiveWindowMode() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stopLiveWindowMode() {
|
||||
}
|
||||
|
||||
private void setCompressor(boolean on) {
|
||||
ByteBuffer status = ByteBuffer.allocateDirect(4);
|
||||
status.order(ByteOrder.LITTLE_ENDIAN);
|
||||
|
||||
CompressorJNI.setCompressor(m_pcm, on, status.asIntBuffer());
|
||||
HALUtil.checkStatus(status.asIntBuffer());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSmartDashboardType() {
|
||||
return "Compressor";
|
||||
}
|
||||
|
||||
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.putBoolean("Enabled", enabled());
|
||||
m_table.putBoolean("Pressure Switch", getPressureSwitchValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,12 +7,13 @@
|
||||
|
||||
package edu.wpi.first.wpilibj;
|
||||
|
||||
import java.nio.IntBuffer;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
|
||||
import edu.wpi.first.wpilibj.communication.FRCNetworkCommunicationsLibrary.tResourceType;
|
||||
import edu.wpi.first.wpilibj.communication.UsageReporting;
|
||||
import edu.wpi.first.wpilibj.hal.HALUtil;
|
||||
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;
|
||||
@@ -29,30 +30,31 @@ import edu.wpi.first.wpilibj.util.CheckedAllocationException;
|
||||
public class Solenoid extends SolenoidBase implements LiveWindowSendable {
|
||||
|
||||
private int m_channel; ///< The channel on the module to control.
|
||||
private ByteBuffer m_port;
|
||||
private ByteBuffer m_solenoid_port;
|
||||
|
||||
/**
|
||||
* Common function to implement constructor behavior.
|
||||
*/
|
||||
private synchronized void initSolenoid() {
|
||||
// checkSolenoidModule(m_moduleNumber);
|
||||
// checkSolenoidChannel(m_channel);
|
||||
//
|
||||
checkSolenoidModule(m_moduleNumber);
|
||||
checkSolenoidChannel(m_channel);
|
||||
|
||||
// try {
|
||||
// m_allocated.allocate((m_moduleNumber - 1) * kSolenoidChannels + m_channel - 1);
|
||||
// } catch (CheckedAllocationException e) {
|
||||
// throw new AllocationException(
|
||||
// "Solenoid channel " + m_channel + " on module " + m_moduleNumber + " is already allocated");
|
||||
// }
|
||||
//
|
||||
// IntBuffer status = IntBuffer.allocate(1);
|
||||
// m_port = SolenoidJNI.getPortWithModule((byte) m_moduleNumber, (byte) m_channel);
|
||||
// HALUtil.checkStatus(status);
|
||||
// SolenoidJNI.initializeSolenoidPort(m_port, status);
|
||||
// HALUtil.checkStatus(status);
|
||||
//
|
||||
// LiveWindow.addActuator("Solenoid", m_moduleNumber, m_channel, this);
|
||||
// UsageReporting.report(tResourceType.kResourceType_Solenoid, m_channel, m_moduleNumber - 1);
|
||||
|
||||
ByteBuffer status = ByteBuffer.allocateDirect(4);
|
||||
status.order(ByteOrder.LITTLE_ENDIAN);
|
||||
|
||||
ByteBuffer port = SolenoidJNI.getPortWithModule((byte) m_moduleNumber, (byte) m_channel);
|
||||
m_solenoid_port = SolenoidJNI.initializeSolenoidPort(port, status.asIntBuffer());
|
||||
HALUtil.checkStatus(status.asIntBuffer());
|
||||
|
||||
LiveWindow.addActuator("Solenoid", m_moduleNumber, m_channel, this);
|
||||
UsageReporting.report(tResourceType.kResourceType_Solenoid, m_channel, m_moduleNumber - 1);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -91,9 +93,10 @@ public class Solenoid extends SolenoidBase implements LiveWindowSendable {
|
||||
* @param on Turn the solenoid output off or on.
|
||||
*/
|
||||
public void set(boolean on) {
|
||||
// IntBuffer status = IntBuffer.allocate(1);
|
||||
// SolenoidJNI.setSolenoid(m_port, (byte) (on ? 1 : 0), status);
|
||||
// HALUtil.checkStatus(status);
|
||||
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());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -102,10 +105,10 @@ public class Solenoid extends SolenoidBase implements LiveWindowSendable {
|
||||
* @return The current value of the solenoid.
|
||||
*/
|
||||
public boolean get() {
|
||||
boolean value = false;
|
||||
// IntBuffer status = IntBuffer.allocate(1);
|
||||
// boolean value = SolenoidJNI.getSolenoid(m_port, status) != 0;
|
||||
// HALUtil.checkStatus(status);
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package edu.wpi.first.wpilibj.hal;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.IntBuffer;
|
||||
|
||||
public class CompressorJNI extends JNIWrapper {
|
||||
public static native ByteBuffer initializeCompressor(byte module);
|
||||
public static native boolean checkCompressorModule(byte module);
|
||||
|
||||
public static native void setCompressor(ByteBuffer pcm_pointer, boolean value, IntBuffer status);
|
||||
public static native boolean getCompressor(ByteBuffer pcm_pointer, IntBuffer status);
|
||||
|
||||
public static native void setClosedLoopControl(ByteBuffer pcm_pointer, boolean value, IntBuffer status);
|
||||
public static native boolean getClosedLoopControl(ByteBuffer pcm_pointer, IntBuffer status);
|
||||
|
||||
public static native boolean getPressureSwitch(ByteBuffer pcm_pointer, IntBuffer status);
|
||||
public static native float getCompressorCurrent(ByteBuffer pcm_pointer, IntBuffer status);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package edu.wpi.first.wpilibj.hal;
|
||||
import java.nio.IntBuffer;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class SolenoidJNI extends JNIWrapper {
|
||||
public static native ByteBuffer initializeSolenoidPort(ByteBuffer portPointer, IntBuffer status);
|
||||
public static native ByteBuffer getPortWithModule(byte module, byte channel);
|
||||
public static native void setSolenoid(ByteBuffer port, byte on, IntBuffer status);
|
||||
public static native byte getSolenoid(ByteBuffer port, IntBuffer status);
|
||||
}
|
||||
Reference in New Issue
Block a user