Repository navigation
Expand file tree
/
Copy pathstartKeyboard.m
More file actions
79 lines (73 loc) · 2.36 KB
/
Copy pathstartKeyboard.m
File metadata and controls
79 lines (73 loc) · 2.36 KB
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
function startKeyboard()
%startKeyboard Open a figure window for keyboard control of the quadruped.
%
% startKeyboard() creates a MATLAB figure that captures key press/release
% events and stores them in global variables. These globals are read by
% readKeyboard() during simulation to drive the robot.
%
% Controls:
% Arrow Up/Down - Forward/backward walking
% Arrow Left/Right - Turn left/right
% A/D - Sidestep left/right
%
% The figure must remain focused (clicked on) to capture key events.
% Closing the figure resets all commands to zero.
global g_key_up g_key_down g_key_left g_key_right g_key_a g_key_d
g_key_up = false;
g_key_down = false;
g_key_left = false;
g_key_right = false;
g_key_a = false;
g_key_d = false;
fig = figure('Name', 'Quadruped Keyboard Control', ...
'NumberTitle', 'off', ...
'MenuBar', 'none', ...
'Position', [100 100 400 250], ...
'KeyPressFcn', @keyDown, ...
'KeyReleaseFcn', @keyUp, ...
'CloseRequestFcn', @onClose);
annotation(fig, 'textbox', [0.05 0.05 0.9 0.9], ...
'String', {'QUADRUPED KEYBOARD CONTROL', '', ...
'Up = Forward', ...
'Down = Backward', ...
'Left = Turn Left', ...
'Right = Turn Right', ...
'A = Sidestep Left', ...
'D = Sidestep Right', ...
'Up/Down + Left/Right = Steer', ...
'', 'Keep this window focused!'}, ...
'FontSize', 12, 'EdgeColor', 'none', ...
'HorizontalAlignment', 'center');
end
function keyDown(~, event)
global g_key_up g_key_down g_key_left g_key_right g_key_a g_key_d
switch event.Key
case 'uparrow', g_key_up = true;
case 'downarrow', g_key_down = true;
case 'leftarrow', g_key_left = true;
case 'rightarrow', g_key_right = true;
case 'a', g_key_a = true;
case 'd', g_key_d = true;
end
end
function keyUp(~, event)
global g_key_up g_key_down g_key_left g_key_right g_key_a g_key_d
switch event.Key
case 'uparrow', g_key_up = false;
case 'downarrow', g_key_down = false;
case 'leftarrow', g_key_left = false;
case 'rightarrow', g_key_right = false;
case 'a', g_key_a = false;
case 'd', g_key_d = false;
end
end
function onClose(src, ~)
global g_key_up g_key_down g_key_left g_key_right g_key_a g_key_d
g_key_up = false;
g_key_down = false;
g_key_left = false;
g_key_right = false;
g_key_a = false;
g_key_d = false;
delete(src);
end