Thursday, September 13, 2007

MATLAB/Simulink works with Windows and TI TMS320DM642 on video processing

Windows platform:
  1. Input video from an AVI file. We recommend 'image signal' to be 'One multidimensional signal'.
  2. Use the first frame as the background image
  3. embed MATLAB function: drag Simulink-User/Defined Functions/Embeded MATLAB Function, double-click to edit.
  4. function y = fcn(u)
    y = false;
    k = 0;
    for i = 158:161
    for j = 220:223
    if u(i,j) > 0
    k = k+ 1;
    end
    end
    end
    if k>15
    y = true;
    end
  5. embed C function: We can either write an S-function using the templates and examples or use the legacy code tool (lct). The legacy code tool is probably the best way to incorporate c-code into our model, the following m-file script shows how to incorporate c-code with the legacy code tool. (lct.m)
  6. %% Initialize the structure necessary for using the legacy code tool
    def = legacy_code('initialize');

    %% Fill in the appropriate structure fields
    % We define the source files, header files, libraries, and associated paths
    % first. I am assuming the files are in the same directory as where this
    % script will run.
    def.SourceFiles = {'my_Fun.c'};
    def.HeaderFiles = {'my_fun.h'};

    % define the name for your s-function
    def.SFunctionName = 'my_Fun_sfun';

    % define the simulink equivalent function spec to the c-code. In our model,
    % the input and output were both boolean, so I changed the function spec to
    % reflect that. Also, by using u1[][], I am assuming a matrix of unknown
    % dimensions is passed to the function. This is the simplest way to set it
    % up.
    def.OutputFcnSpec = 'boolean y1 = my_Fun( boolean u1[][] )';

    %% Auto generate an S-function / compile it / create a block
    legacy_code('sfcn_cmex_generate', def);
    legacy_code('compile', def);
    legacy_code('slblock_generate', def);
  7. Use 'Video Viewer' to check the output video.
  8. To add some operations before Simulink codes running, right-click on blank area and choose 'Model Properties'/Callbacks. Now you can add something on 'PreLoadFun*'. We use the following m-file script to load an image and choose some positions with mouse-click:
  9. f = figure;
    imshow('1st.bmp');
    title('Right Click to stop input');
    hold on;

    % Input features with mouse
    path = [];
    i = 0;
    [x,y,Button] = ginput(1);
    while Button~=3
    pt = [x y];
    path = [path; pt];
    plot(x,y,'g.','Markersize',15)
    if (i~=0)
    plot([x,temp(1)], [y, temp(2)], 'g-');
    end
    i = 1;
    % Give world coordinates of selected feature
    [x,y,Button] = ginput(1);
    temp = pt;
    end
    close(f);
  10. All the mouse-click positions are transferred to 'Draw Polygons' to show the region.

