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
|
2015-08-12 14:02:29 +02:00
|
|
|
* @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2015.
|
|
|
|
* The OpenPilot Team, http://www.openpilot.org Copyright (C) 2015.
|
2010-09-27 09:28:34 +02:00
|
|
|
* @brief Telemetry module, handles telemetry and UAVObject updates
|
2015-08-12 14:02:29 +02:00
|
|
|
*
|
2010-09-27 09:28:34 +02:00
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
|
2015-03-15 00:58:18 +01:00
|
|
|
/* Telemetry uses four tasks. Two are created for the main telemetry
|
|
|
|
* stream called "TelTx" and "TelRx". Two are created to handle the OPLink
|
2015-04-03 13:37:37 +02:00
|
|
|
* radio connection, called "RadioTx" and "RadioRx", the latter being
|
2015-03-15 00:58:18 +01:00
|
|
|
* overridden by USB if connected.
|
|
|
|
*
|
2015-03-16 00:11:42 +01:00
|
|
|
* The following code uses a "local" prefix to refer to the telemetry channel
|
|
|
|
* associated with a port on the FC and a "radio" prefix to refer to the
|
|
|
|
* telemetry channel associated with the OPLink/USB.
|
|
|
|
*
|
|
|
|
* The "local" telemetry port to use is defined by PIOS_COM_TELEM_RF in
|
2015-03-15 00:58:18 +01:00
|
|
|
* PIOS_Board_Init().
|
|
|
|
*
|
2015-03-16 00:11:42 +01:00
|
|
|
* A UAVTalk connection instance, telemUavTalkCon, is associated with the
|
|
|
|
* "local" channel and another, radioUavTalkCon, with the "radio" channel.
|
2015-03-15 00:58:18 +01:00
|
|
|
* Associated with each instance is a transmit routine which will send data
|
|
|
|
* to the appropriate port.
|
|
|
|
*
|
|
|
|
* Data is passed on the telemetry channels using queues. If
|
|
|
|
* PIOS_TELEM_PRIORITY_QUEUE is defined then two queues are created, one normal
|
|
|
|
* priority and the other high priority.
|
|
|
|
*
|
|
|
|
* The "Tx" tasks read events first from the priority queue and then from
|
|
|
|
* the normal queue, passing each event to processObjEvent() which ultimately
|
|
|
|
* passes each event to the UAVTalk library which results in the appropriate
|
|
|
|
* transmit routine being called to send the data back to the recipient on
|
2015-03-16 00:11:42 +01:00
|
|
|
* the "local" or "radio" link.
|
2015-03-15 00:58:18 +01:00
|
|
|
*/
|
|
|
|
|
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
|
2014-04-06 19:26:48 +02:00
|
|
|
#define MAX_QUEUE_SIZE TELEM_QUEUE_SIZE
|
|
|
|
// Three different stack size parameter are accepted for Telemetry(RX PIOS_TELEM_RX_STACK_SIZE)
|
|
|
|
// Tx(PIOS_TELEM_TX_STACK_SIZE) and Radio RX(PIOS_TELEM_RADIO_RX_STACK_SIZE)
|
2014-04-06 19:12:00 +02:00
|
|
|
#ifdef PIOS_TELEM_RX_STACK_SIZE
|
|
|
|
#define STACK_SIZE_RX_BYTES PIOS_TELEM_RX_STACK_SIZE
|
|
|
|
#define STACK_SIZE_TX_BYTES PIOS_TELEM_TX_STACK_SIZE
|
|
|
|
#else
|
|
|
|
#define STACK_SIZE_RX_BYTES PIOS_TELEM_STACK_SIZE
|
|
|
|
#define STACK_SIZE_TX_BYTES PIOS_TELEM_STACK_SIZE
|
|
|
|
#endif
|
2014-04-06 19:26:48 +02:00
|
|
|
|
|
|
|
#ifdef PIOS_TELEM_RADIO_RX_STACK_SIZE
|
|
|
|
#define STACK_SIZE_RADIO_RX_BYTES PIOS_TELEM_RADIO_RX_STACK_SIZE
|
2015-03-15 00:58:18 +01:00
|
|
|
#define STACK_SIZE_RADIO_TX_BYTES PIOS_TELEM_RADIO_TX_STACK_SIZE
|
2014-04-06 19:26:48 +02:00
|
|
|
#else
|
|
|
|
#define STACK_SIZE_RADIO_RX_BYTES STACK_SIZE_RX_BYTES
|
2015-03-15 00:58:18 +01:00
|
|
|
#define STACK_SIZE_RADIO_TX_BYTES STACK_SIZE_TX_BYTES
|
2014-04-06 19:26:48 +02:00
|
|
|
#endif
|
|
|
|
#define TASK_PRIORITY_RX (tskIDLE_PRIORITY + 2)
|
|
|
|
#define TASK_PRIORITY_TX (tskIDLE_PRIORITY + 2)
|
|
|
|
#define TASK_PRIORITY_RADRX (tskIDLE_PRIORITY + 2)
|
2015-03-15 00:58:18 +01:00
|
|
|
#define TASK_PRIORITY_RADTX (tskIDLE_PRIORITY + 2)
|
2014-04-06 19:26:48 +02:00
|
|
|
#define REQ_TIMEOUT_MS 250
|
|
|
|
#define MAX_RETRIES 2
|
|
|
|
#define STATS_UPDATE_PERIOD_MS 4000
|
|
|
|
#define CONNECTION_TIMEOUT_MS 8000
|
2010-09-27 09:28:34 +02:00
|
|
|
|
2015-07-16 01:01:02 +02:00
|
|
|
#ifdef PIOS_INCLUDE_RFM22B
|
|
|
|
#define HAS_RADIO
|
|
|
|
#endif
|
2015-11-14 14:27:12 +01:00
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
// Private types
|
2015-03-15 00:58:18 +01:00
|
|
|
typedef struct {
|
2015-03-15 19:19:19 +01:00
|
|
|
// Determine port on which to communicate telemetry information
|
2015-03-16 00:11:42 +01:00
|
|
|
uint32_t (*getPort)();
|
2015-03-15 00:58:18 +01:00
|
|
|
// Main telemetry queue
|
2015-03-17 21:52:08 +01:00
|
|
|
xQueueHandle queue;
|
2015-11-14 14:27:12 +01:00
|
|
|
|
2015-03-15 19:19:19 +01:00
|
|
|
#ifdef PIOS_TELEM_PRIORITY_QUEUE
|
2015-03-15 00:58:18 +01:00
|
|
|
// Priority telemetry queue
|
|
|
|
xQueueHandle priorityQueue;
|
2015-03-15 19:19:19 +01:00
|
|
|
#endif /* PIOS_TELEM_PRIORITY_QUEUE */
|
2015-11-14 14:27:12 +01:00
|
|
|
|
2015-03-15 00:58:18 +01:00
|
|
|
// Transmit/receive task handles
|
2016-01-04 21:41:45 +01:00
|
|
|
xTaskHandle txTaskHandle;
|
|
|
|
xTaskHandle rxTaskHandle;
|
2015-03-15 00:58:18 +01:00
|
|
|
// Telemetry stream
|
|
|
|
UAVTalkConnection uavTalkCon;
|
2015-03-16 00:11:42 +01:00
|
|
|
} channelContext;
|
2015-11-14 14:27:12 +01:00
|
|
|
|
2015-07-16 01:01:02 +02:00
|
|
|
#ifdef HAS_RADIO
|
2015-03-15 19:19:19 +01:00
|
|
|
// Main telemetry channel
|
2015-03-16 00:11:42 +01:00
|
|
|
static channelContext localChannel;
|
|
|
|
static int32_t transmitLocalData(uint8_t *data, int32_t length);
|
|
|
|
static void registerLocalObject(UAVObjHandle obj);
|
|
|
|
static uint32_t localPort();
|
2015-11-14 14:27:12 +01:00
|
|
|
#endif /* ifdef HAS_RADIO */
|
|
|
|
|
2015-11-13 15:18:17 +01:00
|
|
|
static void updateSettings(channelContext *channel);
|
2010-09-27 09:28:34 +02:00
|
|
|
|
2015-03-15 19:19:19 +01:00
|
|
|
// OPLink telemetry channel
|
2015-03-16 00:11:42 +01:00
|
|
|
static channelContext radioChannel;
|
2015-03-15 00:58:18 +01:00
|
|
|
static int32_t transmitRadioData(uint8_t *data, int32_t length);
|
|
|
|
static void registerRadioObject(UAVObjHandle obj);
|
2015-03-15 19:19:19 +01:00
|
|
|
static uint32_t radioPort();
|
2015-05-24 11:24:18 +02:00
|
|
|
static uint32_t radio_port;
|
2015-05-24 11:18:54 +02:00
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
|
2015-03-15 00:58:18 +01:00
|
|
|
// Telemetry stats
|
2010-09-27 09:28:34 +02:00
|
|
|
static uint32_t txErrors;
|
|
|
|
static uint32_t txRetries;
|
|
|
|
static uint32_t timeOfLastObjectUpdate;
|
|
|
|
|
|
|
|
static void telemetryTxTask(void *parameters);
|
|
|
|
static void telemetryRxTask(void *parameters);
|
2015-03-15 00:58:18 +01:00
|
|
|
static void updateObject(
|
2015-03-16 00:11:42 +01:00
|
|
|
channelContext *channel,
|
2015-03-15 00:58:18 +01:00
|
|
|
UAVObjHandle obj,
|
|
|
|
int32_t eventType);
|
|
|
|
static void processObjEvent(
|
2015-03-16 00:11:42 +01:00
|
|
|
channelContext *channel,
|
2015-03-15 00:58:18 +01:00
|
|
|
UAVObjEvent *ev);
|
|
|
|
static int32_t setUpdatePeriod(
|
2015-03-16 00:11:42 +01:00
|
|
|
channelContext *channel,
|
2015-03-15 00:58:18 +01:00
|
|
|
UAVObjHandle obj,
|
|
|
|
int32_t updatePeriodMs);
|
|
|
|
static int32_t setLoggingPeriod(
|
2015-03-16 00:11:42 +01:00
|
|
|
channelContext *channel,
|
2015-03-15 00:58:18 +01:00
|
|
|
UAVObjHandle obj,
|
|
|
|
int32_t updatePeriodMs);
|
2010-09-27 09:28:34 +02:00
|
|
|
static void updateTelemetryStats();
|
|
|
|
static void gcsTelemetryStatsUpdated();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2015-07-16 01:01:02 +02:00
|
|
|
#ifdef HAS_RADIO
|
2015-04-11 11:06:19 +02:00
|
|
|
// Only start the local telemetry tasks if needed
|
|
|
|
if (localPort()) {
|
|
|
|
UAVObjIterate(®isterLocalObject);
|
|
|
|
|
|
|
|
// Listen to objects of interest
|
|
|
|
#ifdef PIOS_TELEM_PRIORITY_QUEUE
|
|
|
|
GCSTelemetryStatsConnectQueue(localChannel.priorityQueue);
|
|
|
|
#else /* PIOS_TELEM_PRIORITY_QUEUE */
|
|
|
|
GCSTelemetryStatsConnectQueue(localChannel.queue);
|
|
|
|
#endif /* PIOS_TELEM_PRIORITY_QUEUE */
|
|
|
|
// Start telemetry tasks
|
|
|
|
xTaskCreate(telemetryTxTask,
|
|
|
|
"TelTx",
|
|
|
|
STACK_SIZE_TX_BYTES / 4,
|
|
|
|
&localChannel,
|
|
|
|
TASK_PRIORITY_TX,
|
|
|
|
&localChannel.txTaskHandle);
|
|
|
|
PIOS_TASK_MONITOR_RegisterTask(TASKINFO_RUNNING_TELEMETRYTX,
|
|
|
|
localChannel.txTaskHandle);
|
|
|
|
xTaskCreate(telemetryRxTask,
|
|
|
|
"TelRx",
|
|
|
|
STACK_SIZE_RX_BYTES / 4,
|
|
|
|
&localChannel,
|
|
|
|
TASK_PRIORITY_RX,
|
|
|
|
&localChannel.rxTaskHandle);
|
|
|
|
PIOS_TASK_MONITOR_RegisterTask(TASKINFO_RUNNING_TELEMETRYRX,
|
|
|
|
localChannel.rxTaskHandle);
|
|
|
|
}
|
2015-07-16 01:01:02 +02:00
|
|
|
#endif /* ifdef HAS_RADIO */
|
2015-11-14 14:27:12 +01:00
|
|
|
|
2015-09-05 11:49:43 +02:00
|
|
|
// Start the telemetry tasks associated with Radio/USB
|
2015-03-15 00:58:18 +01:00
|
|
|
UAVObjIterate(®isterRadioObject);
|
2013-05-19 16:37:30 +02:00
|
|
|
|
|
|
|
// Listen to objects of interest
|
2015-03-15 00:58:18 +01:00
|
|
|
#ifdef PIOS_TELEM_PRIORITY_QUEUE
|
2015-03-16 00:11:42 +01:00
|
|
|
GCSTelemetryStatsConnectQueue(radioChannel.priorityQueue);
|
2015-03-15 00:58:18 +01:00
|
|
|
#else /* PIOS_TELEM_PRIORITY_QUEUE */
|
2015-03-17 21:52:08 +01:00
|
|
|
GCSTelemetryStatsConnectQueue(radioChannel.queue);
|
2015-03-15 00:58:18 +01:00
|
|
|
#endif /* PIOS_TELEM_PRIORITY_QUEUE */
|
2013-05-19 16:37:30 +02:00
|
|
|
|
2015-03-15 00:58:18 +01:00
|
|
|
xTaskCreate(telemetryTxTask,
|
|
|
|
"RadioTx",
|
|
|
|
STACK_SIZE_RADIO_TX_BYTES / 4,
|
2015-03-16 00:11:42 +01:00
|
|
|
&radioChannel,
|
2015-03-15 00:58:18 +01:00
|
|
|
TASK_PRIORITY_RADTX,
|
2015-03-16 00:11:42 +01:00
|
|
|
&radioChannel.txTaskHandle);
|
2015-04-03 13:37:37 +02:00
|
|
|
PIOS_TASK_MONITOR_RegisterTask(TASKINFO_RUNNING_RADIOTX,
|
2015-03-16 00:11:42 +01:00
|
|
|
radioChannel.txTaskHandle);
|
2015-03-15 00:58:18 +01:00
|
|
|
xTaskCreate(telemetryRxTask,
|
|
|
|
"RadioRx",
|
|
|
|
STACK_SIZE_RADIO_RX_BYTES / 4,
|
2015-03-16 00:11:42 +01:00
|
|
|
&radioChannel,
|
2015-03-15 00:58:18 +01:00
|
|
|
TASK_PRIORITY_RADRX,
|
2015-03-16 00:11:42 +01:00
|
|
|
&radioChannel.rxTaskHandle);
|
2015-03-15 00:58:18 +01:00
|
|
|
PIOS_TASK_MONITOR_RegisterTask(TASKINFO_RUNNING_RADIORX,
|
2015-03-16 00:11:42 +01:00
|
|
|
radioChannel.rxTaskHandle);
|
2013-05-19 16:37:30 +02:00
|
|
|
|
|
|
|
return 0;
|
2011-06-20 07:35:40 +02:00
|
|
|
}
|
|
|
|
|
2015-04-11 11:06:19 +02:00
|
|
|
/* Intialise a telemetry channel */
|
|
|
|
void TelemetryInitializeChannel(channelContext *channel)
|
|
|
|
{
|
|
|
|
// Create object queues
|
|
|
|
channel->queue = xQueueCreate(MAX_QUEUE_SIZE,
|
|
|
|
sizeof(UAVObjEvent));
|
2015-11-14 14:27:12 +01:00
|
|
|
|
2015-04-11 11:06:19 +02:00
|
|
|
#if defined(PIOS_TELEM_PRIORITY_QUEUE)
|
|
|
|
channel->priorityQueue = xQueueCreate(MAX_QUEUE_SIZE,
|
|
|
|
sizeof(UAVObjEvent));
|
|
|
|
#endif /* PIOS_TELEM_PRIORITY_QUEUE */
|
2013-06-17 16:06:47 +02:00
|
|
|
|
2015-04-11 11:06:19 +02:00
|
|
|
// Create periodic event that will be used to update the telemetry stats
|
|
|
|
UAVObjEvent ev;
|
|
|
|
memset(&ev, 0, sizeof(UAVObjEvent));
|
|
|
|
|
|
|
|
#ifdef PIOS_TELEM_PRIORITY_QUEUE
|
|
|
|
EventPeriodicQueueCreate(&ev,
|
|
|
|
channel->priorityQueue,
|
|
|
|
STATS_UPDATE_PERIOD_MS);
|
|
|
|
#else /* PIOS_TELEM_PRIORITY_QUEUE */
|
|
|
|
EventPeriodicQueueCreate(&ev,
|
|
|
|
channel->queue,
|
|
|
|
STATS_UPDATE_PERIOD_MS);
|
|
|
|
#endif /* PIOS_TELEM_PRIORITY_QUEUE */
|
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)
|
|
|
|
{
|
2015-04-11 11:06:19 +02:00
|
|
|
HwSettingsInitialize();
|
|
|
|
|
2015-05-24 11:18:54 +02:00
|
|
|
#ifdef PIOS_INCLUDE_RFM22B
|
|
|
|
OPLinkSettingsInitialize();
|
|
|
|
OPLinkSettingsData data;
|
|
|
|
|
|
|
|
OPLinkSettingsGet(&data);
|
|
|
|
if (data.PPMOnly) {
|
|
|
|
radio_port = 0;
|
|
|
|
} else {
|
|
|
|
radio_port = PIOS_COM_RF;
|
|
|
|
}
|
|
|
|
#else /* PIOS_INCLUDE_RFM22B */
|
2015-07-16 01:01:02 +02:00
|
|
|
radio_port = PIOS_COM_TELEM_RF;
|
2015-05-24 11:18:54 +02:00
|
|
|
#endif /* PIOS_INCLUDE_RFM22B */
|
|
|
|
|
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
|
|
|
|
2015-04-11 11:06:19 +02:00
|
|
|
// Reset link stats
|
|
|
|
txErrors = 0;
|
|
|
|
txRetries = 0;
|
2015-11-14 14:27:12 +01:00
|
|
|
|
2015-07-16 01:01:02 +02:00
|
|
|
#ifdef HAS_RADIO
|
2015-03-15 19:19:19 +01:00
|
|
|
// Set channel port handlers
|
2015-03-16 00:11:42 +01:00
|
|
|
localChannel.getPort = localPort;
|
2013-05-19 16:37:30 +02:00
|
|
|
|
2015-04-11 11:06:19 +02:00
|
|
|
// Set the local telemetry baud rate
|
2015-03-16 00:11:42 +01:00
|
|
|
updateSettings(&localChannel);
|
2013-05-19 16:37:30 +02:00
|
|
|
|
2015-04-11 11:06:19 +02:00
|
|
|
// Only initialise local channel if telemetry port enabled
|
|
|
|
if (localPort()) {
|
|
|
|
// Initialise channel
|
|
|
|
TelemetryInitializeChannel(&localChannel);
|
|
|
|
// Initialise UAVTalk
|
|
|
|
localChannel.uavTalkCon = UAVTalkInitialize(&transmitLocalData);
|
|
|
|
}
|
2015-11-13 15:18:17 +01:00
|
|
|
#endif /* ifdef HAS_RADIO */
|
2015-11-14 14:27:12 +01:00
|
|
|
|
2015-07-16 01:01:02 +02:00
|
|
|
// Set channel port handlers
|
|
|
|
radioChannel.getPort = radioPort;
|
2015-11-13 15:18:17 +01:00
|
|
|
|
|
|
|
// Set the channel port baud rate
|
|
|
|
updateSettings(&radioChannel);
|
|
|
|
|
2015-04-11 11:06:19 +02:00
|
|
|
// Initialise channel
|
|
|
|
TelemetryInitializeChannel(&radioChannel);
|
2013-05-19 16:37:30 +02:00
|
|
|
// Initialise UAVTalk
|
2015-03-16 00:11:42 +01:00
|
|
|
radioChannel.uavTalkCon = UAVTalkInitialize(&transmitRadioData);
|
2013-05-19 16:37:30 +02:00
|
|
|
|
|
|
|
return 0;
|
2010-09-27 09:28:34 +02:00
|
|
|
}
|
|
|
|
|
2013-06-04 05:37:40 +02:00
|
|
|
MODULE_INITCALL(TelemetryInitialize, TelemetryStart);
|
2015-11-14 14:27:12 +01:00
|
|
|
|
2015-07-16 01:01:02 +02:00
|
|
|
#ifdef HAS_RADIO
|
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
|
|
|
|
*/
|
2015-03-16 00:11:42 +01:00
|
|
|
static void registerLocalObject(UAVObjHandle obj)
|
2010-09-27 09:28:34 +02:00
|
|
|
{
|
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
|
2015-03-15 00:58:18 +01:00
|
|
|
#ifdef PIOS_TELEM_PRIORITY_QUEUE
|
2015-03-16 00:11:42 +01:00
|
|
|
UAVObjConnectQueue(obj, localChannel.priorityQueue, EV_MASK_ALL_UPDATES);
|
2015-03-15 00:58:18 +01:00
|
|
|
#else /* PIOS_TELEM_PRIORITY_QUEUE */
|
2015-03-17 21:52:08 +01:00
|
|
|
UAVObjConnectQueue(obj, localChannel.queue, EV_MASK_ALL_UPDATES);
|
2015-03-15 00:58:18 +01:00
|
|
|
#endif /* PIOS_TELEM_PRIORITY_QUEUE */
|
2013-05-19 16:37:30 +02:00
|
|
|
} else {
|
2013-11-17 17:15:23 +01:00
|
|
|
// Setup object for periodic updates
|
2015-03-15 00:58:18 +01:00
|
|
|
updateObject(
|
2015-03-16 00:11:42 +01:00
|
|
|
&localChannel,
|
2015-03-15 00:58:18 +01:00
|
|
|
obj,
|
|
|
|
EV_NONE);
|
|
|
|
}
|
|
|
|
}
|
2015-11-13 15:18:17 +01:00
|
|
|
#endif /* ifdef HAS_RADIO */
|
|
|
|
|
2015-03-15 00:58:18 +01:00
|
|
|
static void registerRadioObject(UAVObjHandle obj)
|
2010-09-27 09:28:34 +02:00
|
|
|
{
|
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
|
2015-03-15 00:58:18 +01:00
|
|
|
#ifdef PIOS_TELEM_PRIORITY_QUEUE
|
2015-03-16 00:11:42 +01:00
|
|
|
UAVObjConnectQueue(obj, radioChannel.priorityQueue, EV_MASK_ALL_UPDATES);
|
2015-03-15 00:58:18 +01:00
|
|
|
#else /* PIOS_TELEM_PRIORITY_QUEUE */
|
2015-03-17 21:52:08 +01:00
|
|
|
UAVObjConnectQueue(obj, radioChannel.queue, EV_MASK_ALL_UPDATES);
|
2015-03-15 00:58:18 +01:00
|
|
|
#endif /* PIOS_TELEM_PRIORITY_QUEUE */
|
2013-05-19 16:37:30 +02:00
|
|
|
} else {
|
2013-11-17 17:15:23 +01:00
|
|
|
// Setup object for periodic updates
|
2015-03-15 00:58:18 +01:00
|
|
|
updateObject(
|
2015-03-16 00:11:42 +01:00
|
|
|
&radioChannel,
|
2015-03-15 00:58:18 +01:00
|
|
|
obj,
|
|
|
|
EV_NONE);
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2010-09-27 09:28:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update object's queue connections and timer, depending on object's settings
|
2015-03-15 19:19:19 +01:00
|
|
|
* \param[in] telemetry channel context
|
2010-09-27 09:28:34 +02:00
|
|
|
* \param[in] obj Object to updates
|
|
|
|
*/
|
2015-03-15 00:58:18 +01:00
|
|
|
static void updateObject(
|
2015-03-16 00:11:42 +01:00
|
|
|
channelContext *channel,
|
2015-03-15 00:58:18 +01:00
|
|
|
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
|
2015-03-16 00:11:42 +01:00
|
|
|
setUpdatePeriod(channel,
|
2015-03-15 00:58:18 +01:00
|
|
|
obj,
|
|
|
|
metadata.telemetryUpdatePeriod);
|
2013-05-19 16:37:30 +02:00
|
|
|
// 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
|
2015-03-16 00:11:42 +01:00
|
|
|
setUpdatePeriod(channel, obj, 0);
|
2013-05-19 16:37:30 +02:00
|
|
|
// 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) {
|
2015-03-16 00:11:42 +01:00
|
|
|
setUpdatePeriod(channel,
|
2015-03-15 00:58:18 +01:00
|
|
|
obj,
|
|
|
|
metadata.telemetryUpdatePeriod);
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
|
|
|
} 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
|
2015-03-16 00:11:42 +01:00
|
|
|
setUpdatePeriod(channel, obj, 0);
|
2013-05-19 16:37:30 +02:00
|
|
|
// 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
|
2015-03-16 00:11:42 +01:00
|
|
|
setLoggingPeriod(channel, obj, metadata.loggingUpdatePeriod);
|
2013-11-17 17:15:23 +01:00
|
|
|
// Connect queue
|
|
|
|
eventMask |= EV_LOGGING_PERIODIC | EV_LOGGING_MANUAL;
|
|
|
|
break;
|
|
|
|
case UPDATEMODE_ONCHANGE:
|
|
|
|
// Set update period
|
2015-03-16 00:11:42 +01:00
|
|
|
setLoggingPeriod(channel, obj, 0);
|
2013-11-17 17:15:23 +01:00
|
|
|
// 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) {
|
2015-03-16 00:11:42 +01:00
|
|
|
setLoggingPeriod(channel,
|
2015-03-15 00:58:18 +01:00
|
|
|
obj,
|
|
|
|
metadata.loggingUpdatePeriod);
|
2013-11-17 17:15:23 +01:00
|
|
|
}
|
|
|
|
} 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
|
2015-03-16 00:11:42 +01:00
|
|
|
setLoggingPeriod(channel, obj, 0);
|
2013-11-17 17:15:23 +01:00
|
|
|
// Connect queue
|
|
|
|
eventMask |= EV_LOGGING_MANUAL;
|
|
|
|
break;
|
|
|
|
}
|
2015-03-15 00:58:18 +01:00
|
|
|
|
2014-02-07 18:36:33 +01:00
|
|
|
// note that all setting objects have implicitly IsPriority=true
|
2015-03-15 00:58:18 +01:00
|
|
|
#ifdef PIOS_TELEM_PRIORITY_QUEUE
|
2014-02-04 21:03:42 +01:00
|
|
|
if (UAVObjIsPriority(obj)) {
|
2015-03-16 00:11:42 +01:00
|
|
|
UAVObjConnectQueue(obj, channel->priorityQueue, eventMask);
|
2015-03-15 00:58:18 +01:00
|
|
|
} else
|
|
|
|
#endif /* PIOS_TELEM_PRIORITY_QUEUE */
|
2015-11-14 14:27:12 +01:00
|
|
|
|
2015-03-17 21:52:08 +01:00
|
|
|
UAVObjConnectQueue(obj, channel->queue, eventMask);
|
2010-09-27 09:28:34 +02:00
|
|
|
}
|
|
|
|
|
2015-03-15 00:58:18 +01:00
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
/**
|
|
|
|
* Processes queue events
|
|
|
|
*/
|
2015-03-15 00:58:18 +01:00
|
|
|
static void processObjEvent(
|
2015-03-16 00:11:42 +01:00
|
|
|
channelContext *channel,
|
2015-03-15 00:58:18 +01:00
|
|
|
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
|
2015-03-16 00:11:42 +01:00
|
|
|
success = UAVTalkSendObject(channel->uavTalkCon,
|
2015-03-15 00:58:18 +01:00
|
|
|
ev->obj,
|
|
|
|
ev->instId,
|
|
|
|
UAVObjGetTelemetryAcked(&metadata), REQ_TIMEOUT_MS);
|
2013-12-19 00:17:08 +01:00
|
|
|
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
|
2015-03-16 00:11:42 +01:00
|
|
|
success = UAVTalkSendObjectRequest(channel->uavTalkCon,
|
2015-03-15 00:58:18 +01:00
|
|
|
ev->obj,
|
|
|
|
ev->instId,
|
|
|
|
REQ_TIMEOUT_MS);
|
2013-12-19 00:17:08 +01:00
|
|
|
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
|
2015-03-15 00:58:18 +01:00
|
|
|
updateObject(
|
2015-03-16 00:11:42 +01:00
|
|
|
channel,
|
2015-03-15 00:58:18 +01:00
|
|
|
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.
|
2015-03-15 00:58:18 +01:00
|
|
|
updateObject(
|
2015-03-16 00:11:42 +01:00
|
|
|
channel,
|
2015-03-15 00:58:18 +01:00
|
|
|
ev->obj,
|
|
|
|
ev->event);
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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.
|
2015-03-15 00:58:18 +01:00
|
|
|
updateObject(
|
2015-03-16 00:11:42 +01:00
|
|
|
channel,
|
2015-03-15 00:58:18 +01:00
|
|
|
ev->obj,
|
|
|
|
ev->event);
|
2013-11-17 17:15:23 +01:00
|
|
|
}
|
|
|
|
}
|
2010-09-27 09:28:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Telemetry transmit task, regular priority
|
|
|
|
*/
|
2015-03-15 00:58:18 +01:00
|
|
|
static void telemetryTxTask(void *parameters)
|
2010-09-27 09:28:34 +02:00
|
|
|
{
|
2015-03-16 00:11:42 +01:00
|
|
|
channelContext *channel = (channelContext *)parameters;
|
2013-05-19 16:37:30 +02:00
|
|
|
UAVObjEvent ev;
|
|
|
|
|
2015-03-15 00:58:18 +01:00
|
|
|
/* Check for a bad context */
|
2015-03-16 00:11:42 +01:00
|
|
|
if (!channel) {
|
2015-03-15 00:58:18 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Loop forever
|
|
|
|
while (1) {
|
2014-02-04 20:49:36 +01:00
|
|
|
/**
|
|
|
|
* Tries to empty the high priority queue before handling any standard priority item
|
|
|
|
*/
|
2015-11-14 14:27:12 +01:00
|
|
|
|
2015-03-15 00:58:18 +01:00
|
|
|
#ifdef PIOS_TELEM_PRIORITY_QUEUE
|
2014-04-05 12:53:31 +02:00
|
|
|
// empty priority queue, non-blocking
|
2015-03-16 00:11:42 +01:00
|
|
|
while (xQueueReceive(channel->priorityQueue, &ev, 0) == pdTRUE) {
|
2014-02-04 21:03:42 +01:00
|
|
|
// Process event
|
2015-03-16 00:11:42 +01:00
|
|
|
processObjEvent(channel, &ev);
|
2014-02-04 21:03:42 +01:00
|
|
|
}
|
2014-04-05 12:53:31 +02:00
|
|
|
// check regular queue and process update - non-blocking
|
2015-03-17 21:52:08 +01:00
|
|
|
if (xQueueReceive(channel->queue, &ev, 0) == pdTRUE) {
|
2013-05-19 16:37:30 +02:00
|
|
|
// Process event
|
2015-03-16 00:11:42 +01:00
|
|
|
processObjEvent(channel, &ev);
|
2014-04-05 12:53:31 +02:00
|
|
|
// if both queues are empty, wait on priority queue for updates (1 tick) then repeat cycle
|
2015-03-16 00:11:42 +01:00
|
|
|
} else if (xQueueReceive(channel->priorityQueue, &ev, 1) == pdTRUE) {
|
2014-04-05 12:53:31 +02:00
|
|
|
// Process event
|
2015-03-16 00:11:42 +01:00
|
|
|
processObjEvent(channel, &ev);
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2014-04-05 12:53:31 +02:00
|
|
|
#else
|
|
|
|
// wait on queue for updates (1 tick) then repeat cycle
|
2015-03-17 21:52:08 +01:00
|
|
|
if (xQueueReceive(channel->queue, &ev, 1) == pdTRUE) {
|
2014-04-05 12:53:31 +02:00
|
|
|
// Process event
|
2015-03-16 00:11:42 +01:00
|
|
|
processObjEvent(channel, &ev);
|
2014-04-05 12:53:31 +02:00
|
|
|
}
|
2015-03-15 00:58:18 +01:00
|
|
|
#endif /* PIOS_TELEM_PRIORITY_QUEUE */
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2010-09-27 09:28:34 +02:00
|
|
|
}
|
2014-02-04 20:49:36 +01:00
|
|
|
|
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
|
|
|
*/
|
2015-03-15 00:58:18 +01:00
|
|
|
static void telemetryRxTask(void *parameters)
|
2010-09-27 09:28:34 +02:00
|
|
|
{
|
2015-03-16 00:11:42 +01:00
|
|
|
channelContext *channel = (channelContext *)parameters;
|
2015-03-15 00:58:18 +01:00
|
|
|
|
|
|
|
/* Check for a bad context */
|
2015-03-16 00:11:42 +01:00
|
|
|
if (!channel) {
|
2015-03-15 00:58:18 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// Task loop
|
|
|
|
while (1) {
|
2015-03-16 00:11:42 +01:00
|
|
|
uint32_t inputPort = channel->getPort();
|
2013-05-19 16:37:30 +02:00
|
|
|
|
|
|
|
if (inputPort) {
|
|
|
|
// Block until data are available
|
2015-03-10 20:35:40 +01:00
|
|
|
uint8_t serial_data[16];
|
2013-05-19 16:37:30 +02:00
|
|
|
uint16_t bytes_to_process;
|
|
|
|
|
|
|
|
bytes_to_process = PIOS_COM_ReceiveBuffer(inputPort, serial_data, sizeof(serial_data), 500);
|
|
|
|
if (bytes_to_process > 0) {
|
2015-04-18 19:10:13 +02:00
|
|
|
UAVTalkProcessInputStream(channel->uavTalkCon, serial_data, bytes_to_process);
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
vTaskDelay(5);
|
|
|
|
}
|
|
|
|
}
|
2010-09-27 09:28:34 +02:00
|
|
|
}
|
|
|
|
|
2015-07-16 01:01:02 +02:00
|
|
|
#ifdef HAS_RADIO
|
2013-06-17 16:06:47 +02:00
|
|
|
/**
|
2015-03-15 19:19:19 +01:00
|
|
|
* Determine the port to be used for communication on the telemetry channel
|
|
|
|
* \return com port number
|
2013-06-17 16:06:47 +02:00
|
|
|
*/
|
2015-03-16 00:11:42 +01:00
|
|
|
static uint32_t localPort()
|
2013-06-17 16:06:47 +02:00
|
|
|
{
|
2015-03-15 19:19:19 +01:00
|
|
|
return PIOS_COM_TELEM_RF;
|
|
|
|
}
|
2015-11-14 14:27:12 +01:00
|
|
|
|
2015-11-13 15:18:17 +01:00
|
|
|
#endif /* ifdef HAS_RADIO */
|
2015-03-15 19:19:19 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine the port to be used for communication on the radio channel
|
|
|
|
* \return com port number
|
|
|
|
*/
|
|
|
|
static uint32_t radioPort()
|
|
|
|
{
|
2015-05-24 11:18:54 +02:00
|
|
|
uint32_t port = radio_port;
|
|
|
|
|
2015-03-15 19:19:19 +01:00
|
|
|
#ifdef PIOS_INCLUDE_USB
|
|
|
|
// if USB is connected, USB takes precedence for telemetry
|
|
|
|
if (PIOS_COM_Available(PIOS_COM_TELEM_USB)) {
|
|
|
|
port = PIOS_COM_TELEM_USB;
|
2013-06-17 16:06:47 +02:00
|
|
|
}
|
2015-03-15 19:19:19 +01:00
|
|
|
#endif /* PIOS_INCLUDE_USB */
|
|
|
|
|
|
|
|
return port;
|
2013-06-17 16:06:47 +02:00
|
|
|
}
|
2015-03-15 19:19:19 +01:00
|
|
|
|
2015-07-16 01:01:02 +02:00
|
|
|
#ifdef HAS_RADIO
|
2014-04-05 12:53:31 +02:00
|
|
|
/**
|
2015-03-15 00:58:18 +01:00
|
|
|
* Transmit data buffer to the modem or USB port.
|
2014-04-05 12:53:31 +02:00
|
|
|
* \param[in] data Data buffer to send
|
|
|
|
* \param[in] length Length of buffer
|
|
|
|
* \return -1 on failure
|
|
|
|
* \return number of bytes transmitted on success
|
|
|
|
*/
|
2015-03-16 00:11:42 +01:00
|
|
|
static int32_t transmitLocalData(uint8_t *data, int32_t length)
|
2014-04-05 12:53:31 +02:00
|
|
|
{
|
2015-03-16 00:11:42 +01:00
|
|
|
uint32_t outputPort = localChannel.getPort();
|
2015-03-15 00:58:18 +01:00
|
|
|
|
|
|
|
if (outputPort) {
|
|
|
|
return PIOS_COM_SendBuffer(outputPort, data, length);
|
2014-04-05 12:53:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2015-11-13 15:18:17 +01:00
|
|
|
#endif /* ifdef HAS_RADIO */
|
2013-06-17 16:06:47 +02:00
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
/**
|
2015-03-15 00:58:18 +01:00
|
|
|
* Transmit data buffer to the radioport.
|
2010-09-27 09:28:34 +02:00
|
|
|
* \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
|
|
|
*/
|
2015-03-15 00:58:18 +01:00
|
|
|
static int32_t transmitRadioData(uint8_t *data, int32_t length)
|
2010-09-27 09:28:34 +02:00
|
|
|
{
|
2015-03-16 00:11:42 +01:00
|
|
|
uint32_t outputPort = radioChannel.getPort();
|
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)
|
2015-03-15 19:19:19 +01:00
|
|
|
* \param[in] telemetry channel context
|
2010-09-27 09:28:34 +02:00
|
|
|
* \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
|
|
|
|
*/
|
2015-03-15 00:58:18 +01:00
|
|
|
static int32_t setUpdatePeriod(
|
2015-03-16 00:11:42 +01:00
|
|
|
channelContext *channel,
|
2015-03-15 00:58:18 +01:00
|
|
|
UAVObjHandle obj,
|
|
|
|
int32_t updatePeriodMs)
|
2010-09-27 09:28:34 +02:00
|
|
|
{
|
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-02-18 01:03:57 +01:00
|
|
|
ev.lowPriority = true;
|
2014-01-06 21:48:34 +01:00
|
|
|
|
2015-03-15 00:58:18 +01:00
|
|
|
#ifdef PIOS_TELEM_PRIORITY_QUEUE
|
2015-03-16 00:11:42 +01:00
|
|
|
xQueueHandle targetQueue = UAVObjIsPriority(obj) ? channel->priorityQueue :
|
2015-03-17 21:52:08 +01:00
|
|
|
channel->queue;
|
2015-03-15 00:58:18 +01:00
|
|
|
#else /* PIOS_TELEM_PRIORITY_QUEUE */
|
2015-03-17 21:52:08 +01:00
|
|
|
xQueueHandle targetQueue = channel->queue;
|
2015-03-15 00:58:18 +01:00
|
|
|
#endif /* PIOS_TELEM_PRIORITY_QUEUE */
|
2014-02-07 18:36:33 +01:00
|
|
|
|
|
|
|
ret = EventPeriodicQueueUpdate(&ev, targetQueue, updatePeriodMs);
|
2014-01-06 21:48:34 +01:00
|
|
|
if (ret == -1) {
|
2014-02-07 18:36:33 +01:00
|
|
|
ret = EventPeriodicQueueCreate(&ev, targetQueue, updatePeriodMs);
|
2014-01-06 21:48:34 +01:00
|
|
|
}
|
|
|
|
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)
|
2015-03-15 19:19:19 +01:00
|
|
|
* \param[in] telemetry channel context
|
2013-11-17 17:15:23 +01:00
|
|
|
* \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
|
|
|
|
*/
|
2015-03-15 00:58:18 +01:00
|
|
|
static int32_t setLoggingPeriod(
|
2015-03-16 00:11:42 +01:00
|
|
|
channelContext *channel,
|
2015-03-15 00:58:18 +01:00
|
|
|
UAVObjHandle obj,
|
|
|
|
int32_t updatePeriodMs)
|
2013-11-17 17:15:23 +01:00
|
|
|
{
|
|
|
|
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-02-18 01:03:57 +01:00
|
|
|
ev.lowPriority = true;
|
2014-01-06 21:48:34 +01:00
|
|
|
|
2015-03-15 00:58:18 +01:00
|
|
|
#ifdef PIOS_TELEM_PRIORITY_QUEUE
|
2015-03-16 00:11:42 +01:00
|
|
|
xQueueHandle targetQueue = UAVObjIsPriority(obj) ? channel->priorityQueue :
|
2015-03-17 21:52:08 +01:00
|
|
|
channel->queue;
|
2015-03-15 00:58:18 +01:00
|
|
|
#else /* PIOS_TELEM_PRIORITY_QUEUE */
|
2015-03-17 21:52:08 +01:00
|
|
|
xQueueHandle targetQueue = channel->queue;
|
2015-03-15 00:58:18 +01:00
|
|
|
#endif /* PIOS_TELEM_PRIORITY_QUEUE */
|
2014-02-07 18:36:33 +01:00
|
|
|
|
|
|
|
ret = EventPeriodicQueueUpdate(&ev, targetQueue, updatePeriodMs);
|
2014-01-06 21:48:34 +01:00
|
|
|
if (ret == -1) {
|
2014-02-07 18:36:33 +01:00
|
|
|
ret = EventPeriodicQueueCreate(&ev, targetQueue, updatePeriodMs);
|
2014-01-06 21:48:34 +01:00
|
|
|
}
|
|
|
|
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
|
2015-07-16 01:01:02 +02:00
|
|
|
UAVTalkGetStats(radioChannel.uavTalkCon, &utalkStats, true);
|
2015-11-14 14:27:12 +01:00
|
|
|
|
2015-07-16 01:01:02 +02:00
|
|
|
#ifdef HAS_RADIO
|
|
|
|
UAVTalkAddStats(localChannel.uavTalkCon, &utalkStats, true);
|
|
|
|
#endif
|
2015-11-14 14:27:12 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2014-04-26 20:48:30 +02:00
|
|
|
// TODO: check whether is there any error condition worth raising an alarm
|
|
|
|
// Disconnection is actually a normal (non)working status so it is not raising alarms anymore.
|
2013-05-19 16:37:30 +02:00
|
|
|
if (flightStats.Status == FLIGHTTELEMETRYSTATS_STATUS_CONNECTED) {
|
|
|
|
AlarmsClear(SYSTEMALARMS_ALARM_TELEMETRY);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
*/
|
2015-03-16 00:11:42 +01:00
|
|
|
static void updateSettings(channelContext *channel)
|
2010-09-27 09:28:34 +02:00
|
|
|
{
|
2015-03-16 00:11:42 +01:00
|
|
|
uint32_t port = channel->getPort();
|
2015-03-15 19:19:19 +01:00
|
|
|
|
|
|
|
if (port) {
|
2013-05-19 16:37:30 +02:00
|
|
|
// Retrieve settings
|
2015-04-11 22:43:04 +02:00
|
|
|
HwSettingsTelemetrySpeedOptions speed;
|
2013-05-19 16:37:30 +02:00
|
|
|
HwSettingsTelemetrySpeedGet(&speed);
|
|
|
|
|
|
|
|
// Set port speed
|
|
|
|
switch (speed) {
|
|
|
|
case HWSETTINGS_TELEMETRYSPEED_2400:
|
2015-03-15 19:19:19 +01:00
|
|
|
PIOS_COM_ChangeBaud(port, 2400);
|
2013-05-19 16:37:30 +02:00
|
|
|
break;
|
|
|
|
case HWSETTINGS_TELEMETRYSPEED_4800:
|
2015-03-15 19:19:19 +01:00
|
|
|
PIOS_COM_ChangeBaud(port, 4800);
|
2013-05-19 16:37:30 +02:00
|
|
|
break;
|
|
|
|
case HWSETTINGS_TELEMETRYSPEED_9600:
|
2015-03-15 19:19:19 +01:00
|
|
|
PIOS_COM_ChangeBaud(port, 9600);
|
2013-05-19 16:37:30 +02:00
|
|
|
break;
|
|
|
|
case HWSETTINGS_TELEMETRYSPEED_19200:
|
2015-03-15 19:19:19 +01:00
|
|
|
PIOS_COM_ChangeBaud(port, 19200);
|
2013-05-19 16:37:30 +02:00
|
|
|
break;
|
|
|
|
case HWSETTINGS_TELEMETRYSPEED_38400:
|
2015-03-15 19:19:19 +01:00
|
|
|
PIOS_COM_ChangeBaud(port, 38400);
|
2013-05-19 16:37:30 +02:00
|
|
|
break;
|
|
|
|
case HWSETTINGS_TELEMETRYSPEED_57600:
|
2015-03-15 19:19:19 +01:00
|
|
|
PIOS_COM_ChangeBaud(port, 57600);
|
2013-05-19 16:37:30 +02:00
|
|
|
break;
|
|
|
|
case HWSETTINGS_TELEMETRYSPEED_115200:
|
2015-03-15 19:19:19 +01:00
|
|
|
PIOS_COM_ChangeBaud(port, 115200);
|
2013-05-19 16:37:30 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-09-27 09:28:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-19 16:37:30 +02:00
|
|
|
* @}
|
|
|
|
* @}
|
|
|
|
*/
|