Advertisement
Guest User

Untitled

a guest
Oct 1st, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Julia 6.24 KB | None | 0 0
  1. ###################
  2. # The Lorenz System      #
  3. # Controller type: RFPT  #
  4. # MIMO System              #
  5. ##################
  6.  
  7. ####################
  8. # The equation of motion #
  9. ####################
  10.  
  11. ##########
  12. # ẋ=σ(y-x)    #
  13. # ẏ=x(ρ-z)-y #
  14. # ż=xy-βz    #
  15. ##########
  16.  
  17.  
  18. #####################
  19. # Functions for FPT #
  20. #####################
  21.  
  22. function KB(n,lambda,errors,nominal)
  23.   out=0
  24.   for l=0:n-1
  25.     out=out+(factorial(n)/(factorial(l)*factorial(n-l))*errors[l+1]*lambda^(n-l))
  26.   end
  27.   out=out+nominal
  28.   return out
  29. end
  30. # Simple Euler integral (rectangle method)
  31. function Integ(Integral,step,input)
  32.     out=Integral+step*input
  33.     return out
  34. end
  35.  
  36. # Simple Sigmoid function, mostly you can use any Sigmoid type function, like tanh()
  37. function sigmoid(x)
  38.     s=x/(1+abs(x))
  39.   return s
  40. end
  41. # The Original Deformation function for SISO case (note here I used tanh() for sigmoid function)
  42. function G_SISO(past_input,past_response,desired,Kc,Bc,Ac)
  43.   out=(Kc+past_input)*(1 + Bc * tanh(Ac * (past_response - desired))) - Kc
  44.   return out
  45. end
  46.  
  47. # The Original Deformation function for MIMO case
  48. function G_MIMO(past_input,past_response,desired,error_limit,Kc,Bc,Ac) # Inputs and Outputs are vectors
  49. #  need control parameters  K B A
  50.     error_norm=vecnorm(past_response - desired,2)
  51.     # If the norm of the error is greater then the limnit compute the deformation
  52.     # (it is not near the Fixed Point)
  53.   if error_norm>error_limit
  54.     e_direction=(past_response-desired)/error_norm
  55.     B_factor= Bc* sigmoid(Ac *error_norm)
  56.     out=(1+B_factor)*past_input +B_factor*Kc*e_direction
  57.   else
  58.     out=past_input # Almost in the Fixed Point
  59.   end
  60.   return out
  61. end
  62.  
  63. #################
  64. # Control Parameters #
  65. #################
  66. Adaptive=1
  67.  
  68. K=-1e4
  69. B=-1  #±1
  70. A=1/(100*K)
  71.  
  72. #############################################
  73. # Parameters to design a Nominal Trajectory #
  74. #############################################
  75.  
  76. A₁=2
  77. ω₁=0.5
  78. A₂=3
  79. ω₂=0.7
  80. A₃=1
  81. ω₃=1
  82.  
  83. #################
  84. # Time variable #
  85. #################
  86.  
  87. # cycle time in seconds
  88. δt=1e-3
  89. # The lenght of the simulation in seconds
  90. LONG=Int(2e4)
  91.  
  92. Λ=32
  93. Λ1=3
  94.  
  95. d1=2
  96. d2=4
  97. d3=5
  98.  
  99. dd1=3
  100. dd2=7
  101. dd3=9
  102.  
  103. l1=0.15
  104. l2=0.10
  105. l3=0.30
  106.  
  107. ##########################
  108. # Exact model parameters #
  109. ##########################
  110. σₑ=5
  111. βₑ=8/3
  112. ρₑ=40
  113.  
  114. ############################
  115. # Approx. model parameters #
  116. ############################
  117. σₐ=4
  118. βₐ=7/3
  119. ρₐ=36
  120.  
  121. #Arrays
  122. xN=zeros(LONG)
  123. xN_p=zeros(LONG)
  124. xN_pp=zeros(LONG)
  125. yN=zeros(LONG)
  126. yN_p=zeros(LONG)
  127. yN_pp=zeros(LONG)
  128. zN=zeros(LONG)
  129. zN_p=zeros(LONG)
  130. zN_pp=zeros(LONG)
  131.  
  132. x_p_des=zeros(LONG)
  133. y_p_des=zeros(LONG)
  134. z_p_des=zeros(LONG)
  135.  
  136. u_x=zeros(LONG)
  137. u_y=zeros(LONG)
  138. u_z=zeros(LONG)
  139.  
  140. t=zeros(LONG)
  141.  
  142. y_Def_pp=zeros(LONG)
  143. Def=zeros(LONG)
  144.  
  145. x=zeros(LONG)
  146. y=zeros(LONG)
  147. z=zeros(LONG)
  148. x_p=zeros(LONG)
  149. y_p=zeros(LONG)
  150. z_p=zeros(LONG)
  151. x_pp=zeros(LONG)
  152. y_pp=zeros(LONG)
  153. z_pp=zeros(LONG)
  154. enorm=zeros(LONG)
  155. ed_x=zeros(LONG)
  156. ed_y=zeros(LONG)
  157. ed_z=zeros(LONG)
  158. bf=zeros(LONG)
  159.  
  160. qdef_x=zeros(LONG)
  161. qdef_y=zeros(LONG)
  162. qdef_z=zeros(LONG)
  163.  
  164. def_err_x=0
  165. def_err_y=0
  166. def_err_z=0
  167. #initial conditions
  168. hint_x=0
  169. hint_y=0
  170. hint_z=0
  171.  
  172. past_input=[0,0,0]
  173. past_response=[0,0,0]
  174. error_limit=1e-3
  175. #Simulation
  176.  
  177. for i=1:LONG-1
  178.   t[i]=δt*i
  179.   #Nominal Trajectory
  180.   xN[i]=A₁*sin(ω₁*t[i])
  181.   xN_p[i]=A₁*ω₁*cos(ω₁*t[i])
  182.   xN_pp[i]=-A₁*ω₁^2*sin(ω₁*t[i])
  183.  
  184.   yN[i]=A₂*sin(ω₂*t[i])
  185.   yN_p[i]=A₂*ω₂*cos(ω₂*t[i])
  186.   yN_pp[i]=-A₂*ω₂^2*sin(ω₂*t[i])
  187.  
  188.   zN[i]=A₃*sin(ω₃*t[i])
  189.   zN_p[i]=A₃*ω₃*cos(ω₃*t[i])
  190.   zN_pp[i]=-A₃*ω₃^2*sin(ω₃*t[i])
  191.   #Errors
  192.   h_x=xN[i]-x[i]
  193.   h_y=yN[i]-y[i]
  194.   h_z=zN[i]-z[i]
  195.  
  196.   hint_x=hint_x+δt*h_x
  197.   hint_y=hint_y+δt*h_y
  198.   hint_z=hint_z+δt*h_z
  199.  
  200.   h_p_x=xN_p[i]-x_p[i]
  201.   h_p_y=yN_p[i]-y_p[i]
  202.   h_p_z=zN_p[i]-z_p[i]
  203. # the error vector
  204.  errors_x=[hint_x,h_x]
  205.  errors_y=[hint_y,h_y]
  206.  errors_z=[hint_z,h_z]
  207.   x_p_des[i]=KB(2,Λ,errors_x,xN_p[i])
  208.   y_p_des[i]=KB(2,Λ,errors_y,yN_p[i])
  209.   z_p_des[i]=KB(2,Λ,errors_z,zN_p[i])
  210.   desired=[x_p_des[i],y_p_des[i], z_p_des[i]]
  211.   #deformation
  212.   if Adaptive==1 && i>10
  213.   past_input=G_MIMO(past_input,past_response,[x_p_des[i],y_p_des[i], z_p_des[i]],error_limit,K,B,A)
  214.  else
  215.    past_input=desired
  216.  end
  217.  #qdef_p_x[i+1]=qDef_pp_mem[1]+δt*qdef_p_x[i]
  218.  qdef_x[i+1]=past_input[1]+δt*qdef_x[i]
  219.  def_err_x=def_err_x+δt*(qdef_x[i]-x[i])
  220.  u_x[i]=(Λ1^2*def_err_x+2*Λ1*(qdef_x[i]-x[i])+past_input[1])/dd1
  221.  
  222.  qdef_y[i+1]=past_input[2]+δt*qdef_y[i]
  223.  def_err_y=def_err_y+δt*(qdef_y[i]-y[i])
  224.  u_y[i]=(Λ1^2*def_err_y+2*Λ1*(qdef_y[i]-y[i])+past_input[2])/dd2
  225.  
  226.  qdef_z[i+1]=past_input[3]+δt*qdef_z[i]
  227.  def_err_z=def_err_z+δt*(qdef_z[i]-z[i])
  228.  u_z[i]=(Λ1^2*def_err_z+2*Λ1*(qdef_z[i]-z[i])+past_input[3])/dd3
  229.  # u_x[i]=l1*(x_p_des[i]-σₐ*(y[i]-x[i]))/dd1
  230.  # u_y[i]=l2*(y_p_des[i]-x[i]*(ρₐ-z[i]))/dd2
  231.  # u_z[i]=l3*(z_p_des[i]-x[i]*y[i]+βₐ*z[i])/dd3
  232.  
  233.  x_p[i]=σₑ*(y[i]-x[i])+d1*u_x[i]
  234.  y_p[i]=x[i]*(ρₑ-z[i])-y[i]+d2*u_y[i]
  235.  z_p[i]=x[i]*y[i]-βₑ*z[i]+d3*u_z[i]
  236.  past_response=[x_p[i],y_p[i],z_p[i]]
  237.  
  238.  
  239.  x[i+1]=Integ(x[i],δt,x_p[i])
  240.  y[i+1]=Integ(y[i],δt,y_p[i])
  241.  z[i+1]=Integ(z[i],δt,z_p[i])
  242.  
  243. end #For
  244. #ppp=zeros(3)
  245.  
  246. using PyPlot
  247. figure(1)
  248. grid("on")
  249. title("Control Signals")
  250. # xlabel(L"Time $[s]$")
  251. # ylabel(L"q $[m]$")
  252. #Nominal
  253. # plot(t[3:LONG-1],xN_p[3:LONG-1],color="#7684FF")
  254. # plot(t[3:LONG-1],yN_p[3:LONG-1],color="#2A40FF")
  255. # plot(t[3:LONG-1],zN_p[3:LONG-1],color="#3B427F")
  256. #Realized
  257. plot(t[3:LONG-1],u_x[3:LONG-1],color="#FFAA41","r--")
  258. plot(t[3:LONG-1],u_y[3:LONG-1],color="#FF9F28","r--")
  259. plot(t[3:LONG-1],u_z[3:LONG-1],color="#B2690E","r--")
  260.  
  261. figure(2)
  262. title("Nominal and Simulated")
  263. plot(t[3:LONG-1],xN_p[3:LONG-1],color="#7684FF")
  264. plot(t[3:LONG-1],yN_p[3:LONG-1],color="#2A40FF")
  265. plot(t[3:LONG-1],zN_p[3:LONG-1],color="#3B427F")
  266. #Realized
  267. plot(t[3:LONG-1],x_p[3:LONG-1],color="#FFAA41","r--")
  268. plot(t[3:LONG-1],y_p[3:LONG-1],color="#FF9F28","r--")
  269. plot(t[3:LONG-1],z_p[3:LONG-1],color="#B2690E","r--")
  270. figure(3)
  271. grid("on")
  272. title("Tracking Error")
  273. xlabel(L"Time $[s]$")
  274. ylabel(L"error $[m]$")
  275. plot(t[3:LONG-1],xN[3:LONG-1]-x[3:LONG-1],color="red")
  276. plot(t[3:LONG-1],yN[3:LONG-1]-y[3:LONG-1],color="green")
  277. plot(t[3:LONG-1],zN[3:LONG-1]-z[3:LONG-1],color="blue")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement