Support for the CAN power distribution panel

Change-Id: If4f8f7f050bf1095221cf16f030a6a8a67532656
This commit is contained in:
thomasclark
2014-05-30 14:04:05 -04:00
parent bf7ef4376c
commit 187aa7a138
13 changed files with 306 additions and 1 deletions

View File

@@ -0,0 +1,75 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2014. 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 java.nio.ByteBuffer;
import java.nio.ByteOrder;
import edu.wpi.first.wpilibj.hal.PDPJNI;
import edu.wpi.first.wpilibj.hal.HALUtil;
import edu.wpi.first.wpilibj.livewindow.LiveWindowSendable;
import edu.wpi.first.wpilibj.can.CANTimeoutException;
/**
* Class for getting voltage, current, and temperature from the CAN PDP
* @author Thomas Clark
*/
public class PowerDistributionPanel extends SensorBase {
public PowerDistributionPanel() {
}
/**
* @return The voltage of the PDP
*/
public double getVoltage() throws CANTimeoutException {
ByteBuffer status = ByteBuffer.allocateDirect(4);
status.order(ByteOrder.LITTLE_ENDIAN);
double voltage = PDPJNI.getPDPVoltage(status.asIntBuffer());
if(status.asIntBuffer().get(0) != 0) {
throw new CANTimeoutException();
}
return voltage;
}
/**
* @return The temperature of the PDP in degrees Celsius
*/
public double getTemperature() throws CANTimeoutException {
ByteBuffer status = ByteBuffer.allocateDirect(4);
status.order(ByteOrder.LITTLE_ENDIAN);
double temperature = PDPJNI.getPDPTemperature(status.asIntBuffer());
if(status.asIntBuffer().get(0) != 0) {
throw new CANTimeoutException();
}
return temperature;
}
/**
* @return The current of one of the PDP channels (channels 1-16) in Amperes
*/
public double getCurrent(int channel) throws CANTimeoutException {
ByteBuffer status = ByteBuffer.allocateDirect(4);
status.order(ByteOrder.LITTLE_ENDIAN);
double current = PDPJNI.getPDPChannelCurrent((byte)channel, status.asIntBuffer());
checkPDPChannel(channel);
if(status.asIntBuffer().get(0) != 0) {
throw new CANTimeoutException();
}
return current;
}
}

View File

@@ -58,6 +58,10 @@ public abstract class SensorBase { // TODO: Refactor
* Number of relay channels per sidecar
*/
public static final int kRelayChannels = 4;
/**
* Number of power distribution channels
*/
public static final int kPDPChannels = 16;
private static int m_defaultAnalogModule = 1;
private static int m_defaultDigitalModule = 1;
@@ -226,6 +230,18 @@ public abstract class SensorBase { // TODO: Refactor
}
}
/**
* 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) {
System.err.println("Requested solenoid channel number is out of range.");
}
}
/**
* Get the number of the default analog module.
*

View File

@@ -0,0 +1,10 @@
package edu.wpi.first.wpilibj.hal;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
public class PDPJNI extends JNIWrapper {
public static native double getPDPTemperature(IntBuffer status);
public static native double getPDPVoltage(IntBuffer status);
public static native double getPDPChannelCurrent(byte channel, IntBuffer status);
}