mirror of
https://bitbucket.org/librepilot/librepilot.git
synced 2024-12-01 09:24:10 +01:00
AndroidGCS: Add copyright headers to the UAVTalk/UAVObject/Telemetry implementations.
This commit is contained in:
parent
2008486922
commit
dc6b3af707
@ -1,3 +1,27 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file Telemetry.java
|
||||
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012.
|
||||
* @brief Port of Telemetry.cpp from the GCS. Handles transactions on the
|
||||
* UAVTalk channel.
|
||||
* @see The GNU Public License (GPL) Version 3
|
||||
*
|
||||
*****************************************************************************/
|
||||
/*
|
||||
* 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 java.io.IOException;
|
||||
@ -14,7 +38,7 @@ import java.util.TimerTask;
|
||||
import android.util.Log;
|
||||
|
||||
public class Telemetry {
|
||||
|
||||
|
||||
private final String TAG = "Telemetry";
|
||||
public static int LOGLEVEL = 0;
|
||||
public static boolean WARN = LOGLEVEL > 2;
|
||||
@ -31,7 +55,7 @@ public class Telemetry {
|
||||
public int rxErrors;
|
||||
public int txRetries;
|
||||
} ;
|
||||
|
||||
|
||||
class ObjectTimeInfo {
|
||||
UAVObject obj;
|
||||
int updatePeriodMs; /** Update period in ms or 0 if no periodic updates are needed */
|
||||
@ -51,7 +75,7 @@ public class Telemetry {
|
||||
int retriesRemaining;
|
||||
boolean acked;
|
||||
} ;
|
||||
|
||||
|
||||
/**
|
||||
* Events generated by objects. Not enum because used in mask.
|
||||
*/
|
||||
@ -59,7 +83,7 @@ public class Telemetry {
|
||||
private static final int EV_UPDATED = 0x02; /** Object data updated by changing the data structure */
|
||||
private static final int EV_UPDATED_MANUAL = 0x04; /** Object update event manually generated */
|
||||
private static final int EV_UPDATE_REQ = 0x08; /** Request to update object data */
|
||||
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
@ -71,33 +95,36 @@ public class Telemetry {
|
||||
// Process all objects in the list
|
||||
List< List<UAVObject> > objs = objMngr.getObjects();
|
||||
ListIterator<List<UAVObject>> li = objs.listIterator();
|
||||
while(li.hasNext())
|
||||
while(li.hasNext())
|
||||
registerObject(li.next().get(0)); // we only need to register one instance per object type
|
||||
|
||||
// Listen to new object creations
|
||||
objMngr.addNewInstanceObserver(new Observer() {
|
||||
@Override
|
||||
public void update(Observable observable, Object data) {
|
||||
newInstance((UAVObject) data);
|
||||
newInstance((UAVObject) data);
|
||||
}
|
||||
});
|
||||
objMngr.addNewObjectObserver(new Observer() {
|
||||
@Override
|
||||
public void update(Observable observable, Object data) {
|
||||
newObject((UAVObject) data);
|
||||
}
|
||||
newObject((UAVObject) data);
|
||||
}
|
||||
});
|
||||
|
||||
// Listen to transaction completions
|
||||
utalk.addObserver(new Observer() {
|
||||
@Override
|
||||
public void update(Observable observable, Object data) {
|
||||
try {
|
||||
transactionCompleted((UAVObject) data);
|
||||
} catch (IOException e) {
|
||||
// Disconnect when stream fails
|
||||
observable.deleteObserver(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// Get GCS stats object
|
||||
gcsStatsObj = objMngr.getObject("GCSTelemetryStats");
|
||||
|
||||
@ -110,16 +137,16 @@ public class Telemetry {
|
||||
txErrors = 0;
|
||||
txRetries = 0;
|
||||
}
|
||||
|
||||
|
||||
synchronized void transTimerSetPeriod(int periodMs) {
|
||||
if(transTimerTask != null)
|
||||
transTimerTask.cancel();
|
||||
|
||||
if(transTimer != null)
|
||||
|
||||
if(transTimer != null)
|
||||
transTimer.purge();
|
||||
|
||||
transTimer = new Timer();
|
||||
|
||||
|
||||
transTimerTask = new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
@ -128,11 +155,11 @@ public class Telemetry {
|
||||
} catch (IOException e) {
|
||||
cancel();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
transTimer.schedule(transTimerTask, periodMs, periodMs);
|
||||
}
|
||||
|
||||
|
||||
synchronized void updateTimerSetPeriod(int periodMs) {
|
||||
if (updateTimer != null) {
|
||||
updateTimer.cancel();
|
||||
@ -152,7 +179,7 @@ public class Telemetry {
|
||||
updateTimerTask.cancel();
|
||||
updateTimer.cancel();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
updateTimer.schedule(updateTimerTask, periodMs, periodMs);
|
||||
|
||||
@ -223,11 +250,12 @@ public class Telemetry {
|
||||
obj = li.next();
|
||||
//TODO: Disconnect all
|
||||
// obj.disconnect(this);
|
||||
|
||||
|
||||
// Connect only the selected events
|
||||
if ( (eventMask&EV_UNPACKED) != 0)
|
||||
{
|
||||
obj.addUnpackedObserver(new Observer() {
|
||||
@Override
|
||||
public void update(Observable observable, Object data) {
|
||||
try {
|
||||
objectUnpacked( (UAVObject) data);
|
||||
@ -235,12 +263,13 @@ public class Telemetry {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
if ( (eventMask&EV_UPDATED) != 0)
|
||||
{
|
||||
obj.addUpdatedAutoObserver(new Observer() {
|
||||
@Override
|
||||
public void update(Observable observable, Object data) {
|
||||
try {
|
||||
objectUpdatedAuto( (UAVObject) data);
|
||||
@ -248,12 +277,13 @@ public class Telemetry {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
if ( (eventMask&EV_UPDATED_MANUAL) != 0)
|
||||
{
|
||||
obj.addUpdatedManualObserver(new Observer() {
|
||||
@Override
|
||||
public void update(Observable observable, Object data) {
|
||||
try {
|
||||
objectUpdatedManual( (UAVObject) data);
|
||||
@ -261,12 +291,13 @@ public class Telemetry {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
if ( (eventMask&EV_UPDATE_REQ) != 0)
|
||||
{
|
||||
obj.addUpdateRequestedObserver(new Observer() {
|
||||
@Override
|
||||
public void update(Observable observable, Object data) {
|
||||
try {
|
||||
updateRequested( (UAVObject) data);
|
||||
@ -274,7 +305,7 @@ public class Telemetry {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -331,7 +362,7 @@ public class Telemetry {
|
||||
|
||||
/**
|
||||
* Called when a transaction is successfully completed (uavtalk event)
|
||||
* @throws IOException
|
||||
* @throws IOException
|
||||
*/
|
||||
private synchronized void transactionCompleted(UAVObject obj) throws IOException
|
||||
{
|
||||
@ -355,7 +386,7 @@ public class Telemetry {
|
||||
|
||||
/**
|
||||
* Called when a transaction is not completed within the timeout period (timer event)
|
||||
* @throws IOException
|
||||
* @throws IOException
|
||||
*/
|
||||
private synchronized void transactionTimeout() throws IOException
|
||||
{
|
||||
@ -387,7 +418,7 @@ public class Telemetry {
|
||||
|
||||
/**
|
||||
* Start an object transaction with UAVTalk, all information is stored in transInfo
|
||||
* @throws IOException
|
||||
* @throws IOException
|
||||
*/
|
||||
private synchronized void processObjectTransaction() throws IOException
|
||||
{
|
||||
@ -421,7 +452,7 @@ public class Telemetry {
|
||||
|
||||
/**
|
||||
* Process the event received from an object
|
||||
* @throws IOException
|
||||
* @throws IOException
|
||||
*/
|
||||
private synchronized void processObjectUpdates(UAVObject obj, int event, boolean allInstances, boolean priority) throws IOException
|
||||
{
|
||||
@ -450,7 +481,7 @@ public class Telemetry {
|
||||
else
|
||||
{
|
||||
if ( objQueue.size() < MAX_QUEUE_SIZE )
|
||||
{
|
||||
{
|
||||
objQueue.add(objInfo);
|
||||
}
|
||||
else
|
||||
@ -469,7 +500,7 @@ public class Telemetry {
|
||||
|
||||
/**
|
||||
* Process events from the object queue
|
||||
* @throws IOException
|
||||
* @throws IOException
|
||||
*/
|
||||
private synchronized void processObjectQueue() throws IOException
|
||||
{
|
||||
@ -536,7 +567,7 @@ public class Telemetry {
|
||||
}
|
||||
|
||||
// If this is a metaobject then make necessary telemetry updates
|
||||
if (objInfo.obj.isMetadata())
|
||||
if (objInfo.obj.isMetadata())
|
||||
{
|
||||
UAVMetaObject metaobj = (UAVMetaObject) objInfo.obj;
|
||||
updateObject( metaobj.getParentObject() );
|
||||
@ -554,11 +585,11 @@ public class Telemetry {
|
||||
/**
|
||||
* Check is any objects are pending for periodic updates
|
||||
* TODO: Clean-up
|
||||
* @throws IOException
|
||||
* @throws IOException
|
||||
*/
|
||||
private synchronized void processPeriodicUpdates() throws IOException
|
||||
{
|
||||
|
||||
|
||||
if (DEBUG) Log.d(TAG, "processPeriodicUpdates()");
|
||||
// Stop timer
|
||||
updateTimer.cancel();
|
||||
@ -669,7 +700,7 @@ public class Telemetry {
|
||||
{
|
||||
registerObject(obj);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Stop all the telemetry timers
|
||||
*/
|
||||
@ -692,24 +723,24 @@ public class Telemetry {
|
||||
/**
|
||||
* Private variables
|
||||
*/
|
||||
private UAVObjectManager objMngr;
|
||||
private UAVTalk utalk;
|
||||
private final UAVObjectManager objMngr;
|
||||
private final UAVTalk utalk;
|
||||
private UAVObject gcsStatsObj;
|
||||
private List<ObjectTimeInfo> objList = new ArrayList<ObjectTimeInfo>();
|
||||
private Queue<ObjectQueueInfo> objQueue = new LinkedList<ObjectQueueInfo>();
|
||||
private Queue<ObjectQueueInfo> objPriorityQueue = new LinkedList<ObjectQueueInfo>();
|
||||
private ObjectTransactionInfo transInfo = new ObjectTransactionInfo();
|
||||
private final List<ObjectTimeInfo> objList = new ArrayList<ObjectTimeInfo>();
|
||||
private final Queue<ObjectQueueInfo> objQueue = new LinkedList<ObjectQueueInfo>();
|
||||
private final Queue<ObjectQueueInfo> objPriorityQueue = new LinkedList<ObjectQueueInfo>();
|
||||
private final ObjectTransactionInfo transInfo = new ObjectTransactionInfo();
|
||||
private boolean transPending;
|
||||
|
||||
|
||||
private Timer updateTimer;
|
||||
private TimerTask updateTimerTask;
|
||||
private Timer transTimer;
|
||||
private TimerTask transTimerTask;
|
||||
|
||||
|
||||
private int timeToNextUpdateMs;
|
||||
private int txErrors;
|
||||
private int txRetries;
|
||||
|
||||
|
||||
/**
|
||||
* Private constants
|
||||
*/
|
||||
@ -718,7 +749,7 @@ public class Telemetry {
|
||||
private static final int MAX_UPDATE_PERIOD_MS = 1000;
|
||||
private static final int MIN_UPDATE_PERIOD_MS = 1;
|
||||
private static final int MAX_QUEUE_SIZE = 20;
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,3 +1,28 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file TelemetryMonitor.java
|
||||
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012.
|
||||
* @brief High level monitoring of telemetry to handle connection and
|
||||
* disconnection and then signal the rest of the application.
|
||||
* This also makes sure to fetch all objects on initial connection.
|
||||
* @see The GNU Public License (GPL) Version 3
|
||||
*
|
||||
*****************************************************************************/
|
||||
/*
|
||||
* 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 java.io.IOException;
|
||||
@ -22,22 +47,22 @@ public class TelemetryMonitor extends Observable{
|
||||
static final int STATS_CONNECT_PERIOD_MS = 1000;
|
||||
static final int CONNECTION_TIMEOUT_MS = 8000;
|
||||
|
||||
private UAVObjectManager objMngr;
|
||||
private Telemetry tel;
|
||||
private final UAVObjectManager objMngr;
|
||||
private final Telemetry tel;
|
||||
// private UAVObject objPending;
|
||||
private UAVObject gcsStatsObj;
|
||||
private UAVObject flightStatsObj;
|
||||
private Timer periodicTask;
|
||||
private int currentPeriod;
|
||||
private long lastUpdateTime;
|
||||
private List<UAVObject> queue;
|
||||
|
||||
private final List<UAVObject> queue;
|
||||
|
||||
private boolean connected = false;
|
||||
private boolean objects_updated = false;
|
||||
|
||||
|
||||
public boolean getConnected() { return connected; };
|
||||
public boolean getObjectsUpdated() { return objects_updated; };
|
||||
|
||||
|
||||
public TelemetryMonitor(UAVObjectManager objMngr, Telemetry tel)
|
||||
{
|
||||
this.objMngr = objMngr;
|
||||
@ -50,6 +75,7 @@ public class TelemetryMonitor extends Observable{
|
||||
flightStatsObj = objMngr.getObject("FlightTelemetryStats");
|
||||
|
||||
flightStatsObj.addUpdatedObserver(new Observer() {
|
||||
@Override
|
||||
public void update(Observable observable, Object data) {
|
||||
try {
|
||||
flightStatsUpdated((UAVObject) data);
|
||||
@ -59,7 +85,7 @@ public class TelemetryMonitor extends Observable{
|
||||
// or fix the stream?
|
||||
flightStatsObj.removeUpdatedObserver(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Start update timer
|
||||
@ -68,17 +94,17 @@ public class TelemetryMonitor extends Observable{
|
||||
|
||||
/**
|
||||
* Initiate object retrieval, initialize queue with objects to be retrieved.
|
||||
* @throws IOException
|
||||
* @throws IOException
|
||||
*/
|
||||
public synchronized void startRetrievingObjects() throws IOException
|
||||
{
|
||||
if (DEBUG) Log.d(TAG, "Start retrieving objects");
|
||||
|
||||
|
||||
// Clear object queue
|
||||
queue.clear();
|
||||
// Get all objects, add metaobjects, settings and data objects with OnChange update mode to the queue
|
||||
List< List<UAVObject> > objs = objMngr.getObjects();
|
||||
|
||||
|
||||
ListIterator<List<UAVObject>> objListIterator = objs.listIterator();
|
||||
while( objListIterator.hasNext() )
|
||||
{
|
||||
@ -121,7 +147,7 @@ public class TelemetryMonitor extends Observable{
|
||||
|
||||
/**
|
||||
* Retrieve the next object in the queue
|
||||
* @throws IOException
|
||||
* @throws IOException
|
||||
*/
|
||||
public synchronized void retrieveNextObject() throws IOException
|
||||
{
|
||||
@ -136,16 +162,17 @@ public class TelemetryMonitor extends Observable{
|
||||
}
|
||||
// Get next object from the queue
|
||||
UAVObject obj = queue.remove(0);
|
||||
|
||||
|
||||
if(obj == null) {
|
||||
throw new Error("Got null object forom transaction queue");
|
||||
}
|
||||
|
||||
|
||||
if (DEBUG) Log.d(TAG, "Retrieving object: " + obj.getName()) ;
|
||||
// Connect to object
|
||||
|
||||
|
||||
// TODO: Does this need to stay here permanently? This appears to be used for setup mainly
|
||||
obj.addTransactionCompleted(new Observer() {
|
||||
@Override
|
||||
public void update(Observable observable, Object data) {
|
||||
UAVObject.TransactionResult result = (UAVObject.TransactionResult) data;
|
||||
if (DEBUG) Log.d(TAG,"Got transaction completed event from " + result.obj.getName() + " status: " + result.success);
|
||||
@ -155,7 +182,7 @@ public class TelemetryMonitor extends Observable{
|
||||
// When the telemetry stream is broken disconnect these updates
|
||||
observable.deleteObserver(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Request update
|
||||
@ -165,7 +192,7 @@ public class TelemetryMonitor extends Observable{
|
||||
|
||||
/**
|
||||
* Called by the retrieved object when a transaction is completed.
|
||||
* @throws IOException
|
||||
* @throws IOException
|
||||
*/
|
||||
public synchronized void transactionCompleted(UAVObject obj, boolean success) throws IOException
|
||||
{
|
||||
@ -175,12 +202,12 @@ public class TelemetryMonitor extends Observable{
|
||||
// TODO: Need to be able to disconnect signals
|
||||
//obj->disconnect(this);
|
||||
// objPending = null;
|
||||
|
||||
|
||||
if(!success) {
|
||||
//Log.e(TAG, "Transaction failed: " + obj.getName() + " sending again.");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// Process next object if telemetry is still available
|
||||
if ( ((String) gcsStatsObj.getField("Status").getValue()).compareTo("Connected") == 0 )
|
||||
{
|
||||
@ -194,7 +221,7 @@ public class TelemetryMonitor extends Observable{
|
||||
|
||||
/**
|
||||
* Called each time the flight stats object is updated by the autopilot
|
||||
* @throws IOException
|
||||
* @throws IOException
|
||||
*/
|
||||
public synchronized void flightStatsUpdated(UAVObject obj) throws IOException
|
||||
{
|
||||
@ -213,7 +240,7 @@ public class TelemetryMonitor extends Observable{
|
||||
private long lastStatsTime;
|
||||
/**
|
||||
* Called periodically to update the statistics and connection status.
|
||||
* @throws IOException
|
||||
* @throws IOException
|
||||
*/
|
||||
public synchronized void processStatsUpdates() throws IOException
|
||||
{
|
||||
@ -222,14 +249,14 @@ public class TelemetryMonitor extends Observable{
|
||||
Telemetry.TelemetryStats telStats = tel.getStats();
|
||||
|
||||
if (DEBUG) Log.d(TAG, "processStatsUpdates() - stats reset");
|
||||
|
||||
|
||||
// Need to compute time because this update is not regular enough
|
||||
float dT = (float) (System.currentTimeMillis() - lastStatsTime) / 1000.0f;
|
||||
float dT = (System.currentTimeMillis() - lastStatsTime) / 1000.0f;
|
||||
lastStatsTime = System.currentTimeMillis();
|
||||
|
||||
// Update stats object
|
||||
gcsStatsObj.getField("RxDataRate").setDouble( (float)telStats.rxBytes / dT );
|
||||
gcsStatsObj.getField("TxDataRate").setDouble( (float)telStats.txBytes / dT );
|
||||
// Update stats object
|
||||
gcsStatsObj.getField("RxDataRate").setDouble( telStats.rxBytes / dT );
|
||||
gcsStatsObj.getField("TxDataRate").setDouble( telStats.txBytes / dT );
|
||||
UAVObjectField field = gcsStatsObj.getField("RxFailures");
|
||||
field.setDouble(field.getDouble() + telStats.rxErrors);
|
||||
field = gcsStatsObj.getField("TxFailures");
|
||||
@ -240,7 +267,7 @@ public class TelemetryMonitor extends Observable{
|
||||
tel.resetStats();
|
||||
|
||||
if (DEBUG) Log.d(TAG, "processStatsUpdates() - stats updated");
|
||||
|
||||
|
||||
// Check for a connection timeout
|
||||
boolean connectionTimeout;
|
||||
if ( telStats.rxObjects > 0 )
|
||||
@ -266,7 +293,7 @@ public class TelemetryMonitor extends Observable{
|
||||
}
|
||||
UAVObjectField statusField = gcsStatsObj.getField("Status");
|
||||
String oldStatus = new String((String) statusField.getValue());
|
||||
|
||||
|
||||
if (DEBUG) Log.d(TAG,"GCS: " + statusField.getValue() + " Flight: " + flightStatsObj.getField("Status").getValue());
|
||||
|
||||
if ( oldStatus.compareTo("Disconnected") == 0 )
|
||||
@ -294,11 +321,11 @@ public class TelemetryMonitor extends Observable{
|
||||
|
||||
// Force telemetry update if not yet connected
|
||||
boolean gcsStatusChanged = !oldStatus.equals(statusField.getValue());
|
||||
|
||||
|
||||
boolean gcsConnected = statusField.getValue().equals("Connected");
|
||||
boolean gcsDisconnected = statusField.getValue().equals("Disconnected");
|
||||
boolean flightConnected = flightStatsObj.getField("Status").equals("Connected");
|
||||
|
||||
|
||||
if ( !gcsConnected || !flightConnected )
|
||||
{
|
||||
if (DEBUG) Log.d(TAG,"Sending gcs status");
|
||||
@ -323,7 +350,7 @@ public class TelemetryMonitor extends Observable{
|
||||
objects_updated = false;
|
||||
setChanged();
|
||||
}
|
||||
|
||||
|
||||
if (DEBUG) Log.d(TAG, "processStatsUpdates() - before notify");
|
||||
notifyObservers();
|
||||
if (DEBUG) Log.d(TAG, "processStatsUpdates() - after notify");
|
||||
@ -344,11 +371,11 @@ public class TelemetryMonitor extends Observable{
|
||||
} catch (IOException e) {
|
||||
// Once the stream has died stop trying to process these updates
|
||||
periodicTask.cancel();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}, currentPeriod, currentPeriod);
|
||||
}
|
||||
|
||||
|
||||
public void stopMonitor()
|
||||
{
|
||||
periodicTask.cancel();
|
||||
|
@ -1,11 +1,34 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file UAVDataObject.java
|
||||
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012.
|
||||
* @brief The base object for all UAVO data.
|
||||
* @see The GNU Public License (GPL) Version 3
|
||||
*
|
||||
*****************************************************************************/
|
||||
/*
|
||||
* 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;
|
||||
|
||||
public abstract class UAVDataObject extends UAVObject {
|
||||
|
||||
|
||||
/**
|
||||
* @brief Constructor for UAVDataObject
|
||||
* @param objID the object id to be created
|
||||
* @param isSingleInst
|
||||
* @param isSingleInst
|
||||
* @param isSet
|
||||
* @param name
|
||||
*/
|
||||
@ -24,6 +47,7 @@ public abstract class UAVDataObject extends UAVObject {
|
||||
super.initialize(instID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isMetadata() { return false; };
|
||||
/**
|
||||
* Assign a metaobject
|
||||
@ -45,6 +69,7 @@ public abstract class UAVDataObject extends UAVObject {
|
||||
/**
|
||||
* Set the object's metadata
|
||||
*/
|
||||
@Override
|
||||
public void setMetadata(Metadata mdata)
|
||||
{
|
||||
if ( mobj != null )
|
||||
@ -56,6 +81,7 @@ public abstract class UAVDataObject extends UAVObject {
|
||||
/**
|
||||
* Get the object's metadata
|
||||
*/
|
||||
@Override
|
||||
public Metadata getMetadata()
|
||||
{
|
||||
if ( mobj != null)
|
||||
@ -75,13 +101,13 @@ public abstract class UAVDataObject extends UAVObject {
|
||||
{
|
||||
return mobj;
|
||||
}
|
||||
|
||||
|
||||
// TODO: Make abstract
|
||||
public UAVDataObject clone(long instID) {
|
||||
return (UAVDataObject) super.clone();
|
||||
}
|
||||
|
||||
|
||||
private UAVMetaObject mobj;
|
||||
private boolean isSet;
|
||||
private final boolean isSet;
|
||||
|
||||
}
|
||||
|
@ -1,3 +1,26 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file UAVMetaObject.java
|
||||
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012.
|
||||
* @brief Base object for all UAVO meta data
|
||||
* @see The GNU Public License (GPL) Version 3
|
||||
*
|
||||
*****************************************************************************/
|
||||
/*
|
||||
* 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 java.nio.ByteBuffer;
|
||||
@ -10,7 +33,7 @@ public class UAVMetaObject extends UAVObject {
|
||||
public UAVMetaObject(long objID, String name, UAVDataObject parent) throws Exception {
|
||||
super(objID, true, name);
|
||||
this.parent = parent;
|
||||
|
||||
|
||||
ownMetadata = new Metadata();
|
||||
|
||||
ownMetadata.flags = 0; // TODO: Fix flags
|
||||
@ -27,7 +50,7 @@ public class UAVMetaObject extends UAVObject {
|
||||
modesBitField.add("FlightUpdateOnChange");
|
||||
modesBitField.add("GCSUpdatePeriodic");
|
||||
modesBitField.add("GCSUpdateOnChange");
|
||||
|
||||
|
||||
List<UAVObjectField> fields = new ArrayList<UAVObjectField>();
|
||||
fields.add( new UAVObjectField("Modes", "", UAVObjectField.FieldType.BITFIELD, 1, modesBitField) );
|
||||
fields.add( new UAVObjectField("Flight Telemetry Update Period", "ms", UAVObjectField.FieldType.UINT16, 1, null) );
|
||||
@ -47,12 +70,12 @@ public class UAVMetaObject extends UAVObject {
|
||||
initializeFields(fields, ByteBuffer.allocate(numBytes), numBytes);
|
||||
|
||||
// Setup metadata of parent
|
||||
parentMetadata = parent.getDefaultMetadata();
|
||||
parentMetadata = parent.getDefaultMetadata();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isMetadata() {
|
||||
return true;
|
||||
public boolean isMetadata() {
|
||||
return true;
|
||||
};
|
||||
|
||||
/**
|
||||
@ -67,6 +90,7 @@ public class UAVMetaObject extends UAVObject {
|
||||
* Set the metadata of the metaobject, this function will
|
||||
* do nothing since metaobjects have read-only metadata.
|
||||
*/
|
||||
@Override
|
||||
public void setMetadata(Metadata mdata)
|
||||
{
|
||||
return; // can not update metaobject's metadata
|
||||
@ -75,6 +99,7 @@ public class UAVMetaObject extends UAVObject {
|
||||
/**
|
||||
* Get the metadata of the metaobject
|
||||
*/
|
||||
@Override
|
||||
public Metadata getMetadata()
|
||||
{
|
||||
return ownMetadata;
|
||||
@ -83,6 +108,7 @@ public class UAVMetaObject extends UAVObject {
|
||||
/**
|
||||
* Get the default metadata
|
||||
*/
|
||||
@Override
|
||||
public Metadata getDefaultMetadata()
|
||||
{
|
||||
return ownMetadata;
|
||||
@ -110,8 +136,8 @@ public class UAVMetaObject extends UAVObject {
|
||||
}
|
||||
|
||||
|
||||
private UAVObject parent;
|
||||
private Metadata ownMetadata;
|
||||
private final UAVObject parent;
|
||||
private final Metadata ownMetadata;
|
||||
private Metadata parentMetadata;
|
||||
|
||||
|
||||
|
@ -1,17 +1,40 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file UAVObject.java
|
||||
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012.
|
||||
* @brief Base object for UAVDataObject and UAVMetaObject.
|
||||
* @see The GNU Public License (GPL) Version 3
|
||||
*
|
||||
*****************************************************************************/
|
||||
/*
|
||||
* 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 java.nio.ByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.Observer;
|
||||
import java.util.Observable;
|
||||
import java.util.Observer;
|
||||
|
||||
public abstract class UAVObject {
|
||||
|
||||
public class CallbackListener extends Observable {
|
||||
private UAVObject parent;
|
||||
|
||||
|
||||
public class CallbackListener extends Observable {
|
||||
private final UAVObject parent;
|
||||
|
||||
public CallbackListener(UAVObject parent) {
|
||||
this.parent = parent;
|
||||
}
|
||||
@ -29,7 +52,7 @@ public abstract class UAVObject {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class TransactionResult {
|
||||
public UAVObject obj;
|
||||
public boolean success;
|
||||
@ -38,8 +61,8 @@ public abstract class UAVObject {
|
||||
this.success = success;
|
||||
}
|
||||
}
|
||||
|
||||
private CallbackListener transactionCompletedListeners = new CallbackListener(this);
|
||||
|
||||
private final CallbackListener transactionCompletedListeners = new CallbackListener(this);
|
||||
public void addTransactionCompleted(Observer o) {
|
||||
synchronized(transactionCompletedListeners) {
|
||||
transactionCompletedListeners.addObserver(o);
|
||||
@ -55,8 +78,8 @@ public abstract class UAVObject {
|
||||
transactionCompletedListeners.event(new TransactionResult(this,status));
|
||||
}
|
||||
}
|
||||
|
||||
private CallbackListener updatedListeners = new CallbackListener(this);
|
||||
|
||||
private final CallbackListener updatedListeners = new CallbackListener(this);
|
||||
public void removeUpdatedObserver(Observer o) {
|
||||
synchronized(updatedListeners) {
|
||||
updatedListeners.deleteObserver(o);
|
||||
@ -75,8 +98,8 @@ public abstract class UAVObject {
|
||||
updatedManual();
|
||||
}
|
||||
public void updated() { updated(true); };
|
||||
|
||||
private CallbackListener unpackedListeners = new CallbackListener(this);
|
||||
|
||||
private final CallbackListener unpackedListeners = new CallbackListener(this);
|
||||
public void addUnpackedObserver(Observer o) {
|
||||
synchronized(unpackedListeners) {
|
||||
unpackedListeners.addObserver(o);
|
||||
@ -88,7 +111,7 @@ public abstract class UAVObject {
|
||||
}
|
||||
}
|
||||
|
||||
private CallbackListener updatedAutoListeners = new CallbackListener(this);
|
||||
private final CallbackListener updatedAutoListeners = new CallbackListener(this);
|
||||
public void addUpdatedAutoObserver(Observer o) {
|
||||
synchronized(updatedAutoListeners) {
|
||||
updatedAutoListeners.addObserver(o);
|
||||
@ -100,7 +123,7 @@ public abstract class UAVObject {
|
||||
}
|
||||
}
|
||||
|
||||
private CallbackListener updatedManualListeners = new CallbackListener(this);
|
||||
private final CallbackListener updatedManualListeners = new CallbackListener(this);
|
||||
public void addUpdatedManualObserver(Observer o) {
|
||||
synchronized(updatedManualListeners) {
|
||||
updatedManualListeners.addObserver(o);
|
||||
@ -112,7 +135,7 @@ public abstract class UAVObject {
|
||||
}
|
||||
}
|
||||
|
||||
private CallbackListener updateRequestedListeners = new CallbackListener(this);
|
||||
private final CallbackListener updateRequestedListeners = new CallbackListener(this);
|
||||
public void addUpdateRequestedObserver(Observer o) {
|
||||
synchronized(updateRequestedListeners) {
|
||||
updateRequestedListeners.addObserver(o);
|
||||
@ -125,7 +148,7 @@ public abstract class UAVObject {
|
||||
}
|
||||
|
||||
public abstract boolean isMetadata();
|
||||
|
||||
|
||||
/**
|
||||
* Object update mode
|
||||
*/
|
||||
@ -172,10 +195,10 @@ public abstract class UAVObject {
|
||||
|
||||
/** Update period used by the telemetry module (only if telemetry mode is PERIODIC) */
|
||||
public int flightTelemetryUpdatePeriod;
|
||||
|
||||
|
||||
/** Update period used by the GCS (only if telemetry mode is PERIODIC) */
|
||||
public int gcsTelemetryUpdatePeriod;
|
||||
|
||||
|
||||
/** Update period used by the GCS (only if telemetry mode is PERIODIC) */
|
||||
public int loggingUpdatePeriod;
|
||||
/**
|
||||
@ -194,7 +217,7 @@ public abstract class UAVObject {
|
||||
private void SET_BITS(int shift, int value, int mask) {
|
||||
this.flags = (this.flags & ~(mask << shift)) | (value << shift);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the UAVObject metadata access member
|
||||
* \return the access type
|
||||
@ -219,7 +242,7 @@ public abstract class UAVObject {
|
||||
* \return the GCS access type
|
||||
*/
|
||||
public AccessMode GetGcsAccess()
|
||||
{
|
||||
{
|
||||
return AccessModeEnum((this.flags >> UAVOBJ_GCS_ACCESS_SHIFT) & 1);
|
||||
}
|
||||
|
||||
@ -382,7 +405,7 @@ public abstract class UAVObject {
|
||||
|
||||
/**
|
||||
* Initialize objects' data fields
|
||||
*
|
||||
*
|
||||
* @param fields
|
||||
* List of fields held by the object
|
||||
* @param data
|
||||
@ -434,7 +457,7 @@ public abstract class UAVObject {
|
||||
|
||||
/**
|
||||
* Get the description of the object
|
||||
*
|
||||
*
|
||||
* @return The description of the object
|
||||
*/
|
||||
public String getDescription() {
|
||||
@ -443,7 +466,7 @@ public abstract class UAVObject {
|
||||
|
||||
/**
|
||||
* Set the description of the object
|
||||
*
|
||||
*
|
||||
* @param The
|
||||
* description of the object
|
||||
* @return
|
||||
@ -525,7 +548,7 @@ public abstract class UAVObject {
|
||||
|
||||
/**
|
||||
* Get a specific field
|
||||
*
|
||||
*
|
||||
* @throws Exception
|
||||
* @returns The field or NULL if not found
|
||||
*/
|
||||
@ -543,7 +566,7 @@ public abstract class UAVObject {
|
||||
|
||||
/**
|
||||
* Pack the object data into a byte array
|
||||
*
|
||||
*
|
||||
* @param dataOut
|
||||
* ByteBuffer to receive the data.
|
||||
* @throws Exception
|
||||
@ -565,7 +588,7 @@ public abstract class UAVObject {
|
||||
|
||||
/**
|
||||
* Unpack the object data from a byte array
|
||||
*
|
||||
*
|
||||
* @param dataIn
|
||||
* The ByteBuffer to pull data from
|
||||
* @throws Exception
|
||||
@ -582,11 +605,11 @@ public abstract class UAVObject {
|
||||
UAVObjectField field = li.next();
|
||||
numBytes += field.unpack(dataIn);
|
||||
}
|
||||
|
||||
|
||||
// Trigger all the listeners for the unpack event
|
||||
unpacked();
|
||||
updated(false);
|
||||
|
||||
|
||||
return numBytes;
|
||||
}
|
||||
|
||||
@ -774,6 +797,7 @@ public abstract class UAVObject {
|
||||
/**
|
||||
* Java specific functions
|
||||
*/
|
||||
@Override
|
||||
public synchronized UAVObject clone() {
|
||||
UAVObject newObj = clone();
|
||||
List<UAVObjectField> newFields = new ArrayList<UAVObjectField>();
|
||||
|
@ -1,3 +1,26 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file Telemetry.java
|
||||
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012.
|
||||
* @brief Implementation of all the UAVObjectFields.
|
||||
* @see The GNU Public License (GPL) Version 3
|
||||
*
|
||||
*****************************************************************************/
|
||||
/*
|
||||
* 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 java.nio.ByteBuffer;
|
||||
@ -6,7 +29,7 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class UAVObjectField {
|
||||
|
||||
|
||||
public enum FieldType { INT8, INT16, INT32, UINT8, UINT16, UINT32, FLOAT32, ENUM, BITFIELD, STRING };
|
||||
|
||||
public UAVObjectField(String name, String units, FieldType type, int numElements, List<String> options) {
|
||||
@ -17,26 +40,26 @@ public class UAVObjectField {
|
||||
elementNames.add(String.valueOf(n));
|
||||
}
|
||||
// Initialize
|
||||
constructorInitialize(name, units, type, elementNames, options);
|
||||
constructorInitialize(name, units, type, elementNames, options);
|
||||
}
|
||||
|
||||
|
||||
public UAVObjectField(String name, String units, FieldType type, List<String> elementNames, List<String> options) {
|
||||
constructorInitialize(name, units, type, elementNames, options);
|
||||
}
|
||||
|
||||
|
||||
public void initialize(UAVObject obj){
|
||||
this.obj = obj;
|
||||
//clear();
|
||||
//clear();
|
||||
}
|
||||
|
||||
|
||||
public UAVObject getObject() {
|
||||
return obj;
|
||||
}
|
||||
|
||||
|
||||
public FieldType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
|
||||
public String getTypeAsString() {
|
||||
switch (type)
|
||||
{
|
||||
@ -62,31 +85,31 @@ public class UAVObjectField {
|
||||
return "string";
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
public String getUnits() {
|
||||
return units;
|
||||
}
|
||||
|
||||
|
||||
public int getNumElements() {
|
||||
return numElements;
|
||||
}
|
||||
|
||||
|
||||
public List<String> getElementNames() {
|
||||
return elementNames;
|
||||
return elementNames;
|
||||
}
|
||||
|
||||
|
||||
public List<String> getOptions() {
|
||||
return options;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This function copies this field from the internal storage of the parent object
|
||||
* This function copies this field from the internal storage of the parent object
|
||||
* to a new ByteBuffer for UAVTalk. It also converts from the java standard (big endian)
|
||||
* to the arm/uavtalk standard (little endian)
|
||||
* @param dataOut
|
||||
@ -98,7 +121,7 @@ public class UAVObjectField {
|
||||
dataOut.order(ByteOrder.LITTLE_ENDIAN);
|
||||
switch (type)
|
||||
{
|
||||
case INT8:
|
||||
case INT8:
|
||||
for (int index = 0; index < numElements; ++index) {
|
||||
Integer val = (Integer) getValue(index);
|
||||
dataOut.put(val.byteValue());
|
||||
@ -116,7 +139,7 @@ public class UAVObjectField {
|
||||
dataOut.putInt(val);
|
||||
}
|
||||
break;
|
||||
case UINT8:
|
||||
case UINT8:
|
||||
// TODO: Deal properly with unsigned
|
||||
for (int index = 0; index < numElements; ++index) {
|
||||
Integer val = (Integer) getValue(index);
|
||||
@ -144,7 +167,7 @@ public class UAVObjectField {
|
||||
case ENUM:
|
||||
List<Byte> l = (List<Byte>) data;
|
||||
for (int index = 0; index < numElements; ++index)
|
||||
dataOut.put((Byte) l.get(index));
|
||||
dataOut.put(l.get(index));
|
||||
break;
|
||||
case BITFIELD:
|
||||
for (int index = 0; index < numElements; ++index) {
|
||||
@ -156,9 +179,9 @@ public class UAVObjectField {
|
||||
throw new Error("Strings not yet implemented");
|
||||
}
|
||||
// Done
|
||||
return getNumBytes();
|
||||
return getNumBytes();
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public synchronized int unpack(ByteBuffer dataIn) {
|
||||
// Unpack each element from input buffer
|
||||
@ -196,7 +219,7 @@ public class UAVObjectField {
|
||||
{
|
||||
List<Short> l = (List<Short>) this.data;
|
||||
for (int index = 0 ; index < numElements; ++index) {
|
||||
int signedval = (int) dataIn.get(); // this sign extends it
|
||||
int signedval = dataIn.get(); // this sign extends it
|
||||
int unsignedval = signedval & 0xff; // drop sign extension
|
||||
l.set(index, (short) unsignedval);
|
||||
}
|
||||
@ -206,7 +229,7 @@ public class UAVObjectField {
|
||||
{
|
||||
List<Integer> l = (List<Integer>) this.data;
|
||||
for (int index = 0 ; index < numElements; ++index) {
|
||||
int signedval = (int) dataIn.getShort(); // this sign extends it
|
||||
int signedval = dataIn.getShort(); // this sign extends it
|
||||
int unsignedval = signedval & 0xffff; // drop sign extension
|
||||
l.set(index, unsignedval);
|
||||
}
|
||||
@ -216,7 +239,7 @@ public class UAVObjectField {
|
||||
{
|
||||
List<Long> l = (List<Long>) this.data;
|
||||
for (int index = 0 ; index < numElements; ++index) {
|
||||
long signedval = (long) dataIn.getInt(); // this sign extends it
|
||||
long signedval = dataIn.getInt(); // this sign extends it
|
||||
long unsignedval = signedval & 0xffffffffL; // drop sign extension
|
||||
l.set(index, unsignedval);
|
||||
}
|
||||
@ -235,7 +258,7 @@ public class UAVObjectField {
|
||||
{
|
||||
List<Short> l = (List<Short>) this.data;
|
||||
for (int index = 0 ; index < numElements; ++index) {
|
||||
int signedval = (int) dataIn.get(); // this sign extends it
|
||||
int signedval = dataIn.get(); // this sign extends it
|
||||
int unsignedval = signedval & 0xff; // drop sign extension
|
||||
l.set(index, (short) unsignedval);
|
||||
}
|
||||
@ -254,9 +277,9 @@ public class UAVObjectField {
|
||||
//throw new Exception("Strings not handled");
|
||||
}
|
||||
// Done
|
||||
return getNumBytes();
|
||||
return getNumBytes();
|
||||
}
|
||||
|
||||
|
||||
public Object getValue() { return getValue(0); };
|
||||
@SuppressWarnings("unchecked")
|
||||
public synchronized Object getValue(int index) {
|
||||
@ -265,7 +288,7 @@ public class UAVObjectField {
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case INT8:
|
||||
@ -287,7 +310,7 @@ public class UAVObjectField {
|
||||
List<Byte> l = (List<Byte>) data;
|
||||
Byte val = l.get(index);
|
||||
|
||||
//if(val >= options.size() || val < 0)
|
||||
//if(val >= options.size() || val < 0)
|
||||
// throw new Exception("Invalid value for" + name);
|
||||
|
||||
return options.get(val);
|
||||
@ -301,10 +324,10 @@ public class UAVObjectField {
|
||||
}
|
||||
}
|
||||
// If this point is reached then we got an invalid type
|
||||
return null;
|
||||
return null;
|
||||
}
|
||||
|
||||
public void setValue(Object data) { setValue(data,0); }
|
||||
|
||||
public void setValue(Object data) { setValue(data,0); }
|
||||
@SuppressWarnings("unchecked")
|
||||
public synchronized void setValue(Object data, int index) {
|
||||
// Check that index is not out of bounds
|
||||
@ -361,15 +384,15 @@ public class UAVObjectField {
|
||||
break;
|
||||
}
|
||||
case ENUM:
|
||||
{
|
||||
{
|
||||
byte val;
|
||||
try {
|
||||
// Test if numeric constant passed in
|
||||
val = ((Number) data).byteValue();
|
||||
} catch (Exception e) {
|
||||
val = (byte) options.indexOf((String) data);
|
||||
val = (byte) options.indexOf(data);
|
||||
}
|
||||
//if(val < 0) throw new Exception("Enumerated value not found");
|
||||
//if(val < 0) throw new Exception("Enumerated value not found");
|
||||
List<Byte> l = (List<Byte>) this.data;
|
||||
l.set(index, val);
|
||||
break;
|
||||
@ -380,7 +403,7 @@ public class UAVObjectField {
|
||||
l.set(index, bound(data).shortValue());
|
||||
break;
|
||||
}
|
||||
case STRING:
|
||||
case STRING:
|
||||
{
|
||||
//throw new Exception("Sorry I haven't implemented strings yet");
|
||||
}
|
||||
@ -388,7 +411,7 @@ public class UAVObjectField {
|
||||
//obj.updated();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public double getDouble() { return getDouble(0); };
|
||||
@SuppressWarnings("unchecked")
|
||||
public double getDouble(int index) {
|
||||
@ -400,24 +423,24 @@ public class UAVObjectField {
|
||||
}
|
||||
return ((Number) getValue(index)).doubleValue();
|
||||
}
|
||||
|
||||
|
||||
public void setDouble(double value) { setDouble(value, 0); };
|
||||
public void setDouble(double value, int index) {
|
||||
setValue(value, index);
|
||||
}
|
||||
|
||||
|
||||
public int getDataOffset() {
|
||||
return offset;
|
||||
return offset;
|
||||
}
|
||||
|
||||
|
||||
public int getNumBytes() {
|
||||
return numBytesPerElement * numElements;
|
||||
}
|
||||
|
||||
|
||||
public int getNumBytesElement() {
|
||||
return numBytesPerElement;
|
||||
}
|
||||
|
||||
|
||||
public boolean isNumeric() {
|
||||
switch (type)
|
||||
{
|
||||
@ -443,9 +466,9 @@ public class UAVObjectField {
|
||||
return false;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public boolean isText() {
|
||||
switch (type)
|
||||
{
|
||||
@ -471,9 +494,10 @@ public class UAVObjectField {
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String sout = new String();
|
||||
sout += name + ": ";
|
||||
@ -484,15 +508,15 @@ public class UAVObjectField {
|
||||
else
|
||||
sout += " ";
|
||||
}
|
||||
if (units.length() > 0)
|
||||
if (units.length() > 0)
|
||||
sout += " (" + units + ")\n";
|
||||
else
|
||||
sout += "\n";
|
||||
return sout;
|
||||
return sout;
|
||||
}
|
||||
|
||||
void fieldUpdated(UAVObjectField field) {
|
||||
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@ -555,7 +579,7 @@ public class UAVObjectField {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public synchronized void constructorInitialize(String name, String units, FieldType type, List<String> elementNames, List<String> options) {
|
||||
// Copy params
|
||||
this.name = name;
|
||||
@ -572,43 +596,43 @@ public class UAVObjectField {
|
||||
switch (type)
|
||||
{
|
||||
case INT8:
|
||||
data = (Object) new ArrayList<Byte>(this.numElements);
|
||||
data = new ArrayList<Byte>(this.numElements);
|
||||
numBytesPerElement = 1;
|
||||
break;
|
||||
case INT16:
|
||||
data = (Object) new ArrayList<Short>(this.numElements);
|
||||
data = new ArrayList<Short>(this.numElements);
|
||||
numBytesPerElement = 2;
|
||||
break;
|
||||
case INT32:
|
||||
data = (Object) new ArrayList<Integer>(this.numElements);
|
||||
data = new ArrayList<Integer>(this.numElements);
|
||||
numBytesPerElement = 4;
|
||||
break;
|
||||
case UINT8:
|
||||
data = (Object) new ArrayList<Short>(this.numElements);
|
||||
data = new ArrayList<Short>(this.numElements);
|
||||
numBytesPerElement = 1;
|
||||
break;
|
||||
case UINT16:
|
||||
data = (Object) new ArrayList<Integer>(this.numElements);
|
||||
data = new ArrayList<Integer>(this.numElements);
|
||||
numBytesPerElement = 2;
|
||||
break;
|
||||
case UINT32:
|
||||
data = (Object) new ArrayList<Long>(this.numElements);
|
||||
data = new ArrayList<Long>(this.numElements);
|
||||
numBytesPerElement = 4;
|
||||
break;
|
||||
case FLOAT32:
|
||||
data = (Object) new ArrayList<Float>(this.numElements);
|
||||
data = new ArrayList<Float>(this.numElements);
|
||||
numBytesPerElement = 4;
|
||||
break;
|
||||
case ENUM:
|
||||
data = (Object) new ArrayList<Byte>(this.numElements);
|
||||
data = new ArrayList<Byte>(this.numElements);
|
||||
numBytesPerElement = 1;
|
||||
break;
|
||||
case BITFIELD:
|
||||
data = (Object) new ArrayList<Short>(this.numElements);
|
||||
data = new ArrayList<Short>(this.numElements);
|
||||
numBytesPerElement = 1;
|
||||
break;
|
||||
case STRING:
|
||||
data = (Object) new ArrayList<String>(this.numElements);
|
||||
data = new ArrayList<String>(this.numElements);
|
||||
numBytesPerElement = 1;
|
||||
break;
|
||||
default:
|
||||
@ -616,7 +640,7 @@ public class UAVObjectField {
|
||||
}
|
||||
clear();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* For numerical types bounds the data appropriately
|
||||
* @param val Can be any object, for numerical tries to cast to Number
|
||||
@ -624,7 +648,7 @@ public class UAVObjectField {
|
||||
* @note This is mostly needed because java has no unsigned integer
|
||||
*/
|
||||
protected Long bound (Object val) {
|
||||
|
||||
|
||||
switch(type) {
|
||||
case ENUM:
|
||||
case STRING:
|
||||
@ -679,15 +703,15 @@ public class UAVObjectField {
|
||||
return (long) 255;
|
||||
return num;
|
||||
}
|
||||
|
||||
|
||||
return num;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public UAVObjectField clone()
|
||||
{
|
||||
UAVObjectField newField = new UAVObjectField(new String(name), new String(units), type,
|
||||
new ArrayList<String>(elementNames),
|
||||
UAVObjectField newField = new UAVObjectField(new String(name), new String(units), type,
|
||||
new ArrayList<String>(elementNames),
|
||||
new ArrayList<String>(options));
|
||||
newField.initialize(obj);
|
||||
newField.data = data;
|
||||
|
@ -1,3 +1,28 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file UAVObjectManager.java
|
||||
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012.
|
||||
* @brief Critical class. This is the data store for all UAVOs. Allows
|
||||
* other objects to access and change this data. Takes care of
|
||||
* propagating changes to the UAV.
|
||||
* @see The GNU Public License (GPL) Version 3
|
||||
*
|
||||
*****************************************************************************/
|
||||
/*
|
||||
* 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 java.util.ArrayList;
|
||||
@ -8,19 +33,19 @@ import java.util.Observer;
|
||||
|
||||
public class UAVObjectManager {
|
||||
|
||||
public class CallbackListener extends Observable {
|
||||
public class CallbackListener extends Observable {
|
||||
public void event (UAVObject obj) {
|
||||
setChanged();
|
||||
notifyObservers(obj);
|
||||
}
|
||||
}
|
||||
private CallbackListener newInstance = new CallbackListener();
|
||||
private final CallbackListener newInstance = new CallbackListener();
|
||||
public void addNewInstanceObserver(Observer o) {
|
||||
synchronized(newInstance) {
|
||||
newInstance.addObserver(o);
|
||||
}
|
||||
}
|
||||
private CallbackListener newObject = new CallbackListener();
|
||||
private final CallbackListener newObject = new CallbackListener();
|
||||
public void addNewObjectObserver(Observer o) {
|
||||
synchronized(newObject) {
|
||||
newObject.addObserver(o);
|
||||
@ -29,7 +54,7 @@ public class UAVObjectManager {
|
||||
private final int MAX_INSTANCES = 10;
|
||||
|
||||
// Use array list to store objects since rarely added or deleted
|
||||
private List<List<UAVObject>> objects = new ArrayList<List<UAVObject>>();
|
||||
private final List<List<UAVObject>> objects = new ArrayList<List<UAVObject>>();
|
||||
|
||||
public UAVObjectManager()
|
||||
{
|
||||
@ -41,7 +66,7 @@ public class UAVObjectManager {
|
||||
* A new instance can be created directly by instantiating a new object or by calling clone() of
|
||||
* an existing object. The object will be registered and will be properly initialized so that it can accept
|
||||
* updates.
|
||||
* @throws Exception
|
||||
* @throws Exception
|
||||
*/
|
||||
public synchronized boolean registerObject(UAVDataObject obj) throws Exception
|
||||
{
|
||||
@ -61,7 +86,7 @@ public class UAVObjectManager {
|
||||
}
|
||||
// The object type has alredy been added, so now we need to initialize the new instance with the appropriate id
|
||||
// There is a single metaobject for all object instances of this type, so no need to create a new one
|
||||
// Get object type metaobject from existing instance
|
||||
// Get object type metaobject from existing instance
|
||||
UAVDataObject refObj = (UAVDataObject) instList.get(0);
|
||||
if (refObj == null)
|
||||
{
|
||||
@ -74,7 +99,7 @@ public class UAVObjectManager {
|
||||
return false;
|
||||
}
|
||||
|
||||
// If InstID is zero then we find the next open instId and create it
|
||||
// If InstID is zero then we find the next open instId and create it
|
||||
if (obj.getInstID() == 0)
|
||||
{
|
||||
// Assign the next available ID and initialize the object instance the nadd
|
||||
@ -183,13 +208,13 @@ public class UAVObjectManager {
|
||||
// If no instances skip
|
||||
if(instList.size() == 0)
|
||||
continue;
|
||||
|
||||
|
||||
// If meta data skip
|
||||
if(instList.get(0).isMetadata())
|
||||
continue;
|
||||
|
||||
List<UAVDataObject> newInstList = new ArrayList<UAVDataObject>();
|
||||
ListIterator<UAVObject> instIt = instList.listIterator();
|
||||
|
||||
List<UAVDataObject> newInstList = new ArrayList<UAVDataObject>();
|
||||
ListIterator<UAVObject> instIt = instList.listIterator();
|
||||
while(instIt.hasNext()) {
|
||||
newInstList.add((UAVDataObject) instIt.next());
|
||||
}
|
||||
@ -216,13 +241,13 @@ public class UAVObjectManager {
|
||||
// If no instances skip
|
||||
if(instList.size() == 0)
|
||||
continue;
|
||||
|
||||
|
||||
// If meta data skip
|
||||
if(!instList.get(0).isMetadata())
|
||||
continue;
|
||||
|
||||
List<UAVMetaObject> newInstList = new ArrayList<UAVMetaObject>();
|
||||
ListIterator<UAVObject> instIt = instList.listIterator();
|
||||
|
||||
List<UAVMetaObject> newInstList = new ArrayList<UAVMetaObject>();
|
||||
ListIterator<UAVObject> instIt = instList.listIterator();
|
||||
while(instIt.hasNext()) {
|
||||
newInstList.add((UAVMetaObject) instIt.next());
|
||||
}
|
||||
@ -232,17 +257,17 @@ public class UAVObjectManager {
|
||||
return mObjects;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Returns a specific object by name only, returns instance ID zero
|
||||
* @param name The object name
|
||||
* @return The object or null if not found
|
||||
*/
|
||||
public UAVObject getObject(String name)
|
||||
public UAVObject getObject(String name)
|
||||
{
|
||||
return getObject(name, 0, 0);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get a specific object given its name and instance ID
|
||||
* @returns The object is found or NULL if not
|
||||
@ -257,11 +282,11 @@ public class UAVObjectManager {
|
||||
* @param objId the object id
|
||||
* @returns The object is found or NULL if not
|
||||
*/
|
||||
public UAVObject getObject(long objId)
|
||||
public UAVObject getObject(long objId)
|
||||
{
|
||||
return getObject(null, objId, 0);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get a specific object given its object and instance ID
|
||||
* @returns The object is found or NULL if not
|
||||
@ -293,7 +318,7 @@ public class UAVObjectManager {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -327,8 +352,8 @@ public class UAVObjectManager {
|
||||
return instList;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -1,3 +1,29 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file UAVTalk.java
|
||||
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012.
|
||||
* @brief The protocol layer implementation of UAVTalk. Serializes objects
|
||||
* for transmission (which is done in the object itself which is aware
|
||||
* of byte packing) wraps that in the UAVTalk packet. Parses UAVTalk
|
||||
* packets and updates the UAVObjectManager.
|
||||
* @see The GNU Public License (GPL) Version 3
|
||||
*
|
||||
*****************************************************************************/
|
||||
/*
|
||||
* 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 java.io.IOException;
|
||||
@ -17,7 +43,7 @@ public class UAVTalk extends Observable {
|
||||
public static boolean DEBUG = LOGLEVEL > 0;
|
||||
|
||||
private Thread inputProcessingThread = null;
|
||||
|
||||
|
||||
/**
|
||||
* A reference to the thread for processing the incoming stream. Currently this method is ONLY
|
||||
* used for unit testing
|
||||
@ -26,6 +52,7 @@ public class UAVTalk extends Observable {
|
||||
if (inputProcessingThread == null)
|
||||
|
||||
inputProcessingThread = new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
while(true) {
|
||||
try {
|
||||
@ -179,7 +206,7 @@ public class UAVTalk extends Observable {
|
||||
|
||||
/**
|
||||
* Process any data in the queue
|
||||
* @throws IOException
|
||||
* @throws IOException
|
||||
*/
|
||||
public boolean processInputStream() throws IOException {
|
||||
int val;
|
||||
@ -195,13 +222,13 @@ public class UAVTalk extends Observable {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Request an update for the specified object, on success the object data
|
||||
* would have been updated by the GCS. \param[in] obj Object to update
|
||||
* \param[in] allInstances If set true then all instances will be updated
|
||||
* \return Success (true), Failure (false)
|
||||
* @throws IOException
|
||||
* @throws IOException
|
||||
*/
|
||||
public boolean sendObjectRequest(UAVObject obj, boolean allInstances) throws IOException {
|
||||
// QMutexLocker locker(mutex);
|
||||
@ -213,7 +240,7 @@ public class UAVTalk extends Observable {
|
||||
* Object to send \param[in] acked Selects if an ack is required \param[in]
|
||||
* allInstances If set true then all instances will be updated \return
|
||||
* Success (true), Failure (false)
|
||||
* @throws IOException
|
||||
* @throws IOException
|
||||
*/
|
||||
public synchronized boolean sendObject(UAVObject obj, boolean acked,
|
||||
boolean allInstances) throws IOException {
|
||||
@ -237,7 +264,7 @@ public class UAVTalk extends Observable {
|
||||
* request object update TYPE_OBJ_ACK: send object with an ack \param[in]
|
||||
* allInstances If set true then all instances will be updated \return
|
||||
* Success (true), Failure (false)
|
||||
* @throws IOException
|
||||
* @throws IOException
|
||||
*/
|
||||
public boolean objectTransaction(UAVObject obj, int type,
|
||||
boolean allInstances) throws IOException {
|
||||
@ -260,7 +287,7 @@ public class UAVTalk extends Observable {
|
||||
/**
|
||||
* Process an byte from the telemetry stream. \param[in] rxbyte Received
|
||||
* byte \return Success (true), Failure (false)
|
||||
* @throws IOException
|
||||
* @throws IOException
|
||||
*/
|
||||
public synchronized boolean processInputByte(int rxbyte) throws IOException {
|
||||
assert (objMngr != null);
|
||||
@ -341,7 +368,7 @@ public class UAVTalk extends Observable {
|
||||
// Search for object, if not found reset state machine
|
||||
rxObjId = rxTmpBuffer.getInt(0);
|
||||
// Because java treats ints as only signed we need to do this manually
|
||||
if (rxObjId < 0)
|
||||
if (rxObjId < 0)
|
||||
rxObjId = 0x100000000l + rxObjId;
|
||||
{
|
||||
UAVObject rxObj = objMngr.getObject(rxObjId);
|
||||
@ -475,11 +502,11 @@ public class UAVTalk extends Observable {
|
||||
* received object \param[in] instId The instance ID of UAVOBJ_ALL_INSTANCES
|
||||
* for all instances. \param[in] data Data buffer \param[in] length Buffer
|
||||
* length \return Success (true), Failure (false)
|
||||
* @throws IOException
|
||||
* @throws IOException
|
||||
*/
|
||||
public boolean receiveObject(int type, long objId, long instId,
|
||||
ByteBuffer data) throws IOException {
|
||||
|
||||
|
||||
if (DEBUG) Log.d(TAG, "Received object ID: " + objId);
|
||||
assert (objMngr != null);
|
||||
|
||||
@ -622,12 +649,12 @@ public class UAVTalk extends Observable {
|
||||
}
|
||||
|
||||
/**
|
||||
* Send an object through the telemetry link.
|
||||
* Send an object through the telemetry link.
|
||||
* @param[in] obj Object to send
|
||||
* @param[in] type Transaction type
|
||||
* @param[in] allInstances True is all instances of the object are to be sent
|
||||
* @param[in] type Transaction type
|
||||
* @param[in] allInstances True is all instances of the object are to be sent
|
||||
* @return Success (true), Failure (false)
|
||||
* @throws IOException
|
||||
* @throws IOException
|
||||
*/
|
||||
public synchronized boolean transmitObject(UAVObject obj, int type, boolean allInstances) throws IOException {
|
||||
// If all instances are requested on a single instance object it is an
|
||||
@ -665,7 +692,7 @@ public class UAVTalk extends Observable {
|
||||
|
||||
/**
|
||||
* Send an object through the telemetry link.
|
||||
* @throws IOException
|
||||
* @throws IOException
|
||||
* @param[in] obj Object handle to send
|
||||
* @param[in] type Transaction type \return Success (true), Failure (false)
|
||||
*/
|
||||
@ -738,11 +765,11 @@ public class UAVTalk extends Observable {
|
||||
|
||||
/**
|
||||
* Update the crc value with new data.
|
||||
*
|
||||
*
|
||||
* Generated by pycrc v0.7.5, http://www.tty1.net/pycrc/ using the
|
||||
* configuration: Width = 8 Poly = 0x07 XorIn = 0x00 ReflectIn = False
|
||||
* XorOut = 0x00 ReflectOut = False Algorithm = table-driven
|
||||
*
|
||||
*
|
||||
* \param crc The current crc value. \param data Pointer to a buffer of \a
|
||||
* data_len bytes. \param length Number of bytes in the \a data buffer.
|
||||
* \return The updated crc value.
|
||||
@ -753,7 +780,7 @@ public class UAVTalk extends Observable {
|
||||
|
||||
int updateCRC(int crc, byte[] data, int length) {
|
||||
for (int i = 0; i < length; i++)
|
||||
crc = updateCRC(crc, (int) data[i]);
|
||||
crc = updateCRC(crc, data[i]);
|
||||
return crc;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user