[hal] Update runtime enum to allow selecting roborio 2 (#3565)

In some cases, knowing roborio 2 might be useful. This also creates a higher level enum that might be usable later for the discussion on more complex runtime types.
This commit is contained in:
Thad House
2021-09-13 22:05:38 -07:00
committed by GitHub
parent 95a12e0ee8
commit 66abb39880
11 changed files with 96 additions and 5 deletions

View File

@@ -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();

View File

@@ -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) {

View File

@@ -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;

View File

@@ -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);
/**

View File

@@ -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<std::pair<void*, void (*)(void*)>> gOnShutdown;
static SimPeriodicCallbackRegistry gSimPeriodicBefore;

View File

@@ -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

View File

@@ -25,6 +25,13 @@
#include "frc/livewindow/LiveWindow.h"
#include "frc/smartdashboard/SmartDashboard.h"
static_assert(frc::RuntimeType::kRoboRIO ==
static_cast<frc::RuntimeType>(HAL_Runtime_RoboRIO));
static_assert(frc::RuntimeType::kRoboRIO2 ==
static_cast<frc::RuntimeType>(HAL_Runtime_RoboRIO2));
static_assert(frc::RuntimeType::kSimulation ==
static_cast<frc::RuntimeType>(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<RuntimeType>(HAL_GetRuntimeType());
}
RobotBase::RobotBase() {
m_threadId = std::this_thread::get_id();

View File

@@ -15,6 +15,7 @@
#include <wpi/mutex.h>
#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.
*

View File

@@ -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

View File

@@ -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;
}
/**

View File

@@ -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;
}
}