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-02-27 20:57:45 +01:00
|
|
|
#include <stdlib.h> // for malloc
|
2010-02-22 03:18:23 +01:00
|
|
|
#include "telemetry.h"
|
|
|
|
#include "uavtalk.h"
|
2010-02-27 20:57:45 +01:00
|
|
|
#include "uavobjectmanager.h"
|
|
|
|
#include "utlist.h"
|
2010-02-22 03:18:23 +01:00
|
|
|
#include "FreeRTOS.h"
|
|
|
|
#include "task.h"
|
|
|
|
#include "queue.h"
|
|
|
|
#include "semphr.h"
|
|
|
|
|
|
|
|
// Private constants
|
|
|
|
#define MAX_QUEUE_SIZE 20
|
|
|
|
#define STACK_SIZE 100
|
|
|
|
#define TASK_PRIORITY 100
|
|
|
|
#define REQ_TIMEOUT_MS 500
|
|
|
|
#define MAX_RETRIES 2
|
2010-02-27 20:57:45 +01:00
|
|
|
#define MAX_UPDATE_PERIOD_MS 1000
|
|
|
|
#define MIN_UPDATE_PERIOD_MS 1
|
|
|
|
|
|
|
|
// Private types
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List of object properties that are needed for the periodic updates.
|
|
|
|
*/
|
|
|
|
struct ObjectListStruct {
|
|
|
|
UAVObjHandle obj; /** Object handle */
|
|
|
|
int32_t updatePeriodMs; /** Update period in ms or 0 if no periodic updates are needed */
|
|
|
|
int32_t timeToNextUpdateMs; /** Time delay to the next update */
|
|
|
|
struct ObjectListStruct* next; /** Needed by linked list library (utlist.h) */
|
|
|
|
};
|
|
|
|
typedef struct ObjectListStruct ObjectList;
|
2010-02-22 03:18:23 +01:00
|
|
|
|
|
|
|
// Private variables
|
|
|
|
xQueueHandle queue;
|
|
|
|
xTaskHandle telemetryTaskHandle;
|
2010-02-27 20:57:45 +01:00
|
|
|
ObjectList* objList;
|
2010-02-22 03:18:23 +01:00
|
|
|
|
|
|
|
// Private functions
|
|
|
|
void telemetryTask();
|
|
|
|
void receiveTask();
|
|
|
|
int32_t transmitData(uint8_t* data, int32_t length);
|
2010-02-27 20:57:45 +01:00
|
|
|
void registerObject(UAVObjHandle obj);
|
|
|
|
void updateObject(UAVObjHandle obj);
|
|
|
|
int32_t addObject(UAVObjHandle obj);
|
|
|
|
int32_t setUpdatePeriod(UAVObjHandle obj, int32_t updatePeriodMs);
|
|
|
|
int32_t processPeriodicUpdates();
|
2010-02-22 03:18:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the telemetry module
|
|
|
|
* \return -1 if initialization failed
|
|
|
|
* \return 0 on success
|
|
|
|
*/
|
|
|
|
int32_t TelemetryInitialize()
|
|
|
|
{
|
2010-02-27 20:57:45 +01:00
|
|
|
// Initialize object list
|
|
|
|
objList = NULL;
|
|
|
|
|
2010-02-22 03:18:23 +01:00
|
|
|
// Create object queue
|
2010-02-27 20:57:45 +01:00
|
|
|
queue = xQueueCreate(MAX_QUEUE_SIZE, sizeof(UAVObjQMsg));
|
2010-02-22 03:18:23 +01:00
|
|
|
|
|
|
|
// TODO: Get telemetry settings object
|
|
|
|
|
|
|
|
// TODO: Initialize communication ports
|
|
|
|
|
|
|
|
// Initialize UAVTalk
|
|
|
|
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
|
|
|
|
|
|
|
// Start tasks
|
|
|
|
xTaskCreate( telemetryTask, (signed char*)"Telemetry", STACK_SIZE, NULL, TASK_PRIORITY, &telemetryTaskHandle );
|
|
|
|
// TODO: Start receive task
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-27 20:57:45 +01:00
|
|
|
* Register a new object, adds object to local list and connects the queue depending on the object's
|
2010-02-22 03:18:23 +01:00
|
|
|
* telemetry settings.
|
|
|
|
* \param[in] obj Object to connect
|
|
|
|
*/
|
2010-02-27 20:57:45 +01:00
|
|
|
void registerObject(UAVObjHandle obj)
|
2010-02-22 03:18:23 +01:00
|
|
|
{
|
2010-02-27 20:57:45 +01:00
|
|
|
// Add object to list
|
|
|
|
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
|
|
|
|
* \param[in] obj Object to updates
|
|
|
|
*/
|
2010-02-27 20:57:45 +01:00
|
|
|
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
|
|
|
|
if (metadata.telemetryUpdateMode == UPDATEMODE_PERIODIC)
|
|
|
|
{
|
|
|
|
// 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
|
|
|
|
eventMask = QMSG_UPDATED_MANUAL|QMSG_UPDATE_REQ;
|
2010-02-27 20:57:45 +01:00
|
|
|
if (UAVObjIsMetaobject(obj))
|
2010-02-22 03:18:23 +01:00
|
|
|
{
|
|
|
|
eventMask |= QMSG_UNPACKED; // we also need to act on remote updates (unpack events)
|
|
|
|
}
|
2010-02-27 20:57:45 +01:00
|
|
|
UAVObjConnect(obj, queue, eventMask);
|
2010-02-22 03:18:23 +01:00
|
|
|
}
|
|
|
|
else if (metadata.telemetryUpdateMode == UPDATEMODE_ONCHANGE)
|
|
|
|
{
|
|
|
|
// Set update period
|
2010-02-27 20:57:45 +01:00
|
|
|
setUpdatePeriod(obj, 0);
|
2010-02-22 03:18:23 +01:00
|
|
|
// Connect queue
|
|
|
|
eventMask = QMSG_UPDATED|QMSG_UPDATED_MANUAL|QMSG_UPDATE_REQ;
|
2010-02-27 20:57:45 +01:00
|
|
|
if (UAVObjIsMetaobject(obj))
|
2010-02-22 03:18:23 +01:00
|
|
|
{
|
|
|
|
eventMask |= QMSG_UNPACKED; // we also need to act on remote updates (unpack events)
|
|
|
|
}
|
2010-02-27 20:57:45 +01:00
|
|
|
UAVObjConnect(obj, queue, eventMask);
|
2010-02-22 03:18:23 +01:00
|
|
|
}
|
|
|
|
else if (metadata.telemetryUpdateMode == UPDATEMODE_MANUAL)
|
|
|
|
{
|
|
|
|
// Set update period
|
2010-02-27 20:57:45 +01:00
|
|
|
setUpdatePeriod(obj, 0);
|
2010-02-22 03:18:23 +01:00
|
|
|
// Connect queue
|
|
|
|
eventMask = QMSG_UPDATED_MANUAL|QMSG_UPDATE_REQ;
|
2010-02-27 20:57:45 +01:00
|
|
|
if (UAVObjIsMetaobject(obj))
|
2010-02-22 03:18:23 +01:00
|
|
|
{
|
|
|
|
eventMask |= QMSG_UNPACKED; // we also need to act on remote updates (unpack events)
|
|
|
|
}
|
2010-02-27 20:57:45 +01:00
|
|
|
UAVObjConnect(obj, queue, eventMask);
|
2010-02-22 03:18:23 +01:00
|
|
|
}
|
|
|
|
else if (metadata.telemetryUpdateMode == UPDATEMODE_NEVER)
|
|
|
|
{
|
|
|
|
// 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-02-27 20:57:45 +01:00
|
|
|
UAVObjDisconnect(obj, queue);
|
2010-02-22 03:18:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Telemetry task. Processes queue events and periodic updates. It does not return.
|
|
|
|
*/
|
|
|
|
void telemetryTask()
|
|
|
|
{
|
2010-02-27 20:57:45 +01:00
|
|
|
int32_t timeToNextUpdateMs;
|
|
|
|
int32_t delayMs;
|
|
|
|
UAVObjQMsg msg;
|
|
|
|
UAVObjMetadata metadata;
|
|
|
|
int32_t retries;
|
|
|
|
int32_t success;
|
2010-02-22 03:18:23 +01:00
|
|
|
|
|
|
|
// Initialize time
|
|
|
|
timeToNextUpdateMs = xTaskGetTickCount()*portTICK_RATE_MS;
|
|
|
|
|
|
|
|
// Loop forever
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
// Calculate delay time
|
|
|
|
delayMs = timeToNextUpdateMs-(xTaskGetTickCount()*portTICK_RATE_MS);
|
|
|
|
if (delayMs < 0)
|
|
|
|
{
|
|
|
|
delayMs = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for queue message
|
|
|
|
if ( xQueueReceive(queue, &msg, delayMs/portTICK_RATE_MS) == pdTRUE )
|
|
|
|
{
|
|
|
|
// Get object metadata
|
2010-02-27 20:57:45 +01:00
|
|
|
UAVObjGetMetadata(msg.obj, &metadata);
|
2010-02-22 03:18:23 +01:00
|
|
|
// Act on event
|
|
|
|
if (msg.event == QMSG_UPDATED || msg.event == QMSG_UPDATED_MANUAL)
|
|
|
|
{
|
|
|
|
// Send update to GCS (with retries)
|
|
|
|
retries = 0;
|
|
|
|
while (retries < MAX_RETRIES && success == -1)
|
|
|
|
{
|
2010-02-27 20:57:45 +01:00
|
|
|
success = UAVTalkSendObject(msg.obj, msg.instId, metadata.ackRequired, REQ_TIMEOUT_MS); // call blocks until ack is received or timeout
|
2010-02-22 03:18:23 +01:00
|
|
|
++retries;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (msg.event == QMSG_UPDATE_REQ)
|
|
|
|
{
|
|
|
|
// Request object update from GCS (with retries)
|
|
|
|
retries = 0;
|
|
|
|
while (retries < MAX_RETRIES && success == -1)
|
|
|
|
{
|
2010-02-27 20:57:45 +01:00
|
|
|
success = UAVTalkSendObjectRequest(msg.obj, msg.instId, REQ_TIMEOUT_MS); // call blocks until update is received or timeout
|
2010-02-22 03:18:23 +01:00
|
|
|
++retries;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If this is a metadata object then make necessary telemetry updates
|
2010-02-27 20:57:45 +01:00
|
|
|
if (UAVObjIsMetaobject(msg.obj))
|
2010-02-22 03:18:23 +01:00
|
|
|
{
|
2010-02-27 20:57:45 +01:00
|
|
|
updateObject(UAVObjGetLinkedObj(msg.obj)); // linked object will be the actual object the metadata are for
|
2010-02-22 03:18:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process periodic updates
|
|
|
|
if ((xTaskGetTickCount()*portTICK_RATE_MS) >= timeToNextUpdateMs )
|
|
|
|
{
|
2010-02-27 20:57:45 +01:00
|
|
|
timeToNextUpdateMs = processPeriodicUpdates();
|
2010-02-22 03:18:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Receive task. Processes received bytes (from the modem or USB) and passes them to
|
2010-02-27 20:57:45 +01:00
|
|
|
* UAVTalk for decoding. Does not return.
|
2010-02-22 03:18:23 +01:00
|
|
|
*/
|
|
|
|
void receiveTask()
|
|
|
|
{
|
|
|
|
// Main thread
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
// TODO: Wait for bytes and pass to UAVTalk for processing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transmit data buffer to the modem or USB port.
|
|
|
|
* \param[in] data Data buffer to send
|
|
|
|
* \param[in] length Length of buffer
|
|
|
|
*/
|
|
|
|
int32_t transmitData(uint8_t* data, int32_t length)
|
|
|
|
{
|
|
|
|
// TODO: Send data to communication port
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-02-27 20:57:45 +01:00
|
|
|
/**
|
|
|
|
* Setup object 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
|
|
|
|
*/
|
|
|
|
int32_t setUpdatePeriod(UAVObjHandle obj, int32_t updatePeriodMs)
|
|
|
|
{
|
|
|
|
ObjectList* objEntry;
|
|
|
|
// Check that the object is not already connected
|
|
|
|
LL_FOREACH(objList, objEntry)
|
|
|
|
{
|
|
|
|
if (objEntry->obj == obj)
|
|
|
|
{
|
|
|
|
objEntry->updatePeriodMs = updatePeriodMs;
|
|
|
|
objEntry->timeToNextUpdateMs = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If this point is reached then the object was not found
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle periodic updates for all objects.
|
|
|
|
* \return The system time until the next update (in ms) or -1 if failed
|
|
|
|
*/
|
|
|
|
int32_t processPeriodicUpdates()
|
|
|
|
{
|
|
|
|
static int32_t timeOfLastUpdate = 0;
|
|
|
|
ObjectList* objEntry;
|
|
|
|
int32_t delaySinceLastUpdateMs;
|
|
|
|
int32_t minDelay = MAX_UPDATE_PERIOD_MS;
|
|
|
|
|
|
|
|
// Iterate through each object and update its timer, if zero then transmit object.
|
|
|
|
// Also calculate smallest delay to next update.
|
|
|
|
delaySinceLastUpdateMs = xTaskGetTickCount()*portTICK_RATE_MS - timeOfLastUpdate;
|
|
|
|
LL_FOREACH(objList, objEntry)
|
|
|
|
{
|
|
|
|
// If object is configured for periodic updates
|
|
|
|
if (objEntry->updatePeriodMs > 0)
|
|
|
|
{
|
|
|
|
objEntry->timeToNextUpdateMs -= delaySinceLastUpdateMs;
|
|
|
|
// Check if time for the next update
|
|
|
|
if (objEntry->timeToNextUpdateMs <= 0)
|
|
|
|
{
|
|
|
|
// Reset timer
|
|
|
|
objEntry->timeToNextUpdateMs = objEntry->updatePeriodMs;
|
|
|
|
// Send object (trigger update)
|
|
|
|
UAVObjUpdated(objEntry->obj);
|
|
|
|
}
|
|
|
|
// Update minimum delay
|
|
|
|
if (objEntry->timeToNextUpdateMs < minDelay)
|
|
|
|
{
|
|
|
|
minDelay = objEntry->timeToNextUpdateMs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if delay for the next update is too short
|
|
|
|
if (minDelay < MIN_UPDATE_PERIOD_MS)
|
|
|
|
{
|
|
|
|
minDelay = MIN_UPDATE_PERIOD_MS;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Done
|
|
|
|
timeOfLastUpdate = xTaskGetTickCount()*portTICK_RATE_MS;
|
|
|
|
return timeOfLastUpdate + minDelay;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a new object to the object list
|
|
|
|
*/
|
|
|
|
int32_t addObject(UAVObjHandle obj)
|
|
|
|
{
|
|
|
|
ObjectList* objEntry;
|
|
|
|
// Check that the object is not already connected
|
|
|
|
LL_FOREACH(objList, objEntry)
|
|
|
|
{
|
|
|
|
if (objEntry->obj == obj)
|
|
|
|
{
|
|
|
|
// Already registered, ignore
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Create handle
|
|
|
|
objEntry = (ObjectList*)malloc(sizeof(ObjectList));
|
|
|
|
if (objEntry == NULL) return -1;
|
|
|
|
objEntry->obj = obj;
|
|
|
|
objEntry->updatePeriodMs = 0;
|
|
|
|
objEntry->timeToNextUpdateMs = 0;
|
|
|
|
// Add to list
|
|
|
|
LL_APPEND(objList, objEntry);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-02-22 03:18:23 +01:00
|
|
|
|
|
|
|
|