2010-02-22 03:18:23 +01:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* @file telemetry.c
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
|
|
|
* @brief Telemetry module, handles telemetry and UAVObject updates
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
|
2010-03-06 07:18:04 +01:00
|
|
|
#include "openpilot.h"
|
2010-04-18 00:16:20 +02:00
|
|
|
#include "flighttelemetrystats.h"
|
2010-05-04 03:29:04 +02:00
|
|
|
#include "gcstelemetrystats.h"
|
2010-05-08 04:44:26 +02:00
|
|
|
#include "telemetrysettings.h"
|
2010-02-22 03:18:23 +01:00
|
|
|
|
|
|
|
// Private constants
|
|
|
|
#define MAX_QUEUE_SIZE 20
|
2010-04-15 04:15:46 +02:00
|
|
|
#define STACK_SIZE configMINIMAL_STACK_SIZE
|
2010-04-24 06:05:39 +02:00
|
|
|
#define TASK_PRIORITY_RX (tskIDLE_PRIORITY + 2)
|
|
|
|
#define TASK_PRIORITY_TX (tskIDLE_PRIORITY + 1)
|
|
|
|
#define TASK_PRIORITY_TXPRI (tskIDLE_PRIORITY + 2)
|
2010-04-15 04:15:46 +02:00
|
|
|
#define REQ_TIMEOUT_MS 250
|
2010-05-08 02:22:07 +02:00
|
|
|
#define MAX_RETRIES 2
|
2010-05-08 06:11:03 +02:00
|
|
|
#define STATS_UPDATE_PERIOD_MS 4000
|
|
|
|
#define CONNECTION_TIMEOUT_MS 8000
|
2010-02-27 20:57:45 +01:00
|
|
|
|
|
|
|
// Private types
|
|
|
|
|
2010-02-22 03:18:23 +01:00
|
|
|
// Private variables
|
2010-03-15 02:54:25 +01:00
|
|
|
static COMPortTypeDef telemetryPort;
|
2010-03-06 07:18:04 +01:00
|
|
|
static xQueueHandle queue;
|
2010-04-24 06:05:39 +02:00
|
|
|
static xQueueHandle priorityQueue;
|
2010-04-15 04:15:46 +02:00
|
|
|
static xTaskHandle telemetryTxTaskHandle;
|
2010-04-24 06:05:39 +02:00
|
|
|
static xTaskHandle telemetryTxPriTaskHandle;
|
2010-04-15 04:15:46 +02:00
|
|
|
static xTaskHandle telemetryRxTaskHandle;
|
2010-04-18 00:16:20 +02:00
|
|
|
static uint32_t txErrors;
|
|
|
|
static uint32_t txRetries;
|
2010-05-08 04:44:26 +02:00
|
|
|
static TelemetrySettingsData settings;
|
2010-05-08 06:11:03 +02:00
|
|
|
static uint32_t timeOfLastObjectUpdate;
|
2010-02-22 03:18:23 +01:00
|
|
|
|
|
|
|
// Private functions
|
2010-04-15 04:15:46 +02:00
|
|
|
static void telemetryTxTask(void* parameters);
|
2010-04-24 06:05:39 +02:00
|
|
|
static void telemetryTxPriTask(void* parameters);
|
2010-04-15 04:15:46 +02:00
|
|
|
static void telemetryRxTask(void* parameters);
|
2010-03-04 08:08:05 +01:00
|
|
|
static int32_t transmitData(uint8_t* data, int32_t length);
|
|
|
|
static void registerObject(UAVObjHandle obj);
|
|
|
|
static void updateObject(UAVObjHandle obj);
|
|
|
|
static int32_t addObject(UAVObjHandle obj);
|
|
|
|
static int32_t setUpdatePeriod(UAVObjHandle obj, int32_t updatePeriodMs);
|
2010-04-24 06:05:39 +02:00
|
|
|
static void processObjEvent(UAVObjEvent* ev);
|
2010-05-04 03:29:04 +02:00
|
|
|
static void updateTelemetryStats();
|
|
|
|
static void gcsTelemetryStatsUpdated();
|
2010-05-08 04:44:26 +02:00
|
|
|
static void updateSettings();
|
2010-02-22 03:18:23 +01:00
|
|
|
|
|
|
|
/**
|
2010-03-06 07:18:04 +01:00
|
|
|
* Initialise the telemetry module
|
|
|
|
* \return -1 if initialisation failed
|
2010-03-04 08:08:05 +01:00
|
|
|
* \return 0 on success
|
|
|
|
*/
|
2010-03-06 07:18:04 +01:00
|
|
|
int32_t TelemetryInitialize(void)
|
2010-02-22 03:18:23 +01:00
|
|
|
{
|
2010-04-18 00:16:20 +02:00
|
|
|
UAVObjEvent ev;
|
|
|
|
|
2010-05-08 06:11:03 +02:00
|
|
|
// Initialize vars
|
|
|
|
timeOfLastObjectUpdate = 0;
|
|
|
|
|
2010-04-24 06:05:39 +02:00
|
|
|
// Create object queues
|
2010-03-04 04:04:39 +01:00
|
|
|
queue = xQueueCreate(MAX_QUEUE_SIZE, sizeof(UAVObjEvent));
|
2010-04-24 06:05:39 +02:00
|
|
|
priorityQueue = xQueueCreate(MAX_QUEUE_SIZE, sizeof(UAVObjEvent));
|
2010-02-22 03:18:23 +01:00
|
|
|
|
2010-05-08 04:44:26 +02:00
|
|
|
// Get telemetry settings object
|
|
|
|
updateSettings();
|
2010-02-22 03:18:23 +01:00
|
|
|
|
2010-03-06 07:18:04 +01:00
|
|
|
// Initialise UAVTalk
|
2010-02-22 03:18:23 +01:00
|
|
|
UAVTalkInitialize(&transmitData);
|
|
|
|
|
2010-02-27 20:57:45 +01:00
|
|
|
// Process all registered objects and connect queue for updates
|
|
|
|
UAVObjIterate(®isterObject);
|
2010-02-22 03:18:23 +01:00
|
|
|
|
2010-04-18 00:16:20 +02:00
|
|
|
// Create periodic event that will be used to update the telemetry stats
|
|
|
|
txErrors = 0;
|
|
|
|
txRetries = 0;
|
|
|
|
memset(&ev, 0, sizeof(UAVObjEvent));
|
2010-05-08 04:44:26 +02:00
|
|
|
EventPeriodicQueueCreate(&ev, priorityQueue, STATS_UPDATE_PERIOD_MS);
|
|
|
|
|
|
|
|
// Listen to objects of interest
|
|
|
|
GCSTelemetryStatsConnectQueue(priorityQueue);
|
|
|
|
TelemetrySettingsConnectQueue(priorityQueue);
|
2010-04-18 00:16:20 +02:00
|
|
|
|
2010-04-15 04:15:46 +02:00
|
|
|
// Start telemetry tasks
|
2010-04-24 06:05:39 +02:00
|
|
|
xTaskCreate(telemetryTxTask, (signed char*)"TelTx", STACK_SIZE, NULL, TASK_PRIORITY_TX, &telemetryTxTaskHandle);
|
|
|
|
xTaskCreate(telemetryTxPriTask, (signed char*)"TelPriTx", STACK_SIZE, NULL, TASK_PRIORITY_TXPRI, &telemetryTxPriTaskHandle);
|
|
|
|
xTaskCreate(telemetryRxTask, (signed char*)"TelRx", STACK_SIZE, NULL, TASK_PRIORITY_RX, &telemetryRxTaskHandle);
|
2010-02-22 03:18:23 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-03-04 08:08:05 +01:00
|
|
|
* Register a new object, adds object to local list and connects the queue depending on the object's
|
|
|
|
* telemetry settings.
|
|
|
|
* \param[in] obj Object to connect
|
|
|
|
*/
|
2010-04-04 04:11:51 +02:00
|
|
|
static void registerObject(UAVObjHandle obj)
|
2010-02-22 03:18:23 +01:00
|
|
|
{
|
2010-03-04 04:04:39 +01:00
|
|
|
// Setup object for periodic updates
|
2010-02-27 20:57:45 +01:00
|
|
|
addObject(obj);
|
2010-02-22 03:18:23 +01:00
|
|
|
|
|
|
|
// Setup object for telemetry updates
|
|
|
|
updateObject(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update object's queue connections and timer, depending on object's settings
|
2010-03-04 08:08:05 +01:00
|
|
|
* \param[in] obj Object to updates
|
2010-02-22 03:18:23 +01:00
|
|
|
*/
|
2010-04-04 04:11:51 +02:00
|
|
|
static void updateObject(UAVObjHandle obj)
|
2010-02-22 03:18:23 +01:00
|
|
|
{
|
2010-02-27 20:57:45 +01:00
|
|
|
UAVObjMetadata metadata;
|
2010-02-22 03:18:23 +01:00
|
|
|
int32_t eventMask;
|
|
|
|
|
|
|
|
// Get metadata
|
2010-02-27 20:57:45 +01:00
|
|
|
UAVObjGetMetadata(obj, &metadata);
|
2010-02-22 03:18:23 +01:00
|
|
|
|
|
|
|
// Setup object depending on update mode
|
2010-03-15 02:54:25 +01:00
|
|
|
if(metadata.telemetryUpdateMode == UPDATEMODE_PERIODIC)
|
|
|
|
{
|
2010-02-22 03:18:23 +01:00
|
|
|
// Set update period
|
2010-02-27 20:57:45 +01:00
|
|
|
setUpdatePeriod(obj, metadata.telemetryUpdatePeriod);
|
2010-02-22 03:18:23 +01:00
|
|
|
// Connect queue
|
2010-03-04 08:08:05 +01:00
|
|
|
eventMask = EV_UPDATED_MANUAL | EV_UPDATE_REQ;
|
2010-03-15 02:54:25 +01:00
|
|
|
if(UAVObjIsMetaobject(obj))
|
|
|
|
{
|
2010-03-04 04:04:39 +01:00
|
|
|
eventMask |= EV_UNPACKED; // we also need to act on remote updates (unpack events)
|
2010-02-22 03:18:23 +01:00
|
|
|
}
|
2010-04-24 06:05:39 +02:00
|
|
|
UAVObjConnectQueue(obj, priorityQueue, eventMask);
|
2010-03-15 02:54:25 +01:00
|
|
|
}
|
|
|
|
else if(metadata.telemetryUpdateMode == UPDATEMODE_ONCHANGE)
|
|
|
|
{
|
2010-02-22 03:18:23 +01:00
|
|
|
// Set update period
|
2010-02-27 20:57:45 +01:00
|
|
|
setUpdatePeriod(obj, 0);
|
2010-02-22 03:18:23 +01:00
|
|
|
// Connect queue
|
2010-03-04 08:08:05 +01:00
|
|
|
eventMask = EV_UPDATED | EV_UPDATED_MANUAL | EV_UPDATE_REQ;
|
2010-03-15 02:54:25 +01:00
|
|
|
if(UAVObjIsMetaobject(obj))
|
|
|
|
{
|
2010-03-04 04:04:39 +01:00
|
|
|
eventMask |= EV_UNPACKED; // we also need to act on remote updates (unpack events)
|
2010-02-22 03:18:23 +01:00
|
|
|
}
|
2010-04-24 06:05:39 +02:00
|
|
|
UAVObjConnectQueue(obj, priorityQueue, eventMask);
|
2010-03-15 02:54:25 +01:00
|
|
|
}
|
|
|
|
else if(metadata.telemetryUpdateMode == UPDATEMODE_MANUAL)
|
|
|
|
{
|
2010-02-22 03:18:23 +01:00
|
|
|
// Set update period
|
2010-02-27 20:57:45 +01:00
|
|
|
setUpdatePeriod(obj, 0);
|
2010-02-22 03:18:23 +01:00
|
|
|
// Connect queue
|
2010-03-04 08:08:05 +01:00
|
|
|
eventMask = EV_UPDATED_MANUAL | EV_UPDATE_REQ;
|
2010-03-15 02:54:25 +01:00
|
|
|
if(UAVObjIsMetaobject(obj))
|
|
|
|
{
|
2010-03-04 04:04:39 +01:00
|
|
|
eventMask |= EV_UNPACKED; // we also need to act on remote updates (unpack events)
|
2010-02-22 03:18:23 +01:00
|
|
|
}
|
2010-04-24 06:05:39 +02:00
|
|
|
UAVObjConnectQueue(obj, priorityQueue, eventMask);
|
2010-03-15 02:54:25 +01:00
|
|
|
}
|
|
|
|
else if(metadata.telemetryUpdateMode == UPDATEMODE_NEVER)
|
|
|
|
{
|
2010-02-22 03:18:23 +01:00
|
|
|
// Set update period
|
2010-02-27 20:57:45 +01:00
|
|
|
setUpdatePeriod(obj, 0);
|
2010-02-22 03:18:23 +01:00
|
|
|
// Disconnect queue
|
2010-04-24 06:05:39 +02:00
|
|
|
UAVObjDisconnectQueue(obj, priorityQueue);
|
2010-02-22 03:18:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-04-24 06:05:39 +02:00
|
|
|
* Processes queue events
|
2010-02-22 03:18:23 +01:00
|
|
|
*/
|
2010-04-24 06:05:39 +02:00
|
|
|
static void processObjEvent(UAVObjEvent* ev)
|
2010-02-22 03:18:23 +01:00
|
|
|
{
|
2010-02-27 20:57:45 +01:00
|
|
|
UAVObjMetadata metadata;
|
2010-05-08 02:22:07 +02:00
|
|
|
FlightTelemetryStatsData flightStats;
|
2010-02-27 20:57:45 +01:00
|
|
|
int32_t retries;
|
|
|
|
int32_t success;
|
2010-02-22 03:18:23 +01:00
|
|
|
|
2010-05-04 03:29:04 +02:00
|
|
|
if ( ev->obj == 0 )
|
2010-03-15 02:54:25 +01:00
|
|
|
{
|
2010-05-04 03:29:04 +02:00
|
|
|
updateTelemetryStats();
|
|
|
|
}
|
|
|
|
else if ( ev->obj == GCSTelemetryStatsHandle() )
|
|
|
|
{
|
|
|
|
gcsTelemetryStatsUpdated();
|
2010-04-24 06:05:39 +02:00
|
|
|
}
|
2010-05-08 04:44:26 +02:00
|
|
|
else if ( ev->obj == TelemetrySettingsHandle() )
|
|
|
|
{
|
|
|
|
updateSettings();
|
|
|
|
}
|
2010-04-24 06:05:39 +02:00
|
|
|
else
|
|
|
|
{
|
2010-05-08 02:22:07 +02:00
|
|
|
// Only process event if connected to GCS or if object FlightTelemetryStats is updated
|
|
|
|
FlightTelemetryStatsGet(&flightStats);
|
2010-05-08 04:44:26 +02:00
|
|
|
if ( flightStats.Status == FLIGHTTELEMETRYSTATS_STATUS_CONNECTED || ev->obj == FlightTelemetryStatsHandle() )
|
2010-04-24 06:05:39 +02:00
|
|
|
{
|
2010-05-08 02:22:07 +02:00
|
|
|
// Get object metadata
|
|
|
|
UAVObjGetMetadata(ev->obj, &metadata);
|
|
|
|
// Act on event
|
|
|
|
retries = 0;
|
|
|
|
success = -1;
|
|
|
|
if(ev->event == EV_UPDATED || ev->event == EV_UPDATED_MANUAL)
|
2010-04-24 06:05:39 +02:00
|
|
|
{
|
2010-05-08 02:22:07 +02:00
|
|
|
// Send update to GCS (with retries)
|
|
|
|
while(retries < MAX_RETRIES && success == -1)
|
|
|
|
{
|
|
|
|
success = UAVTalkSendObject(ev->obj, ev->instId, metadata.telemetryAcked, REQ_TIMEOUT_MS); // call blocks until ack is received or timeout
|
|
|
|
++retries;
|
|
|
|
}
|
|
|
|
// Update stats
|
|
|
|
txRetries += (retries-1);
|
|
|
|
if ( success == -1 )
|
|
|
|
{
|
|
|
|
++txErrors;
|
|
|
|
}
|
2010-04-24 06:05:39 +02:00
|
|
|
}
|
2010-05-08 02:22:07 +02:00
|
|
|
else if(ev->event == EV_UPDATE_REQ)
|
2010-04-24 06:05:39 +02:00
|
|
|
{
|
2010-05-08 02:22:07 +02:00
|
|
|
// Request object update from GCS (with retries)
|
|
|
|
while(retries < MAX_RETRIES && success == -1)
|
|
|
|
{
|
|
|
|
success = UAVTalkSendObjectRequest(ev->obj, ev->instId, REQ_TIMEOUT_MS); // call blocks until update is received or timeout
|
|
|
|
++retries;
|
|
|
|
}
|
|
|
|
// Update stats
|
|
|
|
txRetries += (retries-1);
|
|
|
|
if ( success == -1 )
|
|
|
|
{
|
|
|
|
++txErrors;
|
|
|
|
}
|
2010-04-24 06:05:39 +02:00
|
|
|
}
|
2010-05-08 02:22:07 +02:00
|
|
|
// If this is a metaobject then make necessary telemetry updates
|
|
|
|
if(UAVObjIsMetaobject(ev->obj))
|
2010-03-15 02:54:25 +01:00
|
|
|
{
|
2010-05-08 02:22:07 +02:00
|
|
|
updateObject(UAVObjGetLinkedObj(ev->obj)); // linked object will be the actual object the metadata are for
|
2010-02-22 03:18:23 +01:00
|
|
|
}
|
|
|
|
}
|
2010-04-24 06:05:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Telemetry transmit task, regular priority
|
|
|
|
*/
|
|
|
|
static void telemetryTxTask(void* parameters)
|
|
|
|
{
|
|
|
|
UAVObjEvent ev;
|
|
|
|
|
|
|
|
// Loop forever
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
// Wait for queue message
|
|
|
|
if(xQueueReceive(queue, &ev, portMAX_DELAY) == pdTRUE)
|
|
|
|
{
|
|
|
|
// Process event
|
|
|
|
processObjEvent(&ev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Telemetry transmit task, high priority
|
|
|
|
*/
|
|
|
|
static void telemetryTxPriTask(void* parameters)
|
|
|
|
{
|
|
|
|
UAVObjEvent ev;
|
|
|
|
|
|
|
|
// Loop forever
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
// Wait for queue message
|
|
|
|
if(xQueueReceive(priorityQueue, &ev, portMAX_DELAY) == pdTRUE)
|
|
|
|
{
|
|
|
|
// Process event
|
|
|
|
processObjEvent(&ev);
|
|
|
|
}
|
2010-04-15 04:15:46 +02:00
|
|
|
}
|
|
|
|
}
|
2010-02-22 03:18:23 +01:00
|
|
|
|
2010-04-15 04:15:46 +02:00
|
|
|
/**
|
|
|
|
* Telemetry transmit task. Processes queue events and periodic updates.
|
|
|
|
*/
|
|
|
|
static void telemetryRxTask(void* parameters)
|
|
|
|
{
|
|
|
|
COMPortTypeDef inputPort;
|
|
|
|
int32_t len;
|
|
|
|
|
|
|
|
// Task loop
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
// TODO: Disabled since the USB HID is not fully functional yet
|
|
|
|
inputPort = telemetryPort; // force input port, remove once USB HID is tested
|
|
|
|
// Determine input port (USB takes priority over telemetry port)
|
|
|
|
//if(!PIOS_USB_HID_CheckAvailable())
|
|
|
|
//{
|
|
|
|
// inputPort = telemetryPort;
|
|
|
|
//}
|
|
|
|
//else
|
|
|
|
//{
|
|
|
|
// inputPort = COM_USB_HID;
|
|
|
|
//}
|
|
|
|
|
|
|
|
// Block until data are available
|
2010-04-25 04:21:03 +02:00
|
|
|
// TODO: Currently we periodically check the buffer for data, update once the PIOS_COM is made blocking
|
2010-04-15 04:15:46 +02:00
|
|
|
len = PIOS_COM_ReceiveBufferUsed(inputPort);
|
|
|
|
for (int32_t n = 0; n < len; ++n)
|
2010-03-06 07:18:04 +01:00
|
|
|
{
|
2010-04-15 04:15:46 +02:00
|
|
|
//PIOS_LED_Toggle(LED1);
|
|
|
|
UAVTalkProcessInputStream(PIOS_COM_ReceiveBuffer(inputPort));
|
2010-03-06 07:18:04 +01:00
|
|
|
}
|
2010-04-24 06:05:39 +02:00
|
|
|
vTaskDelay(5); // <- remove when blocking calls are implemented
|
2010-04-18 04:13:59 +02:00
|
|
|
|
2010-02-22 03:18:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transmit data buffer to the modem or USB port.
|
|
|
|
* \param[in] data Data buffer to send
|
2010-03-04 08:08:05 +01:00
|
|
|
* \param[in] length Length of buffer
|
|
|
|
* \return 0 Success
|
2010-02-22 03:18:23 +01:00
|
|
|
*/
|
2010-03-04 08:08:05 +01:00
|
|
|
static int32_t transmitData(uint8_t* data, int32_t length)
|
2010-02-22 03:18:23 +01:00
|
|
|
{
|
2010-04-15 04:15:46 +02:00
|
|
|
COMPortTypeDef outputPort;
|
2010-03-04 08:08:05 +01:00
|
|
|
|
2010-04-15 04:15:46 +02:00
|
|
|
// TODO: Disabled since the USB HID is not fully functional yet
|
|
|
|
outputPort = telemetryPort; // force input port, remove once USB HID is tested
|
|
|
|
// Determine input port (USB takes priority over telemetry port)
|
|
|
|
//if(!PIOS_USB_HID_CheckAvailable())
|
|
|
|
//{
|
|
|
|
// outputPort = telemetryPort;
|
|
|
|
//}
|
|
|
|
//else
|
|
|
|
//{
|
|
|
|
// outputPort = COM_USB_HID;
|
|
|
|
//}
|
2010-03-04 08:08:05 +01:00
|
|
|
|
2010-04-15 04:15:46 +02:00
|
|
|
// TODO: Update once the PIOS_COM is made blocking (it is implemented as a busy loop for now!)
|
|
|
|
//PIOS_LED_Toggle(LED2);
|
|
|
|
return PIOS_COM_SendBuffer(outputPort, data, length);
|
2010-02-22 03:18:23 +01:00
|
|
|
}
|
|
|
|
|
2010-02-27 20:57:45 +01:00
|
|
|
/**
|
2010-03-04 04:04:39 +01:00
|
|
|
* Setup object for periodic updates.
|
|
|
|
* \param[in] obj The object to update
|
|
|
|
* \return 0 Success
|
|
|
|
* \return -1 Failure
|
2010-02-27 20:57:45 +01:00
|
|
|
*/
|
2010-03-04 08:08:05 +01:00
|
|
|
static int32_t addObject(UAVObjHandle obj)
|
2010-02-27 20:57:45 +01:00
|
|
|
{
|
2010-03-04 04:04:39 +01:00
|
|
|
UAVObjEvent ev;
|
2010-02-27 20:57:45 +01:00
|
|
|
|
2010-03-04 04:04:39 +01:00
|
|
|
// Add object for periodic updates
|
|
|
|
ev.obj = obj;
|
|
|
|
ev.instId = UAVOBJ_ALL_INSTANCES;
|
|
|
|
ev.event = EV_UPDATED_MANUAL;
|
2010-04-05 02:59:51 +02:00
|
|
|
return EventPeriodicQueueCreate(&ev, queue, 0);
|
2010-02-27 20:57:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-03-04 04:04:39 +01:00
|
|
|
* Set update period of object (it must be already setup for periodic updates)
|
|
|
|
* \param[in] obj The object to update
|
|
|
|
* \param[in] updatePeriodMs The update period in ms, if zero then periodic updates are disabled
|
|
|
|
* \return 0 Success
|
|
|
|
* \return -1 Failure
|
2010-02-27 20:57:45 +01:00
|
|
|
*/
|
2010-03-04 08:08:05 +01:00
|
|
|
static int32_t setUpdatePeriod(UAVObjHandle obj, int32_t updatePeriodMs)
|
2010-02-27 20:57:45 +01:00
|
|
|
{
|
2010-03-04 04:04:39 +01:00
|
|
|
UAVObjEvent ev;
|
2010-02-22 03:18:23 +01:00
|
|
|
|
2010-03-04 04:04:39 +01:00
|
|
|
// Add object for periodic updates
|
|
|
|
ev.obj = obj;
|
|
|
|
ev.instId = UAVOBJ_ALL_INSTANCES;
|
|
|
|
ev.event = EV_UPDATED_MANUAL;
|
2010-04-05 02:59:51 +02:00
|
|
|
return EventPeriodicQueueUpdate(&ev, queue, updatePeriodMs);
|
2010-03-04 04:04:39 +01:00
|
|
|
}
|
2010-02-22 03:18:23 +01:00
|
|
|
|
2010-05-04 03:29:04 +02:00
|
|
|
/**
|
|
|
|
* Called each time the GCS telemetry stats object is updated.
|
|
|
|
* Trigger a flight telemetry stats update if a connection is not
|
|
|
|
* yet established.
|
|
|
|
*/
|
|
|
|
static void gcsTelemetryStatsUpdated()
|
|
|
|
{
|
|
|
|
FlightTelemetryStatsData flightStats;
|
|
|
|
GCSTelemetryStatsData gcsStats;
|
|
|
|
FlightTelemetryStatsGet(&flightStats);
|
|
|
|
GCSTelemetryStatsGet(&gcsStats);
|
|
|
|
if ( flightStats.Status != FLIGHTTELEMETRYSTATS_STATUS_CONNECTED ||
|
|
|
|
gcsStats.Status != GCSTELEMETRYSTATS_STATUS_CONNECTED )
|
|
|
|
{
|
|
|
|
updateTelemetryStats();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update telemetry statistics and handle connection handshake
|
|
|
|
*/
|
|
|
|
static void updateTelemetryStats()
|
|
|
|
{
|
|
|
|
UAVTalkStats utalkStats;
|
|
|
|
FlightTelemetryStatsData flightStats;
|
|
|
|
GCSTelemetryStatsData gcsStats;
|
|
|
|
uint8_t forceUpdate;
|
2010-05-08 06:11:03 +02:00
|
|
|
uint8_t connectionTimeout;
|
|
|
|
uint32_t timeNow;
|
2010-05-04 03:29:04 +02:00
|
|
|
|
|
|
|
// Get stats
|
|
|
|
UAVTalkGetStats(&utalkStats);
|
|
|
|
UAVTalkResetStats();
|
|
|
|
|
|
|
|
// Get object data
|
|
|
|
FlightTelemetryStatsGet(&flightStats);
|
|
|
|
GCSTelemetryStatsGet(&gcsStats);
|
|
|
|
|
|
|
|
// Update stats object
|
|
|
|
if ( flightStats.Status == FLIGHTTELEMETRYSTATS_STATUS_CONNECTED )
|
|
|
|
{
|
|
|
|
flightStats.RxDataRate = (float)utalkStats.rxBytes / ((float)STATS_UPDATE_PERIOD_MS/1000.0);
|
|
|
|
flightStats.TxDataRate = (float)utalkStats.txBytes / ((float)STATS_UPDATE_PERIOD_MS/1000.0);
|
|
|
|
flightStats.RxFailures += utalkStats.rxErrors;
|
|
|
|
flightStats.TxFailures += txErrors;
|
|
|
|
flightStats.TxRetries += txRetries;
|
|
|
|
txErrors = 0;
|
|
|
|
txRetries = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
flightStats.RxDataRate = 0;
|
|
|
|
flightStats.TxDataRate = 0;
|
|
|
|
flightStats.RxFailures = 0;
|
|
|
|
flightStats.TxFailures = 0;
|
|
|
|
flightStats.TxRetries = 0;
|
|
|
|
txErrors = 0;
|
|
|
|
txRetries = 0;
|
|
|
|
}
|
|
|
|
|
2010-05-08 06:11:03 +02:00
|
|
|
// Check for connection timeout
|
|
|
|
timeNow = xTaskGetTickCount()*portTICK_RATE_MS;
|
|
|
|
if ( utalkStats.rxObjects > 0 )
|
|
|
|
{
|
|
|
|
timeOfLastObjectUpdate = timeNow;
|
|
|
|
}
|
|
|
|
if ( (timeNow - timeOfLastObjectUpdate) > CONNECTION_TIMEOUT_MS )
|
|
|
|
{
|
|
|
|
connectionTimeout = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
connectionTimeout = 0;
|
|
|
|
}
|
|
|
|
|
2010-05-04 03:29:04 +02:00
|
|
|
// Update connection state
|
|
|
|
forceUpdate = 1;
|
|
|
|
if ( flightStats.Status == FLIGHTTELEMETRYSTATS_STATUS_DISCONNECTED )
|
|
|
|
{
|
|
|
|
// Wait for connection request
|
|
|
|
if ( gcsStats.Status == GCSTELEMETRYSTATS_STATUS_HANDSHAKEREQ )
|
|
|
|
{
|
|
|
|
flightStats.Status = FLIGHTTELEMETRYSTATS_STATUS_HANDSHAKEACK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ( flightStats.Status == FLIGHTTELEMETRYSTATS_STATUS_HANDSHAKEACK )
|
|
|
|
{
|
|
|
|
// Wait for connection
|
|
|
|
if ( gcsStats.Status == GCSTELEMETRYSTATS_STATUS_CONNECTED )
|
|
|
|
{
|
|
|
|
flightStats.Status = FLIGHTTELEMETRYSTATS_STATUS_CONNECTED;
|
|
|
|
}
|
|
|
|
else if ( gcsStats.Status == GCSTELEMETRYSTATS_STATUS_DISCONNECTED )
|
|
|
|
{
|
|
|
|
flightStats.Status = FLIGHTTELEMETRYSTATS_STATUS_DISCONNECTED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ( flightStats.Status == FLIGHTTELEMETRYSTATS_STATUS_CONNECTED )
|
|
|
|
{
|
2010-05-08 06:11:03 +02:00
|
|
|
if ( gcsStats.Status != GCSTELEMETRYSTATS_STATUS_CONNECTED || connectionTimeout )
|
2010-05-04 03:29:04 +02:00
|
|
|
{
|
|
|
|
flightStats.Status = FLIGHTTELEMETRYSTATS_STATUS_DISCONNECTED;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
forceUpdate = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
flightStats.Status = FLIGHTTELEMETRYSTATS_STATUS_DISCONNECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the telemetry alarm
|
|
|
|
if ( flightStats.Status == FLIGHTTELEMETRYSTATS_STATUS_CONNECTED )
|
|
|
|
{
|
|
|
|
AlarmsClear(SYSTEMALARMS_ALARM_TELEMETRY);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_TELEMETRY, SYSTEMALARMS_ALARM_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update object
|
|
|
|
FlightTelemetryStatsSet(&flightStats);
|
|
|
|
|
|
|
|
// Force telemetry update if not connected
|
|
|
|
if ( forceUpdate )
|
|
|
|
{
|
|
|
|
FlightTelemetryStatsUpdated();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-08 04:44:26 +02:00
|
|
|
/**
|
|
|
|
* Update the telemetry settings, called on startup and
|
|
|
|
* each time the settings object is updated
|
|
|
|
*/
|
|
|
|
static void updateSettings()
|
|
|
|
{
|
|
|
|
// Set port
|
|
|
|
telemetryPort = COM_USART1;
|
|
|
|
// Retrieve settings
|
|
|
|
TelemetrySettingsGet(&settings);
|
|
|
|
// Set port speed
|
|
|
|
if (settings.Speed == TELEMETRYSETTINGS_SPEED_9600)
|
|
|
|
{
|
|
|
|
PIOS_COM_ChangeBaud(telemetryPort, 9600);
|
|
|
|
}
|
|
|
|
else if (settings.Speed == TELEMETRYSETTINGS_SPEED_57600)
|
|
|
|
{
|
|
|
|
PIOS_COM_ChangeBaud(telemetryPort, 57600);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|