mirror of
https://bitbucket.org/librepilot/librepilot.git
synced 2025-01-18 03:52:11 +01:00
adding juavtalk
git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@3132 ebee16cc-31ac-478f-84a7-5cbb03baadba
This commit is contained in:
parent
c9f9400d75
commit
8e7fd4da81
39
ground/openpilotgcs/src/libs/juavtalk/build.xml
Normal file
39
ground/openpilotgcs/src/libs/juavtalk/build.xml
Normal file
@ -0,0 +1,39 @@
|
||||
<project default="package">
|
||||
|
||||
<property name="version.num" value="0.1.1" />
|
||||
<property name="build.dir" value="../../../../../build/juavtalk"/>
|
||||
<property name="jar.file" value="${build.dir}/juavtalk.jar"/>
|
||||
<property name="src.dir" value="src"/>
|
||||
<property name="classes.dir" value="${build.dir}/classes"/>
|
||||
|
||||
|
||||
<target name="compile" depends="clean">
|
||||
<mkdir dir="${build.dir}" />
|
||||
<mkdir dir="${classes.dir}" />
|
||||
<javac srcdir="${src.dir}" destdir="${classes.dir}" />
|
||||
</target>
|
||||
|
||||
<target name="jar" depends="compile">
|
||||
|
||||
<manifest file="${build.dir}/MANIFEST.MF">
|
||||
<attribute name="Built-By" value="${user.name}" />
|
||||
<attribute name="Implementation-Version" value="${version.num}"/>
|
||||
</manifest>
|
||||
|
||||
<jar destfile="${jar.file}"
|
||||
basedir="${classes.dir}"
|
||||
includes="**/*.class"
|
||||
manifest="${build.dir}/MANIFEST.MF"
|
||||
/>
|
||||
</target>
|
||||
|
||||
<target name="clean">
|
||||
<delete dir="${build.dir}" />
|
||||
<delete>
|
||||
<fileset dir="." includes="${jar.file}"/>
|
||||
</delete>
|
||||
</target>
|
||||
|
||||
<target name="package" depends="compile,jar" />
|
||||
</project>
|
||||
|
@ -0,0 +1,86 @@
|
||||
/*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
package org.openpilot.uavtalk;
|
||||
|
||||
/**
|
||||
******************************************************************************
|
||||
*
|
||||
* @file CRC8.java
|
||||
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2011.
|
||||
* @brief functions to calculate CRC8 checksums for usage in the UAVTalk-Protocol
|
||||
*
|
||||
****************************************************************************
|
||||
*/
|
||||
public class CRC8 {
|
||||
|
||||
/**
|
||||
* update a CRC8 value with one byte of data
|
||||
*
|
||||
* @param crc - start CRC8 Value
|
||||
* @param data - data byte to update the CRC8-Checksum with
|
||||
* @return - the new CRC value
|
||||
*/
|
||||
public static byte byteUpdate(byte crc, byte data) {
|
||||
return (byte)CRC8_TABLE[crc&0xFF ^ data&0xFF];
|
||||
}
|
||||
|
||||
/**
|
||||
* update a CRC8 value with an byte-array
|
||||
*
|
||||
* @param crc - start CRC8 Value
|
||||
* @param data - data byte-array to update the CRC8-Checksum with
|
||||
* @return - the new CRC value
|
||||
*/
|
||||
public static byte arrayUpdate(byte crc, byte[] data, int length) {
|
||||
for (int i=0;i<length;i++)
|
||||
crc = CRC8_TABLE[(crc ^ data[i])&0xFF];
|
||||
return crc;
|
||||
}
|
||||
|
||||
/** CRC lookup table - values from PYCRC **/
|
||||
private final static byte [] CRC8_TABLE = {
|
||||
(byte)0x00, (byte)0x07, (byte)0x0e, (byte)0x09, (byte)0x1c, (byte)0x1b, (byte)0x12, (byte)0x15, (byte)0x38,
|
||||
(byte)0x3f, (byte)0x36, (byte)0x31, (byte)0x24, (byte)0x23, (byte)0x2a, (byte)0x2d, (byte)0x70, (byte)0x77,
|
||||
(byte)0x7e, (byte)0x79, (byte)0x6c, (byte)0x6b, (byte)0x62, (byte)0x65, (byte)0x48, (byte)0x4f, (byte)0x46,
|
||||
(byte)0x41, (byte)0x54, (byte)0x53, (byte)0x5a, (byte)0x5d, (byte)0xe0, (byte)0xe7, (byte)0xee, (byte)0xe9,
|
||||
(byte)0xfc, (byte)0xfb, (byte)0xf2, (byte)0xf5, (byte)0xd8, (byte)0xdf, (byte)0xd6, (byte)0xd1, (byte)0xc4,
|
||||
(byte)0xc3, (byte)0xca, (byte)0xcd, (byte)0x90, (byte)0x97, (byte)0x9e, (byte)0x99, (byte)0x8c, (byte)0x8b,
|
||||
(byte)0x82, (byte)0x85, (byte)0xa8, (byte)0xaf, (byte)0xa6, (byte)0xa1, (byte)0xb4, (byte)0xb3, (byte)0xba,
|
||||
(byte)0xbd, (byte)0xc7, (byte)0xc0, (byte)0xc9, (byte)0xce, (byte)0xdb, (byte)0xdc, (byte)0xd5, (byte)0xd2,
|
||||
(byte)0xff, (byte)0xf8, (byte)0xf1, (byte)0xf6, (byte)0xe3, (byte)0xe4, (byte)0xed, (byte)0xea, (byte)0xb7,
|
||||
(byte)0xb0, (byte)0xb9, (byte)0xbe, (byte)0xab, (byte)0xac, (byte)0xa5, (byte)0xa2, (byte)0x8f, (byte)0x88,
|
||||
(byte)0x81, (byte)0x86, (byte)0x93, (byte)0x94, (byte)0x9d, (byte)0x9a, (byte)0x27, (byte)0x20, (byte)0x29,
|
||||
(byte)0x2e, (byte)0x3b, (byte)0x3c, (byte)0x35, (byte)0x32, (byte)0x1f, (byte)0x18, (byte)0x11, (byte)0x16,
|
||||
(byte)0x03, (byte)0x04, (byte)0x0d, (byte)0x0a, (byte)0x57, (byte)0x50, (byte)0x59, (byte)0x5e, (byte)0x4b,
|
||||
(byte)0x4c, (byte)0x45, (byte)0x42, (byte)0x6f, (byte)0x68, (byte)0x61, (byte)0x66, (byte)0x73, (byte)0x74,
|
||||
(byte)0x7d, (byte)0x7a, (byte)0x89, (byte)0x8e, (byte)0x87, (byte)0x80, (byte)0x95, (byte)0x92, (byte)0x9b,
|
||||
(byte)0x9c, (byte)0xb1, (byte)0xb6, (byte)0xbf, (byte)0xb8, (byte)0xad, (byte)0xaa, (byte)0xa3, (byte)0xa4,
|
||||
(byte)0xf9, (byte)0xfe, (byte)0xf7, (byte)0xf0, (byte)0xe5, (byte)0xe2, (byte)0xeb, (byte)0xec, (byte)0xc1,
|
||||
(byte)0xc6, (byte)0xcf, (byte)0xc8, (byte)0xdd, (byte)0xda, (byte)0xd3, (byte)0xd4, (byte)0x69, (byte)0x6e,
|
||||
(byte)0x67, (byte)0x60, (byte)0x75, (byte)0x72, (byte)0x7b, (byte)0x7c, (byte)0x51, (byte)0x56, (byte)0x5f,
|
||||
(byte)0x58, (byte)0x4d, (byte)0x4a, (byte)0x43, (byte)0x44, (byte)0x19, (byte)0x1e, (byte)0x17, (byte)0x10,
|
||||
(byte)0x05, (byte)0x02, (byte)0x0b, (byte)0x0c, (byte)0x21, (byte)0x26, (byte)0x2f, (byte)0x28, (byte)0x3d,
|
||||
(byte)0x3a, (byte)0x33, (byte)0x34, (byte)0x4e, (byte)0x49, (byte)0x40, (byte)0x47, (byte)0x52, (byte)0x55,
|
||||
(byte)0x5c, (byte)0x5b, (byte)0x76, (byte)0x71, (byte)0x78, (byte)0x7f, (byte)0x6a, (byte)0x6d, (byte)0x64,
|
||||
(byte)0x63, (byte)0x3e, (byte)0x39, (byte)0x30, (byte)0x37, (byte)0x22, (byte)0x25, (byte)0x2c, (byte)0x2b,
|
||||
(byte)0x06, (byte)0x01, (byte)0x08, (byte)0x0f, (byte)0x1a, (byte)0x1d, (byte)0x14, (byte)0x13, (byte)0xae,
|
||||
(byte)0xa9, (byte)0xa0, (byte)0xa7, (byte)0xb2, (byte)0xb5, (byte)0xbc, (byte)0xbb, (byte)0x96, (byte)0x91,
|
||||
(byte)0x98, (byte)0x9f, (byte)0x8a, (byte)0x8d, (byte)0x84, (byte)0x83, (byte)0xde, (byte)0xd9, (byte)0xd0,
|
||||
(byte)0xd7, (byte)0xc2, (byte)0xc5, (byte)0xcc, (byte)0xcb, (byte)0xe6, (byte)0xe1, (byte)0xe8, (byte)0xef,
|
||||
(byte)0xfa, (byte)0xfd, (byte)0xf4, (byte)0xf3
|
||||
};
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
/*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
package org.openpilot.uavtalk;
|
||||
|
||||
import org.openpilot.uavtalk.UAVObjectFieldDescription;
|
||||
|
||||
/**
|
||||
******************************************************************************
|
||||
*
|
||||
* @file UAVObject.java
|
||||
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2011.
|
||||
* @brief abstract class for an UAVObject
|
||||
*
|
||||
****************************************************************************
|
||||
*/
|
||||
public abstract class UAVObject {
|
||||
|
||||
abstract public int getObjID();
|
||||
abstract public String getObjName();
|
||||
abstract public String getObjDescription();
|
||||
|
||||
abstract public Object getField(int fieldid,int arr_pos);
|
||||
abstract public void setField(int fieldid,int arr_pos,Object val);
|
||||
|
||||
public byte[] serialize() {
|
||||
getMetaData().last_serialize=System.currentTimeMillis();
|
||||
return new byte[0];
|
||||
}
|
||||
|
||||
public void deserialize(byte[] data) {
|
||||
deserialize(data,0);
|
||||
}
|
||||
|
||||
public void deserialize(byte[] data,int offset) {
|
||||
getMetaData().last_deserialize=System.currentTimeMillis();
|
||||
}
|
||||
|
||||
abstract public UAVObjectFieldDescription[] getFieldDescriptions();
|
||||
|
||||
private UAVObjectMetaData myMetaData=null;
|
||||
|
||||
public UAVObjectMetaData getMetaData() {
|
||||
if (myMetaData==null) {
|
||||
myMetaData=new UAVObjectMetaData();
|
||||
setGeneratedMetaData();
|
||||
}
|
||||
return myMetaData;
|
||||
}
|
||||
|
||||
abstract public void setGeneratedMetaData();
|
||||
|
||||
public int getDataLength() {
|
||||
return serialize().length;
|
||||
}
|
||||
}
|
@ -0,0 +1,89 @@
|
||||
/*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
package org.openpilot.uavtalk;
|
||||
|
||||
/**
|
||||
******************************************************************************
|
||||
*
|
||||
* @file UAVObjectFieldDescription.java
|
||||
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2011.
|
||||
* @brief Class to deal with the description of an UAVObject Field
|
||||
*
|
||||
****************************************************************************
|
||||
*/
|
||||
public class UAVObjectFieldDescription {
|
||||
private String unit;
|
||||
private String name;
|
||||
private byte type;
|
||||
|
||||
public final static byte FIELDTYPE_INT8 = 0;
|
||||
public final static byte FIELDTYPE_INT16 = 1;
|
||||
public final static byte FIELDTYPE_INT32 =2;
|
||||
public final static byte FIELDTYPE_UINT8 =3;
|
||||
public final static byte FIELDTYPE_UINT16 =4;
|
||||
public final static byte FIELDTYPE_UINT32 =5;
|
||||
public final static byte FIELDTYPE_FLOAT32 =6;
|
||||
public final static byte FIELDTYPE_ENUM =7;
|
||||
|
||||
private int objid;
|
||||
private byte fieldid;
|
||||
|
||||
private String[] enumOptions=new String[] {};
|
||||
private String[] elementNames;
|
||||
|
||||
/**
|
||||
* @param name - the fields name
|
||||
* @param unit - the unit in which the fields value is
|
||||
* @param enumOptions - if field is enumeration - here are the options - otherwise null
|
||||
* @param elementNames - if field is array - here are the names for the elements
|
||||
*/
|
||||
public UAVObjectFieldDescription(String name,int objid,byte fieldid,byte type,String unit,String[] enumOptions,String[] elementNames) {
|
||||
this.name=name;
|
||||
this.unit=unit;
|
||||
this.enumOptions=enumOptions;
|
||||
this.elementNames=elementNames;
|
||||
this.objid=objid;
|
||||
this.fieldid=fieldid;
|
||||
this.type=type;
|
||||
}
|
||||
|
||||
public String getUnit() {
|
||||
return unit;
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public String[] getEnumOptions() {
|
||||
return enumOptions;
|
||||
}
|
||||
public String[] getElementNames() {
|
||||
return elementNames;
|
||||
}
|
||||
|
||||
public int getObjId() {
|
||||
return objid;
|
||||
}
|
||||
|
||||
public byte getFieldId() {
|
||||
return fieldid;
|
||||
}
|
||||
|
||||
public byte getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,100 @@
|
||||
/*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
package org.openpilot.uavtalk;
|
||||
|
||||
/**
|
||||
******************************************************************************
|
||||
*
|
||||
* @file UAVObjectMetaData.java
|
||||
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2011.
|
||||
* @brief Class to handle the metadata of an UAVObject
|
||||
*
|
||||
****************************************************************************
|
||||
*/
|
||||
public class UAVObjectMetaData {
|
||||
|
||||
public final static byte ACCESS_READWRITE=0;
|
||||
public final static byte ACCESS_READONLY=1;
|
||||
public final static byte ACCESS_WRITEONLY=2;
|
||||
|
||||
public final static String getAccessString(byte access_mode) {
|
||||
switch (access_mode) {
|
||||
case ACCESS_READWRITE:
|
||||
return "Read/Write";
|
||||
case ACCESS_WRITEONLY:
|
||||
return "Write only";
|
||||
case ACCESS_READONLY:
|
||||
return "Read only";
|
||||
default:
|
||||
return "unknowm Access Mode";
|
||||
}
|
||||
}
|
||||
|
||||
public final static byte UPDATEMODE_NEVER=0;
|
||||
public final static byte UPDATEMODE_MANUAL=1;
|
||||
public final static byte UPDATEMODE_ONCHANGE=2;
|
||||
public final static byte UPDATEMODE_PERIODIC=3;
|
||||
|
||||
public final static String getUpdateModeString(byte update_mode) {
|
||||
switch(update_mode) {
|
||||
case UPDATEMODE_NEVER:
|
||||
return "never";
|
||||
case UPDATEMODE_MANUAL:
|
||||
return "manual";
|
||||
case UPDATEMODE_ONCHANGE:
|
||||
return "on change";
|
||||
case UPDATEMODE_PERIODIC:
|
||||
return "periodic";
|
||||
default:
|
||||
return "unknown";
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public final static boolean TRUE=true;
|
||||
public final static boolean FALSE=false;
|
||||
|
||||
/* constant */
|
||||
public byte gcsAccess;
|
||||
public boolean gcsTelemetryAcked;
|
||||
public byte gcsTelemetryUpdateMode;
|
||||
public int gcsTelemetryUpdatePeriod;
|
||||
|
||||
public byte flightAccess;
|
||||
public boolean flightTelemetryAcked;
|
||||
public byte flightTelemetryUpdateMode;
|
||||
public int flightTelemetryUpdatePeriod;
|
||||
|
||||
public byte loggingUpdateMode;
|
||||
public int loggingUpdatePeriod;
|
||||
|
||||
/* changing */
|
||||
public long last_serialize;
|
||||
public long last_deserialize;
|
||||
|
||||
public long last_log;
|
||||
public long last_gcs_update;
|
||||
public long last_fligt_update;
|
||||
|
||||
public long last_send_time;
|
||||
|
||||
public boolean gcs_flight_was_acked=false;
|
||||
public boolean gcs_ground_was_acked=false;
|
||||
|
||||
public boolean req_pending=false;
|
||||
public boolean ack_pending=false;
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
package org.openpilot.uavtalk;
|
||||
|
||||
/**
|
||||
******************************************************************************
|
||||
*
|
||||
* @file UAVObjectsInterface.java
|
||||
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2011.
|
||||
* @brief a Interface to a UAVObjects collection
|
||||
*
|
||||
****************************************************************************
|
||||
*/
|
||||
public interface UAVObjectsInterface {
|
||||
|
||||
public void init();
|
||||
public UAVObject[] getUAVObjectArray();
|
||||
public UAVObject getObjectByID(int id) ;
|
||||
|
||||
public UAVObject getObjectByName(String name);
|
||||
public void printAll();
|
||||
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
/*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
package org.openpilot.uavtalk;
|
||||
|
||||
/**
|
||||
******************************************************************************
|
||||
*
|
||||
* @file UAVTalkDefinitions.java
|
||||
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2011.
|
||||
* @brief Provide values which are used in the UAVTalk Protocol
|
||||
*
|
||||
****************************************************************************
|
||||
*/
|
||||
public class UAVTalkDefinitions {
|
||||
|
||||
public final static byte SYNC_VAL = 0x3C;
|
||||
|
||||
public final static byte TYPE_MASK_VER = (byte)0xFC;
|
||||
public final static byte TYPE_MASK_TYPE = (byte)0x03;
|
||||
|
||||
public final static byte TYPE_VER = 0x20;
|
||||
public final static byte TYPE_OBJ = (TYPE_VER | 0x00);
|
||||
public final static byte TYPE_OBJ_REQ = (TYPE_VER | 0x01);
|
||||
public final static byte TYPE_OBJ_ACK = (TYPE_VER | 0x02);
|
||||
public final static byte TYPE_ACK = (TYPE_VER | 0x03);
|
||||
|
||||
|
||||
public final static String getTypeString(byte type) {
|
||||
switch(type) {
|
||||
case TYPE_ACK:
|
||||
return "ack";
|
||||
case TYPE_OBJ:
|
||||
return "obj";
|
||||
case TYPE_OBJ_ACK:
|
||||
return "obj_ack";
|
||||
case TYPE_OBJ_REQ:
|
||||
return "obj_req";
|
||||
}
|
||||
return "unknown type";
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,102 @@
|
||||
/*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
package org.openpilot.uavtalk;
|
||||
import org.openpilot.uavtalk.CRC8;
|
||||
import org.openpilot.uavtalk.UAVObject;
|
||||
|
||||
/**
|
||||
******************************************************************************
|
||||
*
|
||||
* @file UAVTalkHelper.java
|
||||
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2011.
|
||||
* @brief misc static helper functions to deal with UAVTalk
|
||||
*
|
||||
****************************************************************************
|
||||
*/
|
||||
public class UAVTalkHelper {
|
||||
|
||||
public final static int PACKAGE_LENGTH_SYNC=1;
|
||||
public final static int PACKAGE_LENGTH_CRC=1;
|
||||
public final static int PACKAGE_LENGTH_TYPE=1;
|
||||
public final static int PACKAGE_LENGTH_OBJID=4;
|
||||
public final static int PACKAGE_LENGTH_SIZE=2;
|
||||
|
||||
public final static int MIN_PACKAGE_SIZE=PACKAGE_LENGTH_SYNC
|
||||
+ PACKAGE_LENGTH_TYPE
|
||||
+ PACKAGE_LENGTH_SIZE
|
||||
+ PACKAGE_LENGTH_OBJID
|
||||
+ PACKAGE_LENGTH_CRC ;
|
||||
|
||||
/**
|
||||
* generate a UAVTalk package
|
||||
*
|
||||
* @param type - the package
|
||||
* @param obj_id - the object ID
|
||||
* @param payload - the payload byte-array
|
||||
* @return byte-array containing the UAVTalk Package
|
||||
*/
|
||||
public static byte[] generateUAVTalkPackage(byte type,int obj_id,byte[] payload) {
|
||||
byte[] res=new byte[MIN_PACKAGE_SIZE +payload.length];
|
||||
res[0]=UAVTalkDefinitions.SYNC_VAL;
|
||||
res[1]=type;
|
||||
res[2]=(byte)((MIN_PACKAGE_SIZE-1 + payload.length)&0xFF);
|
||||
res[3]=(byte)(((MIN_PACKAGE_SIZE-1 + payload.length)>>8)&0xFF);
|
||||
res[4]=(byte)((obj_id)&0xFF);
|
||||
res[5]=(byte)((obj_id>>8)&0xFF);
|
||||
res[6]=(byte)((obj_id>>16)&0xFF);
|
||||
res[7]=(byte)((obj_id>>24)&0xFF);
|
||||
|
||||
|
||||
for (int i=0;i<payload.length;i++)
|
||||
res[8+i]=payload[i];
|
||||
|
||||
|
||||
res[8+payload.length]=CRC8.arrayUpdate((byte)0,res,8+payload.length);
|
||||
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* generate a UAVTalk package without any payload
|
||||
*
|
||||
* @param type - the package
|
||||
* @param obj_id - the object ID
|
||||
* @return byte-array containing the UAVTalk Package
|
||||
*/
|
||||
public static byte[] generateUAVTalkPackage(byte type,int obj_id) {
|
||||
return generateUAVTalkPackage(type,obj_id,new byte[0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* generate a UAVTalk package and serialize the object as payload if needed
|
||||
*
|
||||
* @param type - the package
|
||||
* @param obj - the object
|
||||
* @return byte-array containing the UAVTalk Package
|
||||
*/
|
||||
public static byte[] generateUAVTalkPackage(byte type,UAVObject obj) {
|
||||
switch (type) {
|
||||
case UAVTalkDefinitions.TYPE_OBJ_ACK:
|
||||
case UAVTalkDefinitions.TYPE_OBJ:
|
||||
return generateUAVTalkPackage(type,obj.getObjID(),obj.serialize());
|
||||
default:
|
||||
return generateUAVTalkPackage(type,obj.getObjID());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package org.openpilot.uavtalk;
|
||||
/**
|
||||
* class with static functions to parse values from the UAVTalk protocol
|
||||
*
|
||||
* @author ligi ( aka: Marcus Bueschleb | mail: ligi at ligi dot de )
|
||||
*/
|
||||
public class ValueParser {
|
||||
|
||||
/**
|
||||
* parse a int value from 4 bytes of some array
|
||||
*
|
||||
* @param offset - where to start in the array
|
||||
* @param arr - the array
|
||||
* @return - the calculated value
|
||||
*/
|
||||
|
||||
public final static int parse_int_from_arr_4(int offset,byte[] arr) {
|
||||
return (
|
||||
((arr[offset+3]&0xFF)<<24) |
|
||||
((arr[offset+2]&0xFF)<<16) |
|
||||
((arr[offset+1]&0xFF)<<8) |
|
||||
arr[offset+0]&0xFF
|
||||
);
|
||||
}
|
||||
|
||||
public final static int parse_int_from_arr_4_2(int offset,byte[] arr) {
|
||||
return (
|
||||
((arr[offset+3])<<24) |
|
||||
((arr[offset+2])<<16) |
|
||||
((arr[offset+1])<<8) |
|
||||
arr[offset+0]
|
||||
);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user