Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Added support for controlling drone via API
- Loading branch information
Showing
17 changed files
with
939 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
import java.net.*; | ||
import java.util.*; | ||
import java.awt.*; | ||
import java.awt.event.*; | ||
import java.nio.*; | ||
|
||
//Main class for controlling the drone with the Emotiv EPOC and EmoKey | ||
class Main extends Frame implements KeyListener { | ||
private static final long serialVersionUID = 1L; | ||
InetAddress inet_addr; | ||
DatagramSocket socket; | ||
int seq = 1; //Send AT command with sequence number 1 will reset the counter | ||
float speed = (float)0.8; //UAV movement speed | ||
boolean shift = false; | ||
FloatBuffer fb; | ||
IntBuffer ib; | ||
|
||
public Main(String name, String args[]) throws Exception { | ||
super(name); | ||
String ip = "192.168.1.1"; //IP address for the UAV WiFi connection | ||
if (args.length >= 1) { | ||
ip = args[0]; | ||
} | ||
StringTokenizer st = new StringTokenizer(ip, "."); | ||
byte[] ip_bytes = new byte[4]; | ||
if (st.countTokens() == 4) { | ||
for (int i = 0; i < 4; i++) { | ||
ip_bytes[i] = (byte)Integer.parseInt(st.nextToken()); | ||
} | ||
} | ||
else { | ||
System.out.println("Incorrect IP address format: " + ip); | ||
System.exit(-1); | ||
} | ||
System.out.println("IP: " + ip); | ||
System.out.println("Speed: " + speed); | ||
ByteBuffer bb = ByteBuffer.allocate(4); | ||
fb = bb.asFloatBuffer(); | ||
ib = bb.asIntBuffer(); | ||
inet_addr = InetAddress.getByAddress(ip_bytes); | ||
socket = new DatagramSocket(); | ||
socket.setSoTimeout(3000); | ||
send_at_cmd("AT*CONFIG=1,\"control:altitude_max\",\"2000\""); //altitude max 2 meters | ||
if (args.length == 2) { //Command line mode | ||
send_at_cmd(args[1]); | ||
System.exit(0); | ||
} | ||
//Use keys from EmoKey to control the drone if different events are triggered | ||
addKeyListener(this); | ||
setSize(320, 160); | ||
setVisible(true); | ||
addWindowListener(new WindowAdapter() { | ||
public void windowClosing(WindowEvent e) { | ||
System.exit(0); | ||
} | ||
}); | ||
Thread t = new Thread(new Reset()); | ||
t.start(); | ||
} | ||
|
||
public static void main(String args[]) throws Exception { | ||
new Main("ARDrone MAIN", args); | ||
} | ||
|
||
@Override | ||
public void keyPressed(KeyEvent e) { | ||
//changes key to its associated integer | ||
int keyCode = e.getKeyCode(); | ||
System.out.println("Key: " + keyCode + " (" + KeyEvent.getKeyText(keyCode) + ")"); | ||
try { | ||
//Use specific key code as a command to the drone | ||
control(keyCode); | ||
} | ||
catch (Exception ex) { | ||
ex.printStackTrace(); | ||
} | ||
} | ||
|
||
@Override | ||
public void keyReleased(KeyEvent arg0) {} | ||
|
||
@Override | ||
public void keyTyped(KeyEvent arg0) {} | ||
|
||
public int intOfFloat(float f) { | ||
fb.put(0, f); | ||
return ib.get(0); | ||
} | ||
|
||
//Control AR.Drone via AT commands per key code | ||
public void control(int keyCode) throws Exception { | ||
String at_cmd = ""; | ||
String action = ""; | ||
switch (keyCode) { | ||
//Only max of 4 cognitive commands at a time recommended | ||
//Any changes to letter meaning/action here must occur in EmoKey mapping too | ||
case 'I'://Up (lift) | ||
action = "Go Up (gaz+)"; | ||
at_cmd = "AT*PCMD=" + (seq++) + ",1,0,0," + intOfFloat(speed) + ",0"; | ||
break; | ||
case 'P': //Forward (push) | ||
action = "Go Forward (pitch+)"; | ||
at_cmd = "AT*PCMD=" + (seq++) + ",1,0," + intOfFloat(-speed) + ",0,0"; | ||
break; | ||
case 'D'://Down (drop) | ||
action = "Go Down (gaz-)"; | ||
at_cmd = "AT*PCMD=" + (seq++) + ",1,0,0," + intOfFloat(-speed) + ",0"; | ||
break; | ||
case 'U': //Backward (pull) | ||
action = "Go Backward (pitch-)"; | ||
at_cmd = "AT*PCMD=" + (seq++) + ",1,0," + intOfFloat(speed) + ",0,0"; | ||
break; | ||
case 'T'://turn left (counterclockwise) | ||
action = "Rotate Left (yaw-)"; | ||
at_cmd = "AT*PCMD=" + (seq++) + ",1,0,0,0," + intOfFloat(-speed); | ||
break; | ||
case 'L': //Left (think left) | ||
action = "Go Left (roll-)"; | ||
at_cmd = "AT*PCMD=" + (seq++) + ",1," + intOfFloat(-speed) + ",0,0,0"; | ||
break; | ||
case 'C'://turn right (clockwise) | ||
action = "Rotate Right (yaw+)"; | ||
at_cmd = "AT*PCMD=" + (seq++) + ",1,0,0,0," + intOfFloat(speed); | ||
break; | ||
case 'R': //Right (think right) | ||
action = "Go Right (roll+)"; | ||
at_cmd = "AT*PCMD=" + (seq++) + ",1," + intOfFloat(speed) + ",0,0,0"; | ||
break; | ||
case 'N'://Hover (neutral) | ||
//Not necessary but can be used to stabilize drone | ||
action = "Hovering"; | ||
at_cmd = "AT*PCMD=" + (seq++) + ",1,0,0,0,0"; | ||
break; | ||
case 'S'://Takeoff (raise brow) | ||
action = "Takeoff"; | ||
at_cmd = "AT*REF=" + (seq++) + ",290718208"; | ||
break; | ||
case 'A'://Land (clench) | ||
action = "Landing"; | ||
at_cmd = "AT*REF=" + (seq++) + ",290717696"; | ||
break; | ||
case 'Z'://reset | ||
action = "Reset"; | ||
at_cmd = "AT*REF=1,290717952"; | ||
break; | ||
default: | ||
break; | ||
} | ||
System.out.println("Speed: " + speed); | ||
System.out.println("Action: " + action); | ||
send_at_cmd(at_cmd); | ||
} | ||
|
||
public void send_at_cmd(String at_cmd) throws Exception { | ||
System.out.println("AT command: " + at_cmd); | ||
byte[] buffer = (at_cmd + "\r").getBytes(); | ||
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, inet_addr, 5556); | ||
socket.send(packet); | ||
//socket.receive(packet); //AR.Drone does not send back ack message (like "OK") | ||
//System.out.println(new String(packet.getData(),0,packet.getLength())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
import java.net.*; | ||
import java.util.*; | ||
import java.awt.*; | ||
import java.awt.event.*; | ||
import java.nio.*; | ||
|
||
//Main class for controlling the drone with the Emotiv EPOC and EmoKey | ||
class Main extends Frame implements KeyListener { | ||
private static final long serialVersionUID = 1L; | ||
InetAddress inet_addr; | ||
DatagramSocket socket; | ||
int seq = 1; //Send AT command with sequence number 1 will reset the counter | ||
float speed = (float)0.8; //UAV movement speed | ||
boolean shift = false; | ||
FloatBuffer fb; | ||
IntBuffer ib; | ||
|
||
public Main(String name, String args[]) throws Exception { | ||
super(name); | ||
String ip = "192.168.1.1"; //IP address for the UAV WiFi connection | ||
if (args.length >= 1) { | ||
ip = args[0]; | ||
} | ||
StringTokenizer st = new StringTokenizer(ip, "."); | ||
byte[] ip_bytes = new byte[4]; | ||
if (st.countTokens() == 4) { | ||
for (int i = 0; i < 4; i++) { | ||
ip_bytes[i] = (byte)Integer.parseInt(st.nextToken()); | ||
} | ||
} | ||
else { | ||
System.out.println("Incorrect IP address format: " + ip); | ||
System.exit(-1); | ||
} | ||
System.out.println("IP: " + ip); | ||
System.out.println("Speed: " + speed); | ||
ByteBuffer bb = ByteBuffer.allocate(4); | ||
fb = bb.asFloatBuffer(); | ||
ib = bb.asIntBuffer(); | ||
inet_addr = InetAddress.getByAddress(ip_bytes); | ||
socket = new DatagramSocket(); | ||
socket.setSoTimeout(3000); | ||
send_at_cmd("AT*CONFIG=1,\"control:altitude_max\",\"2000\""); //altitude max 2 meters | ||
if (args.length == 2) { //Command line mode | ||
send_at_cmd(args[1]); | ||
System.exit(0); | ||
} | ||
//Use keys from EmoKey to control the drone if different events are triggered | ||
addKeyListener(this); | ||
setSize(320, 160); | ||
setVisible(true); | ||
addWindowListener(new WindowAdapter() { | ||
public void windowClosing(WindowEvent e) { | ||
System.exit(0); | ||
} | ||
}); | ||
Thread t = new Thread(new Reset()); | ||
t.start(); | ||
} | ||
|
||
public static void main(String args[]) throws Exception { | ||
new Main("ARDrone MAIN", args); | ||
} | ||
|
||
@Override | ||
public void keyPressed(KeyEvent e) { | ||
//changes key to its associated integer | ||
int keyCode = e.getKeyCode(); | ||
System.out.println("Key: " + keyCode + " (" + KeyEvent.getKeyText(keyCode) + ")"); | ||
try { | ||
//Use specific key code as a command to the drone | ||
control(keyCode); | ||
} | ||
catch (Exception ex) { | ||
ex.printStackTrace(); | ||
} | ||
} | ||
|
||
@Override | ||
public void keyReleased(KeyEvent arg0) {} | ||
|
||
@Override | ||
public void keyTyped(KeyEvent arg0) {} | ||
|
||
public int intOfFloat(float f) { | ||
fb.put(0, f); | ||
return ib.get(0); | ||
} | ||
|
||
//Control AR.Drone via AT commands per key code | ||
public void control(int keyCode) throws Exception { | ||
String at_cmd = ""; | ||
String action = ""; | ||
switch (keyCode) { | ||
//Only max of 4 cognitive commands at a time recommended | ||
//Any changes to letter meaning/action here must occur in EmoKey mapping too | ||
case 'I': //Up (lift) | ||
action = "Go Up (gaz+)"; | ||
at_cmd = "AT*PCMD=" + (seq++) + ",1,0,0," + intOfFloat(speed) + ",0"; | ||
break; | ||
case 'P': //Forward (push) | ||
action = "Go Forward (pitch+)"; | ||
at_cmd = "AT*PCMD=" + (seq++) + ",1,0," + intOfFloat(-speed) + ",0,0"; | ||
break; | ||
case 'D': //Down (drop) | ||
action = "Go Down (gaz-)"; | ||
at_cmd = "AT*PCMD=" + (seq++) + ",1,0,0," + intOfFloat(-speed) + ",0"; | ||
break; | ||
case 'U': //Backward (pull) | ||
action = "Go Backward (pitch-)"; | ||
at_cmd = "AT*PCMD=" + (seq++) + ",1,0," + intOfFloat(speed) + ",0,0"; | ||
break; | ||
case 'T': //turn left (counterclockwise) | ||
action = "Rotate Left (yaw-)"; | ||
at_cmd = "AT*PCMD=" + (seq++) + ",1,0,0,0," + intOfFloat(-speed); | ||
break; | ||
case 'L': //Left (think left) | ||
action = "Go Left (roll-)"; | ||
at_cmd = "AT*PCMD=" + (seq++) + ",1," + intOfFloat(-speed) + ",0,0,0"; | ||
break; | ||
case 'C': //turn right (clockwise) | ||
action = "Rotate Right (yaw+)"; | ||
at_cmd = "AT*PCMD=" + (seq++) + ",1,0,0,0," + intOfFloat(speed); | ||
break; | ||
case 'R': //Right (think right) | ||
action = "Go Right (roll+)"; | ||
at_cmd = "AT*PCMD=" + (seq++) + ",1," + intOfFloat(speed) + ",0,0,0"; | ||
break; | ||
case 'N': //Hover (neutral) | ||
//Not necessary but can be used to stabilize drone | ||
action = "Hovering"; | ||
at_cmd = "AT*PCMD=" + (seq++) + ",1,0,0,0,0"; | ||
break; | ||
case 'S': //Takeoff (raise brow) | ||
action = "Takeoff"; | ||
at_cmd = "AT*REF=" + (seq++) + ",290718208"; | ||
break; | ||
case 'A': //Land (clench) | ||
action = "Landing"; | ||
at_cmd = "AT*REF=" + (seq++) + ",290717696"; | ||
break; | ||
case 'Z': //reset | ||
action = "Reset"; | ||
at_cmd = "AT*REF=1,290717952"; | ||
break; | ||
default: | ||
break; | ||
} | ||
System.out.println("Speed: " + speed); | ||
System.out.println("Action: " + action); | ||
send_at_cmd(at_cmd); | ||
} | ||
|
||
public void send_at_cmd(String at_cmd) throws Exception { | ||
System.out.println("AT command: " + at_cmd); | ||
byte[] buffer = (at_cmd + "\r").getBytes(); | ||
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, inet_addr, 5556); | ||
socket.send(packet); | ||
//socket.receive(packet); //AR.Drone does not send back ack message (like "OK") | ||
//System.out.println(new String(packet.getData(),0,packet.getLength())); | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.launching.macosx.MacOSXType/Java SE 8 [1.8.0_25]"/> | ||
<classpathentry kind="lib" path="/Users/x/Downloads/org.json-20120521.jar"/> | ||
<classpathentry kind="lib" path="/Applications/EmotivXavierpremiumlibraries/docs/Examples/Java/lib/jna.jar"/> | ||
<classpathentry combineaccessrules="false" kind="src" path="/Emotiv-JSON-API"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/bin/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>MindControlledUAV</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
</natures> | ||
</projectDescription> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
eclipse.preferences.version=1 | ||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled | ||
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate | ||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 | ||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve | ||
org.eclipse.jdt.core.compiler.compliance=1.8 | ||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate | ||
org.eclipse.jdt.core.compiler.debug.localVariable=generate | ||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate | ||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error | ||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error | ||
org.eclipse.jdt.core.compiler.source=1.8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Frustration (EmoState INSTANCE, Pointer eState) -> { INSTANCE.ES_AffectivGetFrustrationScore(eState); } | ||
Meditation (EmoState INSTANCE, Pointer eState) -> { INSTANCE.ES_AffectivGetMeditationScore(eState); } | ||
EngagementBoredom (EmoState INSTANCE, Pointer eState) -> { INSTANCE.ES_AffectivGetEngagementBoredomScore(eState); } | ||
ExcitementShortTerm (EmoState INSTANCE, Pointer eState) -> { INSTANCE.ES_AffectivGetExcitementShortTermScore(eState); } | ||
ExcitementLongTerm (EmoState INSTANCE, Pointer eState) -> { INSTANCE.ES_AffectivGetExcitementLongTermScore(eState); } |
Oops, something went wrong.