[hal, wpilib] Add OpMode support (#7744)

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>
This commit is contained in:
Peter Johnson
2025-12-12 21:25:57 -07:00
committed by GitHub
parent 2a41b80e00
commit dacded37e5
163 changed files with 7454 additions and 2175 deletions

View File

@@ -4,7 +4,10 @@
#include <jni.h>
#include "../HALUtil.h"
#include "CallbackStore.h"
#include "OpModeOptionsCallbackStore.h"
#include "SimulatorJNI.h"
#include "org_wpilib_hardware_hal_simulation_DriverStationDataJNI.h"
#include "wpi/hal/simulation/DriverStationData.h"
#include "wpi/hal/simulation/MockHooks.h"
@@ -69,103 +72,53 @@ Java_org_wpilib_hardware_hal_simulation_DriverStationDataJNI_setEnabled
/*
* Class: org_wpilib_hardware_hal_simulation_DriverStationDataJNI
* Method: registerAutonomousCallback
* Method: registerRobotModeCallback
* Signature: (Ljava/lang/Object;Z)I
*/
JNIEXPORT jint JNICALL
Java_org_wpilib_hardware_hal_simulation_DriverStationDataJNI_registerAutonomousCallback
Java_org_wpilib_hardware_hal_simulation_DriverStationDataJNI_registerRobotModeCallback
(JNIEnv* env, jclass, jobject callback, jboolean initialNotify)
{
return sim::AllocateCallbackNoIndex(
env, callback, initialNotify,
&HALSIM_RegisterDriverStationAutonomousCallback);
&HALSIM_RegisterDriverStationRobotModeCallback);
}
/*
* Class: org_wpilib_hardware_hal_simulation_DriverStationDataJNI
* Method: cancelAutonomousCallback
* Method: cancelRobotModeCallback
* Signature: (I)V
*/
JNIEXPORT void JNICALL
Java_org_wpilib_hardware_hal_simulation_DriverStationDataJNI_cancelAutonomousCallback
(JNIEnv* env, jclass, jint handle)
{
return sim::FreeCallbackNoIndex(
env, handle, &HALSIM_CancelDriverStationAutonomousCallback);
}
/*
* Class: org_wpilib_hardware_hal_simulation_DriverStationDataJNI
* Method: getAutonomous
* Signature: ()Z
*/
JNIEXPORT jboolean JNICALL
Java_org_wpilib_hardware_hal_simulation_DriverStationDataJNI_getAutonomous
(JNIEnv*, jclass)
{
return HALSIM_GetDriverStationAutonomous();
}
/*
* Class: org_wpilib_hardware_hal_simulation_DriverStationDataJNI
* Method: setAutonomous
* Signature: (Z)V
*/
JNIEXPORT void JNICALL
Java_org_wpilib_hardware_hal_simulation_DriverStationDataJNI_setAutonomous
(JNIEnv*, jclass, jboolean value)
{
HALSIM_SetDriverStationAutonomous(value);
}
/*
* Class: org_wpilib_hardware_hal_simulation_DriverStationDataJNI
* Method: registerTestCallback
* Signature: (Ljava/lang/Object;Z)I
*/
JNIEXPORT jint JNICALL
Java_org_wpilib_hardware_hal_simulation_DriverStationDataJNI_registerTestCallback
(JNIEnv* env, jclass, jobject callback, jboolean initialNotify)
{
return sim::AllocateCallbackNoIndex(
env, callback, initialNotify, &HALSIM_RegisterDriverStationTestCallback);
}
/*
* Class: org_wpilib_hardware_hal_simulation_DriverStationDataJNI
* Method: cancelTestCallback
* Signature: (I)V
*/
JNIEXPORT void JNICALL
Java_org_wpilib_hardware_hal_simulation_DriverStationDataJNI_cancelTestCallback
Java_org_wpilib_hardware_hal_simulation_DriverStationDataJNI_cancelRobotModeCallback
(JNIEnv* env, jclass, jint handle)
{
return sim::FreeCallbackNoIndex(env, handle,
&HALSIM_CancelDriverStationTestCallback);
&HALSIM_CancelDriverStationRobotModeCallback);
}
/*
* Class: org_wpilib_hardware_hal_simulation_DriverStationDataJNI
* Method: getTest
* Signature: ()Z
* Method: nativeGetRobotMode
* Signature: ()I
*/
JNIEXPORT jboolean JNICALL
Java_org_wpilib_hardware_hal_simulation_DriverStationDataJNI_getTest
JNIEXPORT jint JNICALL
Java_org_wpilib_hardware_hal_simulation_DriverStationDataJNI_nativeGetRobotMode
(JNIEnv*, jclass)
{
return HALSIM_GetDriverStationTest();
return HALSIM_GetDriverStationRobotMode();
}
/*
* Class: org_wpilib_hardware_hal_simulation_DriverStationDataJNI
* Method: setTest
* Signature: (Z)V
* Method: nativeSetRobotMode
* Signature: (I)V
*/
JNIEXPORT void JNICALL
Java_org_wpilib_hardware_hal_simulation_DriverStationDataJNI_setTest
(JNIEnv*, jclass, jboolean value)
Java_org_wpilib_hardware_hal_simulation_DriverStationDataJNI_nativeSetRobotMode
(JNIEnv*, jclass, jint value)
{
HALSIM_SetDriverStationTest(value);
HALSIM_SetDriverStationRobotMode(static_cast<HAL_RobotMode>(value));
}
/*
@@ -423,6 +376,99 @@ Java_org_wpilib_hardware_hal_simulation_DriverStationDataJNI_setMatchTime
HALSIM_SetDriverStationMatchTime(value);
}
/*
* Class: org_wpilib_hardware_hal_simulation_DriverStationDataJNI
* Method: registerOpModeCallback
* Signature: (Ljava/lang/Object;Z)I
*/
JNIEXPORT jint JNICALL
Java_org_wpilib_hardware_hal_simulation_DriverStationDataJNI_registerOpModeCallback
(JNIEnv* env, jclass, jobject callback, jboolean initialNotify)
{
return sim::AllocateCallbackNoIndex(
env, callback, initialNotify,
&HALSIM_RegisterDriverStationOpModeCallback);
}
/*
* Class: org_wpilib_hardware_hal_simulation_DriverStationDataJNI
* Method: cancelOpModeCallback
* Signature: (I)V
*/
JNIEXPORT void JNICALL
Java_org_wpilib_hardware_hal_simulation_DriverStationDataJNI_cancelOpModeCallback
(JNIEnv* env, jclass, jint handle)
{
return sim::FreeCallbackNoIndex(env, handle,
&HALSIM_CancelDriverStationOpModeCallback);
}
/*
* Class: org_wpilib_hardware_hal_simulation_DriverStationDataJNI
* Method: getOpMode
* Signature: ()J
*/
JNIEXPORT jlong JNICALL
Java_org_wpilib_hardware_hal_simulation_DriverStationDataJNI_getOpMode
(JNIEnv* env, jclass)
{
return HALSIM_GetDriverStationOpMode();
}
/*
* Class: org_wpilib_hardware_hal_simulation_DriverStationDataJNI
* Method: setOpMode
* Signature: (J)V
*/
JNIEXPORT void JNICALL
Java_org_wpilib_hardware_hal_simulation_DriverStationDataJNI_setOpMode
(JNIEnv* env, jclass, jlong value)
{
HALSIM_SetDriverStationOpMode(value);
}
/*
* Class: org_wpilib_hardware_hal_simulation_DriverStationDataJNI
* Method: registerOpModeOptionsCallback
* Signature: (Ljava/lang/Object;Z)I
*/
JNIEXPORT jint JNICALL
Java_org_wpilib_hardware_hal_simulation_DriverStationDataJNI_registerOpModeOptionsCallback
(JNIEnv* env, jclass, jobject callback, jboolean initialNotify)
{
return sim::AllocateOpModeOptionsCallback(
env, callback, initialNotify, &HALSIM_RegisterOpModeOptionsCallback);
}
/*
* Class: org_wpilib_hardware_hal_simulation_DriverStationDataJNI
* Method: cancelOpModeOptionsCallback
* Signature: (I)V
*/
JNIEXPORT void JNICALL
Java_org_wpilib_hardware_hal_simulation_DriverStationDataJNI_cancelOpModeOptionsCallback
(JNIEnv* env, jclass, jint handle)
{
sim::FreeOpModeOptionsCallback(env, handle,
&HALSIM_CancelOpModeOptionsCallback);
}
/*
* Class: org_wpilib_hardware_hal_simulation_DriverStationDataJNI
* Method: getOpModeOptions
* Signature: ()[Ljava/lang/Object;
*/
JNIEXPORT jobjectArray JNICALL
Java_org_wpilib_hardware_hal_simulation_DriverStationDataJNI_getOpModeOptions
(JNIEnv* env, jclass)
{
int32_t count;
HAL_OpModeOption* options = HALSIM_GetOpModeOptions(&count);
auto rv = CreateOpModeOptionArray(env, {options, options + count});
HALSIM_FreeOpModeOptionsArray(options, count);
return rv;
}
/*
* Class: org_wpilib_hardware_hal_simulation_DriverStationDataJNI
* Method: setJoystickAxes