how to let pic communicate with matlab

My project uses MATLAB to develop a Band-stop filter, type Butterworth to be spesific for vibration control of a flexible manipulator. My other two friends have also developed an equation for input shaping technique to control the vibration of the flexible manipulator.


I'd like to ask whether there are any previous projects or any examples of programs that convert the Band-stop filter equations or input shaping technique's equations that are gained from MATLAB into PIC programming codes.


In other words, the band-stop filter equation (Laplace transform equations) from MATLAB has to be embedded into the PIC.
In another project(my friends' project), the input shaping technique equations have to be embedded into the PIC too.

the problem is
1. what is the code for send algorithm from PIC to my PC , and receive the data in Matlab ?
2. how to change two impulse sequence equation into source code understand by PIC ?

thank you for help

Replies

  • wassup
    wassup
    I will assume your filter is constant. (Does it change/adaptive ? Hope Not)
    Here is what you can do. To use the filter you need impulse response of the filter. Get it from Matlab.
    The array you get will be in floating point ! Convert that into fixed point. (Read more on fixed point arithmetic) Now once you get the h[n], you will have to implement a convolution algo in PIC.
    Now all I can say is PIC is not built for these kind of algorithms !!!!! You will have to use dsPIC.

    Give me your filter design, I will try to get something out of it. But you will have change you microcontroller !!!!
  • kaibin
    kaibin
    Hi, the band-stop filter is Butterworth type. And yeah, it's constant.
    The band-stop filter needs to filter two peak values, so two butterworth filters with different parameters are needed.


    [b,a] = butter(n, wn, ‘ftype’, ‘s’)

    where n is the filter order, wn = [w1 w2] is the cutoff frequencies

    wn=[90 110]
    wn =

    90 110

    The results:
    >> [b,a]=butter(3,wn,'stop','s')
    b =
    1.0e+011 *
    0.0000 0 0.0000 0 0.0029 0 9.7030
    a =
    1.0e+011 *
    0.0000 0.0000 0.0000 0.0000 0.0030 0.0392 9.7030

    For wn=[340 360], the results are:
    b =
    1.0e+015 *
    0.0000 0 0.0000 0 0.0000 0 1.8338
    a =
    1.0e+015 *
    0.0000 0.0000 0.0000 0.0000 0.0000 0.0006 1.8338

    where the transfer function is

    H(s) = B(s)/A(s) = [b(1)s^n + b(2)s^(n-1) + ... + b(n+1)]/[s^n + a(2)s^(n-1) + ... + a(n+1)]

    Pls help me out, thx.

    I've checked with my supervisor, and he told me to use code C, that is to change the MATLAB instructions into code C instructions, in order to program it into the PIC.

    Is it possible to change MATLAB instructions to code C instructions?

    What is the form of code C file needed so that it can be programmed into the PIC? Is it MEX file or .exe file?
  • wassup
    wassup
    OH GOD ! That really doesn't work out buddy. C files from MATALB require a lot of libraries and porting that into PIC will be impossible. .mex n .exe are executables not C files.
    Also please let me know about the signal you are filtering. Are you filtering it in real time? if not how big the signal ? In that case you will going no where with PIC (sorry to say this, switch to dsPIC).
    This is what you can do for now, remember this is not a very good way to implement filters.
    1) Find h[n] from matlab. Take the array convert it into 8 bit fixed point in matlab. You will have to play with the numbers to get a good approximation. Store this as a array in C.
    2)Implement a convolution function is C. This can be compiled in PIC. But I will tell you it will be very slow, since PIC doesnt have a multiply instructions ! The function will have two arguments. The filter array and the signal you are filtering.

    After this you will again have to fool around with numbers to get a good result. You can avoid mulitpicaltion if your filter coefficients are mutliples of 2. So approximate the fixed point numbers. Hope you got the idea.

    I really need to know what project you are doing, so I can suggest you better alternatives.
  • kaibin
    kaibin
    My project is The Development of Band-stop Filter for Vibration Control Using PIC(dsPIC now, since PIC is not able to handle the algorithms) Microcontroller. The Band-stop filter is used to filter a bang-bang torque input which will then be inserted into a flexible-link modal.

    The flexible-link modal has a state-space parameters of
    A = [0, 0, 0, 1, 0, 0;
    0, 0, 0, 0, 1, 0;
    0, 0, 0, 0, 0, 1;
    0, -6175.8, -0.135271x10^7, 0, 7.8681, -341.14;
    0, 21700.8, 0.257311x10^7, 0,-2.5541, 745.786;
    0, -1629.67, -147554, 0,-0.334002, -46.5154;]

    B = [ 0;
    0;
    0;
    1173.24;
    -1128.74;
    23.8429;]

    C = [1 1 1 0 0 0]

    D = [0]

    All of the following are done in MATLAB simulink:
    When the unfiltered bang-bang torque input is inserted into the flexible-link modal, a magnitude-frequency response output will be shown. The two peak frequencies are natural frequencies that will cause the flexible-link to vibrate.

    To reduce the vibration, the bang-bang torque input is needed to be filtered using the band-stop filter.

    I've asked some advice from a lecturer, and it seems to me that the only way to make this project work is to convert the MATLAB instructions to code C language and program it into the dsPIC. He also mentioned that PIC has limited decimal places. And he said to me it will be tough and the probability to success is just a mere 20%.

    Can you provide any useful MATLAB to code C websites to me for study? I am stuck with no progress at all with the project.

    Please do suggest other alternatives to me. Thanks.
  • yhsam
    yhsam
    kaibin is one of my project group member. he is designing the code of filter, and me is designing the code of positive input shaping.. actually our project is divided into two stage .. first stage, we are use the matlab to design the simulation of flexible manipulator model, and also the positive input algorithm ... below is my simulation

    [​IMG]

    stage 1 was done .. now, we are doing the stage 2.. The positive input shaping algorithm is embedded into dsPIC.. and the positive input shaping block in matlab is replace by the dsPIC... this also means that the flexible manipulator simulation block in matlab will " calculate " the angle , displacement, valocity, etc from the dsPIC, which is embedded with positive input shaping and filter .

    here is my positive input shaping code in matlab

    starting_time=0;
    simulation_time=4;
    sampling_time=0.001;
    t=starting_time:sampling_time:simulation_time;


    for i=1😔0.2/sampling_time)+1
    u(i,1)=0;
    end
    for i=(0.2/sampling_time)+1😔0.5/sampling_time)+1
    u(i,1)=0.3;
    end
    for i=(0.5/sampling_time)+1😔0.8/sampling_time)+1
    u(i,1)=-0.3;
    end
    for i=(0.8/sampling_time)+1😔4/sampling_time)+1
    u(i,1)=0;
    end


    % Positive ZV shaper (2-impulse)
    % mode 1

    pi=22/7;
    z1=0.0086;
    k1=exp((-z1*pi)/((1-z1^2)^0.5));
    wn1=94;
    wd1=wn1*(sqrt(1-z1^2));

    %Determine the amplitudes and time location of the input shaper

    ip=2;

    tt1a(1)=0;
    tt1a(2)=pi/wd1;
    A1a(1)=1/(1+k1);
    A1a(2)=A1a(1)*k1;

    tt1a=tt1a./0.001;
    tt1a=round(tt1a);
    tt1a=tt1a.*0.001;

    for i=1
    v1a(i,1)=A1a(1);
    end

    ni=1;
    while ni<=ip-1
    for i=((tt1a(ni)/sampling_time)+2)😔(tt1a(ni+1)/sampling_time)+1)
    v1a(i,1)=0;
    end
    for i=(tt1a(ni+1)/sampling_time)+1
    v1a(i,1)=A1a(ni+1);
    end
    ni=ni+1;
    end

    for i=((tt1a(ni)/sampling_time)+2)😔(4/sampling_time)+1)
    v1a(i,1)=0;
    end

    %mode 2

    pi=22/7;
    z2=0.0086;
    k2=exp((-z2*pi)/((1-z2^2)^0.5));
    wn2=344.70;
    wd2=wn2*(sqrt(1-z2^2));

    %Determine the amplitudes and time location of the input shaper

    ip=2;

    tt2a(1)=0;
    tt2a(2)=pi/wd2;
    A2a(1)=1/(1+k2);
    A2a(2)=A2a(1)*k2;

    tt2a=tt2a./0.001;
    tt2a=round(tt2a);
    tt2a=tt2a.*0.001;

    for i=1
    v2a(i,1)=A2a(1);
    end

    ni=1;
    while ni<=ip-1
    for i=((tt2a(ni)/sampling_time)+2)😔(tt2a(ni+1)/sampling_time)+1)
    v2a(i,1)=0;
    end
    for i=(tt2a(ni+1)/sampling_time)+1
    v2a(i,1)=A2a(ni+1);
    end
    ni=ni+1;
    end

    for i=((tt2a(ni)/sampling_time)+2)😔(4/sampling_time)+1)
    v2a(i,1)=0;
    end


    imp1a=conv(v1a,v2a);
    impa=imp1a(1😔4/sampling_time)+1);

    shp1a=conv(impa,u);
    PZV=shp1a(1😔4/sampling_time)+1);
  • wassup
    wassup
    OK guys, let me make few things simpler for you.
    @#-Link-Snipped-# I don't know why he is asking you to convert the whole Matlab stuff into C and then porting it to dsPIC. It's doesn't make any sense. I will first tell you how to implement the filter ! @#-Link-Snipped-# I don't understand what you are saying, I need to look into more detail. One of you guys can get in touch with me through mail. But first the filter implementation.
    1) Don't implement filter design in dsPIC !! You already have the filter design from MATLAB.
    2) Now just use imp function in matlab and find h[n]
    3) Find u[n] of bang-bang input.
    4) Store both of them in array
    5) Implement convolution algorithm in C (dsPIC)
    Simple isn't it ?

    Now are you guys simulating every module ? No external hardware ? flexible-link modal this is external right ?
  • wassup
    wassup
    Your system is too simple to port MATLAB code to dsPIC. Just implement convolution you shall be done. It is just 3 for loops.
    But if you really want, you should use realtime toolbox which will convert simulink model to dspic code.
    dsPIC (microchip.com) has an example on this one, but I have never used it, please do check
  • yhsam
    yhsam
    yes ... i have done simulating in every module, and no external hardware, kaibin and me are using the flexible manipulator form matlab simulation block.

    i am apologize about my english and cannot explain clearly.
    stage 1 was done .. now, we are doing the stage 2.. The positive input shaping algorithm is embedded into dsPIC.. and the positive input shaping block in matlab is replace by the dsPIC... this also means that the flexible manipulator simulation block in matlab will " calculate " the angle , displacement, valocity, etc from the dsPIC, which is embedded with positive input shaping and filter .

    the realtime toolbox is get from matlab ?! but i didn't see it ... but i founded a application named " realtime workshop " is it ?!!
  • wassup
    wassup
    OK I postive input shaping seems simple. I guess you can implement that in C without much fuss.
    and yeah filter I already told. There will be examples in dsPIC (application note) for convolution, please check
  • yhsam
    yhsam
    sorry late to reply.. because i am busy with my exam.. my supervisor is decided to use the PIC18LFk50 and with FTDI . its it any comment the programming code for FTDI ?! supervisor suggest me try to design the code for this equation first ..
    [​IMG]

    did u all guys have any idea to design this impulse ?!?! i have read many example ... but its doesn't example similar this ...

    thanks q for your help...
    here is my email address ..
    the kaibin email :
  • yhsam
    yhsam
    any Expert can help me ?! this project is important for me !!
  • wassup
    wassup
    There are two books you can read to implement this.
    One is DSP using Matlab by S K Mitra and this #-Link-Snipped-#
  • adinickluv
    adinickluv
    hey guys can anybody tell me where can i download malab software.
    I also need it..
  • wassup
    wassup
    haha nice try Matlab isn't free ! Crazyengineers don't support piracy 😀
    You can download and try octave which is free and compatible with, #-Link-Snipped-#
  • yhsam
    yhsam
    download ? .. u may download the trial version from mathwork website. but if u want to get the full version, it is illegal. u need to purchase the software.

    for wassup:
    thanks q for your recommend book.. its quick useful for my project..
  • yhsam
    yhsam
    i have an idea the simplest way, how about if i convert the algorithm in matlab editor convert to C code ?! and embedded into PIC ?! its more simple and not need to reprogramm right ?! same right wassup say. anybody can help me what should i do this step ?!!

    i have search in google. founded that its have two tool can do this convert step. one is realtime workshop and another one is catalytic .. which one is better ?!

You are reading an archived discussion.

Related Posts

hi CEns please let me know from where i could get a 8085 virtual kit for practicing programs i've searched many sites. some are fake and the ones which i've...
Hello friends....😁 I was in my college when, looking at our TNP(Training and Placement) cell, i got the idea of CE opening a section of its own where we students...
CEans, For those who deal with lot of mail every day, Microsoft's Outlook is a good program. However, with growing mailbox, Outlook starts crawling. I had been looking at an...
Friends..... How about an essay or a debate contest on some current topic?? i mean some good stuff and general knowledge enhancement.... How would THAT be????
Hi All, This information is dedicated to my those CS/IT engineer friends(obviously complete engineers) who have zeal to learn and explore unknown things. Recently I learnt this tool and hoping...