using System.Runtime.InteropServices;
namespace RobotNet10.RobotApp.Xloc;
///
/// P/Invoke declarations for xloc C API
///
public static class XlocNativeInterface
{
// Library path - adjust/ if needed
private const string LibraryPath = "/usr/lib/libxloc.so";
#region Creation / Destruction
///
/// Create an xloc instance
///
/// TF3 buffer core (pass IntPtr.Zero if not using TF)
/// Handle to xloc instance
[DllImport(LibraryPath, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr xloc_create(IntPtr tfBuffer);
///
/// Destroy an xloc instance
///
/// Handle to xloc instance
[DllImport(LibraryPath, CallingConvention = CallingConvention.Cdecl)]
public static extern void xloc_destroy(IntPtr handle);
#endregion
#region Sensor Data Dispatch
///
/// Dispatch laser scan data to xloc
///
/// Handle to xloc instance
/// Sensor ID (null-terminated string)
/// Pointer to laser scan struct
[DllImport(LibraryPath, CallingConvention = CallingConvention.Cdecl)]
public static extern void xloc_dispatch_laserscan(
IntPtr handle,
[MarshalAs(UnmanagedType.LPUTF8Str)] string sensorId,
ref xloc_laserscan_t scan);
///
/// Dispatch IMU data to xloc
///
/// Handle to xloc instance
/// Sensor ID (null-terminated string)
/// Pointer to IMU struct
[DllImport(LibraryPath, CallingConvention = CallingConvention.Cdecl)]
public static extern void xloc_dispatch_imu(
IntPtr handle,
[MarshalAs(UnmanagedType.LPUTF8Str)] string sensorId,
ref xloc_imu_t imu);
///
/// Dispatch odometry data to xloc
///
/// Handle to xloc instance
/// Sensor ID (null-terminated string)
/// Pointer to odometry struct
[DllImport(LibraryPath, CallingConvention = CallingConvention.Cdecl)]
public static extern void xloc_dispatch_odometry(
IntPtr handle,
[MarshalAs(UnmanagedType.LPUTF8Str)] string sensorId,
ref xloc_odometry_t odom);
#endregion
#region Localization Control
///
/// Activate a map
///
[DllImport(LibraryPath, CallingConvention = CallingConvention.Cdecl)]
public static extern xloc_status_response_t xloc_activate_map(
IntPtr handle,
[MarshalAs(UnmanagedType.LPUTF8Str)] string mapFileName);
///
/// Switch to a different map
///
[DllImport(LibraryPath, CallingConvention = CallingConvention.Cdecl)]
public static extern xloc_status_response_t xloc_switch_map(
IntPtr handle,
[MarshalAs(UnmanagedType.LPUTF8Str)] string mapFileName,
int useInitialPose,
ref xloc_pose_t initialPose);
///
/// Start localization
///
[DllImport(LibraryPath, CallingConvention = CallingConvention.Cdecl)]
public static extern xloc_status_response_t xloc_start_localization(IntPtr handle);
///
/// Stop localization
///
[DllImport(LibraryPath, CallingConvention = CallingConvention.Cdecl)]
public static extern xloc_status_response_t xloc_stop_localization(IntPtr handle);
///
/// Start mapping
///
[DllImport(LibraryPath, CallingConvention = CallingConvention.Cdecl)]
public static extern xloc_status_response_t xloc_start_mapping(IntPtr handle);
///
/// Stop mapping and optionally save the map
///
[DllImport(LibraryPath, CallingConvention = CallingConvention.Cdecl)]
public static extern xloc_status_response_t xloc_stop_mapping(
IntPtr handle,
[MarshalAs(UnmanagedType.LPUTF8Str)] string mapFileName);
///
/// Set initial pose
///
[DllImport(LibraryPath, CallingConvention = CallingConvention.Cdecl)]
public static extern xloc_status_response_t xloc_set_initial_pose(
IntPtr handle,
ref xloc_pose_t initialPose);
///
/// Change map origin
///
[DllImport(LibraryPath, CallingConvention = CallingConvention.Cdecl)]
public static extern xloc_status_response_t xloc_change_map_origin(
IntPtr handle,
ref xloc_pose_t newMapOrigin);
///
/// Start updating existing map
///
[DllImport(LibraryPath, CallingConvention = CallingConvention.Cdecl)]
public static extern xloc_status_response_t xloc_start_update_map(IntPtr handle);
///
/// Stop updating map and optionally save
///
[DllImport(LibraryPath, CallingConvention = CallingConvention.Cdecl)]
public static extern xloc_status_response_t xloc_stop_update_map(
IntPtr handle,
int saveUpdatedMap);
///
/// Reset SLAM error state
///
[DllImport(LibraryPath, CallingConvention = CallingConvention.Cdecl)]
public static extern xloc_status_response_t xloc_reset_slam_error(IntPtr handle);
#endregion
#region Getters
///
/// Get current pose estimate
///
/// Pointer to pose (must be freed with xloc_free_pose)
[DllImport(LibraryPath, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr xloc_get_current_pose(IntPtr handle);
[DllImport(LibraryPath, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr xloc_get_diagnostics(IntPtr handle);
///
/// Get static grid map (from loaded map file)
///
/// Handle to xloc instance
/// If non-zero, reload map from file
/// Pointer to occupancy grid (must be freed with xloc_free_occupancy_grid)
[DllImport(LibraryPath, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr xloc_get_static_grid_map(IntPtr handle, int reloadFromFile);
///
/// Get online grid map (from SLAM)
///
/// Handle to xloc instance
/// Pointer to occupancy grid (must be freed with xloc_free_occupancy_grid)
[DllImport(LibraryPath, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr xloc_get_online_grid_map(IntPtr handle);
#endregion
#region Memory Management
///
/// Free status response
///
[DllImport(LibraryPath, CallingConvention = CallingConvention.Cdecl)]
public static extern void xloc_free_status_response(ref xloc_status_response_t response);
[DllImport(LibraryPath, CallingConvention = CallingConvention.Cdecl)]
public static extern void xloc_free_diagnostics(IntPtr diagnostics);
///
/// Free C string allocated by xloc
///
[DllImport(LibraryPath, CallingConvention = CallingConvention.Cdecl)]
public static extern void xloc_free_cstring(IntPtr str);
///
/// Free pose allocated by xloc
///
[DllImport(LibraryPath, CallingConvention = CallingConvention.Cdecl)]
public static extern void xloc_free_pose(IntPtr pose);
///
/// Free occupancy grid allocated by xloc
///
[DllImport(LibraryPath, CallingConvention = CallingConvention.Cdecl)]
public static extern void xloc_free_occupancy_grid(IntPtr grid);
#endregion
}