2011-06-15 18:18:29 +02:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
* @addtogroup OpenPilotModules OpenPilot Modules
|
|
|
|
* @{
|
|
|
|
* @addtogroup CameraStab Camera Stabilization Module
|
|
|
|
* @brief Camera stabilization module
|
|
|
|
* Updates accessory outputs with values appropriate for camera stabilization
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file camerastab.c
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
|
|
|
* @brief Stabilize camera against the roll pitch and yaw of aircraft
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Output object: Accessory
|
|
|
|
*
|
|
|
|
* This module will periodically calculate the output values for stabilizing the camera
|
|
|
|
*
|
|
|
|
* UAVObjects are automatically generated by the UAVObjectGenerator from
|
|
|
|
* the object definition XML file.
|
|
|
|
*
|
|
|
|
* Modules have no API, all communication to other modules is done through UAVObjects.
|
|
|
|
* However modules may use the API exposed by shared libraries.
|
|
|
|
* See the OpenPilot wiki for more details.
|
|
|
|
* http://www.openpilot.org/OpenPilot_Application_Architecture
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "openpilot.h"
|
|
|
|
|
|
|
|
#include "accessorydesired.h"
|
|
|
|
#include "attitudeactual.h"
|
2011-06-23 07:46:41 +02:00
|
|
|
#include "camerastabsettings.h"
|
2011-08-10 03:43:48 +02:00
|
|
|
#include "cameradesired.h"
|
2011-11-11 11:39:57 +01:00
|
|
|
#include "hwsettings.h"
|
2011-06-15 18:18:29 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Configuration
|
|
|
|
//
|
2011-06-23 07:46:41 +02:00
|
|
|
#define SAMPLE_PERIOD_MS 10
|
2011-06-15 18:18:29 +02:00
|
|
|
|
|
|
|
// Private types
|
|
|
|
|
|
|
|
// Private variables
|
2011-12-16 21:27:13 +01:00
|
|
|
static struct CameraStab_data {
|
|
|
|
portTickType lastSysTime;
|
2011-12-30 18:18:33 +01:00
|
|
|
float inputs[CAMERASTABSETTINGS_INPUT_NUMELEM];
|
|
|
|
float inputs_filtered[CAMERASTABSETTINGS_INPUT_NUMELEM];
|
2011-12-16 21:27:13 +01:00
|
|
|
} *csd;
|
2011-06-15 18:18:29 +02:00
|
|
|
|
|
|
|
// Private functions
|
|
|
|
static void attitudeUpdated(UAVObjEvent* ev);
|
2011-12-16 21:27:13 +01:00
|
|
|
static float bound(float val, float limit);
|
2011-06-15 18:18:29 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialise the module, called on startup
|
|
|
|
* \returns 0 on success or -1 if initialisation failed
|
|
|
|
*/
|
|
|
|
int32_t CameraStabInitialize(void)
|
|
|
|
{
|
2011-11-13 17:27:52 +01:00
|
|
|
bool cameraStabEnabled;
|
2012-01-14 22:55:55 +01:00
|
|
|
|
|
|
|
#ifdef MODULE_CameraStab_BUILTIN
|
|
|
|
cameraStabEnabled = true;
|
|
|
|
#else
|
2011-11-11 11:39:57 +01:00
|
|
|
uint8_t optionalModules[HWSETTINGS_OPTIONALMODULES_NUMELEM];
|
2011-11-13 17:27:52 +01:00
|
|
|
|
|
|
|
HwSettingsInitialize();
|
2011-11-11 11:39:57 +01:00
|
|
|
HwSettingsOptionalModulesGet(optionalModules);
|
2011-06-23 07:46:41 +02:00
|
|
|
|
2011-11-13 17:27:52 +01:00
|
|
|
if (optionalModules[HWSETTINGS_OPTIONALMODULES_CAMERASTAB] == HWSETTINGS_OPTIONALMODULES_ENABLED)
|
|
|
|
cameraStabEnabled = true;
|
|
|
|
else
|
|
|
|
cameraStabEnabled = false;
|
2012-01-14 22:55:55 +01:00
|
|
|
#endif
|
2011-11-13 17:27:52 +01:00
|
|
|
|
|
|
|
if (cameraStabEnabled) {
|
2011-06-15 18:18:29 +02:00
|
|
|
|
2011-12-16 21:27:13 +01:00
|
|
|
// allocate and initialize the static data storage only if module is enabled
|
|
|
|
csd = (struct CameraStab_data *) pvPortMalloc(sizeof(struct CameraStab_data));
|
|
|
|
if (!csd)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
// make sure that all inputs[] and inputs_filtered[] are zeroed
|
|
|
|
memset(csd, 0, sizeof(struct CameraStab_data));
|
|
|
|
csd->lastSysTime = xTaskGetTickCount();
|
|
|
|
|
2011-11-11 11:39:57 +01:00
|
|
|
AttitudeActualInitialize();
|
|
|
|
CameraStabSettingsInitialize();
|
|
|
|
CameraDesiredInitialize();
|
|
|
|
|
2011-12-19 01:16:32 +01:00
|
|
|
UAVObjEvent ev = {
|
|
|
|
.obj = AttitudeActualHandle(),
|
|
|
|
.instId = 0,
|
|
|
|
.event = 0,
|
|
|
|
};
|
|
|
|
EventPeriodicCallbackCreate(&ev, attitudeUpdated, SAMPLE_PERIOD_MS / portTICK_RATE_MS);
|
2011-11-11 11:39:57 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2011-06-15 18:18:29 +02:00
|
|
|
}
|
|
|
|
|
2011-08-20 01:24:06 +02:00
|
|
|
/* stub: module has no module thread */
|
|
|
|
int32_t CameraStabStart(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-11-11 11:39:57 +01:00
|
|
|
MODULE_INITCALL(CameraStabInitialize, CameraStabStart)
|
|
|
|
|
2011-06-15 18:18:29 +02:00
|
|
|
static void attitudeUpdated(UAVObjEvent* ev)
|
|
|
|
{
|
|
|
|
if (ev->obj != AttitudeActualHandle())
|
|
|
|
return;
|
|
|
|
|
|
|
|
AccessoryDesiredData accessory;
|
2011-06-23 07:46:41 +02:00
|
|
|
|
|
|
|
CameraStabSettingsData cameraStab;
|
|
|
|
CameraStabSettingsGet(&cameraStab);
|
|
|
|
|
2011-12-16 21:27:13 +01:00
|
|
|
// Check how long since last update, time delta between calls in ms
|
|
|
|
portTickType thisSysTime = xTaskGetTickCount();
|
2011-12-19 01:16:32 +01:00
|
|
|
float dT = (thisSysTime > csd->lastSysTime) ?
|
|
|
|
(thisSysTime - csd->lastSysTime) / portTICK_RATE_MS :
|
|
|
|
(float)SAMPLE_PERIOD_MS / 1000.0f;
|
2011-12-16 21:27:13 +01:00
|
|
|
csd->lastSysTime = thisSysTime;
|
|
|
|
|
|
|
|
// Read any input channels and apply LPF
|
2011-12-30 18:18:33 +01:00
|
|
|
for (uint8_t i = 0; i < CAMERASTABSETTINGS_INPUT_NUMELEM; i++) {
|
|
|
|
if (cameraStab.Input[i] != CAMERASTABSETTINGS_INPUT_NONE) {
|
|
|
|
if (AccessoryDesiredInstGet(cameraStab.Input[i] - CAMERASTABSETTINGS_INPUT_ACCESSORY0, &accessory) == 0) {
|
2011-12-16 21:27:13 +01:00
|
|
|
float input_rate;
|
|
|
|
switch (cameraStab.StabilizationMode[i]) {
|
|
|
|
case CAMERASTABSETTINGS_STABILIZATIONMODE_ATTITUDE:
|
|
|
|
csd->inputs[i] = accessory.AccessoryVal * cameraStab.InputRange[i];
|
|
|
|
break;
|
|
|
|
case CAMERASTABSETTINGS_STABILIZATIONMODE_AXISLOCK:
|
|
|
|
input_rate = accessory.AccessoryVal * cameraStab.InputRate[i];
|
2011-12-18 00:34:45 +01:00
|
|
|
if (fabs(input_rate) > cameraStab.MaxAxisLockRate)
|
2011-12-19 01:16:32 +01:00
|
|
|
csd->inputs[i] = bound(csd->inputs[i] + input_rate * dT / 1000.0f, cameraStab.InputRange[i]);
|
2011-12-16 21:27:13 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
PIOS_Assert(0);
|
|
|
|
}
|
2011-12-18 01:24:15 +01:00
|
|
|
|
|
|
|
// bypass LPF calculation if ResponseTime is zero
|
2011-12-18 00:34:45 +01:00
|
|
|
float rt = (float)cameraStab.ResponseTime[i];
|
2011-12-18 01:24:15 +01:00
|
|
|
if (rt)
|
2011-12-19 01:16:32 +01:00
|
|
|
csd->inputs_filtered[i] = (rt / (rt + dT)) * csd->inputs_filtered[i]
|
|
|
|
+ (dT / (rt + dT)) * csd->inputs[i];
|
2011-12-18 01:24:15 +01:00
|
|
|
else
|
|
|
|
csd->inputs_filtered[i] = csd->inputs[i];
|
2011-12-16 21:27:13 +01:00
|
|
|
}
|
|
|
|
}
|
2011-06-23 07:46:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set output channels
|
2011-12-16 21:27:13 +01:00
|
|
|
float attitude;
|
|
|
|
float output;
|
|
|
|
|
2011-08-10 03:43:48 +02:00
|
|
|
AttitudeActualRollGet(&attitude);
|
2011-12-30 18:18:33 +01:00
|
|
|
output = bound((attitude + csd->inputs_filtered[CAMERASTABSETTINGS_INPUT_ROLL]) / cameraStab.OutputRange[CAMERASTABSETTINGS_OUTPUTRANGE_ROLL], 1.0f);
|
2011-08-10 03:43:48 +02:00
|
|
|
CameraDesiredRollSet(&output);
|
2011-06-23 07:46:41 +02:00
|
|
|
|
2011-08-10 03:43:48 +02:00
|
|
|
AttitudeActualPitchGet(&attitude);
|
2011-12-30 18:18:33 +01:00
|
|
|
output = bound((attitude + csd->inputs_filtered[CAMERASTABSETTINGS_INPUT_PITCH]) / cameraStab.OutputRange[CAMERASTABSETTINGS_OUTPUTRANGE_PITCH], 1.0f);
|
2011-08-10 03:43:48 +02:00
|
|
|
CameraDesiredPitchSet(&output);
|
|
|
|
|
|
|
|
AttitudeActualYawGet(&attitude);
|
2011-12-30 18:18:33 +01:00
|
|
|
output = bound((attitude + csd->inputs_filtered[CAMERASTABSETTINGS_INPUT_YAW]) / cameraStab.OutputRange[CAMERASTABSETTINGS_OUTPUTRANGE_YAW], 1.0f);
|
2011-08-10 03:43:48 +02:00
|
|
|
CameraDesiredYawSet(&output);
|
2011-06-15 18:18:29 +02:00
|
|
|
}
|
|
|
|
|
2011-12-16 21:27:13 +01:00
|
|
|
float bound(float val, float limit)
|
2011-08-11 21:34:38 +02:00
|
|
|
{
|
2011-12-16 21:27:13 +01:00
|
|
|
return (val > limit) ? limit :
|
|
|
|
(val < -limit) ? -limit :
|
2011-08-11 21:34:38 +02:00
|
|
|
val;
|
|
|
|
}
|
2011-06-15 18:18:29 +02:00
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
*/
|