TI TMS320DM642 platform:
  1. MATLAB/Simulink version:2007b. DM642 version: version 3 with 720MHz, TVP5146 decoder. Code Composer Studio (CCS) version: 3.3. 2007b only works with CCS3.3. 2007a and older versions only work with CCS3.0,3.1 and they don't support CCS3.3.
  2. Choose 'DM642EVM V3' from 'Target for TI C6000/C6000 Target Preferences', double check the CPU clock (720MHz)
  3. Video Capture blockset: Decoder type:TVP5146, Input port:Port 0, NTSC. 'Analog video input', We am using 'S-video' and the Simulink demo uses Composite. 'Data order': 'Column major'.The text characters are flipped if choosing Row. The input source is Camera/DVD player
  4. Video Display: same 'data order'.
  5. MATLAB/Simulink Demo: 'Video Stabilization' and 'Edge Detection' are good demos to check your CCS , DM642 board and video in/out system. Both demos need 'fixed-point' blockset. 'Surveillance' demo does NOT use external camera input or video output. So nothing will show up on our monitor.
  6. Without running anything on DSP, the output display should be color bars.
  7. You can use "Concatenate" block to implement a quad-display for output.
  8. the data type of the substraction of current image from background image is an int16. This was set to uint8 in Windows version and was causing underflow
  9. Open CCS3.3 and make sure the board is connected.
  10. Use 'incremental build' to build the project from CCS, load to DM642 and run.
  11. The following message appear on MATLAB command window during the project building:
  12. >>
    Target Preferences block specifies that the board named 'DM642 EVM XDS560 Emulator' will be used to run generated code.
    However, since only board named 'Blackhawk USB560m - DM642 EVM Emulator' is found in your system, that board will be used.
    ### Connecting to Code Composer Studio(tm) ...
    ### Generating code into build directory: C:\Malab_Eva\lq_TI_ccslink
    Embedded MATLAB parsing for model "lq_TI"...Done
    Embedded MATLAB code generation for model "lq_TI"....Done
    Embedded MATLAB compilation for model "lq_TI"... 1 file(s) copied.
    Done
    Embedded MATLAB parsing for model "lq_TI"...Done
    Embedded MATLAB code generation for model "lq_TI"....Done
    ### Invoking Target Language Compiler on lq_TI.rtw
    tlc
    -r
    C:\Malab_Eva\lq_TI_ccslink\lq_TI.rtw
    C:\Program Files\MATLAB\R2007b\toolbox\rtw\targets\ccslink\ccslink\ccslink_grt.tlc
    -OC:\Malab_Eva\lq_TI_ccslink
    -IC:\Program Files\MATLAB\R2007b\toolbox\rtw\targets\ccslink\ccslink
    -IC:\Program Files\MATLAB\R2007b\toolbox\vipblks\vipmex\tlc_c
    -IC:\Program Files\MATLAB\R2007b\toolbox\rtw\targets\tic6000\tic6000blks\tlc_c
    -IC:\Program Files\MATLAB\R2007b\toolbox\dspblks\dspmex\tlc_c
    -IC:\Program Files\MATLAB\R2007b\toolbox\shared\etargets\etargets\tlc_c
    -IC:\Malab_Eva\lq_TI_ccslink\tlc
    -IC:\Program Files\MATLAB\R2007b\rtw\c\tlc\mw
    -IC:\Program Files\MATLAB\R2007b\rtw\c\tlc\lib
    -IC:\Program Files\MATLAB\R2007b\rtw\c\tlc\blocks
    -IC:\Program Files\MATLAB\R2007b\rtw\c\tlc\fixpt
    -IC:\Program Files\MATLAB\R2007b\stateflow\c\tlc
    -aEnforceIntegerDowncast=1
    -aFoldNonRolledExpr=1
    -aInlineInvariantSignals=1
    -aInlineParameters=0
    -aLocalBlockOutputs=1
    -aRollThreshold=5
    -aZeroInternalMemoryAtStartup=1
    -aZeroExternalMemoryAtStartup=1
    -aInitFltsAndDblsToZero=1
    -aGenerateReport=0
    -aGenCodeOnly=1
    -aRTWVerbose=1
    -aIncludeHyperlinkInReport=0
    -aLaunchReport=0
    -aGenerateTraceInfo=0
    -aForceParamTrailComments=0
    -aGenerateComments=1
    -aIgnoreCustomStorageClasses=1
    -aIncHierarchyInIds=0
    -aMaxRTWIdLen=31
    -aShowEliminatedStatements=0
    -aIncDataTypeInIds=0
    -aInsertBlockDesc=0
    -aSimulinkBlockComments=1
    -aInlinedPrmAccess="Literals"
    -aTargetFcnLib="ansi_tfl_tmw_new.mat"
    -aIsPILTarget=0
    -aLogVarNameModifier="rt_"
    -aGenerateFullHeader=1
    -aexportIDEObj=1
    -aideObjName="CCS_Obj"
    -aProfileGenCode=1
    -aInlineDSPBlks=1
    -abuildAction="Build_and_execute"
    -acompilerOptionsStr="-o2 --mem_model:data=far"
    -alinkerOptionsStr=""
    -asystemStackSize=8192
    -aoverrunNotificationMethod="None"
    -aoverrunNotificationFcn="myfunction"
    -aGenerateTraceInfo=0
    -aProfileNumSamples=100
    -asystemStackSize=8192
    -p10000


    ### Loading TLC function libraries

    ......
    ### Initial pass through model to cache user defined code
    ....
    ### Caching model source code
    .........................................................
    ### Writing header file lq_TI_types.h
    ### Writing header file lq_TI.h
    ### Writing header file lq_TI_private.h
    ### Writing source file lq_TI.c
    .
    ### Writing header file rtmodel.h
    ### Writing source file lq_TI_data.c
    ### Writing header file rt_nonfinite.h
    ### Writing source file rt_nonfinite.c.

    ### Writing source file lq_TI_main.c
    ### Writing header file lq_TI_main.h
    ### Writing header file MW_c6xxx_csl.h
    .
    ### Writing source file MW_c6xxx_csl.c
    ### TLC code generation complete.
    ### Creating project ...
    ### Building Code Composer Studio(tm) project...
    ### Build complete
    ### Downloading COFF file
    ### Downloaded: lq_TI.out
  13. CCS starts running the code on DM642
Problems:
  • In embedded C-function, how to make the input parameter to be an image array
  • Sometimes the program is extremely slow on Windows platform (7-8 frames/second).
  • How to check frame rate on DM642 platform.
  • How to make the debugging process more convenient, such as printing the variable values and image pixel values. Update from ICIP2007: highlight the data line and we can add 'mplayer' on it to show the pixel values. For variable values, we may add a blockset (scope?) to check the value in running time.
  • When will MATLAB/Simulink support DaVinci.

No comments:

Post a Comment