← Unity APP
← Xvisio SDK Documentation Home Page
SLAM is abbreviation for Simultaneous Localization and Mapping. It was first proposed in the field of robotics. Which means that the robot starts from an unknown location in an unknown environment, locates its own position and posture through the repeatedly observed environmental features during the movement process. And then builds an incremental map of the surrounding environment according to its own position, so as to achieve the goal of simultaneous positioning and map construction.
CSLAM is a SLAM mode which contains Loop detection and map optimization.
It is recommended to use CSLAM mode in scenarios which requiring high-precision positioning.
You need to think of how to use map before building a good map. The map must contain all the viewpoint required by the application. If the final application is in another room, there is no reason to record the map in the current room. Similarly, if the final application will display some virtual objects on the ground, there is no reason to record the map in the room looking up with the camera. The moving path of the application should be the final map path. To ensure a good cycle closure, walk twice on the same path is needed: for example, start from a starting point, walk away and return to the starting point, and repeat this process again. During recording, walking twice on the same path can ensure a good overlap between different recording viewpoints of loop closure detection. It is important to avoid moving quickly or facing areas without features during map recording.
APK:Gesture.unity
This APK shows the CSLAM map recognition function which developed based on Xvisio AR glasses. The process of map building, map saving and map loading are completed by opening the CSLAM mode, creating the CSLAM map, and loading the CSLAM map.
Take a Gesture Demo (CslamDemo.unity) as an example.
Step 1:
As below, create corresponding buttons and text boxes in the project to call the CSLAM map interface and status display.
Step 2:
In the following code, we declare the CSLAM interface and callback function to be used. Refer to CSLAM Interface Introduction for more information.
/// <summary>
/// save slam feature point data
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct SlamMap
{
public Vector3 vertices;
};
#region Cslam loading and save function
/// <summary>
/// enable slam interface
/// </summary>
/// <returns></returns>
[DllImport("xslam-unity-wrapper")]
public static extern bool xslam_start_map();
/// <summary>
/// disable slam interface
/// </summary>
/// <returns></returns>
[DllImport("xslam-unity-wrapper")]
public static extern bool xslam_stop_map();
/// <summary>
/// save map interface
/// </summary>
/// <param name="mapStream"></param> save map to the storage address of the local device.
/// <param name="cslamSavedCallback"></param> callback function of saving map successfully
/// <param name="cslamLocalizedCallBack"></param> callback function of getting map matching
/// <returns></returns>
[DllImport("xslam-unity-wrapper")]
public static extern bool xslam_save_map_and_switch_to_cslam(string mapStream, detectCslamSaved_callback cslamSavedCallback, detectLocalized_callback cslamLocalizedCallBack);//size 0.16 rate 4
/// <summary>
/// load map interface
/// </summary>
/// <param name="mapStream"></param> load map path address
/// <param name="cslamSwitchedCallback"></param> load map path
/// <param name="cslamLocalizedCallBack"></param> callback function of getting map matching
/// <returns></returns>
[DllImport("xslam-unity-wrapper")]
public static extern bool xslam_load_map_and_switch_to_cslam(string mapStream, detectSwitched_callback cslamSwitchedCallback, detectLocalized_callback cslamLocalizedCallBack);//size 0.16 rate 4
/// <summary>
/// callback function of loading map
/// </summary>
/// <param name="map_quality"></param>
public delegate void detectSwitched_callback(int map_quality);
/// <summary>
/// callback function of loading map
/// </summary>
/// <param name="status_of_saved_map"></param>
/// <param name="map_quality"></param>
public delegate void detectCslamSaved_callback(int status_of_saved_map, int map_quality);
/// <summary>
/// callback function of getting map matching
/// </summary>
/// <param name="percent"></param>
public delegate void detectLocalized_callback(float percent);
/// <summary>
/// interface of getting feature points
/// </summary>
/// <param name="count"></param>
/// <returns></returns>
[DllImport("xslam-unity-wrapper")]
public static extern IntPtr xslam_get_slam_map(ref int count);
/// <summary>
/// Implementation of callback function for loading map
/// </summary>
/// <param name="map_quality"></param>
[MonoPInvokeCallback(typeof(detectSwitched_callback))]
static void OnCslamSwitched(int map_quality)
{
switch_map_quality = map_quality;
}
/// <summary>
/// Implementation of callback function for loading map
/// </summary>
/// <param name="percentc"></param>
[MonoPInvokeCallback(typeof(detectLocalized_callback))]
static void OnSaveLocalized(float percentc)
{
percentcD = percentc;
}
/// <summary>
/// Implementation of callback function for loading map matching
/// </summary>
/// <param name="percentc"></param>
[MonoPInvokeCallback(typeof(detectLocalized_callback))]
static void OnLoadLocalized(float percentc)
{
percentcD = percentc;
}
/// <summary>
/// Implementation of callback function for loading map
/// </summary>
/// <param name="status_of_saved_map"></param>
/// <param name="map_quality"></param>
[MonoPInvokeCallback(typeof(detectCslamSaved_callback))]
static void OnCslamSaved(int status_of_saved_map, int map_quality)
{
status_of_saved_mapq = status_of_saved_map;
save_map_qualityq = map_quality;
}
Step 3:
Click "start CSLAM" button to enter into CSLAM mode. The glass will scan (#table1) the surrounding environment. After scanning, click the "Save Map" button. At this time, a CSLAM map file named "map.bin" will generate in the map path text box. With the same time, as below, the current map matching will also be displayed in the corresponding text box.
Step 4:
Click "save pose" button, the position of the cheese object will be saved into the app cache.
Step 5:
Restart apk, click "start Cslam" button and "load map" button. The map which created in step 4 will be re-loaded. At the same time, after clicking the "Load Position" button, we can see that the cheese object appears in the position as saved in step 4.