diff --git a/hal/src/main/java/edu/wpi/first/hal/HALUtil.java b/hal/src/main/java/edu/wpi/first/hal/HALUtil.java index 781b5a8c9c..4da20a22e2 100644 --- a/hal/src/main/java/edu/wpi/first/hal/HALUtil.java +++ b/hal/src/main/java/edu/wpi/first/hal/HALUtil.java @@ -15,6 +15,10 @@ public final class HALUtil extends JNIWrapper { public static final int NO_AVAILABLE_RESOURCES = -104; public static final int PARAMETER_OUT_OF_RANGE = -1028; + public static final int RUNTIME_ROBORIO = 0; + public static final int RUNTIME_ROBORIO2 = 1; + public static final int RUNTIME_SIMULATION = 2; + public static native short getFPGAVersion(); public static native int getFPGARevision(); diff --git a/hal/src/main/native/athena/HAL.cpp b/hal/src/main/native/athena/HAL.cpp index 9ad6a2c734..4de5007bda 100644 --- a/hal/src/main/native/athena/HAL.cpp +++ b/hal/src/main/native/athena/HAL.cpp @@ -232,7 +232,11 @@ const char* HAL_GetErrorMessage(int32_t code) { } HAL_RuntimeType HAL_GetRuntimeType(void) { - return HAL_Athena; + nLoadOut::tTargetClass targetClass = nLoadOut::getTargetClass(); + if (targetClass == nLoadOut::kTargetClass_RoboRIO2) { + return HAL_Runtime_RoboRIO2; + } + return HAL_Runtime_RoboRIO; } int32_t HAL_GetFPGAVersion(int32_t* status) { diff --git a/hal/src/main/native/cpp/jni/HALUtil.cpp b/hal/src/main/native/cpp/jni/HALUtil.cpp index b8ca4e2554..1070085c6a 100644 --- a/hal/src/main/native/cpp/jni/HALUtil.cpp +++ b/hal/src/main/native/cpp/jni/HALUtil.cpp @@ -30,6 +30,12 @@ using namespace wpi::java; #define kRIOStatusFeatureNotSupported (kRioStatusOffset - 193) #define kRIOStatusResourceNotInitialized -52010 +static_assert(edu_wpi_first_hal_HALUtil_RUNTIME_ROBORIO == HAL_Runtime_RoboRIO); +static_assert(edu_wpi_first_hal_HALUtil_RUNTIME_ROBORIO2 == + HAL_Runtime_RoboRIO2); +static_assert(edu_wpi_first_hal_HALUtil_RUNTIME_SIMULATION == + HAL_Runtime_Simulation); + static JavaVM* jvm = nullptr; static JException illegalArgExCls; static JException boundaryExCls; diff --git a/hal/src/main/native/include/hal/HALBase.h b/hal/src/main/native/include/hal/HALBase.h index ce2a420711..fb937877fe 100644 --- a/hal/src/main/native/include/hal/HALBase.h +++ b/hal/src/main/native/include/hal/HALBase.h @@ -15,7 +15,7 @@ */ // clang-format off -HAL_ENUM(HAL_RuntimeType) { HAL_Athena, HAL_Mock }; +HAL_ENUM(HAL_RuntimeType) { HAL_Runtime_RoboRIO, HAL_Runtime_RoboRIO2, HAL_Runtime_Simulation }; // clang-format on #ifdef __cplusplus @@ -64,6 +64,11 @@ int32_t HAL_GetFPGAVersion(int32_t* status); */ int64_t HAL_GetFPGARevision(int32_t* status); +/** + * Returns the runtime type of the HAL. + * + * @return HAL Runtime Type + */ HAL_RuntimeType HAL_GetRuntimeType(void); /** diff --git a/hal/src/main/native/sim/HAL.cpp b/hal/src/main/native/sim/HAL.cpp index 3b7b6314e5..085c1d70fa 100644 --- a/hal/src/main/native/sim/HAL.cpp +++ b/hal/src/main/native/sim/HAL.cpp @@ -51,7 +51,7 @@ class SimPeriodicCallbackRegistry : public impl::SimCallbackRegistryBase { }; } // namespace -static HAL_RuntimeType runtimeType{HAL_Mock}; +static HAL_RuntimeType runtimeType{HAL_Runtime_Simulation}; static wpi::spinlock gOnShutdownMutex; static std::vector> gOnShutdown; static SimPeriodicCallbackRegistry gSimPeriodicBefore; diff --git a/hal/src/test/native/cpp/HALTests.cpp b/hal/src/test/native/cpp/HALTests.cpp index f9dd291a88..bda54b8736 100644 --- a/hal/src/test/native/cpp/HALTests.cpp +++ b/hal/src/test/native/cpp/HALTests.cpp @@ -7,6 +7,6 @@ namespace hal { TEST(HALTests, RuntimeType) { - EXPECT_EQ(HAL_RuntimeType::HAL_Mock, HAL_GetRuntimeType()); + EXPECT_EQ(HAL_RuntimeType::HAL_Runtime_Simulation, HAL_GetRuntimeType()); } } // namespace hal diff --git a/wpilibc/src/main/native/cppcs/RobotBase.cpp b/wpilibc/src/main/native/cppcs/RobotBase.cpp index ccb1d02634..78c286c5f4 100644 --- a/wpilibc/src/main/native/cppcs/RobotBase.cpp +++ b/wpilibc/src/main/native/cppcs/RobotBase.cpp @@ -25,6 +25,13 @@ #include "frc/livewindow/LiveWindow.h" #include "frc/smartdashboard/SmartDashboard.h" +static_assert(frc::RuntimeType::kRoboRIO == + static_cast(HAL_Runtime_RoboRIO)); +static_assert(frc::RuntimeType::kRoboRIO2 == + static_cast(HAL_Runtime_RoboRIO2)); +static_assert(frc::RuntimeType::kSimulation == + static_cast(HAL_Runtime_Simulation)); + using SetCameraServerSharedFP = void (*)(frc::CameraServerShared*); using namespace frc; @@ -213,6 +220,10 @@ std::thread::id RobotBase::GetThreadId() { return m_threadId; } +RuntimeType RobotBase::GetRuntimeType() { + return static_cast(HAL_GetRuntimeType()); +} + RobotBase::RobotBase() { m_threadId = std::this_thread::get_id(); diff --git a/wpilibc/src/main/native/include/frc/RobotBase.h b/wpilibc/src/main/native/include/frc/RobotBase.h index b1fe4c8272..70c6093cf7 100644 --- a/wpilibc/src/main/native/include/frc/RobotBase.h +++ b/wpilibc/src/main/native/include/frc/RobotBase.h @@ -15,6 +15,7 @@ #include #include "frc/Errors.h" +#include "frc/RuntimeType.h" namespace frc { @@ -211,6 +212,13 @@ class RobotBase { virtual void EndCompetition() = 0; + /** + * Get the current runtime type. + * + * @return Current runtime type. + */ + static RuntimeType GetRuntimeType(); + /** * Get if the robot is real. * diff --git a/wpilibc/src/main/native/include/frc/RuntimeType.h b/wpilibc/src/main/native/include/frc/RuntimeType.h new file mode 100644 index 0000000000..c3a8a0bb71 --- /dev/null +++ b/wpilibc/src/main/native/include/frc/RuntimeType.h @@ -0,0 +1,9 @@ +// Copyright (c) FIRST and other WPILib contributors. +// Open Source Software; you can modify and/or share it under the terms of +// the WPILib BSD license file in the root directory of this project. + +#pragma once + +namespace frc { +enum RuntimeType { kRoboRIO, kRoboRIO2, kSimulation }; +} // namespace frc diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/RobotBase.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/RobotBase.java index 96c39b51ab..fe48b28b22 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/RobotBase.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/RobotBase.java @@ -160,6 +160,15 @@ public abstract class RobotBase implements AutoCloseable { @Override public void close() {} + /** + * Get the current runtime type. + * + * @return Current runtime type. + */ + public static RuntimeType getRuntimeType() { + return RuntimeType.getValue(HALUtil.getHALRuntimeType()); + } + /** * Get if the robot is a simulation. * @@ -175,7 +184,8 @@ public abstract class RobotBase implements AutoCloseable { * @return If the robot is running in the real world. */ public static boolean isReal() { - return HALUtil.getHALRuntimeType() == 0; + RuntimeType runtimeType = getRuntimeType(); + return runtimeType == RuntimeType.kRoboRIO || runtimeType == RuntimeType.kRoboRIO2; } /** diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/RuntimeType.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/RuntimeType.java new file mode 100644 index 0000000000..e7af118bfd --- /dev/null +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/RuntimeType.java @@ -0,0 +1,34 @@ +// Copyright (c) FIRST and other WPILib contributors. +// Open Source Software; you can modify and/or share it under the terms of +// the WPILib BSD license file in the root directory of this project. + +package edu.wpi.first.wpilibj; + +import edu.wpi.first.hal.HALUtil; + +public enum RuntimeType { + kRoboRIO(HALUtil.RUNTIME_ROBORIO), + kRoboRIO2(HALUtil.RUNTIME_ROBORIO2), + kSimulation(HALUtil.RUNTIME_SIMULATION); + + public final int value; + + RuntimeType(int value) { + this.value = value; + } + + /** + * Construct a runtime type from an int value. + * + * @param type Runtime type as int + * @return Matching runtime type + */ + public static RuntimeType getValue(int type) { + if (type == HALUtil.RUNTIME_ROBORIO) { + return RuntimeType.kRoboRIO; + } else if (type == HALUtil.RUNTIME_ROBORIO2) { + return RuntimeType.kRoboRIO2; + } + return RuntimeType.kSimulation; + } +}