Need help translating a Matlab programme

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Need help translating a Matlab programme

Evee_ipi
Good afternoon all,

First I'd like to grate you all as I'm new to the community. I have been trying to translate this programme I have from Matlab with no luck. We made it as some practice at uni and now I want to run it as I can't own a Matlab license.

As far as I have been able to research, the error might be involved at comparing strings with the "==" function, or maybe with some matrix calculation whoch occur at line 83 of the code.

Anyway, I am unable to fix it after quite some effort. ¿I wil be really gratefull if you could help me. It is about an interactive PID simulator.


function abc(num,den,flag) 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%															      %
%  Animación interactiva de las acciones basicas de control. KC.  %
%                                                                 %
%                      abc([],[-1 -2 -3],10)	                  %
%                                                                 % 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

global sal err acc ;					    		% Señales
global sld_P sld_I sld_D txt_P txt_I txt_D ;		% Controles
global Kp Ki Kd Gp T R ;				      		% Variables


if length(flag)==1									% Se ejecuta al iniciar la simulación
   
  Kp=0 ; Ki=0 ; Kd=0 ; 
   
  Gp=tf(poly(num),poly(den)) ;
  Gba=minreal((Kp+tf(Ki,[1 0])+tf([Kd 0],1))*Gp) ; 
  M=minreal(Gba/(1+Gba)) ;
  T=[0:flag/100:flag] ; R=ones(length(T),1) ;
  Y=lsim(M,R,T) ;
  E=R-Y ;
  U=lsim((M/Gp)*tf(1,[1 10000]),R,T) ;
  
  
  colordef black ;
  fig=figure('Name','Aciones Básicas de Control PID','color',[0 0 0]) ; 
  set(fig,'Units','normalized','Position',[0 0 1 1]) ;
    
  
  subplot('position',[0.05 0.55 0.9 0.4]) ; 
  sal=plot(T,Y,'r','linewidth',2) ;
  hold on ;
  plot(T,R,'y','linewidth',2) ;
  axis([0 10 0 2]) ; grid ;
  title('Referencia vs Salida') ;
  subplot('position',[0.05 0.15 0.4 0.3]) ; 
  err=plot(T,E,'g','linewidth',2) ;
  axis([0 10 -1 1]) ; grid ;
  title('Error') ;
  subplot('position',[0.55 0.15 0.4 0.3]) ; 
  acc=plot(T,U,'b','linewidth',2) ;
  axis([0 10 -.0025 .0025]) ; grid ;
  title('Acción de Control') ;
  

  sld_P=uicontrol('Parent',fig,'Style','slider','Units','normalized', ...
     'Position',[0.033 0.04 0.3 0.05],'Min',0,'Max',75,'Value',Kp, ...
     'Callback','abc([],[],''Sld_P'');');
  
  sld_I=uicontrol('Parent',fig,'Style','slider','Units','normalized', ...
     'Position',[0.366 0.04 0.3 0.05],'Min',0,'Max',20,'Value',Ki, ...
     'Callback','abc([],[],''Sld_I'');');
  
  sld_D=uicontrol('Parent',fig,'Style','slider','Units','normalized', ...
     'Position',[0.699 0.04 0.3 0.05],'Min',0,'Max',25,'Value',Kd, ...
     'Callback','abc([],[],''Sld_D'');');
  
  
  txt_P=uicontrol('Parent',fig,'Style','text','Units','normalized', ...
     'Position',[0.033 0 0.3 0.04],'HorizontalAlignment','center',...
     'BackgroundColor',[0 0 0],'ForegroundColor',[1 1 1], ...
     'FontSize',15,'FontWeight','bold',...
     'String',sprintf('Kp : %3.1f',Kp)) ;
  
  txt_I=uicontrol('Parent',fig,'Style','text','Units','normalized', ...
     'Position',[0.366 0 0.3 0.04],'HorizontalAlignment','center',...
     'BackgroundColor',[0 0 0],'ForegroundColor',[1 1 1], ...
     'FontSize',15,'FontWeight','bold',...
     'String',sprintf('Ki : %3.1f',Ki)) ;

  txt_D=uicontrol('Parent',fig,'Style','text','Units','normalized', ...
     'Position',[0.699 0 0.3 0.04],'HorizontalAlignment','center',...
     'BackgroundColor',[0 0 0],'ForegroundColor',[1 1 1], ...
     'FontSize',15,'FontWeight','bold',...
     'String',sprintf('Kd : %3.1f',Kd)) ;
  
    
     
elseif (flag=='Sld_P' | flag=='Sld_I' | flag=='Sld_D')
   
  Kp=get(sld_P,'value') ;  
  Ki=get(sld_I,'value') ;
  Kd=get(sld_D,'value') ;
  
  
  Gba=minreal((Kp+tf(Ki,[1 0])+tf([Kd 0],1))*Gp) ; 
  M=minreal(Gba/(1+Gba)) ;
  Y=lsim(M,R,T) ;
  E=R-Y ;
  U=lsim((M/Gp)*tf(1,[1 10000]),R,T) ;
  
  set(sal,'Ydata',Y) ;
  set(err,'Ydata',E) ;
  set(acc,'Ydata',U) ;
  
  set(txt_P,'String',sprintf('Kp : %3.1f',Kp)) ;
  set(txt_I,'String',sprintf('Ki : %3.1f',Ki)) ;
  set(txt_D,'String',sprintf('Kd : %3.1f',Kd)) ;  
  
  
end;


Thank you very much in advance,

Evee


abc.m
Reply | Threaded
Open this post in threaded view
|

Re: Need help translating a Matlab programme

NJank


On Mon, Aug 21, 2017 at 1:44 PM, Evee_ipi <[hidden email]> wrote:

Anyway, I am unable to fix it after quite some effort. ¿I wil be really
gratefull if you could help me. It is about an interactive PID simulator.


ok, you should provide the exact input you enter and error messages that you receive, so we can try to understand better what is happening. 

I took the code and ran the sample at the top of the function

>> abc([],[-1 -2 -3],10)
warning: the 'tf' function belongs to the control package from Octave Forge which

you have installed but not loaded.  To load the package, run 'pkg load
control' from the Octave prompt.

Please read <http://www.octave.org/missing.html> to learn how you can
contribute missing functionality.
error: 'tf' undefined near line 20 column 6
error: called from
    abc at line 20 column 5


This means you need to load the control package to access some of the functions.

>> pkg load control
>> abc([],[-1 -2 -3],10)
warning: the 'colordef' function is not yet implemented in Octave

Please read <http://www.octave.org/missing.html> to learn how you can
contribute missing functionality.
error: 'colordef' undefined near line 29 column 3
error: called from
    abc at line 29 column 3


this means that the colordef function is not yet implemented.  if you cannot implement it yourself you will need to work around this issue in your code.

looking at line 83, it's generally bad form to use == to compare strings. strcmp is preferred. == will sometimes work, but it will give an error if the comparison items aren't equal length, rather than just producing 'false'

>> foo = 'test'
>> strcmp(foo,'test')
ans = 1
>> strcmp(foo,'tesy')
ans = 0
>> strcmp(foo,'testy')
ans = 0

>> if (foo=='test'), 1, else, 0, endif
ans =  1
>> if (foo=='tesy'), 1, else, 0, endif
ans = 0
>> foo == 'testy'
error: mx_el_eq: nonconformant arguments (op1 is 1x4, op2 is 1x5)
>> if (foo=='testy'), 1, else, 0, endif
error: mx_el_eq: nonconformant arguments (op1 is 1x4, op2 is 1x5)

so, using == can sometimes result in an error when you just want 'false'

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave