2010-09-27 09:28:34 +02:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
* @addtogroup OpenPilotModules OpenPilot Modules
|
2013-05-19 16:37:30 +02:00
|
|
|
* @{
|
2010-09-27 09:28:34 +02:00
|
|
|
* @addtogroup TelemetryModule Telemetry Module
|
|
|
|
* @brief Main telemetry module
|
|
|
|
* Starts three tasks (RX, TX, and priority TX) that watch event queues
|
|
|
|
* and handle all the telemetry of the UAVobjects
|
2013-05-19 16:37:30 +02:00
|
|
|
* @{
|
2010-09-27 09:28:34 +02: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
|
|
|
|
*/
|
|
|
|
|
2013-05-02 23:31:14 +02:00
|
|
|
#include <openpilot.h>
|
|
|
|
|
2011-06-17 07:13:19 +02:00
|
|
|
#include "telemetry.h"
|
2013-05-02 23:31:14 +02:00
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
#include "flighttelemetrystats.h"
|
|
|
|
#include "gcstelemetrystats.h"
|
2011-09-28 21:09:19 +02:00
|
|
|
#include "hwsettings.h"
|
2013-05-02 23:31:14 +02:00
|
|
|
#include "taskinfo.h"
|
2010-09-27 09:28:34 +02:00
|
|
|
|
|
|
|
// Private constants
|
2013-05-19 16:37:30 +02:00
|
|
|
#define MAX_QUEUE_SIZE TELEM_QUEUE_SIZE
|
|
|
|
#define STACK_SIZE_BYTES PIOS_TELEM_STACK_SIZE
|
|
|
|
#define TASK_PRIORITY_RX (tskIDLE_PRIORITY + 2)
|
|
|
|
#define TASK_PRIORITY_TX (tskIDLE_PRIORITY + 2)
|
2013-06-17 16:06:47 +02:00
|
|
|
#define TASK_PRIORITY_RADRX (tskIDLE_PRIORITY + 2)
|
2013-05-19 16:37:30 +02:00
|
|
|
#define TASK_PRIORITY_TXPRI (tskIDLE_PRIORITY + 2)
|
|
|
|
#define REQ_TIMEOUT_MS 250
|
|
|
|
#define MAX_RETRIES 2
|
2010-09-27 09:28:34 +02:00
|
|
|
#define STATS_UPDATE_PERIOD_MS 4000
|
2013-05-19 16:37:30 +02:00
|
|
|
#define CONNECTION_TIMEOUT_MS 8000
|
2010-09-27 09:28:34 +02:00
|
|
|
|
|
|
|
// Private types
|
|
|
|
|
|
|
|
// Private variables
|
2011-02-12 23:19:43 +01:00
|
|
|
static uint32_t telemetryPort;
|
2010-09-27 09:28:34 +02:00
|
|
|
static xQueueHandle queue;
|
2011-02-05 11:52:52 +01:00
|
|
|
|
|
|
|
#if defined(PIOS_TELEM_PRIORITY_QUEUE)
|
2010-09-27 09:28:34 +02:00
|
|
|
static xQueueHandle priorityQueue;
|
|
|
|
static xTaskHandle telemetryTxPriTaskHandle;
|
2011-02-05 11:52:52 +01:00
|
|
|
static void telemetryTxPriTask(void *parameters);
|
|
|
|
#else
|
|
|
|
#define priorityQueue queue
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static xTaskHandle telemetryTxTaskHandle;
|
2010-09-27 09:28:34 +02:00
|
|
|
static xTaskHandle telemetryRxTaskHandle;
|
2013-06-17 16:06:47 +02:00
|
|
|
#ifdef PIOS_INCLUDE_RFM22B
|
|
|
|
static xTaskHandle radioRxTaskHandle;
|
|
|
|
#endif
|
2010-09-27 09:28:34 +02:00
|
|
|
static uint32_t txErrors;
|
|
|
|
static uint32_t txRetries;
|
|
|
|
static uint32_t timeOfLastObjectUpdate;
|
2011-06-16 16:35:37 +02:00
|
|
|
static UAVTalkConnection uavTalkCon;
|
2013-06-17 16:06:47 +02:00
|
|
|
#ifdef PIOS_INCLUDE_RFM22B
|
|
|
|
static UAVTalkConnection radioUavTalkCon;
|
|
|
|
#endif
|
2010-09-27 09:28:34 +02:00
|
|
|
|
|
|
|
// Private functions
|
|
|
|
static void telemetryTxTask(void *parameters);
|
|
|
|
static void telemetryRxTask(void *parameters);
|
2013-06-17 16:06:47 +02:00
|
|
|
#ifdef PIOS_INCLUDE_RFM22B
|
|
|
|
static void radioRxTask(void *parameters);
|
|
|
|
#endif
|
2013-05-19 16:37:30 +02:00
|
|
|
static int32_t transmitData(uint8_t *data, int32_t length);
|
2010-09-27 09:28:34 +02:00
|
|
|
static void registerObject(UAVObjHandle obj);
|
2012-04-19 03:47:13 +02:00
|
|
|
static void updateObject(UAVObjHandle obj, int32_t eventType);
|
2010-09-27 09:28:34 +02:00
|
|
|
static int32_t setUpdatePeriod(UAVObjHandle obj, int32_t updatePeriodMs);
|
2013-11-17 17:15:23 +01:00
|
|
|
static int32_t setLoggingPeriod(UAVObjHandle obj, int32_t updatePeriodMs);
|
2013-05-19 16:37:30 +02:00
|
|
|
static void processObjEvent(UAVObjEvent *ev);
|
2010-09-27 09:28:34 +02:00
|
|
|
static void updateTelemetryStats();
|
|
|
|
static void gcsTelemetryStatsUpdated();
|
|
|
|
static void updateSettings();
|
2013-06-17 16:06:47 +02:00
|
|
|
static uint32_t getComPort(bool input);
|
2010-09-27 09:28:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialise the telemetry module
|
|
|
|
* \return -1 if initialisation failed
|
|
|
|
* \return 0 on success
|
|
|
|
*/
|
2011-06-20 07:35:40 +02:00
|
|
|
int32_t TelemetryStart(void)
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
// Process all registered objects and connect queue for updates
|
|
|
|
UAVObjIterate(®isterObject);
|
|
|
|
|
|
|
|
// Listen to objects of interest
|
|
|
|
GCSTelemetryStatsConnectQueue(priorityQueue);
|
|
|
|
|
|
|
|
// Start telemetry tasks
|
|
|
|
xTaskCreate(telemetryTxTask, (signed char *)"TelTx", STACK_SIZE_BYTES / 4, NULL, TASK_PRIORITY_TX, &telemetryTxTaskHandle);
|
|
|
|
PIOS_TASK_MONITOR_RegisterTask(TASKINFO_RUNNING_TELEMETRYTX, telemetryTxTaskHandle);
|
|
|
|
xTaskCreate(telemetryRxTask, (signed char *)"TelRx", STACK_SIZE_BYTES / 4, NULL, TASK_PRIORITY_RX, &telemetryRxTaskHandle);
|
|
|
|
PIOS_TASK_MONITOR_RegisterTask(TASKINFO_RUNNING_TELEMETRYRX, telemetryRxTaskHandle);
|
2011-06-20 07:35:40 +02:00
|
|
|
|
2013-06-17 16:06:47 +02:00
|
|
|
#ifdef PIOS_INCLUDE_RFM22B
|
|
|
|
xTaskCreate(radioRxTask, (signed char *)"RadioRx", STACK_SIZE_BYTES / 4, NULL, TASK_PRIORITY_RADRX, &radioRxTaskHandle);
|
|
|
|
PIOS_TASK_MONITOR_RegisterTask(TASKINFO_RUNNING_RADIORX, radioRxTaskHandle);
|
|
|
|
#endif
|
|
|
|
|
2011-06-20 07:35:40 +02:00
|
|
|
#if defined(PIOS_TELEM_PRIORITY_QUEUE)
|
2013-05-19 16:37:30 +02:00
|
|
|
xTaskCreate(telemetryTxPriTask, (signed char *)"TelPriTx", STACK_SIZE_BYTES / 4, NULL, TASK_PRIORITY_TXPRI, &telemetryTxPriTaskHandle);
|
|
|
|
PIOS_TASK_MONITOR_RegisterTask(TASKINFO_RUNNING_TELEMETRYTXPRI, telemetryTxPriTaskHandle);
|
2011-06-20 07:35:40 +02:00
|
|
|
#endif
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
return 0;
|
2011-06-20 07:35:40 +02:00
|
|
|
}
|
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
/**
|
|
|
|
* Initialise the telemetry module
|
|
|
|
* \return -1 if initialisation failed
|
|
|
|
* \return 0 on success
|
|
|
|
*/
|
|
|
|
int32_t TelemetryInitialize(void)
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
FlightTelemetryStatsInitialize();
|
|
|
|
GCSTelemetryStatsInitialize();
|
2011-06-18 18:59:02 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Initialize vars
|
|
|
|
timeOfLastObjectUpdate = 0;
|
2010-09-27 09:28:34 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Create object queues
|
|
|
|
queue = xQueueCreate(MAX_QUEUE_SIZE, sizeof(UAVObjEvent));
|
2011-02-05 11:52:52 +01:00
|
|
|
#if defined(PIOS_TELEM_PRIORITY_QUEUE)
|
2013-05-19 16:37:30 +02:00
|
|
|
priorityQueue = xQueueCreate(MAX_QUEUE_SIZE, sizeof(UAVObjEvent));
|
2011-02-05 11:52:52 +01:00
|
|
|
#endif
|
2010-09-27 09:28:34 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Update telemetry settings
|
|
|
|
telemetryPort = PIOS_COM_TELEM_RF;
|
|
|
|
HwSettingsInitialize();
|
|
|
|
updateSettings();
|
|
|
|
|
|
|
|
// Initialise UAVTalk
|
|
|
|
uavTalkCon = UAVTalkInitialize(&transmitData);
|
2013-06-17 16:06:47 +02:00
|
|
|
#ifdef PIOS_INCLUDE_RFM22B
|
|
|
|
radioUavTalkCon = UAVTalkInitialize(&transmitData);
|
|
|
|
#endif
|
2013-05-19 16:37:30 +02:00
|
|
|
|
|
|
|
// Create periodic event that will be used to update the telemetry stats
|
2014-01-06 21:48:34 +01:00
|
|
|
// FIXME STATS_UPDATE_PERIOD_MS is 4000ms while FlighTelemetryStats update period is 5000ms...
|
2013-06-26 05:54:35 +02:00
|
|
|
txErrors = 0;
|
|
|
|
txRetries = 0;
|
2013-05-19 16:37:30 +02:00
|
|
|
UAVObjEvent ev;
|
|
|
|
memset(&ev, 0, sizeof(UAVObjEvent));
|
|
|
|
EventPeriodicQueueCreate(&ev, priorityQueue, STATS_UPDATE_PERIOD_MS);
|
|
|
|
|
|
|
|
return 0;
|
2010-09-27 09:28:34 +02:00
|
|
|
}
|
|
|
|
|
2013-06-04 05:37:40 +02:00
|
|
|
MODULE_INITCALL(TelemetryInitialize, TelemetryStart);
|
2011-06-17 07:13:19 +02:00
|
|
|
|
2010-09-27 09:28:34 +02: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
|
|
|
|
*/
|
|
|
|
static void registerObject(UAVObjHandle obj)
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
if (UAVObjIsMetaobject(obj)) {
|
2014-01-06 21:48:34 +01:00
|
|
|
// Only connect change notifications for meta objects. No periodic updates
|
2013-05-19 16:37:30 +02:00
|
|
|
UAVObjConnectQueue(obj, priorityQueue, EV_MASK_ALL_UPDATES);
|
|
|
|
} else {
|
2013-11-17 17:15:23 +01:00
|
|
|
// Setup object for periodic updates
|
2013-05-19 16:37:30 +02:00
|
|
|
updateObject(obj, EV_NONE);
|
|
|
|
}
|
2010-09-27 09:28:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update object's queue connections and timer, depending on object's settings
|
|
|
|
* \param[in] obj Object to updates
|
|
|
|
*/
|
2012-04-19 03:47:13 +02:00
|
|
|
static void updateObject(UAVObjHandle obj, int32_t eventType)
|
2010-09-27 09:28:34 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
UAVObjMetadata metadata;
|
2013-11-17 17:15:23 +01:00
|
|
|
UAVObjUpdateMode updateMode, loggingMode;
|
2013-05-19 16:37:30 +02:00
|
|
|
int32_t eventMask;
|
|
|
|
|
|
|
|
if (UAVObjIsMetaobject(obj)) {
|
2013-12-19 00:17:08 +01:00
|
|
|
// This function updates the periodic updates for the object.
|
|
|
|
// Meta Objects cannot have periodic updates.
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get metadata
|
|
|
|
UAVObjGetMetadata(obj, &metadata);
|
2013-11-17 17:15:23 +01:00
|
|
|
updateMode = UAVObjGetTelemetryUpdateMode(&metadata);
|
|
|
|
loggingMode = UAVObjGetLoggingUpdateMode(&metadata);
|
2013-05-19 16:37:30 +02:00
|
|
|
|
|
|
|
// Setup object depending on update mode
|
2013-11-17 17:15:23 +01:00
|
|
|
eventMask = 0;
|
2013-05-19 16:37:30 +02:00
|
|
|
switch (updateMode) {
|
|
|
|
case UPDATEMODE_PERIODIC:
|
|
|
|
// Set update period
|
|
|
|
setUpdatePeriod(obj, metadata.telemetryUpdatePeriod);
|
|
|
|
// Connect queue
|
2013-11-17 17:15:23 +01:00
|
|
|
eventMask |= EV_UPDATED_PERIODIC | EV_UPDATED_MANUAL | EV_UPDATE_REQ;
|
2013-05-19 16:37:30 +02:00
|
|
|
break;
|
|
|
|
case UPDATEMODE_ONCHANGE:
|
|
|
|
// Set update period
|
|
|
|
setUpdatePeriod(obj, 0);
|
|
|
|
// Connect queue
|
2013-11-17 17:15:23 +01:00
|
|
|
eventMask |= EV_UPDATED | EV_UPDATED_MANUAL | EV_UPDATE_REQ;
|
2013-05-19 16:37:30 +02:00
|
|
|
break;
|
|
|
|
case UPDATEMODE_THROTTLED:
|
|
|
|
if ((eventType == EV_UPDATED_PERIODIC) || (eventType == EV_NONE)) {
|
|
|
|
// If we received a periodic update, we can change back to update on change
|
2013-11-17 17:15:23 +01:00
|
|
|
eventMask |= EV_UPDATED | EV_UPDATED_MANUAL | EV_UPDATE_REQ;
|
2013-05-19 16:37:30 +02:00
|
|
|
// Set update period on initialization and metadata change
|
|
|
|
if (eventType == EV_NONE) {
|
|
|
|
setUpdatePeriod(obj, metadata.telemetryUpdatePeriod);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Otherwise, we just received an object update, so switch to periodic for the timeout period to prevent more updates
|
2013-11-17 17:15:23 +01:00
|
|
|
eventMask |= EV_UPDATED_PERIODIC | EV_UPDATED_MANUAL | EV_UPDATE_REQ;
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case UPDATEMODE_MANUAL:
|
|
|
|
// Set update period
|
|
|
|
setUpdatePeriod(obj, 0);
|
|
|
|
// Connect queue
|
2013-11-17 17:15:23 +01:00
|
|
|
eventMask |= EV_UPDATED_MANUAL | EV_UPDATE_REQ;
|
2013-05-19 16:37:30 +02:00
|
|
|
break;
|
|
|
|
}
|
2013-11-17 17:15:23 +01:00
|
|
|
switch (loggingMode) {
|
|
|
|
case UPDATEMODE_PERIODIC:
|
|
|
|
// Set update period
|
|
|
|
setLoggingPeriod(obj, metadata.loggingUpdatePeriod);
|
|
|
|
// Connect queue
|
|
|
|
eventMask |= EV_LOGGING_PERIODIC | EV_LOGGING_MANUAL;
|
|
|
|
break;
|
|
|
|
case UPDATEMODE_ONCHANGE:
|
|
|
|
// Set update period
|
|
|
|
setLoggingPeriod(obj, 0);
|
|
|
|
// Connect queue
|
|
|
|
eventMask |= EV_UPDATED | EV_LOGGING_MANUAL;
|
|
|
|
break;
|
|
|
|
case UPDATEMODE_THROTTLED:
|
|
|
|
if ((eventType == EV_LOGGING_PERIODIC) || (eventType == EV_NONE)) {
|
|
|
|
// If we received a periodic update, we can change back to update on change
|
|
|
|
eventMask |= EV_UPDATED | EV_LOGGING_MANUAL;
|
|
|
|
// Set update period on initialization and metadata change
|
|
|
|
if (eventType == EV_NONE) {
|
|
|
|
setLoggingPeriod(obj, metadata.loggingUpdatePeriod);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Otherwise, we just received an object update, so switch to periodic for the timeout period to prevent more updates
|
|
|
|
eventMask |= EV_LOGGING_PERIODIC | EV_LOGGING_MANUAL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case UPDATEMODE_MANUAL:
|
|
|
|
// Set update period
|
|
|
|
setLoggingPeriod(obj, 0);
|
|
|
|
// Connect queue
|
|
|
|
eventMask |= EV_LOGGING_MANUAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
UAVObjConnectQueue(obj, priorityQueue, eventMask);
|
2010-09-27 09:28:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Processes queue events
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
static void processObjEvent(UAVObjEvent *ev)
|
2010-09-27 09:28:34 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
UAVObjMetadata metadata;
|
|
|
|
UAVObjUpdateMode updateMode;
|
|
|
|
int32_t retries;
|
|
|
|
int32_t success;
|
|
|
|
|
|
|
|
if (ev->obj == 0) {
|
|
|
|
updateTelemetryStats();
|
|
|
|
} else if (ev->obj == GCSTelemetryStatsHandle()) {
|
|
|
|
gcsTelemetryStatsUpdated();
|
|
|
|
} else {
|
|
|
|
// Get object metadata
|
|
|
|
UAVObjGetMetadata(ev->obj, &metadata);
|
|
|
|
updateMode = UAVObjGetTelemetryUpdateMode(&metadata);
|
|
|
|
|
|
|
|
// Act on event
|
|
|
|
retries = 0;
|
|
|
|
success = -1;
|
2014-01-06 21:48:34 +01:00
|
|
|
if ((ev->event == EV_UPDATED && (updateMode == UPDATEMODE_ONCHANGE || updateMode == UPDATEMODE_THROTTLED))
|
2014-01-14 22:46:01 +01:00
|
|
|
|| ev->event == EV_UPDATED_MANUAL
|
|
|
|
|| (ev->event == EV_UPDATED_PERIODIC && updateMode != UPDATEMODE_THROTTLED)) {
|
2013-05-19 16:37:30 +02:00
|
|
|
// Send update to GCS (with retries)
|
|
|
|
while (retries < MAX_RETRIES && success == -1) {
|
2013-12-19 00:17:08 +01:00
|
|
|
// call blocks until ack is received or timeout
|
|
|
|
success = UAVTalkSendObject(uavTalkCon, ev->obj, ev->instId, UAVObjGetTelemetryAcked(&metadata), REQ_TIMEOUT_MS);
|
|
|
|
if (success == -1) {
|
|
|
|
++retries;
|
|
|
|
}
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
|
|
|
// Update stats
|
2013-12-19 00:17:08 +01:00
|
|
|
txRetries += retries;
|
2013-05-19 16:37:30 +02:00
|
|
|
if (success == -1) {
|
|
|
|
++txErrors;
|
|
|
|
}
|
|
|
|
} else if (ev->event == EV_UPDATE_REQ) {
|
|
|
|
// Request object update from GCS (with retries)
|
|
|
|
while (retries < MAX_RETRIES && success == -1) {
|
2013-12-19 00:17:08 +01:00
|
|
|
// call blocks until update is received or timeout
|
|
|
|
success = UAVTalkSendObjectRequest(uavTalkCon, ev->obj, ev->instId, REQ_TIMEOUT_MS);
|
|
|
|
if (success == -1) {
|
|
|
|
++retries;
|
|
|
|
}
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
|
|
|
// Update stats
|
2013-12-19 00:17:08 +01:00
|
|
|
txRetries += retries;
|
2013-05-19 16:37:30 +02:00
|
|
|
if (success == -1) {
|
|
|
|
++txErrors;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If this is a metaobject then make necessary telemetry updates
|
|
|
|
if (UAVObjIsMetaobject(ev->obj)) {
|
2013-12-19 00:17:08 +01:00
|
|
|
// linked object will be the actual object the metadata are for
|
|
|
|
updateObject(UAVObjGetLinkedObj(ev->obj), EV_NONE);
|
2013-05-19 16:37:30 +02:00
|
|
|
} else {
|
|
|
|
if (updateMode == UPDATEMODE_THROTTLED) {
|
|
|
|
// If this is UPDATEMODE_THROTTLED, the event mask changes on every event.
|
|
|
|
updateObject(ev->obj, ev->event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-17 17:15:23 +01:00
|
|
|
// Log UAVObject if necessary
|
|
|
|
if (ev->obj) {
|
|
|
|
updateMode = UAVObjGetLoggingUpdateMode(&metadata);
|
2014-01-06 21:48:34 +01:00
|
|
|
if ((ev->event == EV_UPDATED && (updateMode == UPDATEMODE_ONCHANGE || updateMode == UPDATEMODE_THROTTLED))
|
2014-01-14 22:46:01 +01:00
|
|
|
|| ev->event == EV_LOGGING_MANUAL
|
|
|
|
|| (ev->event == EV_LOGGING_PERIODIC && updateMode != UPDATEMODE_THROTTLED)) {
|
2013-11-17 17:15:23 +01:00
|
|
|
if (ev->instId == UAVOBJ_ALL_INSTANCES) {
|
|
|
|
success = UAVObjGetNumInstances(ev->obj);
|
|
|
|
for (retries = 0; retries < success; retries++) {
|
|
|
|
UAVObjInstanceWriteToLog(ev->obj, retries);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
UAVObjInstanceWriteToLog(ev->obj, ev->instId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (updateMode == UPDATEMODE_THROTTLED) {
|
|
|
|
// If this is UPDATEMODE_THROTTLED, the event mask changes on every event.
|
|
|
|
updateObject(ev->obj, ev->event);
|
|
|
|
}
|
|
|
|
}
|
2010-09-27 09:28:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Telemetry transmit task, regular priority
|
|
|
|
*/
|
2013-05-05 09:02:24 +02:00
|
|
|
static void telemetryTxTask(__attribute__((unused)) void *parameters)
|
2010-09-27 09:28:34 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
UAVObjEvent ev;
|
|
|
|
|
|
|
|
// Loop forever
|
|
|
|
while (1) {
|
|
|
|
// Wait for queue message
|
|
|
|
if (xQueueReceive(queue, &ev, portMAX_DELAY) == pdTRUE) {
|
|
|
|
// Process event
|
|
|
|
processObjEvent(&ev);
|
|
|
|
}
|
|
|
|
}
|
2010-09-27 09:28:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Telemetry transmit task, high priority
|
|
|
|
*/
|
2011-02-05 11:52:52 +01:00
|
|
|
#if defined(PIOS_TELEM_PRIORITY_QUEUE)
|
2013-05-05 09:02:24 +02:00
|
|
|
static void telemetryTxPriTask(__attribute__((unused)) void *parameters)
|
2010-09-27 09:28:34 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
UAVObjEvent ev;
|
|
|
|
|
|
|
|
// Loop forever
|
|
|
|
while (1) {
|
|
|
|
// Wait for queue message
|
|
|
|
if (xQueueReceive(priorityQueue, &ev, portMAX_DELAY) == pdTRUE) {
|
|
|
|
// Process event
|
|
|
|
processObjEvent(&ev);
|
|
|
|
}
|
|
|
|
}
|
2010-09-27 09:28:34 +02:00
|
|
|
}
|
2011-02-05 11:52:52 +01:00
|
|
|
#endif
|
2010-09-27 09:28:34 +02:00
|
|
|
|
|
|
|
/**
|
2013-06-17 16:06:47 +02:00
|
|
|
* Telemetry receive task. Processes queue events and periodic updates.
|
2010-09-27 09:28:34 +02:00
|
|
|
*/
|
2013-05-05 09:02:24 +02:00
|
|
|
static void telemetryRxTask(__attribute__((unused)) void *parameters)
|
2010-09-27 09:28:34 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
// Task loop
|
|
|
|
while (1) {
|
2013-06-17 16:06:47 +02:00
|
|
|
uint32_t inputPort = getComPort(true);
|
2013-05-19 16:37:30 +02:00
|
|
|
|
|
|
|
if (inputPort) {
|
|
|
|
// Block until data are available
|
|
|
|
uint8_t serial_data[1];
|
|
|
|
uint16_t bytes_to_process;
|
|
|
|
|
|
|
|
bytes_to_process = PIOS_COM_ReceiveBuffer(inputPort, serial_data, sizeof(serial_data), 500);
|
|
|
|
if (bytes_to_process > 0) {
|
|
|
|
for (uint8_t i = 0; i < bytes_to_process; i++) {
|
|
|
|
UAVTalkProcessInputStream(uavTalkCon, serial_data[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
vTaskDelay(5);
|
|
|
|
}
|
|
|
|
}
|
2010-09-27 09:28:34 +02:00
|
|
|
}
|
|
|
|
|
2013-06-17 16:06:47 +02:00
|
|
|
#ifdef PIOS_INCLUDE_RFM22B
|
|
|
|
/**
|
|
|
|
* Radio telemetry receive task. Processes queue events and periodic updates.
|
|
|
|
*/
|
|
|
|
static void radioRxTask(__attribute__((unused)) void *parameters)
|
|
|
|
{
|
|
|
|
// Task loop
|
|
|
|
while (1) {
|
|
|
|
if (telemetryPort) {
|
|
|
|
// Block until data are available
|
|
|
|
uint8_t serial_data[1];
|
|
|
|
uint16_t bytes_to_process;
|
|
|
|
|
|
|
|
bytes_to_process = PIOS_COM_ReceiveBuffer(telemetryPort, serial_data, sizeof(serial_data), 500);
|
|
|
|
if (bytes_to_process > 0) {
|
|
|
|
for (uint8_t i = 0; i < bytes_to_process; i++) {
|
|
|
|
UAVTalkProcessInputStream(radioUavTalkCon, serial_data[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
vTaskDelay(5);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* PIOS_INCLUDE_RFM22B */
|
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
/**
|
|
|
|
* Transmit data buffer to the modem or USB port.
|
|
|
|
* \param[in] data Data buffer to send
|
|
|
|
* \param[in] length Length of buffer
|
2011-12-27 18:19:05 +01:00
|
|
|
* \return -1 on failure
|
|
|
|
* \return number of bytes transmitted on success
|
2010-09-27 09:28:34 +02:00
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
static int32_t transmitData(uint8_t *data, int32_t length)
|
2010-09-27 09:28:34 +02:00
|
|
|
{
|
2013-06-17 16:06:47 +02:00
|
|
|
uint32_t outputPort = getComPort(false);
|
2010-09-27 09:28:34 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (outputPort) {
|
|
|
|
return PIOS_COM_SendBuffer(outputPort, data, length);
|
|
|
|
}
|
2012-10-20 20:48:22 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
return -1;
|
2010-09-27 09:28:34 +02: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
|
|
|
|
*/
|
|
|
|
static int32_t setUpdatePeriod(UAVObjHandle obj, int32_t updatePeriodMs)
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
UAVObjEvent ev;
|
2014-01-06 21:48:34 +01:00
|
|
|
int32_t ret;
|
2010-09-27 09:28:34 +02:00
|
|
|
|
2014-01-06 21:48:34 +01:00
|
|
|
// Add or update object for periodic updates
|
2013-05-19 16:37:30 +02:00
|
|
|
ev.obj = obj;
|
|
|
|
ev.instId = UAVOBJ_ALL_INSTANCES;
|
|
|
|
ev.event = EV_UPDATED_PERIODIC;
|
2014-01-06 21:48:34 +01:00
|
|
|
|
|
|
|
ret = EventPeriodicQueueUpdate(&ev, queue, updatePeriodMs);
|
|
|
|
if (ret == -1) {
|
|
|
|
ret = EventPeriodicQueueCreate(&ev, queue, updatePeriodMs);
|
|
|
|
}
|
|
|
|
return ret;
|
2010-09-27 09:28:34 +02:00
|
|
|
}
|
|
|
|
|
2013-11-17 17:15:23 +01:00
|
|
|
/**
|
|
|
|
* Set logging 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
|
|
|
|
*/
|
|
|
|
static int32_t setLoggingPeriod(UAVObjHandle obj, int32_t updatePeriodMs)
|
|
|
|
{
|
|
|
|
UAVObjEvent ev;
|
2014-01-06 21:48:34 +01:00
|
|
|
int32_t ret;
|
2013-11-17 17:15:23 +01:00
|
|
|
|
2014-01-06 21:48:34 +01:00
|
|
|
// Add or update object for periodic updates
|
2013-11-17 17:15:23 +01:00
|
|
|
ev.obj = obj;
|
|
|
|
ev.instId = UAVOBJ_ALL_INSTANCES;
|
|
|
|
ev.event = EV_LOGGING_PERIODIC;
|
2014-01-06 21:48:34 +01:00
|
|
|
|
|
|
|
ret = EventPeriodicQueueUpdate(&ev, queue, updatePeriodMs);
|
|
|
|
if (ret == -1) {
|
|
|
|
ret = EventPeriodicQueueCreate(&ev, queue, updatePeriodMs);
|
|
|
|
}
|
|
|
|
return ret;
|
2013-11-17 17:15:23 +01:00
|
|
|
}
|
|
|
|
|
2010-09-27 09:28:34 +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()
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
FlightTelemetryStatsData flightStats;
|
|
|
|
GCSTelemetryStatsData gcsStats;
|
|
|
|
|
|
|
|
FlightTelemetryStatsGet(&flightStats);
|
|
|
|
GCSTelemetryStatsGet(&gcsStats);
|
|
|
|
if (flightStats.Status != FLIGHTTELEMETRYSTATS_STATUS_CONNECTED || gcsStats.Status != GCSTELEMETRYSTATS_STATUS_CONNECTED) {
|
|
|
|
updateTelemetryStats();
|
|
|
|
}
|
2010-09-27 09:28:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update telemetry statistics and handle connection handshake
|
|
|
|
*/
|
|
|
|
static void updateTelemetryStats()
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
UAVTalkStats utalkStats;
|
|
|
|
FlightTelemetryStatsData flightStats;
|
|
|
|
GCSTelemetryStatsData gcsStats;
|
|
|
|
uint8_t forceUpdate;
|
|
|
|
uint8_t connectionTimeout;
|
|
|
|
uint32_t timeNow;
|
|
|
|
|
|
|
|
// Get stats
|
|
|
|
UAVTalkGetStats(uavTalkCon, &utalkStats);
|
2013-06-17 16:06:47 +02:00
|
|
|
#ifdef PIOS_INCLUDE_RFM22B
|
|
|
|
UAVTalkAddStats(radioUavTalkCon, &utalkStats);
|
|
|
|
UAVTalkResetStats(radioUavTalkCon);
|
|
|
|
#endif
|
2013-05-19 16:37:30 +02:00
|
|
|
UAVTalkResetStats(uavTalkCon);
|
|
|
|
|
|
|
|
// Get object data
|
|
|
|
FlightTelemetryStatsGet(&flightStats);
|
|
|
|
GCSTelemetryStatsGet(&gcsStats);
|
|
|
|
|
|
|
|
// Update stats object
|
|
|
|
if (flightStats.Status == FLIGHTTELEMETRYSTATS_STATUS_CONNECTED) {
|
2014-01-14 22:46:01 +01:00
|
|
|
flightStats.TxDataRate = (float)utalkStats.txBytes / ((float)STATS_UPDATE_PERIOD_MS / 1000.0f);
|
|
|
|
flightStats.TxBytes += utalkStats.txBytes;
|
|
|
|
flightStats.TxFailures += txErrors;
|
|
|
|
flightStats.TxRetries += txRetries;
|
|
|
|
|
|
|
|
flightStats.RxDataRate = (float)utalkStats.rxBytes / ((float)STATS_UPDATE_PERIOD_MS / 1000.0f);
|
|
|
|
flightStats.RxBytes += utalkStats.rxBytes;
|
|
|
|
flightStats.RxFailures += utalkStats.rxErrors;
|
2013-12-19 00:17:08 +01:00
|
|
|
flightStats.RxSyncErrors += utalkStats.rxSyncErrors;
|
2014-01-14 22:46:01 +01:00
|
|
|
flightStats.RxCrcErrors += utalkStats.rxCrcErrors;
|
2013-05-19 16:37:30 +02:00
|
|
|
} else {
|
2014-01-14 22:46:01 +01:00
|
|
|
flightStats.TxDataRate = 0;
|
|
|
|
flightStats.TxBytes = 0;
|
|
|
|
flightStats.TxFailures = 0;
|
|
|
|
flightStats.TxRetries = 0;
|
|
|
|
|
|
|
|
flightStats.RxDataRate = 0;
|
|
|
|
flightStats.RxBytes = 0;
|
|
|
|
flightStats.RxFailures = 0;
|
2013-12-19 00:17:08 +01:00
|
|
|
flightStats.RxSyncErrors = 0;
|
2014-01-14 22:46:01 +01:00
|
|
|
flightStats.RxCrcErrors = 0;
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2014-01-14 22:46:01 +01:00
|
|
|
txErrors = 0;
|
2013-12-19 00:17:08 +01:00
|
|
|
txRetries = 0;
|
2013-05-19 16:37:30 +02:00
|
|
|
|
|
|
|
// Check for connection timeout
|
2014-01-14 22:46:01 +01:00
|
|
|
timeNow = xTaskGetTickCount() * portTICK_RATE_MS;
|
2013-05-19 16:37:30 +02:00
|
|
|
if (utalkStats.rxObjects > 0) {
|
|
|
|
timeOfLastObjectUpdate = timeNow;
|
|
|
|
}
|
|
|
|
if ((timeNow - timeOfLastObjectUpdate) > CONNECTION_TIMEOUT_MS) {
|
|
|
|
connectionTimeout = 1;
|
|
|
|
} else {
|
|
|
|
connectionTimeout = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
if (gcsStats.Status != GCSTELEMETRYSTATS_STATUS_CONNECTED || connectionTimeout) {
|
|
|
|
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-09-27 09:28:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-10-31 22:04:17 +01:00
|
|
|
* Update the telemetry settings, called on startup.
|
|
|
|
* FIXME: This should be in the TelemetrySettings object. But objects
|
|
|
|
* have too much overhead yet. Also the telemetry has no any specific
|
|
|
|
* settings, etc. Thus the HwSettings object which contains the
|
|
|
|
* telemetry port speed is used for now.
|
2010-09-27 09:28:34 +02:00
|
|
|
*/
|
|
|
|
static void updateSettings()
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
if (telemetryPort) {
|
|
|
|
// Retrieve settings
|
|
|
|
uint8_t speed;
|
|
|
|
HwSettingsTelemetrySpeedGet(&speed);
|
|
|
|
|
|
|
|
// Set port speed
|
|
|
|
switch (speed) {
|
|
|
|
case HWSETTINGS_TELEMETRYSPEED_2400:
|
|
|
|
PIOS_COM_ChangeBaud(telemetryPort, 2400);
|
|
|
|
break;
|
|
|
|
case HWSETTINGS_TELEMETRYSPEED_4800:
|
|
|
|
PIOS_COM_ChangeBaud(telemetryPort, 4800);
|
|
|
|
break;
|
|
|
|
case HWSETTINGS_TELEMETRYSPEED_9600:
|
|
|
|
PIOS_COM_ChangeBaud(telemetryPort, 9600);
|
|
|
|
break;
|
|
|
|
case HWSETTINGS_TELEMETRYSPEED_19200:
|
|
|
|
PIOS_COM_ChangeBaud(telemetryPort, 19200);
|
|
|
|
break;
|
|
|
|
case HWSETTINGS_TELEMETRYSPEED_38400:
|
|
|
|
PIOS_COM_ChangeBaud(telemetryPort, 38400);
|
|
|
|
break;
|
|
|
|
case HWSETTINGS_TELEMETRYSPEED_57600:
|
|
|
|
PIOS_COM_ChangeBaud(telemetryPort, 57600);
|
|
|
|
break;
|
|
|
|
case HWSETTINGS_TELEMETRYSPEED_115200:
|
|
|
|
PIOS_COM_ChangeBaud(telemetryPort, 115200);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-09-27 09:28:34 +02:00
|
|
|
}
|
|
|
|
|
2012-08-25 20:45:43 +02:00
|
|
|
/**
|
2013-05-19 16:37:30 +02:00
|
|
|
* Determine input/output com port as highest priority available
|
2013-06-17 16:06:47 +02:00
|
|
|
* @param[in] input Returns the approproate input com port if true, else the appropriate output com port
|
2012-08-25 20:45:43 +02:00
|
|
|
*/
|
2013-06-17 16:06:47 +02:00
|
|
|
static uint32_t getComPort(bool input)
|
2013-05-19 16:37:30 +02:00
|
|
|
{
|
2013-06-17 16:06:47 +02:00
|
|
|
#ifndef PIOS_INCLUDE_RFM22B
|
|
|
|
input = false;
|
|
|
|
#endif
|
2012-08-25 20:45:43 +02:00
|
|
|
#if defined(PIOS_INCLUDE_USB)
|
2013-05-19 16:37:30 +02:00
|
|
|
if (PIOS_COM_Available(PIOS_COM_TELEM_USB)) {
|
|
|
|
return PIOS_COM_TELEM_USB;
|
|
|
|
} else
|
2012-08-25 20:45:43 +02:00
|
|
|
#endif /* PIOS_INCLUDE_USB */
|
2013-06-17 16:06:47 +02:00
|
|
|
if (!input && PIOS_COM_Available(telemetryPort)) {
|
2013-05-19 16:37:30 +02:00
|
|
|
return telemetryPort;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2012-08-25 20:45:43 +02:00
|
|
|
}
|
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
/**
|
2013-05-19 16:37:30 +02:00
|
|
|
* @}
|
|
|
|
* @}
|
|
|
|
*/
|