diff --git a/.gitignore b/.gitignore index cc23a83..e21ef1c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -*.py[cod] +*.py[codi] *.egg-info __pycache__ @@ -7,12 +7,15 @@ __pycache__ *.dll /navx/include -/navx/rpy-include -/navx/_init_navx.py -/navx/pkgcfg.py +/navx/trampolines +/navx/_init__navx.py +/navx/_init_studica_api.py +/navx/navx.pc +/navx/studica_api.pc /navx/version.py .cache .coverage build dist +/lib diff --git a/gen/ITimestampedDataSubscriber.yml b/gen/ITimestampedDataSubscriber.yml deleted file mode 100644 index c7c5217..0000000 --- a/gen/ITimestampedDataSubscriber.yml +++ /dev/null @@ -1,7 +0,0 @@ ---- - -classes: - ITimestampedDataSubscriber: - shared_ptr: true - methods: - timestampedDataReceived: diff --git a/meson.build b/meson.build new file mode 100644 index 0000000..cf42444 --- /dev/null +++ b/meson.build @@ -0,0 +1,23 @@ +project('robotpy-navx', ['cpp'], + default_options: ['warning_level=1', 'cpp_std=c++20', + 'b_colorout=auto', 'optimization=2', 'b_pie=true']) + +subdir('semiwrap') + +navx_sources += files( + 'navx/src/rpy/navx.cpp', +) + +# +# Static link +# + +cpp = meson.get_compiler('cpp') +libs_path = meson.current_source_dir() + '/lib/lib' + +navx_deps += [ + cpp.find_library('StudicaDriver', dirs: libs_path), + cpp.find_library('Studica', dirs: libs_path), +] + +subdir('semiwrap/modules') diff --git a/navx/__init__.py b/navx/__init__.py index 54eecfa..16eaf1b 100644 --- a/navx/__init__.py +++ b/navx/__init__.py @@ -1,6 +1,6 @@ -from . import _init_navx +from . import _init__navx -# autogenerated by 'robotpy-build create-imports navx navx._navx' +# autogenerated by 'semiwrap create-imports navx navx._navx' from ._navx import AHRS __all__ = ["AHRS"] diff --git a/navx/py.typed b/navx/py.typed new file mode 100644 index 0000000..e69de29 diff --git a/navx/src/ahrs.h.patch b/navx/src/ahrs.h.patch deleted file mode 100644 index b4d6387..0000000 --- a/navx/src/ahrs.h.patch +++ /dev/null @@ -1,11 +0,0 @@ ---- navx/include/AHRS.h.old 2022-12-04 15:04:42.698404048 +1100 -+++ navx/include/AHRS.h 2022-12-04 15:07:04.645489460 +1100 -@@ -158,6 +158,8 @@ - - AHRS(frc::SerialPort::Port serial_port_id, AHRS::SerialDataType data_type, uint8_t update_rate_hz); - -+ ~AHRS(); -+ - float GetPitch(); - float GetRoll(); - float GetYaw(); diff --git a/navx/src/rpy/navx.cpp b/navx/src/rpy/navx.cpp index c7e6095..f841613 100644 --- a/navx/src/rpy/navx.cpp +++ b/navx/src/rpy/navx.cpp @@ -1,3 +1,3 @@ -#include +#include -RPYBUILD_PYBIND11_MODULE(m) { initWrapper(m); } \ No newline at end of file +SEMIWRAP_PYBIND11_MODULE(m) { initWrapper(m); } \ No newline at end of file diff --git a/navx/src/sources.patch b/navx/src/sources.patch deleted file mode 100644 index eb39170..0000000 --- a/navx/src/sources.patch +++ /dev/null @@ -1,162 +0,0 @@ -diff -ur orig/AHRS.cpp patched/AHRS.cpp ---- orig/AHRS.cpp 2022-01-06 14:18:30.000000000 -0500 -+++ patched/AHRS.cpp 2022-02-24 00:42:06.632824071 -0500 -@@ -514,6 +514,20 @@ - SerialInit(serial_port_id, SerialDataType::kProcessedData, NAVX_DEFAULT_UPDATE_RATE_HZ); - } - -+AHRS::~AHRS() { -+ io->Stop(); -+ -+ if (task->joinable()) { -+ task->join(); -+ } -+ -+ delete integrator; -+ delete yaw_angle_tracker; -+ delete yaw_offset_tracker; -+ delete ahrs_internal; -+ delete io; -+} -+ - /** - * Returns the current pitch value (in degrees, from -180 to 180) - * reported by the sensor. Pitch is a measure of rotation around -diff -ur orig/SimIO.cpp patched/SimIO.cpp ---- orig/SimIO.cpp 2021-07-24 11:57:50.000000000 -0400 -+++ patched/SimIO.cpp 2022-02-24 01:31:32.806868996 -0500 -@@ -10,6 +10,9 @@ - #include "delay.h" - #include - -+#include -+#include -+ - using namespace frc; - using units::unit_cast; - -@@ -29,6 +32,9 @@ - board_id = {}; - - if (sim_device) { -+ int32_t status = 0; -+ m_notifier = HAL_InitializeNotifier(&status); -+ FRC_CheckErrorStatus(status, "{}", "SimIO initialize notifier"); - - // Booleans - -@@ -146,6 +152,12 @@ - } - - SimIO::~SimIO() { -+ int32_t status = 0; -+ HAL_NotifierHandle handle = m_notifier.exchange(0); -+ HAL_StopNotifier(handle, &status); -+ FRC_ReportError(status, "{}", "~SimIO stop"); -+ HAL_CleanNotifier(handle); -+ FRC_ReportError(status, "{}", "~SimIO clean"); - } - - bool SimIO::IsConnected() { -@@ -174,11 +186,38 @@ - void SimIO::ZeroDisplacement() { - } - -+bool SimIO::Wait(uint64_t us) { -+ int32_t status = 0; -+ HAL_NotifierHandle notifier = m_notifier.load(); -+ if (notifier == 0) { -+ return false; -+ } -+ -+ uint64_t tm = HAL_GetFPGATime(&status) + us; -+ if (status != 0) { -+ return false; -+ } -+ -+ HAL_UpdateNotifierAlarm(notifier, tm, &status); -+ if (status != 0) { -+ return false; -+ } -+ -+ uint64_t curTime = HAL_WaitForNotifierAlarm(notifier, &status); -+ if (curTime == 0 || status != 0) { -+ return false; -+ } -+ -+ return true; -+} -+ - void SimIO::Run() { - /* IO Loop */ - // Simulate startup delay -- delayMillis(50); -- -+ if (!Wait(50000)) { -+ return; -+ } -+ - // Default to connected state - is_connected = true; - notify_sink->ConnectDetected(); -@@ -195,7 +234,9 @@ - start_seconds = unit_cast(Timer::GetFPGATimestamp()); - - while (!stop) { -- delayMillis(20); -+ if (!Wait(20000)) { -+ return; -+ } - sensor_timestamp += 20; - UpdatePeriodicFromSimVariables(sensor_timestamp); - } -@@ -270,6 +311,12 @@ - - void SimIO::Stop() { - stop = true; -+ -+ HAL_NotifierHandle handle = m_notifier.load(); -+ if (handle) { -+ int32_t status; -+ HAL_StopNotifier(handle, &status); -+ } - } - - void SimIO::EnableLogging(bool enable) { -diff -ur orig/SimIO.h patched/SimIO.h ---- orig/SimIO.h 2020-04-23 10:31:38.000000000 -0400 -+++ patched/SimIO.h 2022-02-24 01:31:20.802783612 -0500 -@@ -9,6 +9,7 @@ - #define SRC_SIMIO_H_ - - #include -+#include - #include "IIOProvider.h" - #include "IMUProtocol.h" - #include "AHRSProtocol.h" -@@ -17,6 +18,7 @@ - #include "frc/Timer.h" - - #include -+#include - - class SimIO : public IIOProvider { - private: -@@ -47,6 +49,9 @@ - AHRSProtocol::AHRSPosUpdate ahrs_update; - IMUProtocol::GyroUpdate raw_data_update; - -+ // HAL handle, atomic for proper destruction -+ std::atomic m_notifier{0}; -+ - public: - SimIO( uint8_t update_rate_hz, - IIOCompleteNotification *notify_sink, -@@ -64,6 +69,7 @@ - - double GetRate(); - private: -+ bool Wait(uint64_t us); - void UpdatePeriodicFromSimVariables(long sensor_timestamp); - float CalculateNormal(float in1, float in2); - }; diff --git a/pyproject.toml b/pyproject.toml index 7619875..99bc504 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,60 +1,77 @@ [build-system] +build-backend = "hatchling.build" requires = [ - "robotpy-build~=2025.1.0", - "wpilib<2026,>=2025.3.2", + "hatchling", + "hatch-vcs", + "semiwrap~=0.1", + "hatch-meson~=0.1.0b2", + "hatch-robotpy~=0.2.0", + "wpilib~=2025.3.2.2", ] -[tool.robotpy-build] -base_package = "navx" -[tool.robotpy-build.metadata] +[project] name = "robotpy-navx" -description = "Python wrapper for KauaiLabs NavX sensor" -author = "RobotPy Development Team" -author_email = "robotpy@googlegroups.com" -url = "https://github.com/robotpy/robotpy-navx" +dynamic = ["version"] +description = "Python wrapper for Studica NavX sensor" license = "BSD-3-Clause" -install_requires = [ - "wpilib<2026,>=2025.3.2", +dependencies = [ + "wpilib~=2025.3.2.2", ] -[tool.robotpy-build.static_libs."studica_driver".maven_lib_download] -artifact_id = "Studica-driver" +[[project.authors]] +name = "RobotPy Development Team" +email = "robotpy@googlegroups.com" + +[project.urls] +"Source code" = "https://github.com/robotpy/robotpy-navx" + + +[tool.hatch.version] +source = "vcs" + +[tool.hatch.build.hooks.vcs] +version-file = "navx/version.py" + +[tool.hatch.build.targets.wheel] +packages = ["navx"] + + +[[tool.hatch.build.hooks.robotpy.maven_lib_download]] +artifact_id = "Studica-cpp" group_id = "com.studica.frc" repo_url = "https://dev.studica.com/maven/release/2025" version = "2025.0.1" -libs = ["StudicaDriver"] +staticlibs = ["Studica"] +extract_to = "lib" -[tool.robotpy-build.static_libs."studica_lib".maven_lib_download] -artifact_id = "Studica-cpp" +[[tool.hatch.build.hooks.robotpy.maven_lib_download]] +artifact_id = "Studica-driver" group_id = "com.studica.frc" repo_url = "https://dev.studica.com/maven/release/2025" version = "2025.0.1" -libs = ["Studica"] +staticlibs = ["StudicaDriver"] +extract_to = "lib" + + +[tool.hatch.build.hooks.semiwrap] + +[tool.hatch.build.hooks.meson] -[tool.robotpy-build.wrappers."navx"] + +[tool.semiwrap] +update_init = ["navx"] + +[tool.semiwrap.extension_modules."navx._navx"] name = "navx" + depends = [ - "wpilib_core", - "wpimath_cpp", - "wpimath_geometry", - "wpiHal", - "wpilibc", - "wpilib_core", - "wpimath_cpp", - "wpimath_geometry", - "wpiHal", - "ntcore", - "wpiutil", - "studica_driver", - "studica_lib", + "wpilib", ] -sources = [ - "navx/src/rpy/navx.cpp" +includes = [ + "lib/include" ] -generate = [ - { AHRS = "studica/AHRS.h" }, -] -generation_data = "gen" +[tool.semiwrap.extension_modules."navx._navx".headers] +AHRS = "studica/AHRS.h" diff --git a/semiwrap/.gitignore b/semiwrap/.gitignore new file mode 100644 index 0000000..6cdce10 --- /dev/null +++ b/semiwrap/.gitignore @@ -0,0 +1,3 @@ +/meson.build +/modules/meson.build +/trampolines/meson.build diff --git a/gen/AHRS.yml b/semiwrap/AHRS.yml similarity index 99% rename from gen/AHRS.yml rename to semiwrap/AHRS.yml index a385865..afaf691 100644 --- a/gen/AHRS.yml +++ b/semiwrap/AHRS.yml @@ -1,10 +1,9 @@ ---- extra_includes: - wpi/sendable/SendableHelper.h classes: - AHRS: + ::studica::AHRS: ignored_bases: - wpi::SendableHelper enums: @@ -621,8 +620,7 @@ classes: Calibrate: doc: | Does nothing - AHRS::BoardYawAxis: - shared_ptr: true + ::studica::AHRS::BoardYawAxis: attributes: board_axis: up: diff --git a/setup.py b/setup.py deleted file mode 100644 index 3542d0c..0000000 --- a/setup.py +++ /dev/null @@ -1,5 +0,0 @@ -#!/usr/bin/env python3 - -from robotpy_build.setup import setup - -setup() diff --git a/travis-requirements.txt b/travis-requirements.txt deleted file mode 100644 index 83ede75..0000000 --- a/travis-requirements.txt +++ /dev/null @@ -1,13 +0,0 @@ - -git+git://github.com/robotpy/pynetworktables.git - -git+git://github.com/robotpy/robotpy-wpilib.git#subdirectory=wpilib -git+git://github.com/robotpy/robotpy-wpilib.git#subdirectory=hal-base -git+git://github.com/robotpy/robotpy-wpilib.git#subdirectory=hal-sim - -git+git://github.com/robotpy/pyfrc.git - --e . - -pytest>=2.8 -coverage