diff options
Diffstat (limited to 'Kod/bilbana/studentFunctions')
| -rw-r--r-- | Kod/bilbana/studentFunctions/car_controller.m | 16 | ||||
| -rw-r--r-- | Kod/bilbana/studentFunctions/init_car.m | 25 | ||||
| -rw-r--r-- | Kod/bilbana/studentFunctions/plot_results.m | 15 | ||||
| -rw-r--r-- | Kod/bilbana/studentFunctions/run_simple_example.m | 41 | ||||
| -rw-r--r-- | Kod/bilbana/studentFunctions/update_control.m | 16 | ||||
| -rw-r--r-- | Kod/bilbana/studentFunctions/update_position.m | 39 |
6 files changed, 152 insertions, 0 deletions
diff --git a/Kod/bilbana/studentFunctions/car_controller.m b/Kod/bilbana/studentFunctions/car_controller.m new file mode 100644 index 0000000..069e8f5 --- /dev/null +++ b/Kod/bilbana/studentFunctions/car_controller.m @@ -0,0 +1,16 @@ +function car = car_controller(car_in)
+
+car = car_in;
+
+% Read sensors
+[car, cp_passed] = update_position(car);
+
+if cp_passed
+ % Update speed
+
+ new_control = car.control(end) + 0.5*(rand() - 0.5); % Temporary control policy
+
+ car = update_control(new_control, car);
+end
+
+end
\ No newline at end of file diff --git a/Kod/bilbana/studentFunctions/init_car.m b/Kod/bilbana/studentFunctions/init_car.m new file mode 100644 index 0000000..8206206 --- /dev/null +++ b/Kod/bilbana/studentFunctions/init_car.m @@ -0,0 +1,25 @@ +function car = init_car(track_number)
+
+car = struct();
+
+% Car state
+car.state = 'intro';
+
+% Track number
+car.track_number = track_number;
+
+% Current track segment
+car.position = 0;
+
+% Current lap
+car.lap = 0;
+
+% Start race timer
+car.t0 = tic();
+
+% Control signal
+car.control = nan(1,1);
+car.control_log_time = nan(1,1);
+car.control_log_position = zeros(1,1);
+
+end
\ No newline at end of file diff --git a/Kod/bilbana/studentFunctions/plot_results.m b/Kod/bilbana/studentFunctions/plot_results.m new file mode 100644 index 0000000..34ea2e4 --- /dev/null +++ b/Kod/bilbana/studentFunctions/plot_results.m @@ -0,0 +1,15 @@ +% Plot the results
+figure(100)
+subplot(2,1,1)
+stairs(car.control_log_time, car.control, 'LineWidth', 2) % Gör en trappstegsplot
+xlabel('Time [s]')
+ylabel('Control signal [%]')
+title('Control signal trajectory')
+grid on
+
+subplot(2,1,2)
+stairs(car.control_log_time, car.control_log_position, 'r', 'LineWidth', 2)
+xlabel('Time [s]')
+ylabel('Car position [segment number]')
+title('Car position')
+grid on
diff --git a/Kod/bilbana/studentFunctions/run_simple_example.m b/Kod/bilbana/studentFunctions/run_simple_example.m new file mode 100644 index 0000000..45794f0 --- /dev/null +++ b/Kod/bilbana/studentFunctions/run_simple_example.m @@ -0,0 +1,41 @@ +%% ---------------- Do not touch ---------------- %
+disp('Startar bilbanan. Avsluta med q')
+hf=figure('position',[0 0 eps eps],'menubar','none');
+% ----------------------------------------------- %
+%% User input
+track_number = 1;
+car = init_car(track_number);
+
+%% Init race track
+initialize_counters(car.track_number)
+config_IOs
+start_race(car.track_number)
+
+%% Start cars
+start_control = 29; % 29 works for the white castrol car marked with number 82
+car = update_control(start_control, car);
+
+%% Running loop
+while 1
+
+ % ---------------- Do not touch ---------------- %
+ % Check if user has pressed q
+ if strcmp(get(hf,'currentcharacter'),'q')
+ close(hf)
+ break
+ end
+ % force the event queue to flush
+ figure(hf)
+ drawnow
+ % ---------------------------------------------- %
+
+ car = car_controller(car);
+
+ pause(0.1) % Pause 0.1 s
+end
+
+terminate(1)
+terminate(2)
+
+% run file to plot results
+plot_results();
\ No newline at end of file diff --git a/Kod/bilbana/studentFunctions/update_control.m b/Kod/bilbana/studentFunctions/update_control.m new file mode 100644 index 0000000..ee6599c --- /dev/null +++ b/Kod/bilbana/studentFunctions/update_control.m @@ -0,0 +1,16 @@ +function car = update_control(new_control_signal, car_in)
+
+% Return track struct
+car = car_in;
+
+% Update track struct
+car.control(end+1) = new_control_signal;
+car.control_log_time(end+1) = toc(car.t0);
+car.control_log_position(end+1) = car.position;
+
+% Update speed
+set_car_speed(car.track_number, car.control(end));
+
+end
+
+
diff --git a/Kod/bilbana/studentFunctions/update_position.m b/Kod/bilbana/studentFunctions/update_position.m new file mode 100644 index 0000000..dfca55f --- /dev/null +++ b/Kod/bilbana/studentFunctions/update_position.m @@ -0,0 +1,39 @@ +function [car, gateway_passed] = update_position(car_in)
+
+car = car_in;
+
+[lap_car, chk_pnt, time] = get_car_position(car.track_number);
+
+if lap_car == true
+ % New lap
+
+ % Update position on track
+ car.position = 1;
+
+ % Add lap to lap counter
+ car.lap = car.lap + 1;
+
+ % Update gw passed
+ gateway_passed = true;
+
+ % Make sound
+ beep;
+
+elseif chk_pnt == true && car.position ~= 0
+ % CP passed
+
+ % Update position
+ car.position = car.position + 1;
+
+ % update gw passed
+ gateway_passed = true;
+
+ % Make sound
+ beep;
+
+else
+ gateway_passed = false;
+
+end
+
+end
\ No newline at end of file |
