2010-09-27 09:28:34 +02:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
* @addtogroup OpenPilotModules OpenPilot Modules
|
|
|
|
* @{
|
|
|
|
* @addtogroup ActuatorModule Actuator Module
|
|
|
|
* @brief Compute servo/motor settings based on @ref ActuatorDesired "desired actuator positions" and aircraft type.
|
|
|
|
* This is where all the mixing of channels is computed.
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file actuator.c
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
|
|
|
* @brief Actuator module. Drives the actuators (servos, motors etc).
|
|
|
|
*
|
|
|
|
* @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-12-20 17:38:17 +01:00
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
#include "openpilot.h"
|
|
|
|
#include "actuator.h"
|
|
|
|
#include "actuatorsettings.h"
|
|
|
|
#include "systemsettings.h"
|
|
|
|
#include "actuatordesired.h"
|
|
|
|
#include "actuatorcommand.h"
|
|
|
|
#include "manualcontrolcommand.h"
|
|
|
|
#include "mixersettings.h"
|
|
|
|
#include "mixerstatus.h"
|
|
|
|
|
|
|
|
|
|
|
|
// Private constants
|
|
|
|
#define MAX_QUEUE_SIZE 2
|
2011-02-01 03:18:35 +01:00
|
|
|
|
|
|
|
#if defined(PIOS_ACTUATOR_STACK_SIZE)
|
|
|
|
#define STACK_SIZE_BYTES PIOS_ACTUATOR_STACK_SIZE
|
|
|
|
#else
|
|
|
|
#define STACK_SIZE_BYTES 1312
|
|
|
|
#endif
|
|
|
|
|
2011-01-24 08:52:20 +01:00
|
|
|
#define TASK_PRIORITY (tskIDLE_PRIORITY+4)
|
2010-09-27 09:28:34 +02:00
|
|
|
#define FAILSAFE_TIMEOUT_MS 100
|
2010-10-01 14:33:33 +02:00
|
|
|
#define MAX_MIX_ACTUATORS ACTUATORCOMMAND_CHANNEL_NUMELEM
|
2010-09-27 09:28:34 +02:00
|
|
|
|
|
|
|
// Private types
|
|
|
|
|
|
|
|
|
|
|
|
// Private variables
|
2010-10-01 14:33:20 +02:00
|
|
|
static xQueueHandle queue;
|
2010-09-27 09:28:34 +02:00
|
|
|
static xTaskHandle taskHandle;
|
|
|
|
|
2010-10-03 20:37:07 +02:00
|
|
|
static float lastResult[MAX_MIX_ACTUATORS]={0,0,0,0,0,0,0,0};
|
|
|
|
static float lastFilteredResult[MAX_MIX_ACTUATORS]={0,0,0,0,0,0,0,0};
|
|
|
|
static float filterAccumulator[MAX_MIX_ACTUATORS]={0,0,0,0,0,0,0,0};
|
|
|
|
|
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
// Private functions
|
|
|
|
static void actuatorTask(void* parameters);
|
2011-02-01 03:17:55 +01:00
|
|
|
static void actuator_update_rate(UAVObjEvent *);
|
2010-09-27 09:28:34 +02:00
|
|
|
static int16_t scaleChannel(float value, int16_t max, int16_t min, int16_t neutral);
|
|
|
|
static void setFailsafe();
|
|
|
|
static float MixerCurve(const float throttle, const float* curve);
|
2011-01-10 02:11:44 +01:00
|
|
|
static bool set_channel(uint8_t mixer_channel, uint16_t value);
|
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
float ProcessMixer(const int index, const float curve1, const float curve2,
|
2010-09-27 19:30:42 +02:00
|
|
|
MixerSettingsData* mixerSettings, ActuatorDesiredData* desired,
|
|
|
|
const float period);
|
2010-09-27 09:28:34 +02:00
|
|
|
|
2010-10-01 14:33:33 +02:00
|
|
|
//this structure is equivalent to the UAVObjects for one mixer.
|
|
|
|
typedef struct {
|
|
|
|
uint8_t type;
|
2010-10-01 22:28:14 +02:00
|
|
|
int8_t matrix[5];
|
2010-10-01 14:33:33 +02:00
|
|
|
} __attribute__((packed)) Mixer_t;
|
|
|
|
|
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
/**
|
|
|
|
* @brief Module initialization
|
|
|
|
* @return 0
|
|
|
|
*/
|
|
|
|
int32_t ActuatorInitialize()
|
|
|
|
{
|
|
|
|
// Create object queue
|
2010-10-01 14:33:20 +02:00
|
|
|
queue = xQueueCreate(MAX_QUEUE_SIZE, sizeof(UAVObjEvent));
|
2010-10-03 20:37:07 +02:00
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
// Listen for ExampleObject1 updates
|
2010-10-01 14:33:20 +02:00
|
|
|
ActuatorDesiredConnectQueue(queue);
|
2011-02-01 03:17:55 +01:00
|
|
|
|
|
|
|
// If settings change, update the output rate
|
|
|
|
ActuatorSettingsConnectCallback(actuator_update_rate);
|
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
// Start main task
|
2011-02-01 03:18:35 +01:00
|
|
|
xTaskCreate(actuatorTask, (signed char*)"Actuator", STACK_SIZE_BYTES/4, NULL, TASK_PRIORITY, &taskHandle);
|
2011-01-09 00:43:34 +01:00
|
|
|
TaskMonitorAdd(TASKINFO_RUNNING_ACTUATOR, taskHandle);
|
2011-01-10 01:16:30 +01:00
|
|
|
PIOS_WDG_RegisterFlag(PIOS_WDG_ACTUATOR);
|
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-02-01 03:17:55 +01:00
|
|
|
* @brief Main Actuator module task
|
2010-10-01 14:33:33 +02:00
|
|
|
*
|
|
|
|
* Universal matrix based mixer for VTOL, helis and fixed wing.
|
|
|
|
* Converts desired roll,pitch,yaw and throttle to servo/ESC outputs.
|
|
|
|
*
|
|
|
|
* Because of how the Throttle ranges from 0 to 1, the motors should too!
|
|
|
|
*
|
|
|
|
* Note this code depends on the UAVObjects for the mixers being all being the same
|
|
|
|
* and in sequence. If you change the object definition, make sure you check the code!
|
|
|
|
*
|
|
|
|
* @return -1 if error, 0 if success
|
2010-09-27 09:28:34 +02:00
|
|
|
*/
|
|
|
|
static void actuatorTask(void* parameters)
|
|
|
|
{
|
2010-10-01 14:33:20 +02:00
|
|
|
UAVObjEvent ev;
|
2010-09-27 09:28:34 +02:00
|
|
|
portTickType lastSysTime;
|
2010-10-01 14:33:33 +02:00
|
|
|
portTickType thisSysTime;
|
2010-11-21 14:09:05 +01:00
|
|
|
float dT = 0.0f;
|
2010-09-27 19:30:42 +02:00
|
|
|
ActuatorCommandData command;
|
2010-09-27 09:28:34 +02:00
|
|
|
ActuatorSettingsData settings;
|
2010-10-03 20:37:07 +02:00
|
|
|
|
2010-10-01 14:33:33 +02:00
|
|
|
SystemSettingsData sysSettings;
|
|
|
|
MixerSettingsData mixerSettings;
|
|
|
|
ActuatorDesiredData desired;
|
|
|
|
MixerStatusData mixerStatus;
|
|
|
|
ManualControlCommandData manualControl;
|
2010-11-05 15:28:21 +01:00
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
ActuatorSettingsGet(&settings);
|
2011-02-01 03:17:55 +01:00
|
|
|
PIOS_Servo_SetHz(&settings.ChannelUpdateFreq[0], ACTUATORSETTINGS_CHANNELUPDATEFREQ_NUMELEM);
|
2010-10-03 20:37:07 +02:00
|
|
|
|
|
|
|
float * status = (float *)&mixerStatus; //access status objects as an array of floats
|
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
// Go to the neutral (failsafe) values until an ActuatorDesired update is received
|
|
|
|
setFailsafe();
|
2010-10-01 14:33:33 +02:00
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
// Main task loop
|
|
|
|
lastSysTime = xTaskGetTickCount();
|
|
|
|
while (1)
|
2010-11-23 18:13:42 +01:00
|
|
|
{
|
2011-01-10 01:16:30 +01:00
|
|
|
PIOS_WDG_UpdateFlag(PIOS_WDG_ACTUATOR);
|
2010-11-23 18:13:42 +01:00
|
|
|
|
2010-10-03 20:37:07 +02:00
|
|
|
// Wait until the ActuatorDesired object is updated, if a timeout then go to failsafe
|
|
|
|
if ( xQueueReceive(queue, &ev, FAILSAFE_TIMEOUT_MS / portTICK_RATE_MS) != pdTRUE )
|
|
|
|
{
|
|
|
|
setFailsafe();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-10-01 14:33:33 +02:00
|
|
|
// Check how long since last update
|
|
|
|
thisSysTime = xTaskGetTickCount();
|
|
|
|
if(thisSysTime > lastSysTime) // reuse dt in case of wraparound
|
2010-10-03 20:37:07 +02:00
|
|
|
dT = (thisSysTime - lastSysTime) / portTICK_RATE_MS / 1000.0f;
|
2010-10-01 14:33:33 +02:00
|
|
|
lastSysTime = thisSysTime;
|
2010-10-03 20:37:07 +02:00
|
|
|
|
|
|
|
|
2010-10-01 14:33:33 +02:00
|
|
|
ManualControlCommandGet(&manualControl);
|
|
|
|
SystemSettingsGet(&sysSettings);
|
|
|
|
MixerStatusGet(&mixerStatus);
|
|
|
|
MixerSettingsGet (&mixerSettings);
|
|
|
|
ActuatorDesiredGet(&desired);
|
2010-09-27 09:28:34 +02:00
|
|
|
ActuatorCommandGet(&command);
|
2010-09-27 19:30:42 +02:00
|
|
|
ActuatorSettingsGet(&settings);
|
2010-10-03 20:37:07 +02:00
|
|
|
|
2010-10-01 14:33:33 +02:00
|
|
|
int nMixers = 0;
|
2010-12-14 00:54:30 +01:00
|
|
|
Mixer_t * mixers = (Mixer_t *)&mixerSettings.Mixer1Type;
|
2010-10-01 14:33:33 +02:00
|
|
|
for(int ct=0; ct < MAX_MIX_ACTUATORS; ct++)
|
|
|
|
{
|
2010-12-14 00:54:30 +01:00
|
|
|
if(mixers[ct].type != MIXERSETTINGS_MIXER1TYPE_DISABLED)
|
2010-10-01 14:33:33 +02:00
|
|
|
{
|
|
|
|
nMixers ++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(nMixers < 2) //Nothing can fly with less than two mixers.
|
2010-09-27 19:30:42 +02:00
|
|
|
{
|
2010-10-01 14:33:33 +02:00
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_ACTUATOR, SYSTEMALARMS_ALARM_WARNING);
|
2010-10-03 20:37:07 +02:00
|
|
|
continue;
|
2010-09-27 19:30:42 +02:00
|
|
|
}
|
2010-10-03 20:37:07 +02:00
|
|
|
|
2010-10-01 16:09:14 +02:00
|
|
|
AlarmsClear(SYSTEMALARMS_ALARM_ACTUATOR);
|
2010-10-03 20:37:07 +02:00
|
|
|
|
2010-10-01 14:33:33 +02:00
|
|
|
bool armed = manualControl.Armed == MANUALCONTROLCOMMAND_ARMED_TRUE;
|
2010-11-04 03:30:53 +01:00
|
|
|
armed &= desired.Throttle > 0.00; //zero throttle stops the motors
|
2010-10-03 20:37:07 +02:00
|
|
|
|
2010-10-01 14:33:33 +02:00
|
|
|
float curve1 = MixerCurve(desired.Throttle,mixerSettings.ThrottleCurve1);
|
|
|
|
float curve2 = MixerCurve(desired.Throttle,mixerSettings.ThrottleCurve2);
|
|
|
|
for(int ct=0; ct < MAX_MIX_ACTUATORS; ct++)
|
2010-09-27 19:30:42 +02:00
|
|
|
{
|
2010-12-14 00:54:30 +01:00
|
|
|
if(mixers[ct].type != MIXERSETTINGS_MIXER1TYPE_DISABLED)
|
2010-10-01 14:33:33 +02:00
|
|
|
{
|
|
|
|
status[ct] = ProcessMixer(ct, curve1, curve2, &mixerSettings, &desired, dT);
|
|
|
|
|
|
|
|
if(!armed &&
|
2010-12-14 00:54:30 +01:00
|
|
|
mixers[ct].type == MIXERSETTINGS_MIXER1TYPE_MOTOR)
|
2010-10-01 14:33:33 +02:00
|
|
|
{
|
|
|
|
command.Channel[ct] = settings.ChannelMin[ct]; //force zero throttle
|
2010-10-03 20:37:07 +02:00
|
|
|
filterAccumulator[ct] = 0;
|
|
|
|
lastResult[ct] = 0;
|
2010-10-01 14:33:33 +02:00
|
|
|
}else
|
|
|
|
{
|
2011-01-10 02:11:44 +01:00
|
|
|
// For motors when armed keep above neutral
|
2011-01-10 02:13:55 +01:00
|
|
|
if((mixers[ct].type == MIXERSETTINGS_MIXER1TYPE_MOTOR) && (status[ct] < 0))
|
2011-01-10 02:11:44 +01:00
|
|
|
status[ct] = 0;
|
|
|
|
|
2010-10-01 14:33:33 +02:00
|
|
|
command.Channel[ct] = scaleChannel(status[ct],
|
|
|
|
settings.ChannelMax[ct],
|
2011-02-01 22:05:00 +01:00
|
|
|
settings.ChannelMin[ct],
|
2010-10-01 14:33:33 +02:00
|
|
|
settings.ChannelNeutral[ct]);
|
|
|
|
}
|
|
|
|
}
|
2010-09-27 19:30:42 +02:00
|
|
|
}
|
2010-10-03 20:37:07 +02:00
|
|
|
MixerStatusSet(&mixerStatus);
|
|
|
|
|
2011-01-10 02:11:44 +01:00
|
|
|
// Store update time
|
2011-02-01 03:18:22 +01:00
|
|
|
command.UpdateTime = 1000*dT;
|
2011-01-17 03:11:05 +01:00
|
|
|
if(1000*dT > command.MaxUpdateTime)
|
|
|
|
command.MaxUpdateTime = 1000*dT;
|
2011-01-10 02:11:44 +01:00
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
// Update output object
|
|
|
|
ActuatorCommandSet(&command);
|
|
|
|
// Update in case read only (eg. during servo configuration)
|
|
|
|
ActuatorCommandGet(&command);
|
2010-10-03 20:37:07 +02:00
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
// Update servo outputs
|
2011-01-10 02:11:44 +01:00
|
|
|
bool success = true;
|
2011-01-10 06:42:53 +01:00
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
for (int n = 0; n < ACTUATORCOMMAND_CHANNEL_NUMELEM; ++n)
|
|
|
|
{
|
2011-01-10 02:11:44 +01:00
|
|
|
success &= set_channel(n, command.Channel[n]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!success) {
|
|
|
|
command.NumFailedUpdates++;
|
|
|
|
ActuatorCommandSet(&command);
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_ACTUATOR, SYSTEMALARMS_ALARM_CRITICAL);
|
2010-09-27 09:28:34 +02:00
|
|
|
}
|
2011-01-10 02:11:44 +01:00
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
*Process mixing for one actuator
|
2010-09-27 19:30:42 +02:00
|
|
|
*/
|
2010-09-27 09:28:34 +02:00
|
|
|
|
|
|
|
float ProcessMixer(const int index, const float curve1, const float curve2,
|
2010-09-27 19:30:42 +02:00
|
|
|
MixerSettingsData* mixerSettings, ActuatorDesiredData* desired, const float period)
|
2010-09-27 09:28:34 +02:00
|
|
|
{
|
2010-12-14 00:54:30 +01:00
|
|
|
Mixer_t * mixers = (Mixer_t *)&mixerSettings->Mixer1Type; //pointer to array of mixers in UAVObjects
|
2010-09-27 19:30:42 +02:00
|
|
|
Mixer_t * mixer = &mixers[index];
|
2010-12-14 00:54:30 +01:00
|
|
|
float result = ((mixer->matrix[MIXERSETTINGS_MIXER1VECTOR_THROTTLECURVE1] / 128.0f) * curve1) +
|
|
|
|
((mixer->matrix[MIXERSETTINGS_MIXER1VECTOR_THROTTLECURVE2] / 128.0f) * curve2) +
|
|
|
|
((mixer->matrix[MIXERSETTINGS_MIXER1VECTOR_ROLL] / 128.0f) * desired->Roll) +
|
|
|
|
((mixer->matrix[MIXERSETTINGS_MIXER1VECTOR_PITCH] / 128.0f) * desired->Pitch) +
|
|
|
|
((mixer->matrix[MIXERSETTINGS_MIXER1VECTOR_YAW] / 128.0f) * desired->Yaw);
|
|
|
|
if(mixer->type == MIXERSETTINGS_MIXER1TYPE_MOTOR)
|
2010-09-27 19:30:42 +02:00
|
|
|
{
|
|
|
|
if(result < 0) //idle throttle
|
|
|
|
{
|
|
|
|
result = 0;
|
|
|
|
}
|
2010-10-03 20:37:07 +02:00
|
|
|
|
2010-09-27 19:30:42 +02:00
|
|
|
//feed forward
|
|
|
|
float accumulator = filterAccumulator[index];
|
|
|
|
accumulator += (result - lastResult[index]) * mixerSettings->FeedForward;
|
|
|
|
lastResult[index] = result;
|
|
|
|
result += accumulator;
|
2010-10-03 20:37:07 +02:00
|
|
|
if(period !=0)
|
2010-09-27 19:30:42 +02:00
|
|
|
{
|
2010-10-03 20:37:07 +02:00
|
|
|
if(accumulator > 0)
|
2010-09-27 19:30:42 +02:00
|
|
|
{
|
2010-10-03 20:37:07 +02:00
|
|
|
float filter = mixerSettings->AccelTime / period;
|
|
|
|
if(filter <1)
|
|
|
|
{
|
|
|
|
filter = 1;
|
|
|
|
}
|
|
|
|
accumulator -= accumulator / filter;
|
|
|
|
}else
|
2010-09-27 19:30:42 +02:00
|
|
|
{
|
2010-10-03 20:37:07 +02:00
|
|
|
float filter = mixerSettings->DecelTime / period;
|
|
|
|
if(filter <1)
|
|
|
|
{
|
|
|
|
filter = 1;
|
|
|
|
}
|
|
|
|
accumulator -= accumulator / filter;
|
2010-09-27 19:30:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
filterAccumulator[index] = accumulator;
|
|
|
|
result += accumulator;
|
2010-10-03 20:37:07 +02:00
|
|
|
|
2010-09-27 19:30:42 +02:00
|
|
|
//acceleration limit
|
|
|
|
float dt = result - lastFilteredResult[index];
|
2010-10-01 16:37:03 +02:00
|
|
|
float maxDt = mixerSettings->MaxAccel * period;
|
2010-09-27 19:30:42 +02:00
|
|
|
if(dt > maxDt) //we are accelerating too hard
|
|
|
|
{
|
|
|
|
result = lastFilteredResult[index] + maxDt;
|
|
|
|
}
|
|
|
|
lastFilteredResult[index] = result;
|
|
|
|
}
|
|
|
|
return(result);
|
2010-09-27 09:28:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
*Interpolate a throttle curve. Throttle input should be in the range 0 to 1.
|
|
|
|
*Output is in the range 0 to 1.
|
2010-09-27 19:30:42 +02:00
|
|
|
*/
|
2010-09-27 09:28:34 +02:00
|
|
|
|
|
|
|
#define MIXER_CURVE_ENTRIES 5
|
|
|
|
|
|
|
|
static float MixerCurve(const float throttle, const float* curve)
|
|
|
|
{
|
2010-09-27 19:30:42 +02:00
|
|
|
float scale = throttle * MIXER_CURVE_ENTRIES;
|
|
|
|
int idx1 = scale;
|
|
|
|
scale -= (float)idx1; //remainder
|
|
|
|
if(curve[0] < -1)
|
|
|
|
{
|
|
|
|
return(throttle);
|
|
|
|
}
|
|
|
|
if (idx1 < 0)
|
|
|
|
{
|
|
|
|
idx1 = 0; //clamp to lowest entry in table
|
|
|
|
scale = 0;
|
|
|
|
}
|
|
|
|
int idx2 = idx1 + 1;
|
|
|
|
if(idx2 >= MIXER_CURVE_ENTRIES)
|
|
|
|
{
|
|
|
|
idx2 = MIXER_CURVE_ENTRIES -1; //clamp to highest entry in table
|
|
|
|
if(idx1 >= MIXER_CURVE_ENTRIES)
|
|
|
|
{
|
|
|
|
idx1 = MIXER_CURVE_ENTRIES -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return((curve[idx1] * (1 - scale)) + (curve[idx2] * scale));
|
2010-09-27 09:28:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert channel from -1/+1 to servo pulse duration in microseconds
|
|
|
|
*/
|
|
|
|
static int16_t scaleChannel(float value, int16_t max, int16_t min, int16_t neutral)
|
|
|
|
{
|
|
|
|
int16_t valueScaled;
|
|
|
|
// Scale
|
|
|
|
if ( value >= 0.0)
|
|
|
|
{
|
|
|
|
valueScaled = (int16_t)(value*((float)(max-neutral))) + neutral;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
valueScaled = (int16_t)(value*((float)(neutral-min))) + neutral;
|
|
|
|
}
|
2010-10-03 20:37:07 +02:00
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
if (max>min)
|
|
|
|
{
|
|
|
|
if( valueScaled > max ) valueScaled = max;
|
|
|
|
if( valueScaled < min ) valueScaled = min;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( valueScaled < max ) valueScaled = max;
|
|
|
|
if( valueScaled > min ) valueScaled = min;
|
|
|
|
}
|
2010-10-03 20:37:07 +02:00
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
return valueScaled;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set actuator output to the neutral values (failsafe)
|
|
|
|
*/
|
|
|
|
static void setFailsafe()
|
|
|
|
{
|
2010-09-27 19:30:42 +02:00
|
|
|
ActuatorCommandData command;
|
2010-09-27 09:28:34 +02:00
|
|
|
ActuatorSettingsData settings;
|
2010-10-03 20:37:07 +02:00
|
|
|
|
2010-09-27 19:30:42 +02:00
|
|
|
ActuatorCommandGet(&command);
|
2010-09-27 09:28:34 +02:00
|
|
|
ActuatorSettingsGet(&settings);
|
2010-10-05 16:47:48 +02:00
|
|
|
|
|
|
|
MixerSettingsData mixerSettings;
|
|
|
|
MixerSettingsGet (&mixerSettings);
|
2010-12-14 00:54:30 +01:00
|
|
|
Mixer_t * mixers = (Mixer_t *)&mixerSettings.Mixer1Type; //pointer to array of mixers in UAVObjects
|
2010-10-05 16:47:48 +02:00
|
|
|
|
|
|
|
// Reset ActuatorCommand to safe values
|
2010-09-27 09:28:34 +02:00
|
|
|
for (int n = 0; n < ACTUATORCOMMAND_CHANNEL_NUMELEM; ++n)
|
|
|
|
{
|
2010-12-14 00:54:30 +01:00
|
|
|
if(mixers[n].type == MIXERSETTINGS_MIXER1TYPE_MOTOR)
|
2010-10-05 16:47:48 +02:00
|
|
|
{
|
|
|
|
command.Channel[n] = settings.ChannelMin[n];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
command.Channel[n] = settings.ChannelNeutral[n];
|
|
|
|
}
|
2010-09-27 09:28:34 +02:00
|
|
|
}
|
2010-10-03 20:37:07 +02:00
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
// Set alarm
|
|
|
|
AlarmsSet(SYSTEMALARMS_ALARM_ACTUATOR, SYSTEMALARMS_ALARM_CRITICAL);
|
2010-10-03 20:37:07 +02:00
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
// Update servo outputs
|
|
|
|
for (int n = 0; n < ACTUATORCOMMAND_CHANNEL_NUMELEM; ++n)
|
|
|
|
{
|
2011-01-10 02:11:44 +01:00
|
|
|
set_channel(n, command.Channel[n]);
|
2010-09-27 09:28:34 +02:00
|
|
|
}
|
2010-10-03 20:37:07 +02:00
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
// Update output object
|
|
|
|
ActuatorCommandSet(&command);
|
|
|
|
}
|
|
|
|
|
2011-02-01 03:17:55 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Update the servo update rate
|
|
|
|
*/
|
|
|
|
static void actuator_update_rate(UAVObjEvent * ev)
|
|
|
|
{
|
|
|
|
ActuatorSettingsData settings;
|
|
|
|
if ( ev->obj == ActuatorSettingsHandle() ) {
|
|
|
|
ActuatorSettingsGet(&settings);
|
|
|
|
PIOS_Servo_SetHz(&settings.ChannelUpdateFreq[0], ACTUATORSETTINGS_CHANNELUPDATEFREQ_NUMELEM);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-12 01:39:18 +01:00
|
|
|
#if defined(ARCH_POSIX) || defined(ARCH_WIN32)
|
|
|
|
static bool set_channel(uint8_t mixer_channel, uint16_t value) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#else
|
2011-01-10 02:11:44 +01:00
|
|
|
static bool set_channel(uint8_t mixer_channel, uint16_t value) {
|
|
|
|
|
|
|
|
ActuatorSettingsData settings;
|
|
|
|
ActuatorSettingsGet(&settings);
|
|
|
|
|
|
|
|
switch(settings.ChannelType[mixer_channel]) {
|
|
|
|
case ACTUATORSETTINGS_CHANNELTYPE_PWM:
|
|
|
|
PIOS_Servo_Set(settings.ChannelAddr[mixer_channel], value);
|
|
|
|
return true;
|
2011-02-20 08:14:02 +01:00
|
|
|
#if defined(PIOS_INCLUDE_I2C_ESC)
|
2011-01-10 02:11:44 +01:00
|
|
|
case ACTUATORSETTINGS_CHANNELTYPE_MK:
|
|
|
|
{
|
|
|
|
ManualControlCommandData manual;
|
|
|
|
ManualControlCommandGet(&manual);
|
|
|
|
/* Unfortunately MK controller take forever to start so keep */
|
|
|
|
/* them spinning when armed */
|
|
|
|
if(manual.Armed)
|
|
|
|
value = (value < 0) ? 1 : value;
|
|
|
|
else
|
|
|
|
value = 0;
|
|
|
|
|
|
|
|
return PIOS_SetMKSpeed(settings.ChannelAddr[mixer_channel],value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ACTUATORSETTINGS_CHANNELTYPE_ASTEC4:
|
|
|
|
return PIOS_SetAstec4Speed(settings.ChannelAddr[mixer_channel],value);
|
|
|
|
break;
|
2011-02-20 08:14:02 +01:00
|
|
|
#endif
|
2011-01-10 02:11:44 +01:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
2011-01-12 01:39:18 +01:00
|
|
|
#endif
|
2011-01-10 02:11:44 +01:00
|
|
|
|
2010-09-27 09:28:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @}
|
2010-09-27 19:30:42 +02:00
|
|
|
* @}
|
|
|
|
*/
|