Processing Sensor Data

Sensor applications that access sensor data from the Sensor service can process this data with code that you generate with the MATLAB Coder application.

When you use a MATLAB function to process sensor data, you need to:

Generate C code from a MATLAB function

To generate source code for a MATLAB function that you can use to build into your QNX project do the following:

  1. In MATLAB, create a function to perform the processing of the sensor data. For example, a simple function to perform edge detection on input that's an image:
    function [ out ] = edgeImage( I )
    %EDGEIMAGE Uses a canny edge detector on the input image
    
    out = edge(I, 'canny', 0.05);
    
    end
                    
  2. Launch the MATLAB Coder application.

    The MATLAB Coder app guides you through the the following steps that you need to complete in order to generate your C code:

    • Select
    • Review
    • Define
    • Check
    • Generate
  3. Select the MATLAB function that you want to generate C code from.
  4. Review your code's readiness for code generation. MATLAB Coder may identify issues that can prevent your code to be successfully generated. Address any issues identified, save your changes, and continue.
  5. Define the input types (and output types, if any) associated with the function.

    You can either enter the types in directly, or use a MATLAB script that calls your function to automatically define the input and/or output types.

  6. Check for runtime issues by provide MATLAB with code or a script to use.
  7. Generate the C code. Provide the information for each entry according to the table below:
    Entry Option
    Build type: Source Code
    Output file name file_name (Select the name of the file for the source C code.)
    Language C
    Hardware Board None
    Device (vendor and type) Using the dropdown menus, select your target device's vendor and type. If they're not available, choose what's most appropriate for your device.
    Toolchain Automatically locate an installed toolchain (It's not necessary to specify a toolchain because your generated source C code will be built using the QNX toolchain in your QNX project.)
    Click Generate.
  8. Finish the code generation workflow. MATLAB Coder displays information on the generated code.

For more information on code generation using MATLAB Coder, see Generate Code from Application Containing Image Processing Functions or your MATLAB Coder documentation.

Build and use your C code

An example of the source code is provided which you can look at without a MATLAB license. The source code is located as part of Sensor code example package (adas-sensor-examples-vesrion.zip). After you extract the sensor source code package, its located under directory_of_extracted_zip/adas-sensor-examples-version/ source_package_adas_sensor/apps/matlab/matlab_codegen_example. To build and use it, you do require a license to the MATLAB product from Mathworks. To use the generated C code in your sensor application on your QNX target, do the following:

  1. Build the generated C code using the QNX C compiler. For example, you can use IDE to create and build a QNX project. See QNX Momentics IDE User.
    Note: You may be required to make minor modifications to the generated C code if it uses a library that's not supported by QNX (e.g., omp.h).
  2. Ensure that your sensor application has access to the generated header and library files. This may mean copying the files to a different project or workspace because your sensor application, needs to include the generated header files. For example:
    ...
    #include <edgeImage.h>
    #include <edgeImage_initialize.h>
    #include <edgeImage_terminate.h>
    ...
                        
  3. In your sensor application, you can call the following generated functions, where appropriate, to process the sensor data:
    matlab_function_name_initialize()
    Initialize the MATLAB generated code
    matlab_function_name()
    Execute the MATLAB generated code
    matlab_function_name_terminate()
    Terminate the MATLAB generated code
    For example:
    ...
    unsigned char * cmPixData;  /* grey bytes of pixel data, column major */
    unsigned char * cmEdgeData; /* grey edge pixel data, column major */
    ...
    edgeImage_initialize();
    ...
    /* Receive sensor data*/
    ...
    edgeImage(cmPixData, cmEdgeData);
    ...
    edgeImage_terminate();
                        
    Note: The code generated by MATLAB expects the input data in column major matrix format. You must transpose the sensor data in your sensor application to be in column major matrix format before passing it as input to your MATLAB generated function.