summaryrefslogtreecommitdiff
path: root/tools/scilab/windows/macros/ARDUINO_post_simulate.sci
blob: 5e0c6d1fd52f9ba922f3e9b912fdf70991886a79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) 2011-2011 - DIGITEO - Bruno JOFRET
//
// This file must be used under the terms of the CeCILL.
// This source file is licensed as described in the file COPYING, which
// you should have received as part of this distribution.  The terms
// are also available at
// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
//
//

function []=ARDUINO_post_simulate(%cpr, scs_m, needcompile)


  global port_com 
 
  presence_arduino=%f //indique la presence d'un bloc arduino setup
  // find SCOPE bloc for plotting at the end of simulation
  presence_scope=%f;
  list_scope=[];
  display_now=1;
  grid_on=1;


  for i = 1:size(scs_m.objs)
    curObj= scs_m.objs(i);
    if (typeof(curObj) == "Block" & curObj.gui == "ARDUINO_SETUP")
        presence_arduino=%t   
       
        try
          //closeserial(port_com)
        [a,b,c]=status_serial(1);
         while (b+c > 0) 
            [a,b,c]=status_serial(1);
         end
          close_serial(1)
          disp('Close serial port ok')
        catch
            messagebox("Impossible to close serial port.")
            error('close serial port')
            
        end
    elseif (typeof(curObj) == "Block" & curObj.gui == "TIME_SAMPLE") then
        if exists(curObj.graphics.exprs(3)) then
            display_now=evstr(curObj.graphics.exprs(3));
        else
            display_now=1
        end
        
    elseif (typeof(curObj) == "Block" & curObj.gui == "ARDUINO_SCOPE")
      presence_scope=%t 
      list_scope($+1)=i;
    end
    
  end
  
    // adjust scope and add grid
 if presence_scope & ~display_now then
     plot_aftersim_ard2(list_scope,scs_m)
//   elseif presence_scope & display_now then
//       plot_aftersim_ard1(list_scope,scs_m)
   else
      nicescope()
      if grid_on then
          list_fig=winsid();
          for i=list_fig
              scf(i);
              xgrid;
          end
      end
   end  

  disp('End of post simulation')

endfunction

function plot_aftersim_ard1(list_scope,scs)
    nb_outputs_by_scope=[];
    nb_outputs=[]
    nb_total_outputs=0;
    nb_scope=size(list_scope,1);
    legendes=cell();
//    grid_on=0;
    list_fig=winsid();

   for i=1:size(list_scope,1)
       scf(list_fig(i));
       j=list_scope(i);
       obj=scs_m.objs(j);       
       nb_outputs($+1)=evstr(obj.graphics.exprs(1));
       legendes(i).entries=obj.graphics.exprs(3:$);
       nb_total_outputs=nb_total_outputs+nb_outputs($);
       legend(legendes(i).entries);
   end
    nicescope() 
    xgrid
endfunction


function plot_aftersim_ard2(list_scope,scs)

    nb_outputs_by_scope=[];
    nb_total_outputs=0;
    nb_scope=size(list_scope,1);
    legendes=cell();
//    grid_on=0;

   for i=1:size(list_scope,1)
       j=list_scope(i);
       obj=scs_m.objs(j);       
       nb_outputs($+1)=evstr(obj.graphics.exprs(1));
       legendes(i).entries=obj.graphics.exprs(3:$);
       nb_total_outputs=nb_total_outputs+nb_outputs($);
   end

    c_color=[[0.75,0.75,0];[0.25,0.25,0.25];[0,0,1];[0,0.5,0];[1,0,0];[0,0.75,0.75];[0.75,0,0.75]];
    handle_fig=figure();
    set(handle_fig,"background",8)
    drawlater();
    //extraction des champs stockés
    D=[];
    legend_c=[];
    nb_objs_in_scopeblock=5;
    
    for i=1:nb_scope
        subplot(nb_scope,1,i);
        //legend_c=strsplit(scs.objs(num_scope(i)).graphics.exprs(2)," ");
        legend_c=legendes(i).entries;
        if size(legend_c,1)~=nb_outputs(i) then
            legend_c=_gettext("curv")+string([1:nb_outputs(i)]);
        end
        
        list_obj=scs_m.objs(list_scope(i)).model.rpar.objs;
        no=1;
        for j=1:size(list_obj)
            if (typeof(list_obj(j)) == "Block" & list_obj(j).gui == "TOWS_c") then
                label=list_obj(j).graphics.exprs(2);
                D(i,no)=evstr(label);
                no=no+1;                                    
            end
        end

        for no=1:nb_outputs(i)
            plot(D(i,no).time,D(i,no).values,'color',[c_color(modulo(no,6)+1,1),c_color(modulo(no,6)+1,2),c_color(modulo(no,6)+1,3)],'thickness',2)
        end
        h=legend(legend_c);
        set(h,"background",8)
        xgrid

        //title("scope_"+string(i));
    end
    drawnow();
endfunction