-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathKeyboardInput.java
170 lines (142 loc) · 4.54 KB
/
KeyboardInput.java
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* KeyboardInput.java
* ~~~~~~~~~~~~~~~~~~
* Please do not remove the following notices.
* Copyright (c) 2010 by Geekscape Pty. Ltd.
* License: GPLv3. http://geekscape.org/static/parrot_license.html
*
* To Do
* ~~~~~
* - Use higher-level Parrot communication abstractions.
* - Continual (smoothly) rotate left and right.
* - Move (smoothly) up and down 10 cm jumps.
* - Check smoothness of all commands ...
* - Roll, pitch, vertical, rotation.
*/
import java.awt.event.*;
public class KeyboardInput implements KeyListener {
private static float RATE_SLOW = 3.0f; // roll and pitch less aggressive
private static float RATE_TABLE[] = {
0.99f, 0.05f, 0.10f, 0.20f, 0.30f, 0.40f, 0.50f, 0.60f, 0.70f, 0.85f
};
private float rate = RATE_TABLE[4];
private boolean shiftPressed = false;
private ParrotCommunication parrotCommunication = null;
public KeyboardInput(
ParrotCommunication parrotCommunication) {
this.parrotCommunication = parrotCommunication;
}
public void keyTyped(
KeyEvent keyEvent) {
}
public void keyPressed(
KeyEvent keyEvent) {
int keyCode = keyEvent.getKeyCode();
// System.out.println("KeyCode pressed: " + keyCode);
switch (keyCode) {
case KeyEvent.VK_SHIFT:
shiftPressed = true;
break;
case KeyEvent.VK_SPACE:
case KeyEvent.VK_H:
parrotCommunication.emergencyAbort = true;
parrotCommunication.transmitProgressiveCommand(
ParrotCommunication.MODE_HOVER, 0f, 0f, 0f, 0f
);
break;
case KeyEvent.VK_0:
case KeyEvent.VK_1:
case KeyEvent.VK_2:
case KeyEvent.VK_3:
case KeyEvent.VK_4:
case KeyEvent.VK_5:
case KeyEvent.VK_6:
case KeyEvent.VK_7:
case KeyEvent.VK_8:
case KeyEvent.VK_9:
rate = RATE_TABLE[(keyCode - KeyEvent.VK_0)];
// System.out.println("rate: " + rate);
break;
case KeyEvent.VK_E:
parrotCommunication.transmitRefCommand( // "reset"
ParrotCommunication.BEHAVIOUR_EMERGENCY
);
break;
case KeyEvent.VK_F:
parrotCommunication.transmitCommand("FTRIM", null);
break;
case KeyEvent.VK_L:
parrotCommunication.transmitRefCommand(
ParrotCommunication.BEHAVIOUR_LAND
);
break;
case KeyEvent.VK_Q:
parrotCommunication.transmitRefCommand(
ParrotCommunication.BEHAVIOUR_LAND
);
System.exit(0);
break;
case KeyEvent.VK_T:
parrotCommunication.transmitRefCommand(
ParrotCommunication.BEHAVIOUR_TAKEOFF
);
break;
case KeyEvent.VK_UP:
if (shiftPressed) {
parrotCommunication.transmitProgressiveCommand( // Up
ParrotCommunication.MODE_PROGRESSIVE, 0f, 0f, rate, 0f
);
}
else {
parrotCommunication.transmitProgressiveCommand( // Forward
ParrotCommunication.MODE_PROGRESSIVE, 0f, -rate / RATE_SLOW, 0f, 0f
);
}
break;
case KeyEvent.VK_DOWN:
if (shiftPressed) {
parrotCommunication.transmitProgressiveCommand( // Down
ParrotCommunication.MODE_PROGRESSIVE, 0f, 0f, -rate, 0f
);
}
else {
parrotCommunication.transmitProgressiveCommand( // Back
ParrotCommunication.MODE_PROGRESSIVE, 0f, rate / RATE_SLOW, 0f, 0f
);
}
break;
case KeyEvent.VK_LEFT:
if (shiftPressed) {
parrotCommunication.transmitProgressiveCommand( // Yaw left (rotate)
ParrotCommunication.MODE_PROGRESSIVE, 0f, 0f, 0f, -rate
);
}
else {
parrotCommunication.transmitProgressiveCommand( // Roll left
ParrotCommunication.MODE_PROGRESSIVE, -rate / RATE_SLOW, 0f, 0f, 0f
);
}
break;
case KeyEvent.VK_RIGHT:
if (shiftPressed) {
parrotCommunication.transmitProgressiveCommand( // Yaw right (rotate)
ParrotCommunication.MODE_PROGRESSIVE, 0f, 0f, 0f, rate
);
}
else {
parrotCommunication.transmitProgressiveCommand( // Roll right
ParrotCommunication.MODE_PROGRESSIVE, rate / RATE_SLOW, 0f, 0f, 0f
);
}
break;
}
}
public void keyReleased(
KeyEvent keyEvent) {
int keyCode = keyEvent.getKeyCode();
switch (keyCode) {
case KeyEvent.VK_SHIFT:
shiftPressed = false;
break;
}
}
}