Commit e3dd9624 authored by kazushi.kawamura's avatar kazushi.kawamura

upload source files

parent 8052969a
import com.shigeodayo.ardrone.processing.*;
ARDroneForP5 ardrone;
void setup() {
size(320, 240);
ardrone=new ARDroneForP5("192.168.1.1");
// connect to the AR.Drone
ardrone.connect();
// for getting sensor information
ardrone.connectNav();
// for getting video informationp
ardrone.connectVideo();
// start to control AR.Drone and get sensor and video data of it
ardrone.start();
}
void draw() {
background(204);
// getting image from AR.Drone
// true: resizeing image automatically
// false: not resizing
PImage img = ardrone.getVideoImage(false);
if (img == null)
return;
image(img, 0, 0);
// print out AR.Drone information
ardrone.printARDroneInfo();
// getting sensor information of AR.Drone
float pitch = ardrone.getPitch();
float roll = ardrone.getRoll();
float yaw = ardrone.getYaw();
float altitude = ardrone.getAltitude();
float[] velocity = ardrone.getVelocity();
int battery = ardrone.getBatteryPercentage();
String attitude = "pitch:" + pitch + "\nroll:" + roll + "\nyaw:" + yaw + "\naltitude:" + altitude;
text(attitude, 20, 85);
String vel = "vx:" + velocity[0] + "\nvy:" + velocity[1];
text(vel, 20, 140);
String bat = "battery:" + battery + " %";
text(bat, 20, 170);
}
//PCのキーに応じてAR.Droneを操作できる.
// controlling AR.Drone through key input
void keyPressed() {
if (key == CODED) {
if (keyCode == UP) {
ardrone.forward(); // go forward
}
else if (keyCode == DOWN) {
ardrone.backward(); // go backward
}
else if (keyCode == LEFT) {
ardrone.goLeft(); // go left
}
else if (keyCode == RIGHT) {
ardrone.goRight(); // go right
}
else if (keyCode == SHIFT) {
ardrone.takeOff(); // take off, AR.Drone cannot move while landing
}
else if (keyCode == CONTROL) {
ardrone.landing();
// landing
}
}
else {
if (key == 's') {
ardrone.stop(); // hovering
}
else if (key == 'r') {
ardrone.spinRight(); // spin right
}
else if (key == 'l') {
ardrone.spinLeft(); // spin left
}
else if (key == 'u') {
ardrone.up(); // go up
}
else if (key == 'd') {
ardrone.down(); // go down
}
else if (key == '1') {
ardrone.setHorizontalCamera(); // set front camera
}
else if (key == '2') {
ardrone.setHorizontalCameraWithVertical(); // set front camera with second camera (upper left)
}
else if (key == '3') {
ardrone.setVerticalCamera(); // set second camera
}
else if (key == '4') {
ardrone.setVerticalCameraWithHorizontal(); //set second camera with front camera (upper left)
}
else if (key == '5') {
ardrone.toggleCamera(); // set next camera setting
}
}
}
This diff is collapsed.
/**
ARDroneForP5
https://github.com/shigeodayo/ARDroneForP5
Copyright (C) 2013, Shigeo YOSHIDA.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.shigeodayo.ardrone;
public interface ARDroneInterface {
// connection
public boolean connect();
public boolean connectVideo();
public boolean connectNav();
public void disconnect();
public void start();
// camera
public void setHorizontalCamera();// setFrontCameraStreaming()
public void setVerticalCamera();// setBellyCameraStreaming()
public void setHorizontalCameraWithVertical();// setFrontCameraWithSmallBellyStreaming()
public void setVerticalCameraWithHorizontal();// setBellyCameraWithSmallFrontStreaming()
public void toggleCamera();
// control command
public void landing();
public void takeOff();
public void reset();
public void forward();
public void forward(int speed);
public void backward();
public void backward(int speed);
public void spinRight();
public void spinRight(int speed);
public void spinLeft();
public void spinLeft(int speed);
public void up();
public void up(int speed);
public void down();
public void down(int speed);
public void goRight();
public void goRight(int speed);
public void goLeft();
public void goLeft(int speed);
public void stop();
public void move3D(int speedX, int speedY, int speedZ, int speedSpin);
// speed
public int getSpeed();
public void setSpeed(int speed);
// set max altitude
public void setMaxAltitude(int altitude);
// set min altitude
public void setMinAltitude(int altitude);
}
\ No newline at end of file
/**
ARDroneForP5
https://github.com/shigeodayo/ARDroneForP5
Copyright (C) 2013, Shigeo YOSHIDA.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.shigeodayo.ardrone.command;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import com.shigeodayo.ardrone.manager.AbstractManager;
import com.shigeodayo.ardrone.utils.ARDroneConstants;
public abstract class CommandManager extends AbstractManager {
protected static final String CR = "\r";
protected static final String SEQ = "$SEQ$";
private static int seq = 1;
private FloatBuffer fb = null;
private IntBuffer ib = null;
private boolean landing = true;
private boolean continuance = false;
private String command = null;
/** speed */
private float speed = 0.05f; // 0.01f - 1.0f
protected String VIDEO_CODEC;
public CommandManager(InetAddress inetaddr) {
this.inetaddr = inetaddr;
ByteBuffer bb = ByteBuffer.allocate(4);
fb = bb.asFloatBuffer();
ib = bb.asIntBuffer();
}
public void setHorizontalCamera() {
command = "AT*CONFIG=" + SEQ + ",\"video:video_channel\",\"0\"";
continuance = false;
}
public void setVerticalCamera() {
command = "AT*CONFIG=" + SEQ + ",\"video:video_channel\",\"1\"";
continuance = false;
}
public void setHorizontalCameraWithVertical() {
command = "AT*CONFIG=" + SEQ + ",\"video:video_channel\",\"2\"";
continuance = false;
}
public void setVerticalCameraWithHorizontal() {
command = "AT*CONFIG=" + SEQ + ",\"video:video_channel\",\"3\"";
continuance = false;
}
public void toggleCamera() {
command = "AT*CONFIG=" + SEQ + ",\"video:video_channel\",\"4\"";
continuance = false;
}
public void landing() {
command = "AT*REF=" + SEQ + ",290717696";
continuance = false;
landing = true;
//System.out.println("landing");
}
public void takeOff() {
sendCommand("AT*FTRIM=" + SEQ);
command = "AT*REF=" + SEQ + ",290718208";
continuance = false;
landing = false;
//System.out.println("take off");
}
public void reset() {
command = "AT*REF=" + SEQ + ",290717952";
continuance = true;
landing = true;
}
public void forward() {
command = "AT*PCMD=" + SEQ + ",1,0," + intOfFloat(-speed) + ",0,0"
+ "\r" + "AT*REF=" + SEQ + ",290718208";
continuance = true;
}
public void forward(int speed) {
setSpeed(speed);
forward();
}
public void backward() {
command = "AT*PCMD=" + SEQ + ",1,0," + intOfFloat(speed) + ",0,0"
+ "\r" + "AT*REF=" + SEQ + ",290718208";
continuance = true;
}
public void backward(int speed) {
setSpeed(speed);
backward();
}
public void spinRight() {
command = "AT*PCMD=" + SEQ + ",1,0,0,0," + intOfFloat(speed) + "\r"
+ "AT*REF=" + SEQ + ",290718208";
continuance = true;
}
public void spinRight(int speed) {
setSpeed(speed);
spinRight();
}
public void spinLeft() {
command = "AT*PCMD=" + SEQ + ",1,0,0,0," + intOfFloat(-speed) + "\r"
+ "AT*REF=" + SEQ + ",290718208";
continuance = true;
}
public void spinLeft(int speed) {
setSpeed(speed);
spinLeft();
}
public void up() {
command = "AT*PCMD=" + SEQ + ",1," + intOfFloat(0) + ","
+ intOfFloat(0) + "," + intOfFloat(speed) + "," + intOfFloat(0)
+ "\r" + "AT*REF=" + SEQ + ",290718208";
continuance = true;
}
public void up(int speed) {
setSpeed(speed);
up();
}
public void down() {
command = "AT*PCMD=" + SEQ + ",1," + intOfFloat(0) + ","
+ intOfFloat(0) + "," + intOfFloat(-speed) + ","
+ intOfFloat(0) + "\r" + "AT*REF=" + SEQ + ",290718208";
continuance = true;
}
public void down(int speed) {
setSpeed(speed);
down();
}
public void goRight() {
command = "AT*PCMD=" + SEQ + ",1," + intOfFloat(speed) + ",0,0,0"
+ "\r" + "AT*REF=" + SEQ + ",290718208";
continuance = true;
}
public void goRight(int speed) {
setSpeed(speed);
goRight();
}
public void goLeft() {
command = "AT*PCMD=" + SEQ + ",1," + intOfFloat(-speed) + ",0,0,0"
+ "\r" + "AT*REF=" + SEQ + ",290718208";
continuance = true;
}
public void goLeft(int speed) {
setSpeed(speed);
goLeft();
}
public void stop() {
command = "AT*PCMD=" + SEQ + ",1,0,0,0,0";
continuance = true;
}
public void setSpeed(int speed) {
if (speed > 100)
speed = 100;
else if (speed < 1)
speed = 1;
this.speed = (float) (speed / 100.0);
}
public void enableVideoData() {
command = "AT*CONFIG=" + SEQ + ",\"general:video_enable\",\"TRUE\""
+ CR + "AT*FTRIM=" + SEQ;
continuance = false;
}
public void enableDemoData() {
command = "AT*CONFIG=" + SEQ + ",\"general:navdata_demo\",\"TRUE\""
+ CR + "AT*FTRIM=" + SEQ;
continuance = false;
}
public void disableBootStrap() {
command = "AT*CONFIG_IDS=" + SEQ + ",\"" + ARDroneConstants.SESSION_ID
+ "\",\"" + ARDroneConstants.PROFILE_ID + "\",\""
+ ARDroneConstants.APPLICATION_ID + "\"" + CR;
}
public void sendControlAck() {
command = "AT*CTRL=" + SEQ + ",0";
continuance = false;
}
public int getSpeed() {
return (int) (speed * 100);
}
public void disableAutomaticVideoBitrate() {
command = "AT*CONFIG=" + SEQ + ",\"video:bitrate_ctrl_mode\",\"0\"";
continuance = false;
}
public void setMaxAltitude(int altitude) {
command = "AT*CONFIG=" + SEQ + ",\"control:altitude_max\",\""
+ altitude + "\"";
continuance = false;
}
public void setMinAltitude(int altitude) {
command = "AT*CONFIG=" + SEQ + ",\"control:altitude_min\",\""
+ altitude + "\"";
continuance = false;
}
/*
* Thank you Tarqunio !!
*/
public void move3D(int speedX, int speedY, int speedZ, int speedSpin) {
if (speedX > 100)
speedX = 100;
else if (speedX < -100)
speedX = -100;
if (speedY > 100)
speedY = 100;
else if (speedY < -100)
speedY = -100;
if (speedZ > 100)
speedZ = 100;
else if (speedZ < -100)
speedZ = -100;
command = "AT*PCMD=" + SEQ + ",1," + intOfFloat(-speedY / 100.0f) + ","
+ intOfFloat(-speedX / 100.0f) + ","
+ intOfFloat(-speedZ / 100.0f) + ","
+ intOfFloat(-speedSpin / 100.0f) + "\r" + "AT*REF=" + SEQ
+ ",290718208";
continuance = true;
}
@Override
public void run() {
initializeDrone();
while (true) {
if (this.command != null) {
// sendCommand();
sendCommand(this.command);
if (!continuance) {
command = null;
}
} else {
if (landing) {
sendCommand("AT*PCMD=" + SEQ + ",1,0,0,0,0" + CR
+ "AT*REF=" + SEQ + ",290717696");
} else {
sendCommand("AT*PCMD=" + SEQ + ",1,0,0,0,0" + CR
+ "AT*REF=" + SEQ + ",290718208");
}
}
try {
Thread.sleep(20); // < 50ms
} catch (InterruptedException e) {
e.printStackTrace();
}
if (seq % 5 == 0) { // < 2000ms
sendCommand("AT*COMWDG=" + SEQ);
}
}
}
protected abstract void initializeDrone();
/*
* private void initializeDrone() { sendCommand("AT*CONFIG=" + SEQ +
* ",\"general:navdata_demo\",\"TRUE\"" + CR + "AT*FTRIM=" + SEQ); // 1
* sendCommand("AT*PMODE=" + SEQ + ",2" + CR + "AT*MISC=" + SEQ +
* ",2,20,2000,3000" + CR + "AT*FTRIM=" + SEQ + CR + "AT*REF=" + SEQ +
* ",290717696"); // 2-5 sendCommand("AT*PCMD=" + SEQ + ",1,0,0,0,0" + CR +
* "AT*REF=" + SEQ + ",290717696" + CR + "AT*COMWDG=" + SEQ); // 6-8
* sendCommand("AT*PCMD=" + SEQ + ",1,0,0,0,0" + CR + "AT*REF=" + SEQ +
* ",290717696" + CR + "AT*COMWDG=" + SEQ); // 6-8 sendCommand("AT*FTRIM=" +
* SEQ); //System.out.println("Initialize completed!"); }
*/
/*
* Thank you Dirk !!
*/
protected synchronized void sendCommand(String command) {
int seqIndex = -1;
while ((seqIndex = command.indexOf(SEQ)) != -1)
command = command.substring(0, seqIndex) + (seq++)
+ command.substring(seqIndex + SEQ.length());
byte[] buffer = (command + CR).getBytes();
//System.out.println(command);
DatagramPacket packet = new DatagramPacket(buffer, buffer.length,
inetaddr, ARDroneConstants.PORT);
try {
socket.send(packet);
//Thread.sleep(20); // < 50ms
} catch (IOException e) {
e.printStackTrace();
} /*catch (InterruptedException e) {
e.printStackTrace();
}*/
}
private int intOfFloat(float f) {
fb.put(0, f);
return ib.get(0);
}
}
\ No newline at end of file
/**
ARDroneForP5
https://github.com/shigeodayo/ARDroneForP5
Copyright (C) 2013, Shigeo YOSHIDA.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.shigeodayo.ardrone.command;
import java.net.InetAddress;
import com.shigeodayo.ardrone.utils.ARDroneConstants;
/**
* CommandManager for AR.Drone 1.0
*
* @author shigeo
*
*/
public class CommandManager1 extends CommandManager {
public CommandManager1(InetAddress inetaddr) {
this(inetaddr, false);
}
public CommandManager1(InetAddress inetaddr, boolean useHighRezVideoStreaming) {
super(inetaddr);
if (useHighRezVideoStreaming) // high resolution version is not implemented yet
VIDEO_CODEC = ARDroneConstants.VIDEO_CODEC_H264;
else
VIDEO_CODEC = ARDroneConstants.VIDEO_CODEC_UVLC;
//System.out.println("command manager 1");
}
@Override
protected void initializeDrone() {
try {
sendCommand("AT*CONFIG=" + SEQ +
",\"general:navdata_demo\",\"TRUE\"" + CR + "AT*FTRIM=" + SEQ);
Thread.sleep(20);
sendCommand("AT*PMODE=" + SEQ + ",2" + CR);
Thread.sleep(20);
sendCommand("AT*MISC=" + SEQ + ",2,20,2000,3000" + CR);
Thread.sleep(20);
// enable video
sendCommand("AT*CONFIG=" + SEQ
+ ",\"general:video_enable\",\"TRUE\"" + CR);
Thread.sleep(20);
// fix bit rate
sendCommand("AT*CONFIG=" + SEQ
+ ",\"video:bitrate_ctrl_mode\",\"0\"" + CR);
Thread.sleep(20);
// video codec
sendCommand("AT*CONFIG=" + SEQ + ",\"video:video_codec\",\""
+ VIDEO_CODEC + "\"" + CR);
Thread.sleep(20);
// set front camera
sendCommand("AT*CONFIG=" + SEQ + ",\"video:video_channel\",\"0\""
+ CR);
Thread.sleep(20);
// trim
sendCommand("AT*FTRIM=" + SEQ + CR);
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Initialize AR.Drone 1.0 !!");
}
}
/**
ARDroneForP5
https://github.com/shigeodayo/ARDroneForP5
Copyright (C) 2013, Shigeo YOSHIDA.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.shigeodayo.ardrone.command;
import java.net.InetAddress;
import com.shigeodayo.ardrone.utils.ARDroneConstants;
/**
* CommandManager for AR.Drone 2.0
*
* @author shigeo
*
*/
public class CommandManager2 extends CommandManager {
private final String SESSION_ID = ARDroneConstants.SESSION_ID;
private final String PROFILE_ID = ARDroneConstants.PROFILE_ID;
private final String APPLICATION_ID = ARDroneConstants.APPLICATION_ID;
public CommandManager2(InetAddress inetaddr) {
this(inetaddr, false);
}
public CommandManager2(InetAddress inetaddr,
boolean useHighRezVideoStreaming) {
super(inetaddr);
if (useHighRezVideoStreaming) // high resolution version is not implemented yet
VIDEO_CODEC = ARDroneConstants.VIDEO_CODEC_720P;
else
VIDEO_CODEC = ARDroneConstants.VIDEO_CODEC_360P;
// System.out.println("command manager 2");
}
@Override
protected void initializeDrone() {
try {
sendCommand("AT*CONFIG=" + SEQ
+ ",\"general:navdata_demo\",\"TRUE\"" + CR + "AT*FTRIM="
+ SEQ); // 1
Thread.sleep(20);
sendCommand("AT*PMODE=" + SEQ + ",2" + CR);
Thread.sleep(20);
sendCommand("AT*MISC=" + SEQ + ",2,20,2000,3000" + CR);
Thread.sleep(20);
sendCommand("AT*CONFIG_IDS=" + SEQ + ",\"" + SESSION_ID + "\",\""
+ PROFILE_ID + "\",\"" + APPLICATION_ID + "\"" + CR);
sendCommand("AT*CONFIG=" + SEQ + ",\"custom:session_id\",\""
+ SESSION_ID + "\"" + CR);
Thread.sleep(20);
sendCommand("AT*CONFIG_IDS=" + SEQ + ",\"" + SESSION_ID + "\",\""
+ PROFILE_ID + "\",\"" + APPLICATION_ID + "\"" + CR);
sendCommand("AT*CONFIG=" + SEQ + ",\"custom:profile_id\",\""
+ PROFILE_ID + "\"" + CR);
Thread.sleep(20);
sendCommand("AT*CONFIG_IDS=" + SEQ + ",\"" + SESSION_ID + "\",\""
+ PROFILE_ID + "\",\"" + APPLICATION_ID + "\"" + CR);
sendCommand("AT*CONFIG=" + SEQ + ",\"custom:application_id\",\""
+ APPLICATION_ID + "\"" + CR);
Thread.sleep(20);
// enable video
sendCommand("AT*CONFIG_IDS=" + SEQ + ",\"" + SESSION_ID + "\",\""
+ PROFILE_ID + "\",\"" + APPLICATION_ID + "\"" + CR);
sendCommand("AT*CONFIG=" + SEQ
+ ",\"general:video_enable\",\"TRUE\"" + CR);
Thread.sleep(20);
// fix bit rate
sendCommand("AT*CONFIG_IDS=" + SEQ + ",\"" + SESSION_ID + "\",\""
+ PROFILE_ID + "\",\"" + APPLICATION_ID + "\"" + CR);
sendCommand("AT*CONFIG=" + SEQ
+ ",\"video:bitrate_ctrl_mode\",\"0\"" + CR);
Thread.sleep(20);
// video codec
sendCommand("AT*CONFIG_IDS=" + SEQ + ",\"" + SESSION_ID + "\",\""
+ PROFILE_ID + "\",\"" + APPLICATION_ID + "\"" + CR);
sendCommand("AT*CONFIG=" + SEQ + ",\"video:video_codec\"," + "\""
+ VIDEO_CODEC + "\"" + CR);
Thread.sleep(20);
// set front camera
sendCommand("AT*CONFIG_IDS=" + SEQ + ",\"" + SESSION_ID + "\",\""
+ PROFILE_ID + "\",\"" + APPLICATION_ID + "\"" + CR);
sendCommand("AT*CONFIG=" + SEQ + ",\"video:video_channel\",\"0\""
+ CR);
Thread.sleep(20);
// trim
sendCommand("AT*FTRIM=" + SEQ + CR);
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Initialize AR.Drone 2.0 !!");
}
@Override
public void setHorizontalCamera() {
sendCommand("AT*CONFIG_IDS=" + SEQ + ",\"" + SESSION_ID + "\",\""
+ PROFILE_ID + "\",\"" + APPLICATION_ID + "\"" + CR);
super.setHorizontalCamera();
}
@Override
public void setVerticalCamera() {
sendCommand("AT*CONFIG_IDS=" + SEQ + ",\"" + SESSION_ID + "\",\""
+ PROFILE_ID + "\",\"" + APPLICATION_ID + "\"" + CR);
super.setVerticalCamera();
}
@Override
public void setHorizontalCameraWithVertical() {
}
@Override
public void setVerticalCameraWithHorizontal() {
}
@Override
public void toggleCamera() {
sendCommand("AT*CONFIG_IDS=" + SEQ + ",\"" + SESSION_ID + "\",\""
+ PROFILE_ID + "\",\"" + APPLICATION_ID + "\"" + CR);
super.toggleCamera();
}
}
/**
ARDroneForP5
https://github.com/shigeodayo/ARDroneForP5
Copyright (C) 2013, Shigeo YOSHIDA.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.shigeodayo.ardrone.manager;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public abstract class AbstractManager implements Runnable {
protected InetAddress inetaddr = null;
protected DatagramSocket socket = null;
public boolean connect(int port) {
try {
socket = new DatagramSocket(port);
socket.setSoTimeout(3000);
} catch (SocketException e) {
e.printStackTrace();
return false;
}
return true;
}
public void close() {
socket.close();
}
protected void ticklePort(int port) {
byte[] buf = { 0x01, 0x00, 0x00, 0x00 };
DatagramPacket packet = new DatagramPacket(buf, buf.length, inetaddr,
port);
try {
socket.send(packet);
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*
* Copyright 2010 Cliff L. Biffle. All Rights Reserved.
* Use of this source code is governed by a BSD-style license that can be found
* in the LICENSE file.
*/
package com.shigeodayo.ardrone.navdata;
public interface AttitudeListener {
void attitudeUpdated(float pitch, float roll, float yaw, int altitude);
}
/*
* Copyright 2010 Cliff L. Biffle. All Rights Reserved.
* Use of this source code is governed by a BSD-style license that can be found
* in the LICENSE file.
*/
package com.shigeodayo.ardrone.navdata;
public interface BatteryListener {
void batteryLevelChanged(int percentage);
}
/**
ARDroneForP5
https://github.com/shigeodayo/ARDroneForP5
Copyright (C) 2013, Shigeo YOSHIDA.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.shigeodayo.ardrone.navdata;
public class DetectType {
public DetectType() {
}
}
\ No newline at end of file
/**
ARDroneForP5
https://github.com/shigeodayo/ARDroneForP5
Copyright (C) 2013, Shigeo YOSHIDA.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.shigeodayo.ardrone.navdata;
import java.awt.Dimension;
import java.awt.Point;
public interface DetectionDataListener {
void detectionDataChanged(DetectType type, Point pos, Dimension size,
int distance);
}
/*
* Copyright 2010 Cliff L. Biffle. All Rights Reserved.
* Use of this source code is governed by a BSD-style license that can be found
* in the LICENSE file.
*/
package com.shigeodayo.ardrone.navdata;
public class DroneState {
private final int bits;
public DroneState(int bits) {
this.bits = bits;
}
public String toString() {
return "DroneState(" + Integer.toHexString(bits) + ")";
}
public boolean equals(Object o) {
if (o == null || o.getClass() != getClass())
return false;
return bits == ((DroneState) o).bits;
}
public int hashCode() {
return 31 * bits;
}
}
\ No newline at end of file
/*
* Copyright 2010 Cliff L. Biffle. All Rights Reserved.
* Use of this source code is governed by a BSD-style license that can be found
* in the LICENSE file.
*/
package com.shigeodayo.ardrone.navdata;
public class NavDataException extends Exception {
private static final long serialVersionUID = 1311407045280371188L;
public NavDataException(String message) {
super(message);
}
}
/**
ARDroneForP5
https://github.com/shigeodayo/ARDroneForP5
Copyright (C) 2013, Shigeo YOSHIDA.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.shigeodayo.ardrone.navdata;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.nio.ByteBuffer;
import com.shigeodayo.ardrone.command.CommandManager;
import com.shigeodayo.ardrone.manager.AbstractManager;
import com.shigeodayo.ardrone.utils.ARDroneConstants;
public abstract class NavDataManager extends AbstractManager {
protected CommandManager manager = null;
// listeners
private AttitudeListener attitudeListener = null;
private StateListener stateListener = null;
private VelocityListener velocityListener = null;
private BatteryListener batteryListener = null;
public NavDataManager(InetAddress inetaddr, CommandManager manager) {
this.inetaddr = inetaddr;
this.manager = manager;
}
public void setAttitudeListener(AttitudeListener attitudeListener) {
this.attitudeListener = attitudeListener;
}
public void setBatteryListener(BatteryListener batteryListener) {
this.batteryListener = batteryListener;
}
public void setStateListener(StateListener stateListener) {
this.stateListener = stateListener;
}
public void setVelocityListener(VelocityListener velocityListener) {
this.velocityListener = velocityListener;
}
@Override
public void run() {
initializeDrone();
NavDataParser parser = new NavDataParser();
parser.setAttitudeListener(attitudeListener);
parser.setBatteryListener(batteryListener);
parser.setStateListener(stateListener);
parser.setVelocityListener(velocityListener);
while (true) {
try {
ticklePort(ARDroneConstants.NAV_PORT);
DatagramPacket packet = new DatagramPacket(new byte[1024],
1024, inetaddr, ARDroneConstants.NAV_PORT);
socket.receive(packet);
ByteBuffer buffer = ByteBuffer.wrap(packet.getData(), 0,
packet.getLength());
parser.parseNavData(buffer);
} catch (IOException e) {
e.printStackTrace();
} catch (NavDataException e) {
e.printStackTrace();
}
}
}
protected abstract void initializeDrone();
}
\ No newline at end of file
/**
ARDroneForP5
https://github.com/shigeodayo/ARDroneForP5
Copyright (C) 2013, Shigeo YOSHIDA.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.shigeodayo.ardrone.navdata;
import java.net.InetAddress;
import com.shigeodayo.ardrone.command.CommandManager;
import com.shigeodayo.ardrone.utils.ARDroneConstants;
public class NavDataManager1 extends NavDataManager {
public NavDataManager1(InetAddress inetaddr, CommandManager manager) {
super(inetaddr, manager);
//System.out.println("navdata manager 1");
}
@Override
protected void initializeDrone() {
ticklePort(ARDroneConstants.NAV_PORT);
manager.enableDemoData();
ticklePort(ARDroneConstants.NAV_PORT);
manager.sendControlAck();
}
}
/**
ARDroneForP5
https://github.com/shigeodayo/ARDroneForP5
Copyright (C) 2013, Shigeo YOSHIDA.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.shigeodayo.ardrone.navdata;
import java.net.InetAddress;
import com.shigeodayo.ardrone.command.CommandManager;
import com.shigeodayo.ardrone.utils.ARDroneConstants;
public class NavDataManager2 extends NavDataManager {
public NavDataManager2(InetAddress inetaddr, CommandManager manager) {
super(inetaddr, manager);
//System.out.println("navdata manager 2");
}
@Override
protected void initializeDrone() {
ticklePort(ARDroneConstants.NAV_PORT);
//manager.disableBootStrap();
manager.enableDemoData();
ticklePort(ARDroneConstants.NAV_PORT);
manager.sendControlAck();
}
}
/*
* Copyright 2010 Cliff L. Biffle. All Rights Reserved.
* Use of this source code is governed by a BSD-style license that can be found
* in the LICENSE file.
*/
package com.shigeodayo.ardrone.navdata;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
public class NavDataParser {
private AttitudeListener attitudeListener;
private StateListener stateListener;
private VelocityListener velocityListener;
private BatteryListener batteryListener;
long lastSequenceNumber = 1;
// set listeners
public void setBatteryListener(BatteryListener batteryListener) {
this.batteryListener = batteryListener;
}
public void setAttitudeListener(AttitudeListener attitudeListener) {
this.attitudeListener = attitudeListener;
}
public void setStateListener(StateListener stateListener) {
this.stateListener = stateListener;
}
public void setVelocityListener(VelocityListener velocityListener) {
this.velocityListener = velocityListener;
}
public void parseNavData(ByteBuffer buffer) throws NavDataException {
buffer.order(ByteOrder.LITTLE_ENDIAN);
int magic = buffer.getInt();
// System.out.printf("%02x\n", magic);
requireEquals("Magic must be correct", 0x55667788, magic);
int state = buffer.getInt();
long sequence = buffer.getInt() & 0xFFFFFFFFL;
@SuppressWarnings("unused")
int vision = buffer.getInt();
if (sequence <= lastSequenceNumber && sequence != 1) {
return;
}
lastSequenceNumber = sequence;
if (stateListener != null) {
stateListener.stateChanged(new DroneState(state));
}
while (buffer.position() < buffer.limit()) {
int tag = buffer.getShort() & 0xFFFF;
int payloadSize = (buffer.getShort() & 0xFFFF) - 4;
ByteBuffer optionData = buffer.slice().order(
ByteOrder.LITTLE_ENDIAN);
optionData.limit(payloadSize);
buffer.position(buffer.position() + payloadSize);
dispatch(tag, optionData);
}
}
private void dispatch(int tag, ByteBuffer optionData) {
switch (tag) {
case 0:
processNavDataDemo(optionData);
break;
}
}
private void processNavDataDemo(ByteBuffer optionData) {
@SuppressWarnings("unused")
int controlState = optionData.getInt();
int batteryPercentage = optionData.getInt();
float theta = optionData.getFloat() / 1000;
float phi = optionData.getFloat() / 1000;
float psi = optionData.getFloat() / 1000;
int altitude = optionData.getInt();
float vx = optionData.getFloat();
float vy = optionData.getFloat();
float vz = optionData.getFloat();
if (batteryListener != null) {
batteryListener.batteryLevelChanged(batteryPercentage);
}
if (attitudeListener != null) {
attitudeListener.attitudeUpdated(theta, phi, psi, altitude);
// System.out.println("update in parser");
}
if (velocityListener != null) {
velocityListener.velocityChanged(vx, vy, vz);
}
}
private void requireEquals(String message, int expected, int actual)
throws NavDataException {
if (expected != actual) {
throw new NavDataException(message + " : expected " + expected
+ ", was " + actual);
}
}
}
\ No newline at end of file
/*
* Copyright 2010 Cliff L. Biffle. All Rights Reserved.
* Use of this source code is governed by a BSD-style license that can be found
* in the LICENSE file.
*/
package com.shigeodayo.ardrone.navdata;
public interface StateListener {
void stateChanged(DroneState state);
}
/*
* Copyright 2010 Cliff L. Biffle. All Rights Reserved.
* Use of this source code is governed by a BSD-style license that can be found
* in the LICENSE file.
*/
package com.shigeodayo.ardrone.navdata;
public interface VelocityListener {
void velocityChanged(float vx, float vy, float vz);
}
/**
ARDroneForP5
https://github.com/shigeodayo/ARDroneForP5
Copyright (C) 2013, Shigeo YOSHIDA.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.shigeodayo.ardrone.processing;
import java.awt.Graphics2D;
//import java.awt.Point;
import java.awt.image.BufferedImage;
//import java.awt.image.DataBufferInt;
//import java.awt.image.Raster;
//import java.awt.image.WritableRaster;
import processing.core.PConstants;
import processing.core.PImage;
import com.shigeodayo.ardrone.ARDrone;
import com.shigeodayo.ardrone.navdata.AttitudeListener;
import com.shigeodayo.ardrone.navdata.BatteryListener;
import com.shigeodayo.ardrone.navdata.DroneState;
import com.shigeodayo.ardrone.navdata.StateListener;
import com.shigeodayo.ardrone.navdata.VelocityListener;
import com.shigeodayo.ardrone.utils.ARDroneVersion;
import com.shigeodayo.ardrone.video.ImageListener;
/**
* AR.Drone library for Processing
*
* @author shigeo
*
*/
public class ARDroneForP5 extends ARDrone implements ImageListener,
AttitudeListener, BatteryListener, StateListener, VelocityListener {
private BufferedImage videoImage = null;
private float pitch = 0.0f;
private float roll = 0.0f;
private float yaw = 0.0f;
private float altitude = 0.0f;
private int battery = 0;
private DroneState state = null;
private float vx = 0.0f;
private float vy = 0.0f;
private float[] velocity = new float[2];
private PImage pimg = null;
//private WritableRaster wr = null;
/** constructor */
public ARDroneForP5() {
super();
}
/**
* constructor
*
* @param ipaddr
*/
public ARDroneForP5(String ipaddr) {
super(ipaddr);
}
/**
* constructor
*
* @param ardroneType
*/
public ARDroneForP5(ARDroneVersion ardroneType) {
super(ardroneType);
}
/**
* constructor
*
* @param ipaddr
* @param ardroneType
*/
public ARDroneForP5(String ipaddr, ARDroneVersion ardroneType) {
super(ipaddr, ardroneType);
}
@Override
public boolean connectVideo() {
addImageUpdateListener(this);
// pimg = new PImage(320, 240);
return super.connectVideo();
}
@Override
public boolean connectNav() {
addAttitudeUpdateListener(this);
addBatteryUpdateListener(this);
addStateUpdateListener(this);
addVelocityUpdateListener(this);
return super.connectNav();
}
@Override
public void imageUpdated(BufferedImage image) {
this.videoImage = image;
}
@Override
public void velocityChanged(float vx, float vy, float vz) {
this.vx = vx;
this.vy = vy;
velocity[0] = vx;
velocity[1] = vy;
}
@Override
public void stateChanged(DroneState state) {
this.state = state;
}
@Override
public void batteryLevelChanged(int percentage) {
this.battery = percentage;
}
@Override
public void attitudeUpdated(float pitch, float roll, float yaw, int altitude) {
this.pitch = pitch;
this.yaw = yaw;
this.roll = roll;
this.altitude = altitude;
}
public void printARDroneInfo() {
System.out
.println("--------------------------------------------------------------------");
System.out.println("Attitude: pitch=" + pitch + " roll=" + roll
+ " yaw=" + yaw + " altitude=" + altitude);
System.out.println("Battery: " + battery + "%");
System.out.println("Velocity: vx=" + vx + " vy=" + vy);
System.out
.println("--------------------------------------------------------------------");
}
public PImage getVideoImage(boolean autoResize) {
if (videoImage == null)
return null;
if (autoResize) {
if (videoImage.getWidth() == 176) {
return convertToPImage(resize(videoImage, 320, 240));
}
}
return convertToPImage(videoImage);
}
public float getPitch() {
return pitch;
}
public float getRoll() {
return roll;
}
public float getYaw() {
return yaw;
}
public float getAltitude() {
return altitude;
}
public float getVelocityX() {
return vx;
}
public float getVelocityY() {
return vy;
}
public float[] getVelocity() {
return velocity;
}
public int getBatteryPercentage() {
return battery;
}
private PImage convertToPImage(BufferedImage bufImg) {
if (bufImg == null)
return null;
try {
/*
* if (pimg == null) { System.out.println("new pimage"); //pimg =
* new PImage(bufImg); //pimg = new PImage(); pimg = new PImage(320,
* 240, PConstants.ARGB); DataBufferInt dbi = new
* DataBufferInt(pimg.pixels, pimg.pixels.length); wr =
* Raster.createWritableRaster(bufImg.getSampleModel(), dbi, new
* Point(0, 0)); } else { System.out.println("update pimage");
* bufImg.copyData(wr); pimg.updatePixels(); }
*
* return pimg;
*/
if (pimg == null) {
pimg = new PImage(bufImg.getWidth(), bufImg.getHeight(),
PConstants.ARGB);
//DataBufferInt dbi = new DataBufferInt(pimg.pixels,
// pimg.pixels.length);
//wr = Raster.createWritableRaster(bufImg.getSampleModel(), dbi,
//new Point(0, 0));
}
//bufImg.copyData(wr);
bufImg.getRGB(0, 0, pimg.width, pimg.height, pimg.pixels, 0,
pimg.width);
pimg.updatePixels();
return pimg;
/*
* PImage img = new PImage(bufImg.getWidth(), bufImg.getHeight(),
* PConstants.ARGB); bufImg.getRGB(0, 0, img.width, img.height,
* img.pixels, 0, img.width); img.updatePixels(); return img;
*/
} catch (Exception e) {
// System.err.println("Can't create image from buffer");
// e.printStackTrace();
}
return null;
}
/**
* resize bufferedimage
*
* @param image
* @param width
* @param height
* @return
*/
private BufferedImage resize(BufferedImage image, int width, int height) {
BufferedImage resizedImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(image, 0, 0, width, height, null);
g.dispose();
return resizedImage;
}
}
\ No newline at end of file
/**
ARDroneForP5
https://github.com/shigeodayo/ARDroneForP5
Copyright (C) 2013, Shigeo YOSHIDA.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.shigeodayo.ardrone.utils;
public class ARDroneConstants {
/** default IP address */
public static final String IP_ADDRESS = "192.168.1.1";
/** default PORT */
public static final int PORT = 5556;
public static final int VIDEO_PORT = 5555;
public static final int NAV_PORT = 5554;
public static final int FTP_PORT = 5551;
/** default ID, for AR.Drone 2.0 */
public static final String SESSION_ID = "d2e081a3";
public static final String PROFILE_ID = "be27e2e4";
public static final String APPLICATION_ID = "d87f7e0c";
/** video codec */
public static final String VIDEO_CODEC_UVLC = "0x20"; // 320x240, 15fps for AR.Drone 1.0
public static final String VIDEO_CODEC_H264 = "0x40"; // 640x360, 20fps for AR.Drone 1.0
public static final String VIDEO_CODEC_360P = "0x81"; // 360p, for AR.Drone 2.0
public static final String VIDEO_CODEC_720P = "0x83"; // 720p, for AR.Drone 2.0
}
/**
ARDroneForP5
https://github.com/shigeodayo/ARDroneForP5
Copyright (C) 2013, Shigeo YOSHIDA.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.shigeodayo.ardrone.utils;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import com.shigeodayo.ardrone.ARDrone;
public class ARDroneInfo {
private static final String VERSION_FILE_NAME = "version.txt";
private int major = -1;
private int minor = -1;
private int revision = -1;
private int count = 0;
public ARDroneInfo() {
connectToDroneThroughFtp();
}
public ARDroneVersion getDroneVersion() {
//System.out.println("major:" + major);
switch (major) {
case 1:
return ARDroneVersion.ARDRONE1;
case 2:
return ARDroneVersion.ARDRONE2;
default:
return null;
}
}
private boolean connectToDroneThroughFtp() {
FTPClient client = new FTPClient();
BufferedOutputStream bos = null;
try {
client.connect(ARDroneConstants.IP_ADDRESS, ARDroneConstants.FTP_PORT);
if (!client.login("anonymous", "")) {
ARDrone.error("Login failed", this);
return false;
}
client.setFileType(FTP.BINARY_FILE_TYPE);
bos = new BufferedOutputStream(new OutputStream() {
@Override
public void write(int arg0) throws IOException {
//System.out.println("aa:" + (char)arg0);
switch (count) {
case 0:
major = arg0 - '0';
break;
case 2:
minor = arg0 - '0';
break;
case 4:
revision = arg0 - '0';
break;
default:
break;
}
count++;
}
});
if (!client.retrieveFile("/" + VERSION_FILE_NAME, bos)) {
ARDrone.error("Cannot find \"" + VERSION_FILE_NAME + "\"", this);
return false;
}
bos.flush();
//System.out.print("major:" + major);
//System.out.print(" minor:" + minor);
//System.out.println(" revision:" + revision);
//System.out.println("done");
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
try {
if (bos != null) {
bos.flush();
bos.close();
}
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
return true;
}
}
/**
ARDroneForP5
https://github.com/shigeodayo/ARDroneForP5
Copyright (C) 2013, Shigeo YOSHIDA.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.shigeodayo.ardrone.utils;
public enum ARDroneVersion {
ARDRONE1, ARDRONE2
}
This diff is collapsed.
/**
ARDroneForP5
https://github.com/shigeodayo/ARDroneForP5
Copyright (C) 2013, Shigeo YOSHIDA.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.shigeodayo.ardrone.video;
public class ImageDataException extends Exception {
private static final long serialVersionUID = -3948568506319692544L;
public ImageDataException(String message) {
super(message);
}
}
/**
ARDroneForP5
https://github.com/shigeodayo/ARDroneForP5
Copyright (C) 2013, Shigeo YOSHIDA.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.shigeodayo.ardrone.video;
import java.awt.image.BufferedImage;
public interface ImageListener {
void imageUpdated(BufferedImage image);
}
package com.shigeodayo.ardrone.video;
//Copyright © 2007-2011, PARROT SA, all rights reserved.
//DISCLAIMER
//The APIs is provided by PARROT and contributors "AS IS" and any express or implied warranties, including, but not limited to, the implied warranties of merchantability
//and fitness for a particular purpose are disclaimed. In no event shall PARROT and contributors be liable for any direct, indirect, incidental, special, exemplary, or
//consequential damages (including, but not limited to, procurement of substitute goods or services; loss of use, data, or profits; or business interruption) however
//caused and on any theory of liability, whether in contract, strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this
//software, even if advised of the possibility of such damage.
//Author : Daniel Schmidt
//Publishing date : 2010-01-06
//based on work by : Wilke Jansoone
//Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions
//are met:
//- Redistributions of source code must retain the above copyright notice, this list of conditions, the disclaimer and the original author of the source code.
//- Neither the name of the PixVillage Team, nor the names of its contributors may be used to endorse or promote products derived from this software without
//specific prior written permission.
public class ImageSlice {
MacroBlock[] MacroBlocks;
ImageSlice(int macroBlockCount) {
MacroBlocks = new MacroBlock[macroBlockCount];
for (int index = 0; index < macroBlockCount; index++) {
MacroBlocks[index] = new MacroBlock();
}
}
}
\ No newline at end of file
package com.shigeodayo.ardrone.video;
//Copyright © 2007-2011, PARROT SA, all rights reserved.
//DISCLAIMER
//The APIs is provided by PARROT and contributors "AS IS" and any express or implied warranties, including, but not limited to, the implied warranties of merchantability
//and fitness for a particular purpose are disclaimed. In no event shall PARROT and contributors be liable for any direct, indirect, incidental, special, exemplary, or
//consequential damages (including, but not limited to, procurement of substitute goods or services; loss of use, data, or profits; or business interruption) however
//caused and on any theory of liability, whether in contract, strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this
//software, even if advised of the possibility of such damage.
//Author : Daniel Schmidt
//Publishing date : 2010-01-06
//based on work by : Wilke Jansoone
//Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions
//are met:
//- Redistributions of source code must retain the above copyright notice, this list of conditions, the disclaimer and the original author of the source code.
//- Neither the name of the PixVillage Team, nor the names of its contributors may be used to endorse or promote products derived from this software without
//specific prior written permission.
public class MacroBlock {
// /#//#region ants
// private int _BlockWidth = 8;
// private int _BlockSize = 64;
// //#endregion
// //#region Properties
short[][] DataBlocks;
// //#endregion
// //#region ruction
MacroBlock() {
DataBlocks = new short[6][];
for (int index = 0; index < 6; index++) {
DataBlocks[index] = new short[64];
}
}
// //#endregion
}
\ No newline at end of file
package com.shigeodayo.ardrone.video;
//Copyright © 2007-2011, PARROT SA, all rights reserved.
//DISCLAIMER
//The APIs is provided by PARROT and contributors "AS IS" and any express or implied warranties, including, but not limited to, the implied warranties of merchantability
//and fitness for a particular purpose are disclaimed. In no event shall PARROT and contributors be liable for any direct, indirect, incidental, special, exemplary, or
//consequential damages (including, but not limited to, procurement of substitute goods or services; loss of use, data, or profits; or business interruption) however
//caused and on any theory of liability, whether in contract, strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this
//software, even if advised of the possibility of such damage.
//Author : Daniel Schmidt
//Publishing date : 2010-01-06
//based on work by : Wilke Jansoone
//Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions
//are met:
//- Redistributions of source code must retain the above copyright notice, this list of conditions, the disclaimer and the original author of the source code.
//- Neither the name of the PixVillage Team, nor the names of its contributors may be used to endorse or promote products derived from this software without
//specific prior written permission.
public class PictureFormats {
// / <summary>
// / 176px x 144px
// / </summary>
public static final int Cif = 1;
// / <summary>
// / 320px x 240px
// / </summary>
public static final int Vga = 2;
}
//Copyright 2007-2011, PARROT SA, all rights reserved.
//DISCLAIMER
//The APIs is provided by PARROT and contributors "AS IS" and any express or implied warranties, including, but not limited to, the implied warranties of merchantability
//and fitness for a particular purpose are disclaimed. In no event shall PARROT and contributors be liable for any direct, indirect, incidental, special, exemplary, or
//consequential damages (including, but not limited to, procurement of substitute goods or services; loss of use, data, or profits; or business interruption) however
//caused and on any theory of liability, whether in contract, strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this
//software, even if advised of the possibility of such damage.
//Author : Daniel Schmidt
//Publishing date : 2010-01-06
//based on work by : Wilke Jansoone
//Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions
//are met:
//- Redistributions of source code must retain the above copyright notice, this list of conditions, the disclaimer and the original author of the source code.
//- Neither the name of the PixVillage Team, nor the names of its contributors may be used to endorse or promote products derived from this software without
// specific prior written permission.
//modified :Shigeo Yoshida, 2011-02-23
package com.shigeodayo.ardrone.video;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
public class ReadRawFileImage {
public ReadRawFileImage() {
}
public BufferedImage readUINT_RGBImage(byte[] rawData)
throws FileNotFoundException, IOException {
int length = 0;
try {
byte[] processedData = process(rawData);
int[] pixelData = new int[processedData.length / 3];
int raw, pixel = 0, j = 0;
for (int i = 0; i < pixelData.length; i++) {
pixel = 0;
raw = processedData[j++] & 0xFF;
pixel |= (raw << 16);
raw = processedData[j++] & 0xFF;
pixel |= (raw << 8);
raw = processedData[j++] & 0xFF;
pixel |= (raw << 0);
pixelData[i] = pixel;
}
// System.out.println(pixelData.length);
length = pixelData.length;
if (length == 76800) {
BufferedImage image = new BufferedImage(320, 240,
BufferedImage.TYPE_INT_RGB);
image.setRGB(0, 0, 320, 240, pixelData, 0, 320);
return image;
} else if (length == 25344) {
BufferedImage image = new BufferedImage(176, 144,
BufferedImage.TYPE_INT_RGB);
image.setRGB(0, 0, 176, 144, pixelData, 0, 176);
return image;
}
/*
* BufferedImage image = new BufferedImage(640, 480,
* BufferedImage.TYPE_INT_RGB);
*
* image.setRGB(0, 0, 640, 480, pixelData, 0, 480);
*/
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
// System.out.println(length);
}
return null;
}
private byte[] process(final byte[] rawData) {
final BufferedVideoImage image = new BufferedVideoImage();
image.AddImageStream(ByteBuffer.wrap(rawData));
final uint[] outData = image.getPixelData();
ByteBuffer buffer = ByteBuffer.allocate(outData.length * 3);
for (int i = 0; i < outData.length; i++) {
int myInt = outData[i].intValue();
buffer.put((byte) ((myInt >> 16) & 0xFF));
buffer.put((byte) ((myInt >> 8) & 0xFF));
buffer.put((byte) (myInt & 0xFF));
}
return buffer.array();
}
}
This diff is collapsed.
/**
ARDroneForP5
https://github.com/shigeodayo/ARDroneForP5
Copyright (C) 2013, Shigeo YOSHIDA.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.shigeodayo.ardrone.video;
import java.net.InetAddress;
import com.shigeodayo.ardrone.command.CommandManager;
import com.shigeodayo.ardrone.manager.AbstractManager;
import com.shigeodayo.ardrone.utils.ARDroneConstants;
public abstract class VideoManager extends AbstractManager {
protected CommandManager manager = null;
protected ImageListener listener = null;
public VideoManager(InetAddress inetaddr, CommandManager manager) {
this.inetaddr = inetaddr;
this.manager = manager;
}
public void setImageListener(ImageListener listener) {
this.listener = listener;
}
public void removeImageListener() {
listener = null;
}
protected void setVideoPort() {
ticklePort(ARDroneConstants.VIDEO_PORT);
manager.enableVideoData();
ticklePort(ARDroneConstants.VIDEO_PORT);
manager.disableAutomaticVideoBitrate();
}
}
\ No newline at end of file
/**
ARDroneForP5
https://github.com/shigeodayo/ARDroneForP5
Copyright (C) 2013, Shigeo YOSHIDA.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.shigeodayo.ardrone.video;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import com.shigeodayo.ardrone.command.CommandManager;
import com.shigeodayo.ardrone.utils.ARDroneConstants;
/**
* VideoManager for AR.Drone 1.0
*
* @author shigeo
*
*/
public class VideoManager1 extends VideoManager {
private ReadRawFileImage rrfi = null;
public VideoManager1(InetAddress inetaddr, CommandManager manager) {
super(inetaddr, manager);
rrfi = new ReadRawFileImage();
//System.out.println("video manager 1");
}
@Override
public void run() {
setVideoPort();
byte[] buf = new byte[153600];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
BufferedImage image = null;
while (true) {
try {
ticklePort(ARDroneConstants.VIDEO_PORT);
socket.receive(packet);
image = rrfi.readUINT_RGBImage(buf);
if (listener != null) {
listener.imageUpdated(image);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
\ No newline at end of file
This diff is collapsed.
package com.shigeodayo.ardrone.video;
import java.io.ByteArrayInputStream;
//import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
//import java.io.DataOutputStream;
import java.nio.ByteBuffer;
//import java.security.AllPermission;
//Copyright 2007-2011, PARROT SA, all rights reserved.
//DISCLAIMER
//The APIs is provided by PARROT and contributors "AS IS" and any express or implied warranties, including, but not limited to, the implied warranties of merchantability
//and fitness for a particular purpose are disclaimed. In no event shall PARROT and contributors be liable for any direct, indirect, incidental, special, exemplary, or
//consequential damages (including, but not limited to, procurement of substitute goods or services; loss of use, data, or profits; or business interruption) however
//caused and on any theory of liability, whether in contract, strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this
//software, even if advised of the possibility of such damage.
//Author : Daniel Schmidt
//Publishing date : 2010-01-06
//based on work by : Wilke Jansoone
//Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions
//are met:
//- Redistributions of source code must retain the above copyright notice, this list of conditions, the disclaimer and the original author of the source code.
//- Neither the name of the PixVillage Team, nor the names of its contributors may be used to endorse or promote products derived from this software without
//specific prior written permission.
public class uint {
public String toString() {
return Integer.toString(base2, 2);
}
public uint(int base) {
this.base2 = base;
}
public uint(uint that) {
this.base2 = that.base2;
}
public uint(byte[] bp, int start) {
try {
byte[] b = new byte[4];
b[0] = bp[start + 3];
b[1] = bp[start + 2];
b[2] = bp[start + 1];
b[3] = bp[start + 0];
ByteArrayInputStream bas = new ByteArrayInputStream(b);
DataInputStream din = new DataInputStream(bas);
this.base2 = din.readInt();
} catch (Exception e) {
throw new RuntimeException("error creating uint", e);
}
}
public uint(ByteBuffer bp, int start) {
try {
ByteBuffer bb = ByteBuffer.allocate(4);
bb.put(bp.array()[start + 3]);
bb.put(bp.array()[start + 2]);
bb.put(bp.array()[start + 1]);
bb.put(bp.array()[start + 0]);
bb.flip();
this.base2 = bb.getInt();
} catch (Exception e) {
throw new RuntimeException("error creating uint", e);
}
}
private int base2;
public short times(short i) {
return (short) (intValue() * i);
}
public uint shiftRight(int i) {
// System.out.println("shiftRight[0] " + base2 + " " + i);
// String str = Integer.toBinaryString(base);
int base = base2;
// System.out.println("shiftRight[n][1] " + uint.toBinaryString(base));
base = base >>> i;
// System.out.println("shiftRight[n][2] " + uint.toBinaryString(base));
return new uint(base);
}
public uint shiftLeft(int i) {
int base = base2;
base <<= i;
return new uint(base);
// return Integer.parseInt(base, 2);
}
public int flipBits() {
int base = ~base2;
return base;
}
public int intValue() {
return base2;
}
public uint and(int andval) {
int retval = base2 & andval;
return new uint(retval);
}
public void shiftLeftEquals(int i) {
int base = base2;
base <<= i;
base2 = base;
}
public void shiftRightEquals(int i) {
int base = base2;
base >>>= i;
base2 = base;
}
public uint or(uint orval) {
int retval = base2 | orval.base2;
return new uint(retval);
}
}
This diff is collapsed.
/**
ARDroneForP5
https://github.com/shigeodayo/ARDroneForP5
Copyright (C) 2013, Shigeo YOSHIDA.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package exmaples;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import com.shigeodayo.ardrone.utils.ARDroneConstants;
import java.io.IOException;
import java.io.FileOutputStream;
public class FtpClientExample {
public static void main(String[] args) {
FTPClient client = new FTPClient();
FileOutputStream fos = null;
try {
client.connect(ARDroneConstants.IP_ADDRESS, ARDroneConstants.FTP_PORT);
if (!client.login("anonymous", ""))
System.err.println("login failed");
client.setFileType(FTP.BINARY_FILE_TYPE);
String filename = "version.txt";
fos = new FileOutputStream(filename);
if (!client.retrieveFile("/" + filename, fos))
System.err.println("cannot find file");
System.out.println("done");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.flush();
fos.close();
}
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
\ No newline at end of file
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment