git.haldean.org droidcopter / 8f098a3
Keyboard control now works as expected -- correct diagonal movement and no repeat commands are sent. Will Brown 11 years ago
3 changed file(s) with 47 addition(s) and 67 deletion(s). Raw diff Collapse all Expand all
2020 private static double bearing = 0;
2121
2222 /* In meters per second (we hope.) */
23 private static final int VELOCITY = 2;
24 /* In seconds */
25 private static final int STEP_DURATION = 1000;
23 public static final int VELOCITY = 2;
2624
2725 /**
2826 * This is a convenience class that is never instantiated.
3331
3432 /**
3533 * Tell the helicopter to go at a specific velocity in a specific
36 * direction.
34 * direction, for a specific amount of time.
3735 *
3836 * @param velocities An array whose values correspond to the North,
3937 * South, East and West portions of the desired velocity.
129127 DataReceiver.sendToDefault(task.toString());
130128 Debug.log("Sent new navigation task: " + task.toString());
131129 }
132
133 private static double[] velocityFor(int i, boolean positive) {
134 double[] v = new double[]{0, 0, 0};
135 v[i] = positive ? VELOCITY : -VELOCITY;
136 return v;
137 }
138
139 public static void engage() {
140 manualVelocity(velocityFor(0, true), bearing);
141 }
142
143 public static void engageReverse() {
144 manualVelocity(velocityFor(0, false), bearing);
145 }
146
147 public static void toPort() {
148 manualVelocity(velocityFor(1, false), bearing);
149 }
150
151 public static void toStarboard() {
152 manualVelocity(velocityFor(1, true), bearing);
153 }
154
155 public static void ascend() {
156 manualVelocity(velocityFor(2, true), bearing);
157 }
158
159 public static void descend() {
160 manualVelocity(velocityFor(2, false), bearing);
161 }
162
163 public static void fullStop() {
164 manualVelocity(new double[]{0, 0, 0}, bearing);
165 }
166
167 public static void setCourseFor(double theta) {
168 manualVelocity(new double[]{0, 0, 0}, theta);
169 }
170130 }
33 import static net.java.games.input.Component.Identifier.Key;
44
55 import java.util.ArrayList;
6 import java.util.Arrays;
67 import java.util.HashMap;
78 import javax.swing.JOptionPane;
89
910 public class KeyboardController extends UiController {
11 private boolean enable = true;
12 private boolean dryRun = false;
13
1014 private ArrayList<Keyboard> keyboards;
11 private boolean enable = true;
1215 private HashMap<Key, Long> keypresses;
1316 private ServerHost ui;
17
18 private double[] lastVelocities;
19 private double theta = 0;
1420
21 private int POLL_PERIOD = 40;
22 private int SLEEP_TIME = 500;
23
1524 public KeyboardController(ServerHost ui) {
1625 this.ui = ui;
1726 keypresses = new HashMap<Key, Long>();
5867 k.poll();
5968 }
6069
61 /* WASD for forward-left-back-right */
62 if (isKeyPressed(Key.W, true))
63 EnsignCrusher.engage();
64 else if (isKeyPressed(Key.S, true))
65 EnsignCrusher.engageReverse();
70 double[] newVelocities = new double[3];
6671
67 /* QE for up-down */
68 if (isKeyPressed(Key.Q, true))
69 EnsignCrusher.ascend();
70 else if (isKeyPressed(Key.E, true))
71 EnsignCrusher.descend();
72 /* WASD for forward-left-back-right. I miss Lisp. */
73 newVelocities[0] =
74 isKeyPressed(Key.W, true) && isKeyPressed(Key.S, true) ? 0 :
75 (isKeyPressed(Key.W, true) ? EnsignCrusher.VELOCITY :
76 (isKeyPressed(Key.S, true) ? -EnsignCrusher.VELOCITY : 0));
7277
73 if (isKeyPressed(Key.A, true))
74 EnsignCrusher.toPort();
75 else if (isKeyPressed(Key.D, true))
76 EnsignCrusher.toStarboard();
78 newVelocities[1] =
79 isKeyPressed(Key.A, true) && isKeyPressed(Key.D, true) ? 0 :
80 (isKeyPressed(Key.D, true) ? EnsignCrusher.VELOCITY :
81 (isKeyPressed(Key.A, true) ? -EnsignCrusher.VELOCITY : 0));
82
83 newVelocities[2] =
84 isKeyPressed(Key.Q, true) && isKeyPressed(Key.E, true) ? 0 :
85 (isKeyPressed(Key.Q, true) ? EnsignCrusher.VELOCITY :
86 (isKeyPressed(Key.E, true) ? -EnsignCrusher.VELOCITY : 0));
7787
7888 /* Rotation */
79 if (isKeyPressed(Key.R))
80 getRotation();
89 boolean newRotation = isKeyPressed(Key.R);
90 if (newRotation) getRotation();
8191
82 if (keyReleased(Key.W) || keyReleased(Key.S) ||
83 keyReleased(Key.D) || keyReleased(Key.A) ||
84 keyReleased(Key.Q) || keyReleased(Key.E))
85 EnsignCrusher.fullStop();
92 if (! Arrays.equals(newVelocities, lastVelocities) || newRotation) {
93 setVelocity(newVelocities, theta);
94 lastVelocities = newVelocities;
95 }
96 }
97
98 private void setVelocity(double[] velocities, double bearing) {
99 if (dryRun) {
100 System.out.println(Arrays.toString(velocities) + ", " + bearing);
101 } else {
102 EnsignCrusher.manualVelocity(velocities, bearing);
103 }
86104 }
87105
88106 private void getRotation() {
93111 if (theta < 0 || theta >= 360) {
94112 throw new NumberFormatException("Bearing must be between 0 and 360.");
95113 }
96 EnsignCrusher.setCourseFor(theta);
114 this.theta = theta;
97115 } catch (NumberFormatException e) {
98116 JOptionPane.showMessageDialog(ui, e.toString(), "Rotation Command Failed",
99117 JOptionPane.ERROR_MESSAGE);
106124 return isKeyPressed(key, false);
107125 }
108126
109 private boolean keyReleased(Key key) {
127 private boolean isKeyReleased(Key key) {
110128 for (Keyboard k : keyboards) {
111129 if (k.isKeyDown(key)) {
112130 return false;
141159
142160 public static void main(String args[]) {
143161 KeyboardController k = new KeyboardController(null);
162 k.dryRun = true;
144163 k.run();
145164 }
146165 }
00 #!/bin/bash
11
2 export CLASSPATH=$CLASSPATH:`ls -1 jars/*.jar jars/worldwind/*.jar | xargs | sed "s/ /:/g"`:.
2 export CLASSPATH=$ANDROIDPATH:$CLASSPATH:./lib/simplegraph/:`ls -1 jars/*.jar jars/worldwind/*.jar | xargs | sed "s/ /:/g"`:/usr/share/java/j3dcore.jar:/usr/share/java/j3dutils.jar:/usr/share/java/vecmath.jar:.
33
44 if [ -z "$1" ]; then
55 echo "Must supply a class to test" 1>&2
77 echo "Running test for class $1" 1>&2
88 javac -cp $CLASSPATH "org/haldean/chopper/server/$1.java" && \
99 echo "Compiled" 1>&2 && \
10 java -Djava.library.path=jars/worldwind/:/lib/ -cp $CLASSPATH "org.haldean.chopper.server.$1"
10 java -Djava.library.path=jars/worldwind/:/lib/:jars/ \
11 -cp $CLASSPATH "org.haldean.chopper.server.$1"
1112 fi