mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
User code: - OpModeRobot used as the robot base class - LinearOpMode and PeriodicOpMode are provided opmode base classes - In Java, annotations can be used to automatically register opmode classes Additional user code functionality: - OpMode (string) is available in addition to the overall auto/teleop/test robot mode - OpMode does not indicate enable (enable/disable is still separate) - The HAL API uses integer UIDs; these are exposed at the user API level as well for faster checks - User code creates opmodes on startup (these have name, category, description, etc). DS: - DS will present opmode selection lists for auto and teleop for match/practice. During a match, the DS will automatically activate the selected opmode in the corresponding match period. - For testing, an overall mode is selected (e.g. teleop/auto/test) and a single opmode is selected Future work: - Command framework support/integration - Python annotation support - Unit tests (needs race-free DS sim updates) - Porting of examples Co-authored-by: Joseph Eng <91924258+KangarooKoala@users.noreply.github.com>
282 lines
6.7 KiB
C++
282 lines
6.7 KiB
C++
// 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.
|
|
|
|
#include "SimulatorJNI.h"
|
|
|
|
#include "BufferCallbackStore.h"
|
|
#include "CallbackStore.h"
|
|
#include "ConstBufferCallbackStore.h"
|
|
#include "OpModeOptionsCallbackStore.h"
|
|
#include "SimDeviceDataJNI.h"
|
|
#include "org_wpilib_hardware_hal_simulation_SimulatorJNI.h"
|
|
#include "wpi/hal/HAL.h"
|
|
#include "wpi/hal/handles/HandlesInternal.h"
|
|
#include "wpi/hal/simulation/MockHooks.h"
|
|
#include "wpi/util/jni_util.hpp"
|
|
|
|
using namespace wpi::util::java;
|
|
|
|
static JavaVM* jvm = nullptr;
|
|
static JClass notifyCallbackCls;
|
|
static JClass bufferCallbackCls;
|
|
static JClass constBufferCallbackCls;
|
|
static JClass biConsumerCls;
|
|
static jmethodID notifyCallbackCallback;
|
|
static jmethodID bufferCallbackCallback;
|
|
static jmethodID constBufferCallbackCallback;
|
|
static jmethodID biConsumerCallback;
|
|
|
|
static const JClassInit classes[] = {
|
|
{"org/wpilib/hardware/hal/simulation/NotifyCallback", ¬ifyCallbackCls},
|
|
{"org/wpilib/hardware/hal/simulation/BufferCallback", &bufferCallbackCls},
|
|
{"org/wpilib/hardware/hal/simulation/ConstBufferCallback",
|
|
&constBufferCallbackCls},
|
|
{"java/util/function/BiConsumer", &biConsumerCls},
|
|
};
|
|
|
|
static const struct JMethodInit {
|
|
JClass* cls;
|
|
const char* name;
|
|
const char* sig;
|
|
jmethodID* method;
|
|
} methods[] = {
|
|
{¬ifyCallbackCls, "callbackNative", "(Ljava/lang/String;IJD)V",
|
|
¬ifyCallbackCallback},
|
|
{&bufferCallbackCls, "callback", "(Ljava/lang/String;[BI)V",
|
|
&bufferCallbackCallback},
|
|
{&constBufferCallbackCls, "callback", "(Ljava/lang/String;[BI)V",
|
|
&constBufferCallbackCallback},
|
|
{&biConsumerCls, "accept", "(Ljava/lang/Object;Ljava/lang/Object;)V",
|
|
&biConsumerCallback},
|
|
};
|
|
|
|
namespace wpi::hal::sim {
|
|
jint SimOnLoad(JavaVM* vm, void* reserved) {
|
|
jvm = vm;
|
|
|
|
JNIEnv* env;
|
|
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
|
|
return JNI_ERR;
|
|
}
|
|
|
|
for (auto& c : classes) {
|
|
*c.cls = JClass(env, c.name);
|
|
if (!*c.cls) {
|
|
return JNI_ERR;
|
|
}
|
|
}
|
|
|
|
for (auto& m : methods) {
|
|
*m.method = env->GetMethodID(*m.cls, m.name, m.sig);
|
|
if (!*m.method) {
|
|
return JNI_ERR;
|
|
}
|
|
}
|
|
|
|
InitializeStore();
|
|
InitializeBufferStore();
|
|
InitializeConstBufferStore();
|
|
InitializeOpModeOptionsStore();
|
|
if (!InitializeSimDeviceDataJNI(env)) {
|
|
return JNI_ERR;
|
|
}
|
|
|
|
return JNI_VERSION_1_6;
|
|
}
|
|
|
|
void SimOnUnload(JavaVM* vm, void* reserved) {
|
|
JNIEnv* env;
|
|
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
|
|
return;
|
|
}
|
|
|
|
notifyCallbackCls.free(env);
|
|
bufferCallbackCls.free(env);
|
|
constBufferCallbackCls.free(env);
|
|
biConsumerCls.free(env);
|
|
FreeSimDeviceDataJNI(env);
|
|
jvm = nullptr;
|
|
}
|
|
|
|
JavaVM* GetJVM() {
|
|
return jvm;
|
|
}
|
|
|
|
jmethodID GetNotifyCallback() {
|
|
return notifyCallbackCallback;
|
|
}
|
|
|
|
jmethodID GetBufferCallback() {
|
|
return bufferCallbackCallback;
|
|
}
|
|
|
|
jmethodID GetConstBufferCallback() {
|
|
return constBufferCallbackCallback;
|
|
}
|
|
|
|
jmethodID GetBiConsumerCallback() {
|
|
return biConsumerCallback;
|
|
}
|
|
} // namespace wpi::hal::sim
|
|
|
|
extern "C" {
|
|
/*
|
|
* Class: org_wpilib_hardware_hal_simulation_SimulatorJNI
|
|
* Method: setRuntimeType
|
|
* Signature: (I)V
|
|
*/
|
|
JNIEXPORT void JNICALL
|
|
Java_org_wpilib_hardware_hal_simulation_SimulatorJNI_setRuntimeType
|
|
(JNIEnv*, jclass, jint type)
|
|
{
|
|
HALSIM_SetRuntimeType(static_cast<HAL_RuntimeType>(type));
|
|
}
|
|
|
|
/*
|
|
* Class: org_wpilib_hardware_hal_simulation_SimulatorJNI
|
|
* Method: waitForProgramStart
|
|
* Signature: ()V
|
|
*/
|
|
JNIEXPORT void JNICALL
|
|
Java_org_wpilib_hardware_hal_simulation_SimulatorJNI_waitForProgramStart
|
|
(JNIEnv*, jclass)
|
|
{
|
|
HALSIM_WaitForProgramStart();
|
|
}
|
|
|
|
/*
|
|
* Class: org_wpilib_hardware_hal_simulation_SimulatorJNI
|
|
* Method: setProgramStarted
|
|
* Signature: (Z)V
|
|
*/
|
|
JNIEXPORT void JNICALL
|
|
Java_org_wpilib_hardware_hal_simulation_SimulatorJNI_setProgramStarted
|
|
(JNIEnv*, jclass, jboolean started)
|
|
{
|
|
HALSIM_SetProgramStarted(started);
|
|
}
|
|
|
|
/*
|
|
* Class: org_wpilib_hardware_hal_simulation_SimulatorJNI
|
|
* Method: getProgramStarted
|
|
* Signature: ()Z
|
|
*/
|
|
JNIEXPORT jboolean JNICALL
|
|
Java_org_wpilib_hardware_hal_simulation_SimulatorJNI_getProgramStarted
|
|
(JNIEnv*, jclass)
|
|
{
|
|
return HALSIM_GetProgramStarted();
|
|
}
|
|
|
|
/*
|
|
* Class: org_wpilib_hardware_hal_simulation_SimulatorJNI
|
|
* Method: setProgramState
|
|
* Signature: (J)V
|
|
*/
|
|
JNIEXPORT void JNICALL
|
|
Java_org_wpilib_hardware_hal_simulation_SimulatorJNI_setProgramState
|
|
(JNIEnv*, jclass, jlong word)
|
|
{
|
|
HALSIM_SetProgramState({word});
|
|
}
|
|
|
|
/*
|
|
* Class: org_wpilib_hardware_hal_simulation_SimulatorJNI
|
|
* Method: nativeGetProgramState
|
|
* Signature: ()J
|
|
*/
|
|
JNIEXPORT jlong JNICALL
|
|
Java_org_wpilib_hardware_hal_simulation_SimulatorJNI_nativeGetProgramState
|
|
(JNIEnv*, jclass)
|
|
{
|
|
HAL_ControlWord word;
|
|
HALSIM_GetProgramState(&word);
|
|
return word.value;
|
|
}
|
|
|
|
/*
|
|
* Class: org_wpilib_hardware_hal_simulation_SimulatorJNI
|
|
* Method: restartTiming
|
|
* Signature: ()V
|
|
*/
|
|
JNIEXPORT void JNICALL
|
|
Java_org_wpilib_hardware_hal_simulation_SimulatorJNI_restartTiming
|
|
(JNIEnv*, jclass)
|
|
{
|
|
HALSIM_RestartTiming();
|
|
}
|
|
|
|
/*
|
|
* Class: org_wpilib_hardware_hal_simulation_SimulatorJNI
|
|
* Method: pauseTiming
|
|
* Signature: ()V
|
|
*/
|
|
JNIEXPORT void JNICALL
|
|
Java_org_wpilib_hardware_hal_simulation_SimulatorJNI_pauseTiming
|
|
(JNIEnv*, jclass)
|
|
{
|
|
HALSIM_PauseTiming();
|
|
}
|
|
|
|
/*
|
|
* Class: org_wpilib_hardware_hal_simulation_SimulatorJNI
|
|
* Method: resumeTiming
|
|
* Signature: ()V
|
|
*/
|
|
JNIEXPORT void JNICALL
|
|
Java_org_wpilib_hardware_hal_simulation_SimulatorJNI_resumeTiming
|
|
(JNIEnv*, jclass)
|
|
{
|
|
HALSIM_ResumeTiming();
|
|
}
|
|
|
|
/*
|
|
* Class: org_wpilib_hardware_hal_simulation_SimulatorJNI
|
|
* Method: isTimingPaused
|
|
* Signature: ()Z
|
|
*/
|
|
JNIEXPORT jboolean JNICALL
|
|
Java_org_wpilib_hardware_hal_simulation_SimulatorJNI_isTimingPaused
|
|
(JNIEnv*, jclass)
|
|
{
|
|
return HALSIM_IsTimingPaused();
|
|
}
|
|
|
|
/*
|
|
* Class: org_wpilib_hardware_hal_simulation_SimulatorJNI
|
|
* Method: stepTiming
|
|
* Signature: (J)V
|
|
*/
|
|
JNIEXPORT void JNICALL
|
|
Java_org_wpilib_hardware_hal_simulation_SimulatorJNI_stepTiming
|
|
(JNIEnv*, jclass, jlong delta)
|
|
{
|
|
HALSIM_StepTiming(delta);
|
|
}
|
|
|
|
/*
|
|
* Class: org_wpilib_hardware_hal_simulation_SimulatorJNI
|
|
* Method: stepTimingAsync
|
|
* Signature: (J)V
|
|
*/
|
|
JNIEXPORT void JNICALL
|
|
Java_org_wpilib_hardware_hal_simulation_SimulatorJNI_stepTimingAsync
|
|
(JNIEnv*, jclass, jlong delta)
|
|
{
|
|
HALSIM_StepTimingAsync(delta);
|
|
}
|
|
|
|
/*
|
|
* Class: org_wpilib_hardware_hal_simulation_SimulatorJNI
|
|
* Method: resetHandles
|
|
* Signature: ()V
|
|
*/
|
|
JNIEXPORT void JNICALL
|
|
Java_org_wpilib_hardware_hal_simulation_SimulatorJNI_resetHandles
|
|
(JNIEnv*, jclass)
|
|
{
|
|
wpi::hal::HandleBase::ResetGlobalHandles();
|
|
}
|
|
} // extern "C"
|