52 #include "txpidsettings.h"
53 #include "manualcontrolcommand.h"
54 #include "stabilizationsettings.h"
55 #include "sensorsettings.h"
56 #include "vbarsettings.h"
57 #include "vtolpathfollowersettings.h"
59 #include "flightstatus.h"
60 #include "modulesettings.h"
65 #define SAMPLE_PERIOD_MS 200
66 #define TELEMETRY_UPDATE_PERIOD_MS 0 // 0 = update on change (default)
69 #if (TXPIDSETTINGS_PIDS_NUMELEM != TXPIDSETTINGS_INPUTS_NUMELEM) || \
70 (TXPIDSETTINGS_PIDS_NUMELEM != TXPIDSETTINGS_MINPID_NUMELEM) || \
71 (TXPIDSETTINGS_PIDS_NUMELEM != TXPIDSETTINGS_MAXPID_NUMELEM)
72 #error Invalid TxPID UAVObject definition (inconsistent number of field elements)
78 StabilizationSettingsData
stab;
82 #if (TELEMETRY_UPDATE_PERIOD_MS != 0)
83 UAVObjMetadata metadata;
92 void *ctx,
void *obj,
int len);
93 static bool update(
float *var,
float val);
94 static float scale(
float val,
float inMin,
float inMax,
float outMin,
float outMax);
104 #ifdef MODULE_TxPID_BUILTIN
105 module_enabled =
true;
107 uint8_t module_state[MODULESETTINGS_ADMINSTATE_NUMELEM];
108 ModuleSettingsAdminStateGet(module_state);
109 if (module_state[MODULESETTINGS_ADMINSTATE_TXPID] == MODULESETTINGS_ADMINSTATE_ENABLED) {
110 module_enabled =
true;
114 if (TxPIDSettingsInitialize() == -1) {
115 module_enabled =
false;
119 if (module_enabled) {
122 if (txpid_data != NULL) {
123 memset(txpid_data, 0x00,
sizeof(*txpid_data));
125 if (TxPIDSettingsInitialize() == -1) {
126 module_enabled =
false;
143 if (txpid_data == NULL) {
150 #if (TELEMETRY_UPDATE_PERIOD_MS != 0)
157 if (StabilizationSettingsInitialize() == -1){
161 StabilizationSettingsGetMetadata(&txpid_data->metadata);
162 txpid_data->metadata.telemetryAcked = 0;
165 StabilizationSettingsSetMetadata(&txpid_data->metadata);
177 void *ctx,
void *obj,
int len)
179 (void) ev; (void) ctx; (void) obj; (void) len;
181 TxPIDSettingsGet(&txpid_data->
inst);
183 if (txpid_data->
inst.UpdateMode == TXPIDSETTINGS_UPDATEMODE_NEVER)
187 FlightStatusArmedGet(&armed);
188 if ((txpid_data->
inst.UpdateMode == TXPIDSETTINGS_UPDATEMODE_WHENARMED) &&
189 (armed == FLIGHTSTATUS_ARMED_DISARMED))
192 StabilizationSettingsGet(&txpid_data->
stab);
193 SensorSettingsGet(&txpid_data->
sensor);
194 VbarSettingsGet(&txpid_data->
vbar);
197 if (VtolPathFollowerSettingsHandle()) {
201 bool vtolPathFollowerSettingsNeedsUpdate =
false;
203 bool stabilizationSettingsNeedsUpdate =
false;
204 bool sensorSettingsNeedsUpdate =
false;
205 bool vbarSettingsNeedsUpdate =
false;
208 for (uint8_t
i = 0;
i < TXPIDSETTINGS_PIDS_NUMELEM;
i++) {
209 if (txpid_data->
inst.PIDs[
i] != TXPIDSETTINGS_PIDS_DISABLED) {
212 if (txpid_data->
inst.Inputs[
i] == TXPIDSETTINGS_INPUTS_THROTTLE) {
213 ManualControlCommandThrottleGet(&value);
215 txpid_data->
inst.ThrottleRange[TXPIDSETTINGS_THROTTLERANGE_MIN],
216 txpid_data->
inst.ThrottleRange[TXPIDSETTINGS_THROTTLERANGE_MAX],
217 txpid_data->
inst.MinPID[
i], txpid_data->
inst.MaxPID[
i]);
221 int idx = txpid_data->
inst.Inputs[
i] - TXPIDSETTINGS_INPUTS_ACCESSORY0;
227 if (idx >= MANUALCONTROLCOMMAND_ACCESSORY_NUMELEM) {
231 float accessories[MANUALCONTROLCOMMAND_ACCESSORY_NUMELEM];
233 ManualControlCommandAccessoryGet(accessories);
235 value =
scale(accessories[idx], -1.0, 1.0, txpid_data->
inst.MinPID[
i], txpid_data->
inst.MaxPID[
i]);
238 switch (txpid_data->
inst.PIDs[
i]) {
239 case TXPIDSETTINGS_PIDS_ROLLRATEKP:
240 stabilizationSettingsNeedsUpdate |=
update(&txpid_data->
stab.RollRatePID[STABILIZATIONSETTINGS_ROLLRATEPID_KP], value);
242 case TXPIDSETTINGS_PIDS_ROLLRATEKI:
243 stabilizationSettingsNeedsUpdate |=
update(&txpid_data->
stab.RollRatePID[STABILIZATIONSETTINGS_ROLLRATEPID_KI], value);
245 case TXPIDSETTINGS_PIDS_ROLLRATEKD:
246 stabilizationSettingsNeedsUpdate |=
update(&txpid_data->
stab.RollRatePID[STABILIZATIONSETTINGS_ROLLRATEPID_KD], value);
248 case TXPIDSETTINGS_PIDS_ROLLRATEILIMIT:
249 stabilizationSettingsNeedsUpdate |=
update(&txpid_data->
stab.RollRatePID[STABILIZATIONSETTINGS_ROLLRATEPID_ILIMIT], value);
251 case TXPIDSETTINGS_PIDS_ROLLATTITUDEKP:
252 stabilizationSettingsNeedsUpdate |=
update(&txpid_data->
stab.RollPI[STABILIZATIONSETTINGS_ROLLPI_KP], value);
254 case TXPIDSETTINGS_PIDS_ROLLATTITUDEKI:
255 stabilizationSettingsNeedsUpdate |=
update(&txpid_data->
stab.RollPI[STABILIZATIONSETTINGS_ROLLPI_KI], value);
257 case TXPIDSETTINGS_PIDS_ROLLATTITUDEILIMIT:
258 stabilizationSettingsNeedsUpdate |=
update(&txpid_data->
stab.RollPI[STABILIZATIONSETTINGS_ROLLPI_ILIMIT], value);
260 case TXPIDSETTINGS_PIDS_PITCHRATEKP:
261 stabilizationSettingsNeedsUpdate |=
update(&txpid_data->
stab.PitchRatePID[STABILIZATIONSETTINGS_PITCHRATEPID_KP], value);
263 case TXPIDSETTINGS_PIDS_PITCHRATEKI:
264 stabilizationSettingsNeedsUpdate |=
update(&txpid_data->
stab.PitchRatePID[STABILIZATIONSETTINGS_PITCHRATEPID_KI], value);
266 case TXPIDSETTINGS_PIDS_PITCHRATEKD:
267 stabilizationSettingsNeedsUpdate |=
update(&txpid_data->
stab.PitchRatePID[STABILIZATIONSETTINGS_PITCHRATEPID_KD], value);
269 case TXPIDSETTINGS_PIDS_PITCHRATEILIMIT:
270 stabilizationSettingsNeedsUpdate |=
update(&txpid_data->
stab.PitchRatePID[STABILIZATIONSETTINGS_PITCHRATEPID_ILIMIT], value);
272 case TXPIDSETTINGS_PIDS_PITCHATTITUDEKP:
273 stabilizationSettingsNeedsUpdate |=
update(&txpid_data->
stab.PitchPI[STABILIZATIONSETTINGS_PITCHPI_KP], value);
275 case TXPIDSETTINGS_PIDS_PITCHATTITUDEKI:
276 stabilizationSettingsNeedsUpdate |=
update(&txpid_data->
stab.PitchPI[STABILIZATIONSETTINGS_PITCHPI_KI], value);
278 case TXPIDSETTINGS_PIDS_PITCHATTITUDEILIMIT:
279 stabilizationSettingsNeedsUpdate |=
update(&txpid_data->
stab.PitchPI[STABILIZATIONSETTINGS_PITCHPI_ILIMIT], value);
281 case TXPIDSETTINGS_PIDS_ROLLPITCHRATEKP:
282 stabilizationSettingsNeedsUpdate |=
update(&txpid_data->
stab.RollRatePID[STABILIZATIONSETTINGS_ROLLRATEPID_KP], value);
283 stabilizationSettingsNeedsUpdate |=
update(&txpid_data->
stab.PitchRatePID[STABILIZATIONSETTINGS_PITCHRATEPID_KP], value);
285 case TXPIDSETTINGS_PIDS_ROLLPITCHRATEKI:
286 stabilizationSettingsNeedsUpdate |=
update(&txpid_data->
stab.RollRatePID[STABILIZATIONSETTINGS_ROLLRATEPID_KI], value);
287 stabilizationSettingsNeedsUpdate |=
update(&txpid_data->
stab.PitchRatePID[STABILIZATIONSETTINGS_PITCHRATEPID_KI], value);
289 case TXPIDSETTINGS_PIDS_ROLLPITCHRATEKD:
290 stabilizationSettingsNeedsUpdate |=
update(&txpid_data->
stab.RollRatePID[STABILIZATIONSETTINGS_ROLLRATEPID_KD], value);
291 stabilizationSettingsNeedsUpdate |=
update(&txpid_data->
stab.PitchRatePID[STABILIZATIONSETTINGS_PITCHRATEPID_KD], value);
293 case TXPIDSETTINGS_PIDS_ROLLPITCHRATEILIMIT:
294 stabilizationSettingsNeedsUpdate |=
update(&txpid_data->
stab.RollRatePID[STABILIZATIONSETTINGS_ROLLRATEPID_ILIMIT], value);
295 stabilizationSettingsNeedsUpdate |=
update(&txpid_data->
stab.PitchRatePID[STABILIZATIONSETTINGS_PITCHRATEPID_ILIMIT], value);
297 case TXPIDSETTINGS_PIDS_ROLLPITCHATTITUDEKP:
298 stabilizationSettingsNeedsUpdate |=
update(&txpid_data->
stab.RollPI[STABILIZATIONSETTINGS_ROLLPI_KP], value);
299 stabilizationSettingsNeedsUpdate |=
update(&txpid_data->
stab.PitchPI[STABILIZATIONSETTINGS_PITCHPI_KP], value);
301 case TXPIDSETTINGS_PIDS_ROLLPITCHATTITUDEKI:
302 stabilizationSettingsNeedsUpdate |=
update(&txpid_data->
stab.RollPI[STABILIZATIONSETTINGS_ROLLPI_KI], value);
303 stabilizationSettingsNeedsUpdate |=
update(&txpid_data->
stab.PitchPI[STABILIZATIONSETTINGS_PITCHPI_KI], value);
305 case TXPIDSETTINGS_PIDS_ROLLPITCHATTITUDEILIMIT:
306 stabilizationSettingsNeedsUpdate |=
update(&txpid_data->
stab.RollPI[STABILIZATIONSETTINGS_ROLLPI_ILIMIT], value);
307 stabilizationSettingsNeedsUpdate |=
update(&txpid_data->
stab.PitchPI[STABILIZATIONSETTINGS_PITCHPI_ILIMIT], value);
309 case TXPIDSETTINGS_PIDS_YAWRATEKP:
310 stabilizationSettingsNeedsUpdate |=
update(&txpid_data->
stab.YawRatePID[STABILIZATIONSETTINGS_YAWRATEPID_KP], value);
312 case TXPIDSETTINGS_PIDS_YAWRATEKI:
313 stabilizationSettingsNeedsUpdate |=
update(&txpid_data->
stab.YawRatePID[STABILIZATIONSETTINGS_YAWRATEPID_KI], value);
315 case TXPIDSETTINGS_PIDS_YAWRATEKD:
316 stabilizationSettingsNeedsUpdate |=
update(&txpid_data->
stab.YawRatePID[STABILIZATIONSETTINGS_YAWRATEPID_KD], value);
318 case TXPIDSETTINGS_PIDS_YAWRATEILIMIT:
319 stabilizationSettingsNeedsUpdate |=
update(&txpid_data->
stab.YawRatePID[STABILIZATIONSETTINGS_YAWRATEPID_ILIMIT], value);
321 case TXPIDSETTINGS_PIDS_YAWATTITUDEKP:
322 stabilizationSettingsNeedsUpdate |=
update(&txpid_data->
stab.YawPI[STABILIZATIONSETTINGS_YAWPI_KP], value);
324 case TXPIDSETTINGS_PIDS_YAWATTITUDEKI:
325 stabilizationSettingsNeedsUpdate |=
update(&txpid_data->
stab.YawPI[STABILIZATIONSETTINGS_YAWPI_KI], value);
327 case TXPIDSETTINGS_PIDS_YAWATTITUDEILIMIT:
328 stabilizationSettingsNeedsUpdate |=
update(&txpid_data->
stab.YawPI[STABILIZATIONSETTINGS_YAWPI_ILIMIT], value);
330 case TXPIDSETTINGS_PIDS_SENSORSCUTOFF:
331 sensorSettingsNeedsUpdate |=
update(&txpid_data->
sensor.LowpassCutoff, value);
333 case TXPIDSETTINGS_PIDS_ROLLVBARSENSITIVITY:
334 vbarSettingsNeedsUpdate |=
update(&txpid_data->
vbar.VbarSensitivity[VBARSETTINGS_VBARSENSITIVITY_ROLL], value);
336 case TXPIDSETTINGS_PIDS_PITCHVBARSENSITIVITY:
337 vbarSettingsNeedsUpdate |=
update(&txpid_data->
vbar.VbarSensitivity[VBARSETTINGS_VBARSENSITIVITY_PITCH], value);
339 case TXPIDSETTINGS_PIDS_ROLLPITCHVBARSENSITIVITY:
340 vbarSettingsNeedsUpdate |=
update(&txpid_data->
vbar.VbarSensitivity[VBARSETTINGS_VBARSENSITIVITY_ROLL], value);
341 vbarSettingsNeedsUpdate |=
update(&txpid_data->
vbar.VbarSensitivity[VBARSETTINGS_VBARSENSITIVITY_PITCH], value);
344 case TXPIDSETTINGS_PIDS_YAWVBARSENSITIVITY:
345 vbarSettingsNeedsUpdate |=
update(&txpid_data->
vbar.VbarSensitivity[VBARSETTINGS_VBARSENSITIVITY_YAW], value);
347 case TXPIDSETTINGS_PIDS_ROLLVBARKP:
348 vbarSettingsNeedsUpdate |=
update(&txpid_data->
vbar.VbarRollPID[VBARSETTINGS_VBARROLLPID_KP], value);
350 case TXPIDSETTINGS_PIDS_ROLLVBARKI:
351 vbarSettingsNeedsUpdate |=
update(&txpid_data->
vbar.VbarRollPID[VBARSETTINGS_VBARROLLPID_KI], value);
353 case TXPIDSETTINGS_PIDS_ROLLVBARKD:
354 vbarSettingsNeedsUpdate |=
update(&txpid_data->
vbar.VbarRollPID[VBARSETTINGS_VBARROLLPID_KD], value);
356 case TXPIDSETTINGS_PIDS_PITCHVBARKP:
357 vbarSettingsNeedsUpdate |=
update(&txpid_data->
vbar.VbarPitchPID[VBARSETTINGS_VBARPITCHPID_KP], value);
359 case TXPIDSETTINGS_PIDS_PITCHVBARKI:
360 vbarSettingsNeedsUpdate |=
update(&txpid_data->
vbar.VbarPitchPID[VBARSETTINGS_VBARPITCHPID_KI], value);
362 case TXPIDSETTINGS_PIDS_PITCHVBARKD:
363 vbarSettingsNeedsUpdate |=
update(&txpid_data->
vbar.VbarPitchPID[VBARSETTINGS_VBARPITCHPID_KD], value);
365 case TXPIDSETTINGS_PIDS_ROLLPITCHVBARKP:
366 vbarSettingsNeedsUpdate |=
update(&txpid_data->
vbar.VbarRollPID[VBARSETTINGS_VBARROLLPID_KP], value);
367 vbarSettingsNeedsUpdate |=
update(&txpid_data->
vbar.VbarPitchPID[VBARSETTINGS_VBARPITCHPID_KP], value);
369 case TXPIDSETTINGS_PIDS_ROLLPITCHVBARKI:
370 vbarSettingsNeedsUpdate |=
update(&txpid_data->
vbar.VbarRollPID[VBARSETTINGS_VBARROLLPID_KI], value);
371 vbarSettingsNeedsUpdate |=
update(&txpid_data->
vbar.VbarPitchPID[VBARSETTINGS_VBARPITCHPID_KI], value);
373 case TXPIDSETTINGS_PIDS_ROLLPITCHVBARKD:
374 vbarSettingsNeedsUpdate |=
update(&txpid_data->
vbar.VbarRollPID[VBARSETTINGS_VBARROLLPID_KD], value);
375 vbarSettingsNeedsUpdate |=
update(&txpid_data->
vbar.VbarPitchPID[VBARSETTINGS_VBARPITCHPID_KD], value);
377 case TXPIDSETTINGS_PIDS_YAWVBARKP:
378 vbarSettingsNeedsUpdate |=
update(&txpid_data->
vbar.VbarYawPID[VBARSETTINGS_VBARYAWPID_KP], value);
380 case TXPIDSETTINGS_PIDS_YAWVBARKI:
381 vbarSettingsNeedsUpdate |=
update(&txpid_data->
vbar.VbarYawPID[VBARSETTINGS_VBARYAWPID_KI], value);
383 case TXPIDSETTINGS_PIDS_YAWVBARKD:
384 vbarSettingsNeedsUpdate |=
update(&txpid_data->
vbar.VbarYawPID[VBARSETTINGS_VBARYAWPID_KD], value);
386 case TXPIDSETTINGS_PIDS_CAMERATILT:
387 stabilizationSettingsNeedsUpdate |=
update(&txpid_data->
stab.CameraTilt, value);
389 case TXPIDSETTINGS_PIDS_HORIZONTALPOSKP:
392 case TXPIDSETTINGS_PIDS_HORIZONTALPOSKI:
395 case TXPIDSETTINGS_PIDS_HORIZONTALPOSILIMIT:
398 case TXPIDSETTINGS_PIDS_HORIZONTALVELKP:
401 case TXPIDSETTINGS_PIDS_HORIZONTALVELKI:
404 case TXPIDSETTINGS_PIDS_HORIZONTALVELKD:
417 if (stabilizationSettingsNeedsUpdate) {
418 StabilizationSettingsSet(&txpid_data->
stab);
421 if (sensorSettingsNeedsUpdate) {
422 SensorSettingsSet(&txpid_data->
sensor);
425 if (vbarSettingsNeedsUpdate) {
426 VbarSettingsSet(&txpid_data->
vbar);
429 if (vtolPathFollowerSettingsNeedsUpdate) {
431 if (VtolPathFollowerSettingsHandle()) {
444 static float scale(
float val,
float inMin,
float inMax,
float outMin,
float outMax)
447 if (val > inMax) val = inMax;
448 if (val < inMin) val = inMin;
454 val = (val - inMin) / (inMax - inMin);
457 if (outMin > outMax) {
464 return (outMax - outMin) * val + outMin;
471 static bool update(
float *var,
float val)
MODULE_INITCALL(TxPIDInitialize, TxPIDStart)
static void updatePIDs(const UAVObjEvent *ev, void *ctx, void *obj, int len)
static float scale(float val, float inMin, float inMax, float outMin, float outMax)
VtolPathFollowerSettingsData vtolPathFollowerSettingsData
int32_t TxPIDInitialize(void)
void * PIOS_malloc(size_t size)
StabilizationSettingsData stab
int32_t EventPeriodicCallbackCreate(UAVObjEvent *ev, UAVObjEventCallback cb, uint16_t periodMs)
static int32_t TxPIDStart(void)
Event dispatcher, distributes object events as callbacks. Alternative to using tasks and queues...
SensorSettingsData sensor
static struct txpid_struct * txpid_data
Includes PiOS and core architecture components.
static bool update(float *var, float val)
#define TELEMETRY_UPDATE_PERIOD_MS