Make predictions using a .onnx model file directly in the Flutter app.

Key Files

  • onnx_model.dart: Loads model.onnxand provides apredict()` function.
  • sensor_based_nav_controller.dart: Collects sensor data and triggers the prediction.

How It Works

  1. HardwareSensorService provides sensor data (accelerometer, gyroscope, magnetometer).

  2. SensorBasedNavController:

    • Collects 10 current sensor values (_currentSensorSamples)
    • Stores historical values (_historicalBursts) and past predictions (_historicalDeltas)
    • Prediction Trigger: Once 10 samples are collected, OnnxModel.predict() is called with both current and historical data
      Code Reference on GitLab: Sensor-Based-Navigation -> MainBranch
    • After prediction, old data stores are updated
    • Data is updated every 100ms
  3. The prediction result is displayed or used within the app.

  4. Prerequisite: @ build.gradle

    android {
        compileSdk = flutter.compileSdkVersion
        ndkVersion = "27.0.12077973"
      
        defaultConfig {
            minSdk = 24
            targetSdk = 35
        }
    
    

Usage

  • subscribe to one or more streams;
      class _MyHomePageState extends State<MyHomePage> {
     
    final _controller = SensorBasedNavController3.instance; //Model3/Controller3 is currently the only one that can be used. Please use this one.
    Future<void> initializeAndLoad() async {
      await _controller.initialize();
      await startPrediction();
    }
    
    Future<void> startPrediction() async {
      // Typically, only one of these 3 streams should be required, depending on how the navigation is to be implemented. 
      // This example is only intended to show how they are subscribed to and what data they return
      _controller.coordiates.listen((coordiatesValues) { //Type: LatLng[double Latitude, double Longitude]
        setState(() {
          LatLng currentCoordinates = coordiatesValues; 
        });
      });
      _controller.navData.listen((navDataValues) { // Type navData[double distance, double heading, double height] 
        setState(() {
          NavData currentNavData = navDataValues;
          double distance = currentNavData.distance
          double heading = currentNavData.heading
          double attitude = currentNavData.height
        });
      });
      _controller.meters.listen((metersData) { // Type List<double> double metersToNorth, double metersToEast
        setState(() {
          double northing = metersData[0]
          double easting = metersData[1]
        });
      });
    }
    
Set current coordinates
````dart 
    // Set cooridants as the starting point, for example.
    LatLng newStartCoordinates = LatLng(pos.latitude, pos.longitude);
    double height = 0;
    _controller.setCoordinates(newStartCoordinates, height);

Release mode known Runtime Errors that could occur

  • Create or update the file: android/app/proguard-rules.pro and add the following lines:
    -keep class ai.onnxruntime.** { *; }
    -keep class com.microsoft.** { *; }
    
  • Update your build.gradle file to include the ProGuard rules:
    configre your build.gradle file to use the proguard rules:
    
    buildTypes {
        release {
            signingConfig = signingConfigs.debug
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

Libraries

sensor_based_nav