diff options
| -rw-r--r-- | Kod/bilbana/files/Bana1.mat | bin | 621 -> 1216 bytes | |||
| -rw-r--r-- | Kod/bilbana/files/Bana2.mat | bin | 623 -> 1207 bytes | |||
| -rw-r--r-- | Kod/bilbana/yc4/do_boot.m | 62 | ||||
| -rw-r--r-- | Kod/bilbana/yc4/do_car.m | 31 | ||||
| -rw-r--r-- | Kod/bilbana/yc4/do_gov.m | 9 | ||||
| -rw-r--r-- | Kod/bilbana/yc4/fit_percents.m | 11 | ||||
| -rw-r--r-- | Kod/bilbana/yc4/main.m | 71 | ||||
| -rw-r--r-- | Kod/bilbana/yc4/setup_car.m | 17 | ||||
| -rw-r--r-- | Kod/scripts/test_givare.m | 3 | ||||
| -rw-r--r-- | Kod/scripts/test_givare_kompenserad.m | 79 |
10 files changed, 256 insertions, 27 deletions
diff --git a/Kod/bilbana/files/Bana1.mat b/Kod/bilbana/files/Bana1.mat Binary files differindex d83861f..e745c59 100644 --- a/Kod/bilbana/files/Bana1.mat +++ b/Kod/bilbana/files/Bana1.mat diff --git a/Kod/bilbana/files/Bana2.mat b/Kod/bilbana/files/Bana2.mat Binary files differindex 622fc18..743de15 100644 --- a/Kod/bilbana/files/Bana2.mat +++ b/Kod/bilbana/files/Bana2.mat diff --git a/Kod/bilbana/yc4/do_boot.m b/Kod/bilbana/yc4/do_boot.m new file mode 100644 index 0000000..5019319 --- /dev/null +++ b/Kod/bilbana/yc4/do_boot.m @@ -0,0 +1,62 @@ +function [car, boot] = do_boot(car, boot) +%BOOT Summary of this function goes here +% Detailed explanation goes here +if car.running == true + %% BEFORE FIRST LAP + if car.lap == 0 + t = toc(boot.time); + if t > 0.6 + car.constant = car.constant + 0.12; + disp('###') + disp(car.num) + disp(car.constant) + boot.time = tic; + end + end + %% WHEN NEW LAP + if car.new_lap == 1 + car.constant = car.constant + 0.4; + disp('###') + disp(car.num) + disp(car.constant) + end + %% First segment + if car.lap == 1 && car.segment == 1 || car.lap == 1 && car.segment == 2 + t = toc(boot.time); + if t > 0.8 + car.constant = car.constant + 0.06; + disp('###') + disp(car.num) + disp(car.constant) + boot.time = tic; + end + + end + %% ide höj carconstant så att den blir mer aggresivare ju längre tid som det går t.ex efter 3.5 s + %% if car.lap == 1 && car.segment == 1 || car.lap == 1 && car.segment == 2 + %% t = toc(boot.time); + %% if t > 1.0 + %% car.constant = car.constant + 0.5; + %% disp('###') + %% disp(car.num) + %% disp(car.constant) + %% boot.time = tic; + %% end + %% + %% end + + %% END BOOTSTRAP + if car.segment > 3 + disp(car.constant); + seg_time = car.seg_times(1, 3) + laptime_forecast = seg_time / 0.102 + forecast_ref_diff = laptime_forecast - car.ref_time + forecast_ref_diff_rel = forecast_ref_diff / car.ref_time + car.constant = car.constant + (forecast_ref_diff_rel * 0.15) + boot.status = 0; + disp('END OF BOOTSTRAP') + disp(car.num) + disp(car.constant) + end +end +end
\ No newline at end of file diff --git a/Kod/bilbana/yc4/do_car.m b/Kod/bilbana/yc4/do_car.m index f205f02..cf534fa 100644 --- a/Kod/bilbana/yc4/do_car.m +++ b/Kod/bilbana/yc4/do_car.m @@ -1,4 +1,4 @@ -function [car, stop, display_data] = do_car(car, t, display_data) +function [car, stop, display_data] = do_car(car, t, display_data, boot) %DO_CAR Ger nya värden till struct car, avgör om koden ska stoppas samt hämtar displaydata. %{ Input/Output: @@ -44,7 +44,7 @@ end %% READ INPUT FROM TRACK if car.running == true if car.lap ~= 0 - if toc(car.seg_tic) > 9.0 + if toc(car.seg_tic) > 9.0 && not(boot.status) set_car_speed(1, 0); set_car_speed(2, 0); %disp(strjoin({'AvÃ¥kning bil', num2str(car.num)})); @@ -90,20 +90,31 @@ if car.running == true if car.new_lap == false % choose_position krachar vid nytt varv (seg 10) if car.lap ~= 0 car.seg_times(car.lap, car.segment) = toc(car.seg_tic); - end + end + + seg_time = car.seg_times(car.lap, car.segment) + lap_time_now = toc(car.lap_tic) + % s = vt + % v = s/t + % t = s/v + prev_seg_v = car.seg_len(car.segment) / toc(car.seg_tic) + track_remaining = car.pos_at(length(car.pos_at)) - car.pos_at(car.segment + 1) + + car.forecasts(car.lap, car.segment) = lap_time_now + track_remaining/prev_seg_v + car.segment = car.segment + 1; car.seg_tic = tic; if car.lap > 2 % S�kerhetsmarginal (B�r vara 1?) disp(car) [new_position, seg_plus] = ... choose_position(car.position, car.segment, car.num, car.pos_at); - if seg_plus ~= 0 && car.segment == 2 + if seg_plus ~= 0 && car.segment == 2 disp('Hoppar �ver missad givare 1/2'); else car.position = new_position; car.segment = car.segment + seg_plus; end - if seg_plus ~= 0 + if seg_plus ~= 0 && car.segment ~= 2 car.seg_times(car.lap, car.segment - seg_plus - 1) = 0; disp(car.seg_times(car.lap, :)) disp(seg_plus) @@ -118,7 +129,9 @@ if car.running == true %% NEW LAP if car.new_lap == true - car.lap_constants = gov_set(get_car_constant(car.num)); + disp('NEW LAP') + + car.lap_constants = gov_set(car.constant); car.new_lap = false; %TODO remove beep; if car.lap == 0 @@ -138,6 +151,9 @@ if car.running == true car.lap_tic = tic; car.position = 0; + % save segment percentage from last lap + car.percents = fit_percents(car.percents, car.lap_times(car.lap), car.seg_times(car.lap,:)) + if car.lap == 1 && size(car.seg_times, 2) < 9 disp('FEL: För få segment!!') car.stopped = true; @@ -156,8 +172,7 @@ end %% CALCULATE if car.running == true && car.automatic == true car.v = get_new_v(car.position, car.map); - seg_constant = get_seg_constant(car.position, car.lap_constants, car.num, car.pos_at); - car.u = get_new_u(car.v, seg_constant); + car.u = get_new_u(car.v, car.constant); end %% CONTROLLER diff --git a/Kod/bilbana/yc4/do_gov.m b/Kod/bilbana/yc4/do_gov.m new file mode 100644 index 0000000..d38336a --- /dev/null +++ b/Kod/bilbana/yc4/do_gov.m @@ -0,0 +1,9 @@ +function [ car ] = do_gov( car ) +%UNTITLED Summary of this function goes here +% Detailed explanation goes here +if car.new_check_point + status = car.forecasts(car.lap, car.segment-1)/car.ref_time; + car.constant = car.constant + (status - 1) * 0.01; + disp(car.constant); +end + diff --git a/Kod/bilbana/yc4/fit_percents.m b/Kod/bilbana/yc4/fit_percents.m new file mode 100644 index 0000000..ffdb9fc --- /dev/null +++ b/Kod/bilbana/yc4/fit_percents.m @@ -0,0 +1,11 @@ +function [ new_percents ] = fit_percents( percents, lap_time, seg_times ) +%FIT_PERCENTS Summary of this function goes here +% Detailed explanation goes here +new_percents = []; +for i = 1:length(percents) + old_p = percents(i); + cur_p = seg_times(i) / lap_time; + new_p = old_p - (old_p + cur_p) / 2; + new_percents(i) = new_p; +end + diff --git a/Kod/bilbana/yc4/main.m b/Kod/bilbana/yc4/main.m index 9b31618..7f5264b 100644 --- a/Kod/bilbana/yc4/main.m +++ b/Kod/bilbana/yc4/main.m @@ -1,4 +1,5 @@ clear all; +display_active = false; %% INIT TRACK disp('Startar bilbanan. Avsluta med q.') @@ -17,12 +18,13 @@ global log_debug; log_debug = true; global log_verbose; log_verbose = false; - % INIT DISPLAY -addpath display/ClientServerApp/Release -cd display/ClientServerApp/Release -!startServer -cd ../../.. +if display_active + addpath display/ClientServerApp/Release + cd display/ClientServerApp/Release + !startServer + cd ../../.. +end display = struct; display.data = [clear_display()]; @@ -53,10 +55,13 @@ car1.seg_constant_list = []; % TODO car1.position = 0; car1.pos_at = [0.0 2.53 3.05 4.73 7.68 8.98 10.93 14.69 17.57 19.60]; car1.seg_len = [2.53 0.53 1.68 2.92 1.2 2.01 3.83 2.89 1.99]; +car1.percents = []; % TODO car1.map = Bana1; car1.approximation = []; car1.miss_probability = 0.0; car1.lap_constants = [1,1,1,1,1,1,1,1,1]; % TODO +car1.constant = 0.1; +car1.stop = false; car2 = struct; car2.num = 2; @@ -71,10 +76,23 @@ car2.seg_times = []; car2.position = 0; car2.pos_at = [0.0 2.53 3.05 4.92 7.62 9.02 10.72 14.68 17.76 19.95]; car2.seg_len = [2.53 0.52 1.87 2.70 1.40 1.70 4.03 3.08 2.19]; +car2.percents = [0.088, 0.022, 0.102, 0.15, 0.058, 0.11, 0.212, 0.146, 0.113]; + car2.map = Bana2; car2.miss_probability = 0.1; car2.seg_constant_list = []; % TODO car2.lap_constants = [1,1,1,1,1,1,1,1,1]; % TODO +car2.seg_constant = 1; +car2.constant = 0.1; +car2.stop = false; + +boot1 = struct; +boot1.status = false; +boot1.time = 0; + +boot2 = struct; +boot2.status = false; +boot2.time = 0; t = 0; highToc = 0; @@ -86,6 +104,8 @@ car1.response = input('Vill du köra bil 1? [N] ', 's'); if car1.response == 'J' car1.running = true; car1.automatic = true; + boot1.status = true; + boot1.time = tic; elseif car1.response == 'M' car1.running = true; car1.automatic = false; @@ -98,6 +118,8 @@ car2.response = input('Vill du köra bil 2? [N] ', 's'); if car2.response == 'J' car2.running = true; car2.automatic = true; + boot2.status = true; + boot2.time = tic; elseif car2.response == 'M' car2.running = true; car2.automatic = false; @@ -113,8 +135,8 @@ elseif not(isreal(ref_time)) ref_time = 13; end %} -ref_time = 13; - +car1.ref_time = 15; +car2.ref_time = 15; %% MAIN LOOP while 1 readTime = tic; @@ -129,10 +151,25 @@ while 1 figure(hf) drawnow - - [car1, car1.stop, display.data] = do_car(car1, t, display.data); - [car2, car2.stop, display.data] = do_car(car2, t, display.data); - + + %% CORE OF LOOP + [car1, car1.stop, display.data] = do_car(car1, t, display.data, boot1); + [car2, car2.stop, display.data] = do_car(car2, t, display.data, boot2); + %% BOOTSTRAP + if boot1.status + [car1, boot1] = do_boot(car1, boot1); + end + if boot2.status + [car2, boot2] = do_boot(car2, boot2); + end + %% GOVERNOR + if not(boot1.status) && car1.lap ~= 0 + car1 = do_gov(car1); + end + if not(boot2.status) && car2.lap ~= 0 + car2 = do_gov(car2); + end + %% if car1.stop == true disp('stopped by car 1'); break; @@ -150,7 +187,7 @@ while 1 while 1 %Whileloop med paus som k�rs till pausen �verskridit 0.07 sekunder % DISPLAY display.send_delay = tic; - if toc(display.last_send) > display.send_interval + if toc(display.last_send) > display.send_interval && display_active % queue control signal if car1.running && car1.automatic % display.data = [display.data, put_text(20, 16 + (16 * 1), 'L', num2str(car1.u))]; @@ -197,18 +234,18 @@ disp(car2); terminate(1); terminate(2); - -matlabclient(3); - +if display_active + matlabclient(3); +end %% DISPLAY GRAPHS if car1.running == true - graphs(car1.lap_times, ref_time, car1.seg_times, 1); + graphs(car1.lap_times, car1.ref_time, car1.seg_times, 1); end if car2.running == true - graphs(car2.lap_times, ref_time, car2.seg_times, 2); + graphs(car2.lap_times, car2.ref_time, car2.seg_times, 2); end %% SAVE VARIABLES FROM CAR STRUCT diff --git a/Kod/bilbana/yc4/setup_car.m b/Kod/bilbana/yc4/setup_car.m new file mode 100644 index 0000000..1ab75e0 --- /dev/null +++ b/Kod/bilbana/yc4/setup_car.m @@ -0,0 +1,17 @@ +function [] = setup_car(car, track) +%UNTITLED2 Summary of this function goes here +% Detailed explanation goes here +disp('J = Ja (automatiskt), M = Ja (manuellt), N = Nej'); + +car.response = input('Vill du köra bil 1? [N] ', 's'); +if car.response == 'J' + car.running = true; + car.automatic = true; +elseif car.response == 'M' + car.running = true; + car.automatic = false; +else + car.running = false; +end +end + diff --git a/Kod/scripts/test_givare.m b/Kod/scripts/test_givare.m index 93ff860..726fe82 100644 --- a/Kod/scripts/test_givare.m +++ b/Kod/scripts/test_givare.m @@ -48,7 +48,6 @@ while 1 end pause(0.1) end - %% terminate(1) -terminate(2)
\ No newline at end of file +terminate(2) diff --git a/Kod/scripts/test_givare_kompenserad.m b/Kod/scripts/test_givare_kompenserad.m new file mode 100644 index 0000000..2ac0841 --- /dev/null +++ b/Kod/scripts/test_givare_kompenserad.m @@ -0,0 +1,79 @@ +disp('Startar bilbanan. Avsluta med q.') +hf=figure('position',[0 0 eps eps],'menubar','none'); + +initialize_counters(1) +initialize_counters(2) + +start_race(1) +start_race(2) + +config_IOs + +car1 = struct; +car2 = struct; + +while 1 + %% PRE-LOOP + if strcmp(get(hf,'currentcharacter'),'q') + close(hf) + break + end + + figure(hf) + drawnow + + %% READ + [car1.new_lap, car1.new_check_point, car1.time] = get_car_position(1); + [car2.new_lap, car2.new_check_point, car2.time] = get_car_position(2); + + if car1.new_lap || car1.new_check_point == true || car2.new_lap == true || car2.new_check_point == true + disp('###') + end + + if car1.new_check_point + beep; + disp('car 1 cp') + end + if car2.new_check_point + beep; + disp('car 2 cp') + end + if car1.new_lap + beep; + disp('car 1 lap') + end + if car2.new_lap + beep; + disp('car 2 lap') + end + % KOMPENSERA FÖR TRASIG BANA + if car1.new_lap && (car2.new_check_point || car1.new_check_point) + car2.new_lap = 0; + elseif car1.new_lap + car2.new_lap = 1; + end + pause(0.1) + + if car1.new_lap || car1.new_check_point == true || car2.new_lap == true || car2.new_check_point == true + disp('---') + end + + if car1.new_lap + disp('New lap car 1') + end + if car1.new_check_point + disp('New checkpoint car 1') + end + if car2.new_lap + disp('NEW LAP CAR 2!') + end + if car2.new_check_point + disp('NEW CHECKPOINT CAR 2!') + end + + pause(0.1) +end + +%% +terminate(1) +terminate(2)
\ No newline at end of file |
