-
Notifications
You must be signed in to change notification settings - Fork 36
/
processKeyboard.m
88 lines (65 loc) · 2.08 KB
/
processKeyboard.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
function processKeyboard(actor, key, dt)
%% Keys function
%
% W ---> Forward
% S ---> Brake
% A ---> Left
% D ---> Right
% R ---> Reverse gear
% F ---> Forward gear
% E ---> Enter auto control
% Q ---> Quit auto control
key = lower(key);
if strcmp(key, 'timeout')
return;
elseif strcmp(key, char(0))
control = actor.get_control();
control.steer = 0;
actor.apply_control(control);
end
% Change the drive mode
if strcmp(key, 'e')
actor.set_autopilot(true);
elseif strcmp(key, 'q')
actor.set_autopilot(false);
% Throttle
elseif strcmp(key, 'w')
control = actor.get_control();
control.throttle = control.throttle + 0.5 * dt;
control.brake = 0;
if control.throttle > 1
control.throttle = 1;
end
actor.apply_control(control);
elseif strcmp(key, 's')
control = actor.get_control();
control.throttle = 0;
control.brake = 1;
actor.apply_control(control);
% Steer
elseif strcmp(key, 'd')
control = actor.get_control();
control.steer = control.steer + 0.5 * dt;
if control.steer > 1
control.steer = 1;
end
actor.apply_control(control);
elseif strcmp(key, 'a')
control = actor.get_control();
control.steer = control.steer - 0.5 * dt;
if control.steer < -1
control.steer = -1;
end
actor.apply_control(control);
% Direction
elseif strcmp(key, 'r')
control = actor.get_control();
control.reverse = true;
actor.apply_control(control);
elseif strcmp(key, 'f')
control = actor.get_control();
control.reverse = false;
control.throttle = 0.5;
actor.apply_control(control);
end
end