Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.93 KB | None | 0 0
  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. % Multilayer Perceptron for Tanker Ship Heading Regulation
  3. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4. %
  5. % By: Kevin Passino
  6. %
  7. %
  8. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  9. clear % Clear all variables in memory
  10.  
  11. vary = [1,2,3,4,5,6,7,8,9,10];
  12. for fun = 1:length(vary)
  13.  
  14. Kp = vary(fun);
  15. Kd = 20;
  16.  
  17. % Initialize ship parameters
  18. % (can test two conditions, "ballast" or "full"):
  19.  
  20. ell=350; % Length of the ship (in meters)
  21. u=5; % Nominal speed (in meters/sec)
  22. %u=3; % A lower speed where the ship is more difficult to control
  23. abar=1; % Parameters for nonlinearity
  24. bbar=1;
  25.  
  26. % The parameters for the tanker under "ballast" conditions
  27. % (a heavy ship) are:
  28.  
  29. K_0=5.88;
  30. tau_10=-16.91;
  31. tau_20=0.45;
  32. tau_30=1.43;
  33.  
  34. % The parameters for the tanker under "full" conditions (a ship
  35. % that weighs less than one under "ballast" conditions) are:
  36.  
  37. %K_0=0.83;
  38. %tau_10=-2.88;
  39. %tau_20=0.38;
  40. %tau_30=1.07;
  41.  
  42. % Some other plant parameters are:
  43.  
  44. K=K_0*(u/ell);
  45. tau_1=tau_10*(ell/u);
  46. tau_2=tau_20*(ell/u);
  47. tau_3=tau_30*(ell/u);
  48.  
  49. % Parameters for the multilayer perceptron
  50.  
  51. % The first hidden layer is trivial, with unity weights and a zero bias
  52.  
  53. % Weights/biases in the second hidden layer:
  54.  
  55. w112=10;
  56. w122=10;
  57. b12=-200*pi/180;
  58. b22=+200*pi/180;
  59.  
  60. % Weights/biases in the third hidden layer:
  61.  
  62. w113=-80*pi/180;
  63. w223=-80*pi/180;
  64. b13=0;
  65. b23=80*pi/180;
  66.  
  67. % The bias in the output layer is zero and the two
  68. % output layer weights are unity.
  69.  
  70.  
  71. % Now, you can proceed to do the simulation or simply view the nonlinear
  72. % surface generated by the neural controller.
  73.  
  74. %flag1=input('\n Do you want to simulate the \n neural control system \n for the tanker? \n (type 1 for yes and 0 for no) ');
  75.  
  76. %if flag1==1,
  77.  
  78. % Next, we initialize the simulation:
  79.  
  80. t=0; % Reset time to zero
  81. index=1; % This is time's index (not time, its index).
  82. tstop=4000; % Stopping time for the simulation (in seconds)
  83. step=1; % Integration step size
  84. T=10; % The controller is implemented in discrete time and
  85. % this is the sampling time for the controller.
  86. % Note that the integration step size and the sampling
  87. % time are not the same. In this way we seek to simulate
  88. % the continuous time system via the Runge-Kutta method and
  89. % the discrete time controller as if it were
  90. % implemented by a digital computer. Hence, we sample
  91. % the plant output every T seconds and at that time
  92. % output a new value of the controller output.
  93. counter=10; % This counter will be used to count the number of integration
  94. % steps that have been taken in the current sampling interval.
  95. % Set it to 10 to begin so that it will compute a controller
  96. % output at the first step.
  97. % For our example, when 10 integration steps have been
  98. % taken we will then we will sample the ship heading
  99. % and the reference heading and compute a new output
  100. % for the controller.
  101. eold=0; % Initialize the past value of the error (for use
  102. % in computing the change of the error, c). Notice
  103. % that this is somewhat of an arbitrary choice since
  104. % there is no last time step. The same problem is
  105. % encountered in implementation.
  106. x=[0;0;0]; % First, set the state to be a vector
  107. x(1)=0; % Set the initial heading to be zero
  108. x(2)=0; % Set the initial heading rate to be zero.
  109. % We would also like to set x(3) initially but this
  110. % must be done after we have computed the output
  111. % of the controller. In this case, by
  112. % choosing the reference trajectory to be
  113. % zero at the beginning and the other initial conditions
  114. % as they are, and the controller as designed,
  115. % we will know that the output of the controller
  116. % will start out at zero so we could have set
  117. % x(3)=0 here. To keep things more general, however,
  118. % we set the intial condition immediately after
  119. % we compute the first controller output in the
  120. % loop below.
  121.  
  122.  
  123. % Next, we start the simulation of the system. This is the main
  124. % loop for the simulation of the control system.
  125.  
  126. while t <= tstop
  127.  
  128. % First, we define the reference input psi_r (desired heading).
  129.  
  130. if t<100, psi_r(index)=0; end % Request heading of 0 deg
  131. if t>=100, psi_r(index)=45*(pi/180); end % Request heading of 45 deg
  132. if t>2000, psi_r(index)=0; end % Then request heading of 0 deg
  133. %if t>4000, psi_r(index)=45*(pi/180); end % Then request heading of 45 deg
  134. %if t>6000, psi_r(index)=0; end % Then request heading of 0 deg
  135. %if t>8000, psi_r(index)=45*(pi/180); end % Then request heading of 45 deg
  136. %if t>10000, psi_r(index)=0; end % Then request heading of 0 deg
  137. %if t>12000, psi_r(index)=45*(pi/180); end % Then request heading of 45 deg
  138.  
  139. % Next, suppose that there is sensor noise for the heading sensor with that is
  140. % additive, with a uniform distribution on [- 0.01,+0.01] deg.
  141. %s(index)=0.01*(pi/180)*(2*rand-1);
  142. s(index)=0; % This allows us to remove the noise.
  143.  
  144. psi(index)=x(1)+s(index); % Heading of the ship (possibly with sensor noise).
  145.  
  146. if counter == 10, % When the counter reaches 10 then execute the
  147. % controller
  148.  
  149. counter=0; % First, reset the counter
  150.  
  151. e(index)=psi_r(index)-psi(index); % Computes error (first layer of perceptron)
  152. c(index)=(e(index)-eold)/T; % Sets the value of c
  153.  
  154. eold=e(index); % Save the past value of e for use in the above
  155. % computation the next time around the loop
  156. %
  157. % A conventinal proportional-derivative controller:
  158. delta(index)= Kp * -e(index) + Kd * -c(index);
  159.  
  160. else % This goes with the "if" statement to check if the counter=10
  161. % so the next lines up to the next "end" statement are executed
  162. % whenever counter is not equal to 10
  163.  
  164. % Now, even though we do not compute the a new control output at each
  165. % time instant, we do want to save the data at its inputs and output at
  166. % each time instant for the sake of plotting it. Hence, we need to
  167. % compute these here (note that we simply hold the values constant):
  168.  
  169. e(index)=e(index-1);
  170. delta(index)=delta(index-1);
  171.  
  172. end % This is the end statement for the "if counter=10" statement
  173.  
  174. % Next, comes the plant:
  175. % Now, for the first step, we set the initial condition for the
  176. % third state x(3).
  177.  
  178. if t==0, x(3)=-(K*tau_3/(tau_1*tau_2))*delta(index); end
  179.  
  180. % Next, the Runge-Kutta equations are used to find the next state.
  181. % Clearly, it would be better to use a Matlab "function" for
  182. % F (but here we do not, so we can have only one program).
  183.  
  184. time(index)=t;
  185.  
  186. % First, we define a wind disturbance against the body of the ship
  187. % that has the effect of pressing water against the rudder
  188.  
  189. %w(index)=0.5*(pi/180)*sin(2*pi*0.001*t); % This is an additive sine disturbance to
  190. % the rudder input. It is of amplitude of
  191. % 0.5 deg. and its period is 1000sec.
  192. %delta(index)=delta(index)+w(index);
  193.  
  194.  
  195. % Next, implement the nonlinearity where the rudder angle is saturated
  196. % at +-80 degrees
  197.  
  198. if delta(index) >= 80*(pi/180), delta(index)=80*(pi/180); end
  199. if delta(index) <= -80*(pi/180), delta(index)=-80*(pi/180); end
  200.  
  201. % Next, we use the formulas to implement the Runge-Kutta method
  202. % (note that here only an approximation to the method is implemented where
  203. % we do not compute the function at multiple points in the integration step size).
  204.  
  205. F=[ x(2) ;
  206. x(3)+ (K*tau_3/(tau_1*tau_2))*delta(index) ;
  207. -((1/tau_1)+(1/tau_2))*(x(3)+ (K*tau_3/(tau_1*tau_2))*delta(index))-...
  208. (1/(tau_1*tau_2))*(abar*x(2)^3 + bbar*x(2)) + (K/(tau_1*tau_2))*delta(index) ];
  209.  
  210. k1=step*F;
  211. xnew=x+k1/2;
  212.  
  213. F=[ xnew(2) ;
  214. xnew(3)+ (K*tau_3/(tau_1*tau_2))*delta(index) ;
  215. -((1/tau_1)+(1/tau_2))*(xnew(3)+ (K*tau_3/(tau_1*tau_2))*delta(index))-...
  216. (1/(tau_1*tau_2))*(abar*xnew(2)^3 + bbar*xnew(2)) + (K/(tau_1*tau_2))*delta(index) ];
  217.  
  218. k2=step*F;
  219. xnew=x+k2/2;
  220.  
  221. F=[ xnew(2) ;
  222. xnew(3)+ (K*tau_3/(tau_1*tau_2))*delta(index) ;
  223. -((1/tau_1)+(1/tau_2))*(xnew(3)+ (K*tau_3/(tau_1*tau_2))*delta(index))-...
  224. (1/(tau_1*tau_2))*(abar*xnew(2)^3 + bbar*xnew(2)) + (K/(tau_1*tau_2))*delta(index) ];
  225.  
  226. k3=step*F;
  227. xnew=x+k3;
  228.  
  229. F=[ xnew(2) ;
  230. xnew(3)+ (K*tau_3/(tau_1*tau_2))*delta(index) ;
  231. -((1/tau_1)+(1/tau_2))*(xnew(3)+ (K*tau_3/(tau_1*tau_2))*delta(index))-...
  232. (1/(tau_1*tau_2))*(abar*xnew(2)^3 + bbar*xnew(2)) + (K/(tau_1*tau_2))*delta(index) ];
  233.  
  234. k4=step*F;
  235. x=x+(1/6)*(k1+2*k2+2*k3+k4); % Calculated next state
  236.  
  237.  
  238. t=t+step; % Increments time
  239. index=index+1; % Increments the indexing term so that
  240. % index=1 corresponds to time t=0.
  241. counter=counter+1; % Indicates that we computed one more integration step
  242.  
  243. end % This end statement goes with the first "while" statement
  244. % in the program so when this is complete the simulation is done.
  245.  
  246. %
  247. % Next, we provide plots of the input and output of the ship
  248. % along with the reference heading that we want to track.
  249. %
  250.  
  251. % First, we convert from rad. to degrees
  252. psi_r=psi_r*(180/pi);
  253. psi=psi*(180/pi);
  254. delta=delta*(180/pi);
  255. e=e*(180/pi);
  256.  
  257. % Next, we provide plots of data from the simulation
  258.  
  259. figure(1)
  260. clf
  261. subplot(311)
  262. plot(time,psi,'k-',time,psi_r,'k--')
  263. grid on
  264. title('Ship heading (solid) and desired ship heading (dashed), deg.')
  265. subplot(312)
  266. plot(time,e,'k-')
  267. grid on
  268. title('Ship heading error between ship heading and desired heading, deg.')
  269. subplot(313)
  270. plot(time,delta,'k-')
  271. grid on
  272. xlabel('Time (sec)')
  273. title('Rudder angle (\delta), deg.')
  274. zoom
  275.  
  276.  
  277. %end % This ends the if statement (on flag1) on whether you want to do a simulation
  278. % or just see the control surface
  279.  
  280. %end
  281.  
  282. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  283. % End of program %
  284. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  285. if (fun ==1)
  286. save('ship_data(1)','psi','psi_r','delta', 'Kp', 'Kd');
  287. elseif (fun ==2)
  288. save('ship_data(2)','psi','psi_r','delta', 'Kp', 'Kd');
  289. elseif (fun ==3)
  290. save('ship_data(3)','psi','psi_r','delta', 'Kp', 'Kd');
  291. elseif (fun ==4)
  292. save('ship_data(4)','psi','psi_r','delta', 'Kp', 'Kd');
  293. elseif (fun ==5)
  294. save('ship_data(5)','psi','psi_r','delta', 'Kp', 'Kd');
  295. elseif (fun ==6)
  296. save('ship_data(6)','psi','psi_r','delta', 'Kp', 'Kd');
  297. elseif (fun ==7)
  298. save('ship_data(7)','psi','psi_r','delta', 'Kp', 'Kd');
  299. elseif (fun ==8)
  300. save('ship_data(8)','psi','psi_r','delta', 'Kp', 'Kd');
  301. elseif (fun ==9)
  302. save('ship_data(9)','psi','psi_r','delta', 'Kp', 'Kd');
  303. elseif (fun ==10)
  304. save('ship_data(10)','psi','psi_r','delta', 'Kp', 'Kd');
  305. end
  306. end
  307.  
  308. S=stepinfo(psi(1:2000));
  309.  
  310. Tr2=S.RiseTime;
  311. Ts2=S.SettlingTime;
  312. Po2=S.Overshoot;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement