diff --git a/hal/include/HAL/HAL.hpp b/hal/include/HAL/HAL.hpp index 853655d9d3..cb8f5f5d69 100644 --- a/hal/include/HAL/HAL.hpp +++ b/hal/include/HAL/HAL.hpp @@ -22,6 +22,7 @@ #include "Interrupts.hpp" #include "Errors.hpp" #include "PDP.hpp" +#include "Power.hpp" #include "Utilities.hpp" #include "Semaphore.hpp" diff --git a/hal/include/HAL/Power.hpp b/hal/include/HAL/Power.hpp new file mode 100644 index 0000000000..10e9096f31 --- /dev/null +++ b/hal/include/HAL/Power.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include + +extern "C" +{ + float getVinVoltage(int32_t *status); + float getVinCurrent(int32_t *status); + float getUserVoltage6V(int32_t *status); + float getUserCurrent6V(int32_t *status); + float getUserVoltage5V(int32_t *status); + float getUserCurrent5V(int32_t *status); + float getUserVoltage3V3(int32_t *status); + float getUserCurrent3V3(int32_t *status); +} diff --git a/hal/lib/Athena/Power.cpp b/hal/lib/Athena/Power.cpp new file mode 100644 index 0000000000..d4edb75dc6 --- /dev/null +++ b/hal/lib/Athena/Power.cpp @@ -0,0 +1,74 @@ +#include "HAL/Power.hpp" +#include "ChipObject.h" + +static tPower *power = NULL; + +static void initializePower(int32_t *status) { + if(power == NULL) { + power = tPower::create(status); + } +} + +/** + * Get the roboRIO input voltage + */ +float getVinVoltage(int32_t *status) { + initializePower(status); + return power->readVinVoltage(status) / 4.096f * 0.025733f - 0.029f; +} + +/** + * Get the roboRIO input current + */ +float getVinCurrent(int32_t *status) { + initializePower(status); + return power->readVinCurrent(status) / 4.096f * 0.017042 - 0.071f; +} + +/** + * Get the 6V rail voltage + */ +float getUserVoltage6V(int32_t *status) { + initializePower(status); + return power->readUserVoltage6V(status) / 4.096f * 0.007019f - 0.014f; +} + +/** + * Get the 6V rail current + */ +float getUserCurrent6V(int32_t *status) { + initializePower(status); + return power->readUserCurrent6V(status) / 4.096f * 0.005566f - 0.009f; +} + +/** + * Get the 5V rail voltage + */ +float getUserVoltage5V(int32_t *status) { + initializePower(status); + return power->readUserVoltage5V(status) / 4.096f * 0.004962f - 0.013f; +} + +/** + * Get the 5V rail current + */ +float getUserCurrent5V(int32_t *status) { + initializePower(status); + return power->readUserCurrent5V(status) / 4.096f * 0.001996f - 0.002f; +} + +/** + * Get the 3.3V rail voltage + */ +float getUserVoltage3V3(int32_t *status) { + initializePower(status); + return power->readUserVoltage3V3(status) / 4.096f * 0.004902f - 0.01f; +} + +/** + * Get the 3.3V rail current + */ +float getUserCurrent3V3(int32_t *status) { + initializePower(status); + return power->readUserCurrent3V3(status) / 4.096f * 0.002486f - 0.003f; +} diff --git a/wpilibc/wpilibC++Devices/src/DriverStation.cpp b/wpilibc/wpilibC++Devices/src/DriverStation.cpp index beda69ebad..8c35b93ae7 100644 --- a/wpilibc/wpilibC++Devices/src/DriverStation.cpp +++ b/wpilibc/wpilibC++Devices/src/DriverStation.cpp @@ -162,7 +162,11 @@ void DriverStation::GetData() */ float DriverStation::GetBatteryVoltage() { - return 0.0f; // TODO + int32_t status = 0; + float voltage = getVinVoltage(&status); + wpi_setErrorWithContext(status, "getVinVoltage"); + + return voltage; } /** diff --git a/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/DriverStation.java b/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/DriverStation.java index 7686a6dc7f..0678e303f2 100644 --- a/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/DriverStation.java +++ b/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/DriverStation.java @@ -6,6 +6,7 @@ /*----------------------------------------------------------------------------*/ package edu.wpi.first.wpilibj; +import java.nio.IntBuffer; import java.nio.ByteBuffer; import java.nio.ByteOrder; @@ -13,6 +14,7 @@ import edu.wpi.first.wpilibj.communication.FRCNetworkCommunicationsLibrary; import edu.wpi.first.wpilibj.communication.HALControlWord; import edu.wpi.first.wpilibj.communication.HALAllianceStationID; import edu.wpi.first.wpilibj.hal.HALUtil; +import edu.wpi.first.wpilibj.hal.PowerJNI; import edu.wpi.first.wpilibj.Timer; /** @@ -95,10 +97,7 @@ public class DriverStation implements RobotState.Interface { m_semaphore = new Object(); m_dataSem = new Object(); - m_packetDataAvailableSem = ByteBuffer.allocateDirect(4); - // set the byte order - m_packetDataAvailableSem.order(ByteOrder.LITTLE_ENDIAN); - + m_packetDataAvailableSem = HALUtil.initializeMutexNormal(); FRCNetworkCommunicationsLibrary.setNewDataSem(m_packetDataAvailableSem); m_thread = new Thread(new DriverStationTask(this), "FRCDriverStation"); @@ -209,7 +208,11 @@ public class DriverStation implements RobotState.Interface { * @return The battery voltage. */ public double getBatteryVoltage() { - return 0.0; // TODO + IntBuffer status = ByteBuffer.allocateDirect(4).asIntBuffer(); + float voltage = PowerJNI.getVinVoltage(status); + HALUtil.checkStatus(status); + + return voltage; } /** diff --git a/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/hal/PowerJNI.java b/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/hal/PowerJNI.java new file mode 100644 index 0000000000..faef60ead0 --- /dev/null +++ b/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/hal/PowerJNI.java @@ -0,0 +1,14 @@ +package edu.wpi.first.wpilibj.hal; + +import java.nio.IntBuffer; + +public class PowerJNI extends JNIWrapper { + public static native float getVinVoltage(IntBuffer status); + public static native float getVinCurrent(IntBuffer status); + public static native float getUserVoltage6V(IntBuffer status); + public static native float getUserCurrent6V(IntBuffer status); + public static native float getUserVoltage5V(IntBuffer status); + public static native float getUserCurrent5V(IntBuffer status); + public static native float getUserVoltage3V3(IntBuffer status); + public static native float getUserCurrent3V3(IntBuffer status); +} diff --git a/wpilibj/wpilibJavaJNI/lib/PowerJNI.cpp b/wpilibj/wpilibJavaJNI/lib/PowerJNI.cpp new file mode 100644 index 0000000000..0d5f802c4f --- /dev/null +++ b/wpilibj/wpilibJavaJNI/lib/PowerJNI.cpp @@ -0,0 +1,99 @@ +#include +#include "edu_wpi_first_wpilibj_hal_PowerJNI.h" +#include "HAL/Power.hpp" + +/* + * Class: edu_wpi_first_wpilibj_hal_PowerJNI + * Method: getVinVoltage + * Signature: (Ljava/nio/IntBuffer;)F + */ +JNIEXPORT jfloat JNICALL Java_edu_wpi_first_wpilibj_hal_PowerJNI_getVinVoltage + (JNIEnv * env, jclass, jobject status) +{ + jint * statusPtr = (jint*)env->GetDirectBufferAddress(status); + return getVinVoltage(statusPtr); +} + +/* + * Class: edu_wpi_first_wpilibj_hal_PowerJNI + * Method: getVinCurrent + * Signature: (Ljava/nio/IntBuffer;)F + */ +JNIEXPORT jfloat JNICALL Java_edu_wpi_first_wpilibj_hal_PowerJNI_getVinCurrent + (JNIEnv * env, jclass, jobject status) +{ + jint * statusPtr = (jint*)env->GetDirectBufferAddress(status); + return getVinCurrent(statusPtr); +} + +/* + * Class: edu_wpi_first_wpilibj_hal_PowerJNI + * Method: getUserVoltage6V + * Signature: (Ljava/nio/IntBuffer;)F + */ +JNIEXPORT jfloat JNICALL Java_edu_wpi_first_wpilibj_hal_PowerJNI_getUserVoltage6V + (JNIEnv * env, jclass, jobject status) +{ + jint * statusPtr = (jint*)env->GetDirectBufferAddress(status); + return getUserVoltage6V(statusPtr); +} + +/* + * Class: edu_wpi_first_wpilibj_hal_PowerJNI + * Method: getUserCurrent6V + * Signature: (Ljava/nio/IntBuffer;)F + */ +JNIEXPORT jfloat JNICALL Java_edu_wpi_first_wpilibj_hal_PowerJNI_getUserCurrent6V + (JNIEnv * env, jclass, jobject status) +{ + jint * statusPtr = (jint*)env->GetDirectBufferAddress(status); + return getUserCurrent6V(statusPtr); +} + +/* + * Class: edu_wpi_first_wpilibj_hal_PowerJNI + * Method: getUserVoltage5V + * Signature: (Ljava/nio/IntBuffer;)F + */ +JNIEXPORT jfloat JNICALL Java_edu_wpi_first_wpilibj_hal_PowerJNI_getUserVoltage5V + (JNIEnv * env, jclass, jobject status) +{ + jint * statusPtr = (jint*)env->GetDirectBufferAddress(status); + return getUserVoltage5V(statusPtr); +} + +/* + * Class: edu_wpi_first_wpilibj_hal_PowerJNI + * Method: getUserCurrent5V + * Signature: (Ljava/nio/IntBuffer;)F + */ +JNIEXPORT jfloat JNICALL Java_edu_wpi_first_wpilibj_hal_PowerJNI_getUserCurrent5V + (JNIEnv * env, jclass, jobject status) +{ + jint * statusPtr = (jint*)env->GetDirectBufferAddress(status); + return getUserCurrent5V(statusPtr); +} + +/* + * Class: edu_wpi_first_wpilibj_hal_PowerJNI + * Method: getUserVoltage3V3 + * Signature: (Ljava/nio/IntBuffer;)F + */ +JNIEXPORT jfloat JNICALL Java_edu_wpi_first_wpilibj_hal_PowerJNI_getUserVoltage3V3 + (JNIEnv * env, jclass, jobject status) +{ + jint * statusPtr = (jint*)env->GetDirectBufferAddress(status); + return getUserVoltage3V3(statusPtr); +} + +/* + * Class: edu_wpi_first_wpilibj_hal_PowerJNI + * Method: getUserCurrent3V3 + * Signature: (Ljava/nio/IntBuffer;)F + */ +JNIEXPORT jfloat JNICALL Java_edu_wpi_first_wpilibj_hal_PowerJNI_getUserCurrent3V3 + (JNIEnv * env, jclass, jobject status) +{ + jint * statusPtr = (jint*)env->GetDirectBufferAddress(status); + return getUserCurrent3V3(statusPtr); +} diff --git a/wpilibj/wpilibJavaJNI/pom.xml b/wpilibj/wpilibJavaJNI/pom.xml index 3ef82d2a4d..6da2c6629c 100644 --- a/wpilibj/wpilibJavaJNI/pom.xml +++ b/wpilibj/wpilibJavaJNI/pom.xml @@ -126,6 +126,7 @@ this default location, specify a value for the 'embeddedJDKHome' property at the edu.wpi.first.wpilibj.hal.SolenoidJNI edu.wpi.first.wpilibj.hal.CompressorJNI edu.wpi.first.wpilibj.hal.PDPJNI + edu.wpi.first.wpilibj.hal.PowerJNI