package nl.ru.ai.projects.parrot.dronecontrol.javadronecontrol;
import java.net.DatagramPacket;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.List;
int result = 0;
for (int i = 0; i < len; i++) {
byte b = bytes.get();
result += b < 0 ? 0x0100 + b : b;
}
return result;
}
public static final int HID_CHECKSUM = 0xFFFF;
public static final int ARDRONE_FLY_MASK = 1 << 0;
public static final int ARDRONE_VIDEO_MASK = 1 << 1;
public static final int ARDRONE_VISION_MASK = 1 << 2;
public static final int ARDRONE_CONTROL_MASK = 1 << 3;
public static final int ARDRONE_ALTITUDE_MASK = 1 << 4;
public static final int ARDRONE_USER_FEEDBACK_START = 1 << 5;
public static final int ARDRONE_COMMAND_MASK = 1 << 6;
public static final int ARDRONE_FW_FILE_MASK = 1 << 7;
public static final int ARDRONE_FW_VER_MASK = 1 << 8;
public static final int ARDRONE_NAVDATA_DEMO_MASK = 1 << 10;
public static final int ARDRONE_NAVDATA_BOOTSTRAP = 1 << 11;
public static final int ARDRONE_MOTORS_MASK = 1 << 12;
public static final int ARDRONE_COM_LOST_MASK = 1 << 13;
public static final int ARDRONE_VBAT_LOW = 1 << 15;
public static final int ARDRONE_USER_EL = 1 << 16;
public static final int ARDRONE_TIMER_ELAPSED = 1 << 17;
public static final int ARDRONE_ANGLES_OUT_OF_RANGE = 1 << 19;
public static final int ARDRONE_ULTRASOUND_MASK = 1 << 21;
public static final int ARDRONE_CUTOUT_MASK = 1 << 22;
public static final int ARDRONE_PIC_VERSION_MASK = 1 << 23;
public static final int ARDRONE_ATCODEC_THREAD_ON = 1 << 24;
public static final int ARDRONE_NAVDATA_THREAD_ON = 1 << 25;
public static final int ARDRONE_VIDEO_THREAD_ON = 1 << 26;
public static final int ARDRONE_ACQ_THREAD_ON = 1 << 27;
public static final int ARDRONE_CTRL_WATCHDOG_MASK = 1 << 28;
public static final int ARDRONE_ADC_WATCHDOG_MASK = 1 << 29;
public static final int ARDRONE_COM_WATCHDOG_MASK = 1 << 30;
public static final int ARDRONE_EMERGENCY_MASK = 1 << 31;
private static final long serialVersionUID = -9002844056303380102L;
super(why);
}
}
private class {
public int headerID;
public byte[] data;
public (ByteBuffer src) throws NavdataPacketFormatException {
if (src.remaining() < 4) {
throw new NavdataPacketFormatException("Header of option too small");
}
headerID = src.getShort();
if (headerID < 0) {
headerID += 0x00010000;
}
int size = src.getShort() - 4;
if (src.remaining() < size) {
throw new NavdataPacketFormatException("Data field of option to long");
}
data = new byte[size];
src.get(data);
}
}
public static final int HID = 0;
public int controlState, batteryPercentage;
public float[] euler;
public int altitude;
public float[] velocity;
ByteBuffer bb = ByteBuffer.wrap(ba);
bb.order(ByteOrder.LITTLE_ENDIAN);
controlState = bb.getInt();
batteryPercentage = bb.getInt();
euler = new float[3];
euler[0] = bb.getFloat();
euler[1] = bb.getFloat();
euler[2] = bb.getFloat();
altitude = bb.getInt();
velocity = new float[3];
velocity[0] = bb.getFloat();
velocity[1] = bb.getFloat();
velocity[2] = bb.getFloat();
}
}
public int droneState = -1;
public int sequenceNumber = -1;
public int visionFlag = -1;
public DemoDataStruct demodata = null;
public void parseNavdata(DatagramPacket packet)
throws NavdataPacketFormatException {
ByteBuffer sourceByteBuffer = ByteBuffer.wrap(packet.getData(), packet.getOffset(), packet.getLength());
sourceByteBuffer.order(ByteOrder.LITTLE_ENDIAN);
{
int header = sourceByteBuffer.getInt();
if (header != 0x55667788) {
throw new NavdataPacketFormatException("Magic number mismatch");
}
}
droneState = sourceByteBuffer.getInt();
sequenceNumber = sourceByteBuffer.getInt();
visionFlag = sourceByteBuffer.getInt();
List<DataHeader> headers = new ArrayList<DataHeader>();
while (sourceByteBuffer.remaining() > 0) {
headers.add(new DataHeader(sourceByteBuffer));
}
if (headers.size() <= 0) {
throw new NavdataPacketFormatException("To few options: at least checksum must be contained in navdata");
}
{
DataHeader checksumHeader = headers.get(headers.size() - 1);
if (checksumHeader.headerID != HID_CHECKSUM) {
throw new NavdataPacketFormatException("Last package must be checksum");
}
sourceByteBuffer.rewind();
int checksum = computeChecksum(sourceByteBuffer, packet.getLength() - 8);
{
ByteBuffer checksumBuffer = ByteBuffer.wrap(checksumHeader.data);
checksumBuffer.order(ByteOrder.LITTLE_ENDIAN);
if (checksumBuffer.getInt() != checksum) {
throw new NavdataPacketFormatException("Checksum error!");
}
}
headers.remove(checksumHeader);
}
for (DataHeader header : headers) {
switch (header.headerID) {
case DemoDataStruct.HID:
demodata = new DemoDataStruct(header.data);
break;
}
}
}
}