mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-26 01:51:41 +00:00
[hal] Fix unfinished/incorrect GetCPUTemp functions (#5598)
This commit is contained in:
@@ -151,6 +151,14 @@ public class RoboRioDataJNI extends JNIWrapper {
|
||||
|
||||
public static native void setBrownoutVoltage(double brownoutVoltage);
|
||||
|
||||
public static native int registerCPUTempCallback(NotifyCallback callback, boolean initialNotify);
|
||||
|
||||
public static native void cancelCPUTempCallback(int uid);
|
||||
|
||||
public static native double getCPUTemp();
|
||||
|
||||
public static native void setCPUTemp(double cpuTemp);
|
||||
|
||||
public static native String getSerialNumber();
|
||||
|
||||
public static native void setSerialNumber(String serialNumber);
|
||||
|
||||
@@ -28,7 +28,7 @@ DEFINE_CAPI(int32_t, UserFaults6V, 0)
|
||||
DEFINE_CAPI(int32_t, UserFaults5V, 0)
|
||||
DEFINE_CAPI(int32_t, UserFaults3V3, 0)
|
||||
DEFINE_CAPI(double, BrownoutVoltage, 6.75)
|
||||
DEFINE_CAPI(int32_t, CPUTemp, 16)
|
||||
DEFINE_CAPI(double, CPUTemp, 45.0)
|
||||
|
||||
int32_t HALSIM_RegisterRoboRioSerialNumberCallback(
|
||||
HAL_RoboRioStringCallback callback, void* param, HAL_Bool initialNotify) {
|
||||
|
||||
@@ -828,6 +828,56 @@ Java_edu_wpi_first_hal_simulation_RoboRioDataJNI_setBrownoutVoltage
|
||||
HALSIM_SetRoboRioBrownoutVoltage(value);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_simulation_RoboRioDataJNI
|
||||
* Method: registerCPUTempCallback
|
||||
* Signature: (Ljava/lang/Object;Z)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_edu_wpi_first_hal_simulation_RoboRioDataJNI_registerCPUTempCallback
|
||||
(JNIEnv* env, jclass, jobject callback, jboolean initialNotify)
|
||||
{
|
||||
return sim::AllocateCallbackNoIndex(env, callback, initialNotify,
|
||||
&HALSIM_RegisterRoboRioCPUTempCallback);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_simulation_RoboRioDataJNI
|
||||
* Method: cancelCPUTempCallback
|
||||
* Signature: (I)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL
|
||||
Java_edu_wpi_first_hal_simulation_RoboRioDataJNI_cancelCPUTempCallback
|
||||
(JNIEnv* env, jclass, jint handle)
|
||||
{
|
||||
return sim::FreeCallbackNoIndex(env, handle,
|
||||
&HALSIM_CancelRoboRioCPUTempCallback);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_simulation_RoboRioDataJNI
|
||||
* Method: getCPUTemp
|
||||
* Signature: ()D
|
||||
*/
|
||||
JNIEXPORT jdouble JNICALL
|
||||
Java_edu_wpi_first_hal_simulation_RoboRioDataJNI_getCPUTemp
|
||||
(JNIEnv*, jclass)
|
||||
{
|
||||
return HALSIM_GetRoboRioCPUTemp();
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_simulation_RoboRioDataJNI
|
||||
* Method: setCPUTemp
|
||||
* Signature: (D)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL
|
||||
Java_edu_wpi_first_hal_simulation_RoboRioDataJNI_setCPUTemp
|
||||
(JNIEnv*, jclass, jdouble cpuTemp)
|
||||
{
|
||||
HALSIM_SetRoboRioCPUTemp(cpuTemp);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_simulation_RoboRioDataJNI
|
||||
* Method: getSerialNumber
|
||||
|
||||
@@ -145,8 +145,8 @@ int32_t HALSIM_RegisterRoboRioCPUTempCallback(HAL_NotifyCallback callback,
|
||||
void* param,
|
||||
HAL_Bool initialNotify);
|
||||
void HALSIM_CancelRoboRioCPUTempCallback(int32_t uid);
|
||||
HAL_Bool HALSIM_GetRoboRioCPUTemp(void);
|
||||
void HALSIM_SetRoboRioUserCPUTemp(HAL_Bool userActive3V3);
|
||||
double HALSIM_GetRoboRioCPUTemp(void);
|
||||
void HALSIM_SetRoboRioCPUTemp(double cpuTemp);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
|
||||
@@ -32,7 +32,7 @@ void RoboRioData::ResetData() {
|
||||
userFaults5V.Reset(0);
|
||||
userFaults3V3.Reset(0);
|
||||
brownoutVoltage.Reset(6.75);
|
||||
cpuTemp.Reset(100);
|
||||
cpuTemp.Reset(45.0);
|
||||
m_serialNumber = "";
|
||||
m_comments = "";
|
||||
}
|
||||
@@ -133,7 +133,7 @@ DEFINE_CAPI(int32_t, UserFaults6V, userFaults6V)
|
||||
DEFINE_CAPI(int32_t, UserFaults5V, userFaults5V)
|
||||
DEFINE_CAPI(int32_t, UserFaults3V3, userFaults3V3)
|
||||
DEFINE_CAPI(double, BrownoutVoltage, brownoutVoltage)
|
||||
DEFINE_CAPI(int32_t, CPUTemp, cpuTemp)
|
||||
DEFINE_CAPI(double, CPUTemp, cpuTemp)
|
||||
|
||||
int32_t HALSIM_RegisterRoboRioSerialNumberCallback(
|
||||
HAL_RoboRioStringCallback callback, void* param, HAL_Bool initialNotify) {
|
||||
|
||||
@@ -58,7 +58,7 @@ class RoboRioData {
|
||||
SimDataValue<int32_t, HAL_MakeInt, GetUserFaults3V3Name> userFaults3V3{0};
|
||||
SimDataValue<double, HAL_MakeDouble, GetBrownoutVoltageName> brownoutVoltage{
|
||||
6.75};
|
||||
SimDataValue<double, HAL_MakeDouble, GetCPUTempName> cpuTemp{100};
|
||||
SimDataValue<double, HAL_MakeDouble, GetCPUTempName> cpuTemp{45.0};
|
||||
|
||||
int32_t RegisterSerialNumberCallback(HAL_RoboRioStringCallback callback,
|
||||
void* param, HAL_Bool initialNotify);
|
||||
|
||||
@@ -284,6 +284,23 @@ void RoboRioSim::SetBrownoutVoltage(units::volt_t vInVoltage) {
|
||||
HALSIM_SetRoboRioBrownoutVoltage(vInVoltage.value());
|
||||
}
|
||||
|
||||
std::unique_ptr<CallbackStore> RoboRioSim::RegisterCPUTempCallback(
|
||||
NotifyCallback callback, bool initialNotify) {
|
||||
auto store = std::make_unique<CallbackStore>(
|
||||
-1, callback, &HALSIM_CancelRoboRioCPUTempCallback);
|
||||
store->SetUid(HALSIM_RegisterRoboRioCPUTempCallback(
|
||||
&CallbackStoreThunk, store.get(), initialNotify));
|
||||
return store;
|
||||
}
|
||||
|
||||
units::celsius_t RoboRioSim::GetCPUTemp() {
|
||||
return units::celsius_t{HALSIM_GetRoboRioCPUTemp()};
|
||||
}
|
||||
|
||||
void RoboRioSim::SetCPUTemp(units::celsius_t cpuTemp) {
|
||||
HALSIM_SetRoboRioCPUTemp(cpuTemp.value());
|
||||
}
|
||||
|
||||
std::string RoboRioSim::GetSerialNumber() {
|
||||
char serialNum[9];
|
||||
size_t len = HALSIM_GetRoboRioSerialNumber(serialNum, sizeof(serialNum));
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include <string>
|
||||
|
||||
#include <units/current.h>
|
||||
#include <units/temperature.h>
|
||||
#include <units/voltage.h>
|
||||
|
||||
#include "frc/simulation/CallbackStore.h"
|
||||
@@ -435,6 +436,31 @@ class RoboRioSim {
|
||||
*/
|
||||
static void SetBrownoutVoltage(units::volt_t brownoutVoltage);
|
||||
|
||||
/**
|
||||
* Register a callback to be run whenever the cpu temp changes.
|
||||
*
|
||||
* @param callback the callback
|
||||
* @param initialNotify whether to call the callback with the initial state
|
||||
* @return the CallbackStore object associated with this callback
|
||||
*/
|
||||
[[nodiscard]]
|
||||
static std::unique_ptr<CallbackStore> RegisterCPUTempCallback(
|
||||
NotifyCallback callback, bool initialNotify);
|
||||
|
||||
/**
|
||||
* Get the cpu temp.
|
||||
*
|
||||
* @return the cpu temp.
|
||||
*/
|
||||
static units::celsius_t GetCPUTemp();
|
||||
|
||||
/**
|
||||
* Define the cpu temp.
|
||||
*
|
||||
* @param cpuTemp the new cpu temp.
|
||||
*/
|
||||
static void SetCPUTemp(units::celsius_t cpuTemp);
|
||||
|
||||
/**
|
||||
* Get the serial number.
|
||||
*
|
||||
|
||||
@@ -207,6 +207,21 @@ TEST(RoboRioSimTest, Set3V3) {
|
||||
EXPECT_EQ(kTestFaults, RobotController::GetFaultCount3V3());
|
||||
}
|
||||
|
||||
TEST(RoboRioSimTest, SetCPUTemp) {
|
||||
RoboRioSim::ResetData();
|
||||
|
||||
DoubleCallback callback;
|
||||
auto cbHandle =
|
||||
RoboRioSim::RegisterCPUTempCallback(callback.GetCallback(), false);
|
||||
constexpr double kCPUTemp = 100.0;
|
||||
|
||||
RoboRioSim::SetCPUTemp(units::celsius_t{kCPUTemp});
|
||||
EXPECT_TRUE(callback.WasTriggered());
|
||||
EXPECT_EQ(kCPUTemp, callback.GetLastValue());
|
||||
EXPECT_EQ(kCPUTemp, RoboRioSim::GetCPUTemp().value());
|
||||
EXPECT_EQ(kCPUTemp, RobotController::GetCPUTemp().value());
|
||||
}
|
||||
|
||||
TEST(RoboRioSimTest, SetSerialNumber) {
|
||||
const std::string kSerialNum = "Hello";
|
||||
|
||||
|
||||
@@ -525,6 +525,38 @@ public final class RoboRioSim {
|
||||
RoboRioDataJNI.setBrownoutVoltage(vInVoltage);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a callback to be run whenever the cpu temp changes.
|
||||
*
|
||||
* @param callback the callback
|
||||
* @param initialNotify whether to call the callback with the initial state
|
||||
* @return the {@link CallbackStore} object associated with this callback. Save a reference to
|
||||
* this object so GC doesn't cancel the callback.
|
||||
*/
|
||||
public static CallbackStore registerCPUTempCallback(
|
||||
NotifyCallback callback, boolean initialNotify) {
|
||||
int uid = RoboRioDataJNI.registerCPUTempCallback(callback, initialNotify);
|
||||
return new CallbackStore(uid, RoboRioDataJNI::cancelCPUTempCallback);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the cpu temp.
|
||||
*
|
||||
* @return the cpu temp.
|
||||
*/
|
||||
public static double getCPUTemp() {
|
||||
return RoboRioDataJNI.getCPUTemp();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the cpu temp.
|
||||
*
|
||||
* @param cpuTemp the new cpu temp.
|
||||
*/
|
||||
public static void setCPUTemp(double cpuTemp) {
|
||||
RoboRioDataJNI.setCPUTemp(cpuTemp);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the serial number.
|
||||
*
|
||||
|
||||
@@ -209,6 +209,23 @@ class RoboRioSimTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCPUTemp() {
|
||||
RoboRioSim.resetData();
|
||||
|
||||
DoubleCallback callback = new DoubleCallback();
|
||||
|
||||
try (CallbackStore cb = RoboRioSim.registerCPUTempCallback(callback, false)) {
|
||||
final double kCPUTemp = 100.0;
|
||||
|
||||
RoboRioSim.setCPUTemp(kCPUTemp);
|
||||
assertTrue(callback.wasTriggered());
|
||||
assertEquals(kCPUTemp, callback.getSetValue());
|
||||
assertEquals(kCPUTemp, RoboRioSim.getCPUTemp());
|
||||
assertEquals(kCPUTemp, RobotController.getCPUTemp());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSerialNumber() {
|
||||
RoboRioSim.resetData();
|
||||
|
||||
Reference in New Issue
Block a user