Creation Native Code Library from MATLAB-code by TMC Compiler for using in a mobile application


Example:  TunerDemo  application for Android (TM)


The source of this example is available in subdirectory

 /examples/Simo_ADT/Simo_ADT_ws/TunerDemo

 of TMC Compiler distribution

This example illustrates the usage of TMC Compiler for creation a native code math library from MATLAB-code by TMC Compiler. The library implements native methods of a Java class in Android application. The application GUI is written in Java and calls the native code using Java Native Interface (JNI).

Prerequisites


1.Installed Android Development Tools (ADT) as a plugin for the Eclipse IDE from http://http://developer.android.com/sdk/installing/installing-adt.html
2.Installed Native Development Kit (NDK) from http://developer.android.com/tools/sdk/ndk/index.html

Note: The support of ADT is announced as ending now and the developers are recommended to migrate to Android Studio. However, ADT platform had been selected due to its native code debugging support.

Application GUI

After a launch main activity window appears:
TunerDemo window

User should enter the two parameters and presses "Run" button to start calculations. The calculations progress status will be shown in a tip tool. When finished, user should press "Show Nichols" button to view the results.
"Nichols plot" activity window appears and displays a graph that is updated by moving a slider or pressing one of buttons:
Nichols plot window



Native methods: interface between Java and C


The application is written in Java. The main activity class (file: src/com/tmc/tunerdemo/MainActivity.java)  is defined as

public class MainActivity extends Activity implements Runnable

This class supports multi-threading: the calculations in a math library are performed in a separate thread. The class declaration contains the following definitions for native methods:

public native String  runTmcTuning(float M,float R,float L, float Ts);
public native int pidgetNumFreqs();
public native int pidgetTypeCrl();
public native int pidgetNumPlants();
public native int pidgetGetPlants(double [] a);
public native int pidgetNumCrl();
public native int pidgetGetCrl(double [] a);

and the static code:
static {
    System.loadLibrary("TmcTunerDemo");
}

The first method performs calculations, others retrieve the results. The JNI-wrapper is manually written in C-code (file: jni/mainTunerDemoJni.c). It contains the native methods implementation:

jint Java_com_tmc_tunerdemo_MainActivity_pidgetNumFreqs(JNIEnv *env, jobject callingObject)
 {
 ...
 // Calls to the auto-generated tmc-functions here
...
 }

The calculation progress and exception handling in the native library are implemented by calling Java methods from C-code. Refer to file tmc_jni_except.c, functions  javaDispString and javaRaiseExcept.

Native code generation

To generate the C-code run the batch file MakeEx2_minigw.bat from the example directory. The C-code is put it into /jni/ directory of the project. Then the shared library (libTmcTunerDemo.so) is compiled by ADT environment as defined in its configuration file Android.mk. The code is linked with library tmcruntime_mingw_armv7.a. This pre-built library supported with the TMC Compiler was built by GCC toolchain for ARM-v7 CPU.












 

No comments: