Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %% Simulator
- % ideally this program only runs after drone has taken off and is in the
- % air, and ends before landing
- % need algorithm to tweak gains per conditions, as well z distance and time
- % seems to not be calculated correctly
- clear all
- close all
- Va = 370/3600; %kps
- % Va = 101; %kps
- psi = [];
- psi(1) = 0;
- DistanceTraveled = 0;
- wn = 0;
- we = 0;
- dt = 0.0051;
- dt = 0.0001;
- bx = 0.00008525; %0.00008525;
- bxdot = 0.0195; % 0.0195;
- bh = 0.000083525; %0.000083525;
- bhdot = 0.0291; % 0.0291;
- bx = 25.558525; %0.00008525; 2.558525
- bxdot = 25.99759195; % 0.0195; 2.99759195
- bh = 0.00008525; %0.000083525;
- bhdot = 0.0195; % 0.0291;
- bva = 0.5;
- Pn = [];
- % Va = [];
- Vac = Va;
- h_dot = [];
- Pn_dot = [];
- Pe_dot = [];
- x_ddot = [];
- h_ddot = [];
- Va_dot = [];
- h = [];
- x = [];
- xc = [];
- % hc = [];
- h(1) = 0;
- Pn(1) = 0;
- Pe(1) = 0;
- h_dot(1) = 0;
- x_dot(1) = 0;
- Target1Acquired = 0;
- Target2Acquired = 0;
- Target3Acquired = 0;
- Arrival2Target1Acquired = 0;
- Arrival2Target2Acquired = 0;
- Arrival2Target3Acquired = 0;
- DepartureTarget1Acquired = 0;
- DepartureTarget2Acquired = 0;
- DepartureTarget3Acquired = 0;
- success = 0;
- start = [0,0,0];
- t1 = [70,70,5];
- t1 = [-6,00,0];
- t2 = [150, 120, 10];
- t2 = [4, 8, 0];
- PC1 = [0.00, -0.27]; % Path circle center 1
- PC2 = [-14.7338, 0.0450];
- PC3 = [7.7301, 8.0062];
- PC4 = [3.1173, 9.7568];
- PC5 = [0.1909, 0.1909];
- tA = [90, -170.4105, -1.3112, 115.7497, -135.00]; % target location
- pDA = [90.8752, 108.2160, 69.2173, 162.9902]; % target departure location
- pAA = [-89.1248, -71.7840, 69.2173, 162.9902]; % Target arrival location
- r= 0.270; % radius of path circle
- start= [0,0,0];
- t1 = [0,0,0];
- t2 = [0,0,0];
- t3 = [0,0,0];
- t4 = [0,0,0];
- t5 = [0,0,0];
- At1 = [0,0,0];
- Dt1 = [0,0,0];
- At2 = [0,0,0];
- Dt2 = [0,0,0];
- At3 = [0,0,0];
- Dt3 = [0,0,0];
- At4 = [0,0,0];
- start(1) = PC1(1) + r*cosd(pDA(1));
- start(2) = PC1(2) + r*sind(pDA(1));
- t1(1) = PC2(1) + r*cosd(tA(2));
- t1(2) = PC2(2) + r*sind(tA(2));
- t2(1) = PC3(1) + r*cosd(tA(3));
- t2(2) = PC3(2) + r*sind(tA(3));
- t3(1) = PC4(1) + r*cosd(tA(4));
- t3(2) = PC4(2) + r*sind(tA(4));
- At1(1) = PC2(1) + r*cosd(pAA(1));
- At1(2) = PC2(2) + r*sind(pAA(1));
- At2(1) = PC3(1) + r*cosd(pAA(2));
- At2(2) = PC3(2) + r*sind(pAA(2));
- At3(1) = PC4(1) + r*cosd(pAA(3));
- At3(2) = PC4(2) + r*sind(pAA(3));
- At4(1) = PC1(1) + r*cosd(pAA(4));
- At4(2) = PC1(2) + r*sind(pAA(4));
- Dt1(1) = PC2(1) + r*cosd(pDA(2));
- Dt1(2) = PC2(2) + r*sind(pDA(2));
- Dt2(1) = PC3(1) + r*cosd(pDA(3));
- Dt2(2) = PC3(2) + r*sind(pDA(3));
- Dt3(1) = PC4(1) + r*cosd(pDA(4));
- Dt3(2) = PC4(2) + r*sind(pDA(4));
- hc = At1(3);
- posx = [];
- posy = [];
- posx(1) = start(1);
- posy(1) = start(2);
- Pn(1) = start(1);
- Pe(1) = start(2);
- % DepartureTarget1Acquired=1;
- t = At1;
- xc(1) = atan2d((t(2)-start(2)),(t(1)-start(1)));
- x(1) = xc(1);
- for k = 1:4500000
- % Position = [posx(k), posy(k), h(k)];
- Position = [Pn(k), Pe(k), h(k)];
- [D2At1, D2t1,D2Dt1] = Distance2target3(Position,At1,t1,Dt1);
- [D2At2, D2t2,D2Dt2] = Distance2target3(Position,At2,t2,Dt2);
- [D2At3, D2t3,D2Dt3] = Distance2target3(Position,At3,t3,Dt3);
- [D2At4, D2H,~ ] = Distance2target3(Position,At4,start,Dt3);
- % [D2H, ~] = Distance2target(Position,start,start);
- [D2H2d, ~] = Distance2target(Position,start,start);
- Dist2T1(k) = D2t1;
- Dist2T2(k) = D2t2;
- if (D2At1 < .1)
- t = t1;
- hc = t1(3);
- Arrival2Target1Acquired=1;
- end
- if ((D2t1 < .1) && (Arrival2Target1Acquired==1))
- t = Dt1;
- hc = Dt1(3);
- Target1Acquired=1;
- end
- if ((D2Dt1 < .1) && (Target1Acquired==1))
- t = At2;
- hc = t2(3);
- DepartureTarget1Acquired=1;
- end
- if ((D2At2 < .1) && (DepartureTarget1Acquired==1))
- t = t2;
- hc = t2(3);
- Arrival2Target2Acquired=1;
- end
- if ((D2t2 < .1) && (Arrival2Target2Acquired==1))
- t = Dt2;
- hc = Dt2(3);
- Target2Acquired=1;
- end
- if ((D2Dt2 < 0.1) && (Target2Acquired==1))
- t = At3;
- hc = t3(3);
- DepartureTarget2Acquired=1;
- end
- if ((D2At3 < .1) && (DepartureTarget2Acquired==1))
- t = t3;
- hc = t3(3);
- Arrival2Target3Acquired=1;
- end
- if ((D2t3 < .2) && (Arrival2Target3Acquired==1))
- Target3Acquired=1;
- %%%%%%%%%% HERE IS WHERE THE IF STATEMENT NO LONGER EXECUTES%%%%%%%%%%%%
- t = Dt3;
- hc = Dt3(3);
- end
- if ((D2Dt3 < .4) && (Target3Acquired==1))
- t = At4;
- hc = At4(3);
- DepartureTarget3Acquired=1;
- end
- if ((D2At4 < .1) && (DepartureTarget3Acquired==1))
- break
- end
- if ((Target1Acquired==1) && (Target2Acquired==1) && (Target3Acquired==1))
- success = 1;
- end
- % % %
- % % % if ( (success == 1))
- % % %
- % % % bh = 0.0013525; %0.000083525;
- % % % bhdot = 0.1191; % 0.0291;
- % % % bx = 0.000098525; %0.00008525;
- % % % bxdot = 0.01; % 0.0195;
- % % % bx = 0.00008525; %0.000525;
- % % % bxdot = 0.0195; % 0.1;
- % % % bh = 0.00008525; %0.000083525;
- % % % bhdot = 0.0195; % 0.0291;
- % % %
- % % % % hc = (start(3) + hc)/2;
- % % % end
- % if ((D2H2d < 10) && (success == 1))
- % hc = start(3);
- % end
- if ((D2H < 2) && (success == 1))
- break
- end
- Pn_dot(k) = Va*cosd(psi(k)) + wn; %
- Pe_dot(k) = Va*sind(psi(k)) + we;
- xc(k) = atan2d((t(2)-posy(k)),(t(1)-posx(k)));
- xc(k) = atan2d((t(2)-Pe(k)),(t(1)-Pn(k)));
- % xc(k) = atand((t(2)-Pn(k))/(t(1)-Pe(k)));
- x_ddot(k) = bxdot*(0-x_dot(k)) +bx*(xc(k) - x(k));
- h_ddot(k) = bhdot*(0-h_dot(k)) + bh*(hc - h(k));
- if (h_ddot(k) > 1e-4)
- h_ddot(k) = 1e-4;
- end
- if (h_ddot(k) < -1e-4)
- h_ddot(k) = -1e-4;
- end
- % if (x_ddot(k) > 15)
- % x_ddot(k) = 15;
- % end
- % if (x_ddot(k) < -15)
- % x_ddot(k) = -15;
- % end
- posx(k+1) = posx(k) + Va*dt*cosd(psi(k));
- posy(k+1) = posy(k) + Va*dt*sind(psi(k));
- % Va_dot(k) = bva*(Vac-Va);
- Pn(k+1) = Pn(k) + Pn_dot(k)*dt;
- Pe(k+1) = Pe(k) + Pe_dot(k)*dt;
- % Va(k+1) = Va(k) + Va_dot(k)*dt;
- h_dot(k+1) = h_dot(k) + h_ddot(k)*dt;
- h(k+1) = h(k) + h_dot(k)*dt;
- x_dot(k+1) = x_dot(k) + x_ddot(k)*dt;
- x(k+1) = x(k) + x_dot(k)*dt;
- psi(k+1) = x(k+1);
- % if (h(k+1) > t1(3))
- % break
- % end
- DistanceTraveled = DistanceTraveled + sqrt((Pn(k+1)-Pn(k))^2 + (Pe(k+1)-Pe(k))^2 + (h(k+1)-h(k))^2); %km
- if (DistanceTraveled > 300)
- break
- end
- end
- MissionTime = (k*dt)/60; %minutes
- % DistanceTraveled = (k*dt)*Va; % km
- str1 = "Mission sucessful ";
- str2 = "Need more time ";
- str3 = "Mission completed in " + MissionTime + " minutes.";
- str4 = "Mission ran for " + MissionTime + " minutes.";
- str5 = "Total distance traveled was " + DistanceTraveled + " kilometers.";
- strT1 = "Target 1 captured. ";
- strT2 = "Target 2 captured. ";
- strT3 = "Target 3 captured. ";
- if (Target1Acquired ==1)
- fprintf(strT1); fprintf('\n');
- end
- if (Target2Acquired ==1)
- fprintf(strT2); fprintf('\n');
- end
- if (Target3Acquired ==1)
- fprintf(strT3); fprintf('\n');
- end
- if ((D2H < 2) && (success == 1))
- fprintf(str1); fprintf('\n');
- fprintf(str3); fprintf('\n');
- fprintf(str5); fprintf('\n');
- else
- fprintf(str2); fprintf('\n');
- fprintf(str4); fprintf('\n');
- fprintf(str5); fprintf('\n');
- end
- % figure(42)
- %
- % plot3(t1(1),t1(2),t1(3),'r o',t2(1),t2(2),t2(3),'k o' );
- % hold on
- % grid on
- % curve = animatedline('Linewidth',1);
- % set(gca,'Xlim',[(min(Pn)-10) (max(Pn)+10)], 'Ylim',[(min(Pe)-10) (max(Pe)+10)], 'Zlim',[(min(h)-10) (max(h)+10)]);
- % title('Trajectory of vehicle')
- % xlabel('x km');
- % ylabel('y km');
- % zlabel('z km');
- % legend({ 'target_1','target_2','Vehicle path'}, 'Location', 'southwest');
- %
- %
- % for g=1:k
- % addpoints(curve,Pn(g),Pe(g),h(g));
- % head = scatter3(Pn(g),Pe(g),h(g),'*','b');
- % drawnow limitrate
- % % pause(0.00005)
- % delete(head);
- % end
- %
- % hold off
- figure(41)
- % axis('Xlim',[(min(Pn)-10) (max(Pn)+10)], 'Ylim',[(min(Pe)-10) (max(Pe)+10)], 'Zlim',[(min(h)-10) (max(h)+10)]);
- plot3(Pn,Pe,h)
- grid on;
- hold on
- plot3(t1(1),t1(2),t1(3),'r o',At1(1),At1(2),At1(3),'r *',Dt1(1),Dt1(2),Dt1(3),'r *' );
- hold on
- plot3(t2(1),t2(2),t2(3),'k o',At2(1),At2(2),At2(3),'k *',Dt2(1),Dt2(2),Dt2(3),'k *' );
- hold on
- plot3(t3(1),t3(2),t3(3),'b o',At3(1),At3(2),At3(3),'b *',Dt3(1),Dt3(2),Dt3(3),'b *' );
- hold on
- % plot3(p(1,breakpoint-1),p(2,breakpoint-1),p(3,breakpoint-1),'*')
- hold off;
- title('Trajectory of vehicle')
- xlabel('x km');
- ylabel('y km');
- zlabel('z km');
- legend({'Vehicle stepping on path', 'target_1','target_2'...
- }, 'Location', 'northwest');
- % axis square;
- % %
- % % figure(1)
- % % plot(x)
- % % title('height position')
- % % xlabel('time (s)');
- % % ylabel('position km');
- % %
- % % figure(2)
- % % plot(x_dot)
- % % title('height velocity')
- % % xlabel('time (s)');
- % % ylabel('velocity km/s');
- % %
- % % figure(3)
- % % plot(x_ddot)
- % % title('height acceleration')
- % % xlabel('time (s)');
- % % ylabel('acceleration km/ss');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement