From 6faf1a73aca305a32e3686b6077a5eac0d90c88c Mon Sep 17 00:00:00 2001 From: Corvus Corax Date: Sun, 24 Aug 2014 00:16:34 +0200 Subject: [PATCH] OP-1456 get rid of cast_struct_to_array because it is error prone! --- flight/libraries/alarms.c | 25 +++++---- flight/libraries/notification.c | 1 - flight/modules/CameraStab/camerastab.c | 29 +++++------ flight/modules/ManualControl/armhandler.c | 3 +- .../modules/ManualControl/stabilizedhandler.c | 15 +++--- flight/modules/PathFollower/pathfollower.c | 1 - flight/modules/PathPlanner/pathplanner.c | 1 - flight/modules/Receiver/receiver.c | 21 ++++---- flight/modules/Sensors/sensors.c | 3 +- flight/modules/Stabilization/innerloop.c | 17 +++---- flight/modules/Stabilization/outerloop.c | 19 ++++--- flight/modules/Stabilization/relay_tuning.c | 23 ++++----- flight/modules/Stabilization/stabilization.c | 51 +++++++++---------- flight/modules/Stabilization/virtualflybar.c | 3 +- flight/modules/StateEstimation/filterekf.c | 15 +++--- flight/modules/System/systemmod.c | 7 ++- flight/modules/TxPID/txpid.c | 17 +++---- flight/pios/inc/pios_struct_helper.h | 9 ---- flight/uavobjects/inc/uavobjectmanager.h | 5 ++ .../flight/uavobjectgeneratorflight.cpp | 6 ++- 20 files changed, 127 insertions(+), 144 deletions(-) diff --git a/flight/libraries/alarms.c b/flight/libraries/alarms.c index 8805ee8a6..db555ad8e 100644 --- a/flight/libraries/alarms.c +++ b/flight/libraries/alarms.c @@ -28,7 +28,6 @@ */ #include -#include #include "inc/alarms.h" // Private constants @@ -83,9 +82,9 @@ int32_t AlarmsSet(SystemAlarmsAlarmElem alarm, SystemAlarmsAlarmOptions severity SystemAlarmsAlarmGet(&alarms); uint16_t flightTime = (uint16_t)xTaskGetTickCount() * (uint16_t)portTICK_RATE_MS; // this deliberately overflows every 2^16 milliseconds to save memory if (((uint16_t)(flightTime - lastAlarmChange[alarm]) > PIOS_ALARM_GRACETIME && - cast_struct_to_array(alarms, alarms.Actuator)[alarm] != severity) - || cast_struct_to_array(alarms, alarms.Actuator)[alarm] < severity) { - cast_struct_to_array(alarms, alarms.Actuator)[alarm] = severity; + SystemAlarmsAlarmToArray(alarms)[alarm] != severity) + || SystemAlarmsAlarmToArray(alarms)[alarm] < severity) { + SystemAlarmsAlarmToArray(alarms)[alarm] = severity; lastAlarmChange[alarm] = flightTime; SystemAlarmsAlarmSet(&alarms); } @@ -122,11 +121,11 @@ int32_t ExtendedAlarmsSet(SystemAlarmsAlarmElem alarm, SystemAlarmsGet(&alarms); uint16_t flightTime = (uint16_t)xTaskGetTickCount() * (uint16_t)portTICK_RATE_MS; // this deliberately overflows every 2^16 milliseconds to save memory if (((uint16_t)(flightTime - lastAlarmChange[alarm]) > PIOS_ALARM_GRACETIME && - cast_struct_to_array(alarms.Alarm, alarms.Alarm.Actuator)[alarm] != severity) - || cast_struct_to_array(alarms.Alarm, alarms.Alarm.Actuator)[alarm] < severity) { - cast_struct_to_array(alarms.ExtendedAlarmStatus, alarms.ExtendedAlarmStatus.BootFault)[alarm] = status; - cast_struct_to_array(alarms.ExtendedAlarmSubStatus, alarms.ExtendedAlarmStatus.BootFault)[alarm] = subStatus; - cast_struct_to_array(alarms.Alarm, alarms.Alarm.Actuator)[alarm] = severity; + SystemAlarmsAlarmToArray(alarms.Alarm)[alarm] != severity) + || SystemAlarmsAlarmToArray(alarms.Alarm)[alarm] < severity) { + SystemAlarmsExtendedAlarmStatusToArray(alarms.ExtendedAlarmStatus)[alarm] = status; + SystemAlarmsExtendedAlarmSubStatusToArray(alarms.ExtendedAlarmSubStatus)[alarm] = subStatus; + SystemAlarmsAlarmToArray(alarms.Alarm)[alarm] = severity; lastAlarmChange[alarm] = flightTime; SystemAlarmsSet(&alarms); } @@ -152,7 +151,7 @@ SystemAlarmsAlarmOptions AlarmsGet(SystemAlarmsAlarmElem alarm) // Read alarm SystemAlarmsAlarmGet(&alarms); - return cast_struct_to_array(alarms, alarms.Actuator)[alarm]; + return SystemAlarmsAlarmToArray(alarms)[alarm]; } /** @@ -244,7 +243,7 @@ static int32_t hasSeverity(SystemAlarmsAlarmOptions severity) // Go through alarms and check if any are of the given severity or higher for (uint32_t n = 0; n < SYSTEMALARMS_ALARM_NUMELEM; ++n) { - if (cast_struct_to_array(alarms, alarms.Actuator)[n] >= severity) { + if (SystemAlarmsAlarmToArray(alarms)[n] >= severity) { xSemaphoreGiveRecursive(lock); return 1; } @@ -272,8 +271,8 @@ SystemAlarmsAlarmOptions AlarmsGetHighestSeverity() // Go through alarms and find the highest severity uint32_t n = 0; while (n < SYSTEMALARMS_ALARM_NUMELEM && highest != SYSTEMALARMS_ALARM_CRITICAL) { - if (cast_struct_to_array(alarmsData, alarmsData.Actuator)[n] > highest) { - highest = cast_struct_to_array(alarmsData, alarmsData.Actuator)[n]; + if (SystemAlarmsAlarmToArray(alarmsData)[n] > highest) { + highest = SystemAlarmsAlarmToArray(alarmsData)[n]; } n++; } diff --git a/flight/libraries/notification.c b/flight/libraries/notification.c index 9015e4dd3..2d11d224a 100644 --- a/flight/libraries/notification.c +++ b/flight/libraries/notification.c @@ -25,7 +25,6 @@ */ #include "inc/notification.h" #include -#include #include #include #include diff --git a/flight/modules/CameraStab/camerastab.c b/flight/modules/CameraStab/camerastab.c index 9d479c530..27ba36d28 100644 --- a/flight/modules/CameraStab/camerastab.c +++ b/flight/modules/CameraStab/camerastab.c @@ -52,7 +52,6 @@ #include "camerastabsettings.h" #include "cameradesired.h" #include "hwsettings.h" -#include // // Configuration @@ -172,22 +171,22 @@ static void attitudeUpdated(UAVObjEvent *ev) // process axes for (uint8_t i = 0; i < CAMERASTABSETTINGS_INPUT_NUMELEM; i++) { // read and process control input - if (cast_struct_to_array(cameraStab.Input, cameraStab.Input.Roll)[i] != CAMERASTABSETTINGS_INPUT_NONE) { - if (AccessoryDesiredInstGet(cast_struct_to_array(cameraStab.Input, cameraStab.Input.Roll)[i] - + if (CameraStabSettingsInputToArray(cameraStab.Input)[i] != CAMERASTABSETTINGS_INPUT_NONE) { + if (AccessoryDesiredInstGet(CameraStabSettingsInputToArray(cameraStab.Input)[i] - CAMERASTABSETTINGS_INPUT_ACCESSORY0, &accessory) == 0) { float input_rate; - switch (cast_struct_to_array(cameraStab.StabilizationMode, cameraStab.StabilizationMode.Roll)[i]) { + switch (CameraStabSettingsStabilizationModeToArray(cameraStab.StabilizationMode)[i]) { case CAMERASTABSETTINGS_STABILIZATIONMODE_ATTITUDE: csd->inputs[i] = accessory.AccessoryVal * - cast_struct_to_array(cameraStab.InputRange, cameraStab.InputRange.Roll)[i]; + CameraStabSettingsInputRangeToArray(cameraStab.InputRange)[i]; break; case CAMERASTABSETTINGS_STABILIZATIONMODE_AXISLOCK: input_rate = accessory.AccessoryVal * - cast_struct_to_array(cameraStab.InputRate, cameraStab.InputRate.Roll)[i]; + CameraStabSettingsInputRateToArray(cameraStab.InputRate)[i]; if (fabsf(input_rate) > cameraStab.MaxAxisLockRate) { csd->inputs[i] = boundf(csd->inputs[i] + input_rate * 0.001f * dT_millis, - -cast_struct_to_array(cameraStab.InputRange, cameraStab.InputRange.Roll)[i], - cast_struct_to_array(cameraStab.InputRange, cameraStab.InputRange.Roll)[i]); + -CameraStabSettingsInputRangeToArray(cameraStab.InputRange)[i], + CameraStabSettingsInputRangeToArray(cameraStab.InputRange)[i]); } break; default: @@ -214,14 +213,14 @@ static void attitudeUpdated(UAVObjEvent *ev) } #ifdef USE_GIMBAL_LPF - if (cast_struct_to_array(cameraStab.ResponseTime, cameraStab.ResponseTime.Roll)[i]) { - float rt = (float)cast_struct_to_array(cameraStab.ResponseTime, cameraStab.ResponseTime.Roll)[i]; + if (CameraStabSettingsResponseTimeToArray(cameraStab.ResponseTime)[i]) { + float rt = (float)CameraStabSettingsResponseTimeToArray(cameraStab.ResponseTime)[i]; attitude = csd->attitudeFiltered[i] = ((rt * csd->attitudeFiltered[i]) + (dT_millis * attitude)) / (rt + dT_millis); } #endif #ifdef USE_GIMBAL_FF - if (cast_struct_to_array(cameraStab.FeedForward, cameraStab.FeedForward.Roll)[i]) { + if (CameraStabSettingsFeedForwardToArray(cameraStab.FeedForward)[i]) { applyFeedForward(i, dT_millis, &attitude, &cameraStab); } #endif @@ -229,7 +228,7 @@ static void attitudeUpdated(UAVObjEvent *ev) // bounding for elevon mixing occurs on the unmixed output // to limit the range of the mixed output you must limit the range // of both the unmixed pitch and unmixed roll - float output = boundf((attitude + csd->inputs[i]) / cast_struct_to_array(cameraStab.OutputRange, cameraStab.OutputRange.Roll)[i], -1.0f, 1.0f); + float output = boundf((attitude + csd->inputs[i]) / CameraStabSettingsOutputRangeToArray(cameraStab.OutputRange)[i], -1.0f, 1.0f); // set output channels switch (i) { @@ -316,12 +315,12 @@ void applyFeedForward(uint8_t index, float dT_millis, float *attitude, CameraSta // apply feed forward float accumulator = csd->ffFilterAccumulator[index]; accumulator += (*attitude - csd->ffLastAttitude[index]) * - (float)cast_struct_to_array(cameraStab->FeedForward, cameraStab->FeedForward.Roll)[index] * gimbalTypeCorrection; + (float)CameraStabSettingsFeedForwardToArray(cameraStab->FeedForward)[index] * gimbalTypeCorrection; csd->ffLastAttitude[index] = *attitude; *attitude += accumulator; - float filter = (float)((accumulator > 0.0f) ? cast_struct_to_array(cameraStab->AccelTime, cameraStab->AccelTime.Roll)[index] : - cast_struct_to_array(cameraStab->DecelTime, cameraStab->DecelTime.Roll)[index]) / dT_millis; + float filter = (float)((accumulator > 0.0f) ? CameraStabSettingsAccelTimeToArray(cameraStab->AccelTime)[index] : + CameraStabSettingsDecelTimeToArray(cameraStab->DecelTime)[index]) / dT_millis; if (filter < 1.0f) { filter = 1.0f; } diff --git a/flight/modules/ManualControl/armhandler.c b/flight/modules/ManualControl/armhandler.c index 7dbf79336..dbaefa779 100644 --- a/flight/modules/ManualControl/armhandler.c +++ b/flight/modules/ManualControl/armhandler.c @@ -29,7 +29,6 @@ */ #include "inc/manualcontrol.h" -#include #include #include #include @@ -259,7 +258,7 @@ static bool okToArm(void) // Check each alarm for (int i = 0; i < SYSTEMALARMS_ALARM_NUMELEM; i++) { - if (cast_struct_to_array(alarms.Alarm, alarms.Alarm.Actuator)[i] >= SYSTEMALARMS_ALARM_CRITICAL) { // found an alarm thats set + if (SystemAlarmsAlarmToArray(alarms.Alarm)[i] >= SYSTEMALARMS_ALARM_CRITICAL) { // found an alarm thats set if (i == SYSTEMALARMS_ALARM_GPS || i == SYSTEMALARMS_ALARM_TELEMETRY) { continue; } diff --git a/flight/modules/ManualControl/stabilizedhandler.c b/flight/modules/ManualControl/stabilizedhandler.c index 2ab4222f9..582929425 100644 --- a/flight/modules/ManualControl/stabilizedhandler.c +++ b/flight/modules/ManualControl/stabilizedhandler.c @@ -29,7 +29,6 @@ */ #include "inc/manualcontrol.h" -#include #include #include #include @@ -70,27 +69,27 @@ void stabilizedHandler(bool newinit) FlightStatusGet(&flightStatus); switch (flightStatus.FlightMode) { case FLIGHTSTATUS_FLIGHTMODE_STABILIZED1: - stab_settings = cast_struct_to_array(settings.Stabilization1Settings, settings.Stabilization1Settings.Roll); + stab_settings = FlightModeSettingsStabilization1SettingsToArray(settings.Stabilization1Settings); break; case FLIGHTSTATUS_FLIGHTMODE_STABILIZED2: - stab_settings = cast_struct_to_array(settings.Stabilization2Settings, settings.Stabilization2Settings.Roll); + stab_settings = FlightModeSettingsStabilization2SettingsToArray(settings.Stabilization2Settings); break; case FLIGHTSTATUS_FLIGHTMODE_STABILIZED3: - stab_settings = cast_struct_to_array(settings.Stabilization3Settings, settings.Stabilization3Settings.Roll); + stab_settings = FlightModeSettingsStabilization3SettingsToArray(settings.Stabilization3Settings); break; case FLIGHTSTATUS_FLIGHTMODE_STABILIZED4: - stab_settings = cast_struct_to_array(settings.Stabilization4Settings, settings.Stabilization4Settings.Roll); + stab_settings = FlightModeSettingsStabilization4SettingsToArray(settings.Stabilization4Settings); break; case FLIGHTSTATUS_FLIGHTMODE_STABILIZED5: - stab_settings = cast_struct_to_array(settings.Stabilization5Settings, settings.Stabilization5Settings.Roll); + stab_settings = FlightModeSettingsStabilization5SettingsToArray(settings.Stabilization5Settings); break; case FLIGHTSTATUS_FLIGHTMODE_STABILIZED6: - stab_settings = cast_struct_to_array(settings.Stabilization6Settings, settings.Stabilization6Settings.Roll); + stab_settings = FlightModeSettingsStabilization6SettingsToArray(settings.Stabilization6Settings); break; default: // Major error, this should not occur because only enter this block when one of these is true AlarmsSet(SYSTEMALARMS_ALARM_MANUALCONTROL, SYSTEMALARMS_ALARM_CRITICAL); - stab_settings = cast_struct_to_array(settings.Stabilization1Settings, settings.Stabilization1Settings.Roll); + stab_settings = FlightModeSettingsStabilization1SettingsToArray(settings.Stabilization1Settings); return; } diff --git a/flight/modules/PathFollower/pathfollower.c b/flight/modules/PathFollower/pathfollower.c index 67226589b..039e85722 100644 --- a/flight/modules/PathFollower/pathfollower.c +++ b/flight/modules/PathFollower/pathfollower.c @@ -51,7 +51,6 @@ #include #include #include -#include #include #include #include diff --git a/flight/modules/PathPlanner/pathplanner.c b/flight/modules/PathPlanner/pathplanner.c index 32d2afbae..2ab9f1a61 100644 --- a/flight/modules/PathPlanner/pathplanner.c +++ b/flight/modules/PathPlanner/pathplanner.c @@ -43,7 +43,6 @@ #include "waypoint.h" #include "waypointactive.h" #include "flightmodesettings.h" -#include #include "paths.h" #include "plans.h" diff --git a/flight/modules/Receiver/receiver.c b/flight/modules/Receiver/receiver.c index 6f79ba16d..cf3230751 100644 --- a/flight/modules/Receiver/receiver.c +++ b/flight/modules/Receiver/receiver.c @@ -33,7 +33,6 @@ */ #include -#include #include #include #include @@ -210,12 +209,12 @@ static void receiverTask(__attribute__((unused)) void *parameters) for (uint8_t n = 0; n < MANUALCONTROLSETTINGS_CHANNELGROUPS_NUMELEM && n < MANUALCONTROLCOMMAND_CHANNEL_NUMELEM; ++n) { extern uint32_t pios_rcvr_group_map[]; - if (cast_struct_to_array(settings.ChannelGroups, settings.ChannelGroups.Roll)[n] >= MANUALCONTROLSETTINGS_CHANNELGROUPS_NONE) { + if (ManualControlSettingsChannelGroupsToArray(settings.ChannelGroups)[n] >= MANUALCONTROLSETTINGS_CHANNELGROUPS_NONE) { cmd.Channel[n] = PIOS_RCVR_INVALID; } else { cmd.Channel[n] = PIOS_RCVR_Read(pios_rcvr_group_map[ - cast_struct_to_array(settings.ChannelGroups, settings.ChannelGroups.Pitch)[n]], - cast_struct_to_array(settings.ChannelNumber, settings.ChannelNumber.Pitch)[n]); + ManualControlSettingsChannelGroupsToArray(settings.ChannelGroups)[n]], + ManualControlSettingsChannelNumberToArray(settings.ChannelNumber)[n]); } // If a channel has timed out this is not valid data and we shouldn't update anything @@ -224,9 +223,9 @@ static void receiverTask(__attribute__((unused)) void *parameters) valid_input_detected = false; } else { scaledChannel[n] = scaleChannel(cmd.Channel[n], - cast_struct_to_array(settings.ChannelMax, settings.ChannelMax.Pitch)[n], - cast_struct_to_array(settings.ChannelMin, settings.ChannelMin.Pitch)[n], - cast_struct_to_array(settings.ChannelNeutral, settings.ChannelNeutral.Pitch)[n]); + ManualControlSettingsChannelMaxToArray(settings.ChannelMax)[n], + ManualControlSettingsChannelMinToArray(settings.ChannelMin)[n], + ManualControlSettingsChannelNeutralToArray(settings.ChannelNeutral)[n]); } } @@ -445,8 +444,8 @@ static void receiverTask(__attribute__((unused)) void *parameters) if (pios_usb_rctx_id) { PIOS_USB_RCTX_Update(pios_usb_rctx_id, cmd.Channel, - cast_struct_to_array(settings.ChannelMin, settings.ChannelMin.Roll), - cast_struct_to_array(settings.ChannelMax, settings.ChannelMax.Roll), + ManualControlSettingsChannelMinToArray(settings.ChannelMin), + ManualControlSettingsChannelMaxToArray(settings.ChannelMax), NELEMENTS(cmd.Channel)); } #endif /* PIOS_INCLUDE_USB_RCTX */ @@ -661,8 +660,8 @@ static void applyDeadband(float *value, float deadband) */ static void applyLPF(float *value, ManualControlSettingsResponseTimeElem channel, ManualControlSettingsData *settings, float dT) { - if (cast_struct_to_array(settings->ResponseTime, settings->ResponseTime.Roll)[channel]) { - float rt = (float)cast_struct_to_array(settings->ResponseTime, settings->ResponseTime.Roll)[channel]; + if (ManualControlSettingsResponseTimeToArray(settings->ResponseTime)[channel]) { + float rt = (float)ManualControlSettingsResponseTimeToArray(settings->ResponseTime)[channel]; inputFiltered[channel] = ((rt * inputFiltered[channel]) + (dT * (*value))) / (rt + dT); *value = inputFiltered[channel]; } diff --git a/flight/modules/Sensors/sensors.c b/flight/modules/Sensors/sensors.c index b24e34317..fb51af90e 100644 --- a/flight/modules/Sensors/sensors.c +++ b/flight/modules/Sensors/sensors.c @@ -62,7 +62,6 @@ #include #include -#include // Private constants #define STACK_SIZE_BYTES 1000 @@ -495,7 +494,7 @@ static void settingsUpdatedCb(__attribute__((unused)) UAVObjEvent *objEv) } else { Quaternion2R(rotationQuat, R); } - matrix_mult_3x3f((float(*)[3])cast_struct_to_array(cal.mag_transform, cal.mag_transform.r0c0), R, mag_transform); + matrix_mult_3x3f((float(*)[3])RevoCalibrationmag_transformToArray(cal.mag_transform), R, mag_transform); } /** * @} diff --git a/flight/modules/Stabilization/innerloop.c b/flight/modules/Stabilization/innerloop.c index e7254b933..68d6499da 100644 --- a/flight/modules/Stabilization/innerloop.c +++ b/flight/modules/Stabilization/innerloop.c @@ -32,7 +32,6 @@ */ #include -#include #include #include #include @@ -165,21 +164,21 @@ static void stabilizationInnerloopTask() dT = PIOS_DELTATIME_GetAverageSeconds(&timeval); for (t = 0; t < AXES; t++) { - bool reinit = (cast_struct_to_array(enabled, enabled.Roll)[t] != previous_mode[t]); - previous_mode[t] = cast_struct_to_array(enabled, enabled.Roll)[t]; + bool reinit = (StabilizationStatusInnerLoopToArray(enabled)[t] != previous_mode[t]); + previous_mode[t] = StabilizationStatusInnerLoopToArray(enabled)[t]; if (t < STABILIZATIONSTATUS_INNERLOOP_THRUST) { if (reinit) { stabSettings.innerPids[t].iAccumulator = 0; } - switch (cast_struct_to_array(enabled, enabled.Roll)[t]) { + switch (StabilizationStatusInnerLoopToArray(enabled)[t]) { case STABILIZATIONSTATUS_INNERLOOP_VIRTUALFLYBAR: stabilization_virtual_flybar(gyro_filtered[t], rate[t], &actuatorDesiredAxis[t], dT, reinit, t, &stabSettings.settings); break; case STABILIZATIONSTATUS_INNERLOOP_RELAYTUNING: rate[t] = boundf(rate[t], - -cast_struct_to_array(stabSettings.stabBank.MaximumRate, stabSettings.stabBank.MaximumRate.Roll)[t], - cast_struct_to_array(stabSettings.stabBank.MaximumRate, stabSettings.stabBank.MaximumRate.Roll)[t] + -StabilizationBankMaximumRateToArray(stabSettings.stabBank.MaximumRate)[t], + StabilizationBankMaximumRateToArray(stabSettings.stabBank.MaximumRate)[t] ); stabilization_relay_rate(rate[t] - gyro_filtered[t], &actuatorDesiredAxis[t], t, reinit); break; @@ -198,8 +197,8 @@ static void stabilizationInnerloopTask() case STABILIZATIONSTATUS_INNERLOOP_RATE: // limit rate to maximum configured limits (once here instead of 5 times in outer loop) rate[t] = boundf(rate[t], - -cast_struct_to_array(stabSettings.stabBank.MaximumRate, stabSettings.stabBank.MaximumRate.Roll)[t], - cast_struct_to_array(stabSettings.stabBank.MaximumRate, stabSettings.stabBank.MaximumRate.Roll)[t] + -StabilizationBankMaximumRateToArray(stabSettings.stabBank.MaximumRate)[t], + StabilizationBankMaximumRateToArray(stabSettings.stabBank.MaximumRate)[t] ); actuatorDesiredAxis[t] = pid_apply_setpoint(&stabSettings.innerPids[t], speedScaleFactor, rate[t], gyro_filtered[t], dT); break; @@ -209,7 +208,7 @@ static void stabilizationInnerloopTask() break; } } else { - switch (cast_struct_to_array(enabled, enabled.Roll)[t]) { + switch (StabilizationStatusInnerLoopToArray(enabled)[t]) { case STABILIZATIONSTATUS_INNERLOOP_CRUISECONTROL: actuatorDesiredAxis[t] = cruisecontrol_apply_factor(rate[t]); break; diff --git a/flight/modules/Stabilization/outerloop.c b/flight/modules/Stabilization/outerloop.c index 7d865b556..588fdb50f 100644 --- a/flight/modules/Stabilization/outerloop.c +++ b/flight/modules/Stabilization/outerloop.c @@ -32,7 +32,6 @@ */ #include -#include #include #include #include @@ -115,7 +114,7 @@ static void stabilizationOuterloopTask() float q_error[4]; for (t = 0; t < 3; t++) { - switch (cast_struct_to_array(enabled, enabled.Roll)[t]) { + switch (StabilizationStatusOuterLoopToArray(enabled)[t]) { case STABILIZATIONSTATUS_OUTERLOOP_ATTITUDE: case STABILIZATIONSTATUS_OUTERLOOP_RATTITUDE: case STABILIZATIONSTATUS_OUTERLOOP_WEAKLEVELING: @@ -150,14 +149,14 @@ static void stabilizationOuterloopTask() #endif /* if defined(PIOS_QUATERNION_STABILIZATION) */ } for (t = 0; t < AXES; t++) { - bool reinit = (cast_struct_to_array(enabled, enabled.Roll)[t] != previous_mode[t]); - previous_mode[t] = cast_struct_to_array(enabled, enabled.Roll)[t]; + bool reinit = (StabilizationStatusOuterLoopToArray(enabled)[t] != previous_mode[t]); + previous_mode[t] = StabilizationStatusOuterLoopToArray(enabled)[t]; if (t < STABILIZATIONSTATUS_OUTERLOOP_THRUST) { if (reinit) { stabSettings.outerPids[t].iAccumulator = 0; } - switch (cast_struct_to_array(enabled, enabled.Roll)[t]) { + switch (StabilizationStatusOuterLoopToArray(enabled)[t]) { case STABILIZATIONSTATUS_OUTERLOOP_ATTITUDE: rateDesiredAxis[t] = pid_apply(&stabSettings.outerPids[t], local_error[t], dT); break; @@ -167,11 +166,11 @@ static void stabilizationOuterloopTask() stickinput[0] = boundf(stabilizationDesiredAxis[0] / stabSettings.stabBank.RollMax, -1.0f, 1.0f); stickinput[1] = boundf(stabilizationDesiredAxis[1] / stabSettings.stabBank.PitchMax, -1.0f, 1.0f); stickinput[2] = boundf(stabilizationDesiredAxis[2] / stabSettings.stabBank.YawMax, -1.0f, 1.0f); - float rateDesiredAxisRate = stickinput[t] * cast_struct_to_array(stabSettings.stabBank.ManualRate, stabSettings.stabBank.ManualRate.Roll)[t]; + float rateDesiredAxisRate = stickinput[t] * StabilizationBankManualRateToArray(stabSettings.stabBank.ManualRate)[t]; // limit corrective rate to maximum rates to not give it overly large impact over manual rate when joined together rateDesiredAxis[t] = boundf(pid_apply(&stabSettings.outerPids[t], local_error[t], dT), - -cast_struct_to_array(stabSettings.stabBank.ManualRate, stabSettings.stabBank.ManualRate.Roll)[t], - cast_struct_to_array(stabSettings.stabBank.ManualRate, stabSettings.stabBank.ManualRate.Roll)[t] + -StabilizationBankManualRateToArray(stabSettings.stabBank.ManualRate)[t], + StabilizationBankManualRateToArray(stabSettings.stabBank.ManualRate)[t] ); // Compute the weighted average rate desired // Using max() rather than sqrt() for cpu speed; @@ -232,7 +231,7 @@ static void stabilizationOuterloopTask() stickinput[0] = boundf(stabilizationDesiredAxis[0] / stabSettings.stabBank.RollMax, -1.0f, 1.0f); stickinput[1] = boundf(stabilizationDesiredAxis[1] / stabSettings.stabBank.PitchMax, -1.0f, 1.0f); stickinput[2] = boundf(stabilizationDesiredAxis[2] / stabSettings.stabBank.YawMax, -1.0f, 1.0f); - float rate_input = stickinput[t] * cast_struct_to_array(stabSettings.stabBank.ManualRate, stabSettings.stabBank.ManualRate.Roll)[t]; + float rate_input = stickinput[t] * StabilizationBankManualRateToArray(stabSettings.stabBank.ManualRate)[t]; float weak_leveling = local_error[t] * stabSettings.settings.WeakLevelingKp; weak_leveling = boundf(weak_leveling, -stabSettings.settings.MaxWeakLevelingRate, stabSettings.settings.MaxWeakLevelingRate); @@ -246,7 +245,7 @@ static void stabilizationOuterloopTask() break; } } else { - switch (cast_struct_to_array(enabled, enabled.Roll)[t]) { + switch (StabilizationStatusOuterLoopToArray(enabled)[t]) { #ifdef REVOLUTION case STABILIZATIONSTATUS_OUTERLOOP_ALTITUDE: rateDesiredAxis[t] = stabilizationAltitudeHold(stabilizationDesiredAxis[t], ALTITUDEHOLD, reinit); diff --git a/flight/modules/Stabilization/relay_tuning.c b/flight/modules/Stabilization/relay_tuning.c index 316340f1b..d4e4c8320 100644 --- a/flight/modules/Stabilization/relay_tuning.c +++ b/flight/modules/Stabilization/relay_tuning.c @@ -32,7 +32,6 @@ */ #include "openpilot.h" -#include #include "stabilization.h" #include "relaytuning.h" #include "relaytuningsettings.h" @@ -72,8 +71,8 @@ int stabilization_relay_rate(float error, float *output, int axis, bool reinit) // On first run initialize estimates to something reasonable if (reinit) { rateRelayRunning[axis] = false; - cast_struct_to_array(relay.Period, relay.Period.Roll)[axis] = 200; - cast_struct_to_array(relay.Gain, relay.Gain.Roll)[axis] = 0; + RelayTuningPeriodToArray(relay.Period)[axis] = 200; + RelayTuningGainToArray(relay.Gain)[axis] = 0; accum_sin = 0; accum_cos = 0; @@ -96,14 +95,14 @@ int stabilization_relay_rate(float error, float *output, int axis, bool reinit) /**** The code below here is to estimate the properties of the oscillation ****/ // Make sure the period can't go below limit - if (cast_struct_to_array(relay.Period, relay.Period.Roll)[axis] < DEGLITCH_TIME) { - cast_struct_to_array(relay.Period, relay.Period.Roll)[axis] = DEGLITCH_TIME; + if (RelayTuningPeriodToArray(relay.Period)[axis] < DEGLITCH_TIME) { + RelayTuningPeriodToArray(relay.Period)[axis] = DEGLITCH_TIME; } // Project the error onto a sine and cosine of the same frequency // to accumulate the average amplitude int32_t dT = thisTime - lastHighTime; - float phase = ((float)360 * (float)dT) / cast_struct_to_array(relay.Period, relay.Period.Roll)[axis]; + float phase = ((float)360 * (float)dT) / RelayTuningPeriodToArray(relay.Period)[axis]; if (phase >= 360) { phase = 0; } @@ -126,15 +125,15 @@ int stabilization_relay_rate(float error, float *output, int axis, bool reinit) if (rateRelayRunning[axis] == false) { rateRelayRunning[axis] = true; - cast_struct_to_array(relay.Period, relay.Period.Roll)[axis] = 200; - cast_struct_to_array(relay.Gain, relay.Gain.Roll)[axis] = 0; + RelayTuningPeriodToArray(relay.Period)[axis] = 200; + RelayTuningGainToArray(relay.Gain)[axis] = 0; } else { // Low pass filter each amplitude and period - cast_struct_to_array(relay.Gain, relay.Gain.Roll)[axis] = - cast_struct_to_array(relay.Gain, relay.Gain.Roll)[axis] * + RelayTuningGainToArray(relay.Gain)[axis] = + RelayTuningGainToArray(relay.Gain)[axis] * AMPLITUDE_ALPHA + this_gain * (1 - AMPLITUDE_ALPHA); - cast_struct_to_array(relay.Period, relay.Period.Roll)[axis] = - cast_struct_to_array(relay.Period, relay.Period.Roll)[axis] * + RelayTuningPeriodToArray(relay.Period)[axis] = + RelayTuningPeriodToArray(relay.Period)[axis] * PERIOD_ALPHA + dT * (1 - PERIOD_ALPHA); } lastHighTime = thisTime; diff --git a/flight/modules/Stabilization/stabilization.c b/flight/modules/Stabilization/stabilization.c index 92b9081d0..202a5c89f 100644 --- a/flight/modules/Stabilization/stabilization.c +++ b/flight/modules/Stabilization/stabilization.c @@ -33,7 +33,6 @@ #include -#include #include #include #include @@ -134,54 +133,54 @@ static void StabilizationDesiredUpdatedCb(__attribute__((unused)) UAVObjEvent *e StabilizationDesiredStabilizationModeGet(&mode); for (t = 0; t < AXES; t++) { - switch (cast_struct_to_array(mode, mode.Roll)[t]) { + switch (StabilizationDesiredStabilizationModeToArray(mode)[t]) { case STABILIZATIONDESIRED_STABILIZATIONMODE_MANUAL: - cast_struct_to_array(status.OuterLoop, status.OuterLoop.Roll)[t] = STABILIZATIONSTATUS_OUTERLOOP_DIRECT; - cast_struct_to_array(status.InnerLoop, status.InnerLoop.Roll)[t] = STABILIZATIONSTATUS_INNERLOOP_DIRECT; + StabilizationStatusOuterLoopToArray(status.OuterLoop)[t] = STABILIZATIONSTATUS_OUTERLOOP_DIRECT; + StabilizationStatusInnerLoopToArray(status.InnerLoop)[t] = STABILIZATIONSTATUS_INNERLOOP_DIRECT; break; case STABILIZATIONDESIRED_STABILIZATIONMODE_RATE: - cast_struct_to_array(status.OuterLoop, status.OuterLoop.Roll)[t] = STABILIZATIONSTATUS_OUTERLOOP_DIRECT; - cast_struct_to_array(status.InnerLoop, status.InnerLoop.Roll)[t] = STABILIZATIONSTATUS_INNERLOOP_RATE; + StabilizationStatusOuterLoopToArray(status.OuterLoop)[t] = STABILIZATIONSTATUS_OUTERLOOP_DIRECT; + StabilizationStatusInnerLoopToArray(status.InnerLoop)[t] = STABILIZATIONSTATUS_INNERLOOP_RATE; break; case STABILIZATIONDESIRED_STABILIZATIONMODE_ATTITUDE: - cast_struct_to_array(status.OuterLoop, status.OuterLoop.Roll)[t] = STABILIZATIONSTATUS_OUTERLOOP_ATTITUDE; - cast_struct_to_array(status.InnerLoop, status.InnerLoop.Roll)[t] = STABILIZATIONSTATUS_INNERLOOP_RATE; + StabilizationStatusOuterLoopToArray(status.OuterLoop)[t] = STABILIZATIONSTATUS_OUTERLOOP_ATTITUDE; + StabilizationStatusInnerLoopToArray(status.InnerLoop)[t] = STABILIZATIONSTATUS_INNERLOOP_RATE; break; case STABILIZATIONDESIRED_STABILIZATIONMODE_AXISLOCK: - cast_struct_to_array(status.OuterLoop, status.OuterLoop.Roll)[t] = STABILIZATIONSTATUS_OUTERLOOP_DIRECT; - cast_struct_to_array(status.InnerLoop, status.InnerLoop.Roll)[t] = STABILIZATIONSTATUS_INNERLOOP_AXISLOCK; + StabilizationStatusOuterLoopToArray(status.OuterLoop)[t] = STABILIZATIONSTATUS_OUTERLOOP_DIRECT; + StabilizationStatusInnerLoopToArray(status.InnerLoop)[t] = STABILIZATIONSTATUS_INNERLOOP_AXISLOCK; break; case STABILIZATIONDESIRED_STABILIZATIONMODE_WEAKLEVELING: - cast_struct_to_array(status.OuterLoop, status.OuterLoop.Roll)[t] = STABILIZATIONSTATUS_OUTERLOOP_WEAKLEVELING; - cast_struct_to_array(status.InnerLoop, status.InnerLoop.Roll)[t] = STABILIZATIONSTATUS_INNERLOOP_RATE; + StabilizationStatusOuterLoopToArray(status.OuterLoop)[t] = STABILIZATIONSTATUS_OUTERLOOP_WEAKLEVELING; + StabilizationStatusInnerLoopToArray(status.InnerLoop)[t] = STABILIZATIONSTATUS_INNERLOOP_RATE; break; case STABILIZATIONDESIRED_STABILIZATIONMODE_VIRTUALBAR: - cast_struct_to_array(status.OuterLoop, status.OuterLoop.Roll)[t] = STABILIZATIONSTATUS_OUTERLOOP_DIRECT; - cast_struct_to_array(status.InnerLoop, status.InnerLoop.Roll)[t] = STABILIZATIONSTATUS_INNERLOOP_VIRTUALFLYBAR; + StabilizationStatusOuterLoopToArray(status.OuterLoop)[t] = STABILIZATIONSTATUS_OUTERLOOP_DIRECT; + StabilizationStatusInnerLoopToArray(status.InnerLoop)[t] = STABILIZATIONSTATUS_INNERLOOP_VIRTUALFLYBAR; break; case STABILIZATIONDESIRED_STABILIZATIONMODE_RATTITUDE: - cast_struct_to_array(status.OuterLoop, status.OuterLoop.Roll)[t] = STABILIZATIONSTATUS_OUTERLOOP_RATTITUDE; - cast_struct_to_array(status.InnerLoop, status.InnerLoop.Roll)[t] = STABILIZATIONSTATUS_INNERLOOP_RATE; + StabilizationStatusOuterLoopToArray(status.OuterLoop)[t] = STABILIZATIONSTATUS_OUTERLOOP_RATTITUDE; + StabilizationStatusInnerLoopToArray(status.InnerLoop)[t] = STABILIZATIONSTATUS_INNERLOOP_RATE; break; case STABILIZATIONDESIRED_STABILIZATIONMODE_RELAYRATE: - cast_struct_to_array(status.OuterLoop, status.OuterLoop.Roll)[t] = STABILIZATIONSTATUS_OUTERLOOP_DIRECT; - cast_struct_to_array(status.InnerLoop, status.InnerLoop.Roll)[t] = STABILIZATIONSTATUS_INNERLOOP_RELAYTUNING; + StabilizationStatusOuterLoopToArray(status.OuterLoop)[t] = STABILIZATIONSTATUS_OUTERLOOP_DIRECT; + StabilizationStatusInnerLoopToArray(status.InnerLoop)[t] = STABILIZATIONSTATUS_INNERLOOP_RELAYTUNING; break; case STABILIZATIONDESIRED_STABILIZATIONMODE_RELAYATTITUDE: - cast_struct_to_array(status.OuterLoop, status.OuterLoop.Roll)[t] = STABILIZATIONSTATUS_OUTERLOOP_ATTITUDE; - cast_struct_to_array(status.InnerLoop, status.InnerLoop.Roll)[t] = STABILIZATIONSTATUS_INNERLOOP_RELAYTUNING; + StabilizationStatusOuterLoopToArray(status.OuterLoop)[t] = STABILIZATIONSTATUS_OUTERLOOP_ATTITUDE; + StabilizationStatusInnerLoopToArray(status.InnerLoop)[t] = STABILIZATIONSTATUS_INNERLOOP_RELAYTUNING; break; case STABILIZATIONDESIRED_STABILIZATIONMODE_ALTITUDEHOLD: - cast_struct_to_array(status.OuterLoop, status.OuterLoop.Roll)[t] = STABILIZATIONSTATUS_OUTERLOOP_ALTITUDE; - cast_struct_to_array(status.InnerLoop, status.InnerLoop.Roll)[t] = STABILIZATIONSTATUS_INNERLOOP_CRUISECONTROL; + StabilizationStatusOuterLoopToArray(status.OuterLoop)[t] = STABILIZATIONSTATUS_OUTERLOOP_ALTITUDE; + StabilizationStatusInnerLoopToArray(status.InnerLoop)[t] = STABILIZATIONSTATUS_INNERLOOP_CRUISECONTROL; break; case STABILIZATIONDESIRED_STABILIZATIONMODE_ALTITUDEVARIO: - cast_struct_to_array(status.OuterLoop, status.OuterLoop.Roll)[t] = STABILIZATIONSTATUS_OUTERLOOP_ALTITUDEVARIO; - cast_struct_to_array(status.InnerLoop, status.InnerLoop.Roll)[t] = STABILIZATIONSTATUS_INNERLOOP_CRUISECONTROL; + StabilizationStatusOuterLoopToArray(status.OuterLoop)[t] = STABILIZATIONSTATUS_OUTERLOOP_ALTITUDEVARIO; + StabilizationStatusInnerLoopToArray(status.InnerLoop)[t] = STABILIZATIONSTATUS_INNERLOOP_CRUISECONTROL; break; case STABILIZATIONDESIRED_STABILIZATIONMODE_CRUISECONTROL: - cast_struct_to_array(status.OuterLoop, status.OuterLoop.Roll)[t] = STABILIZATIONSTATUS_OUTERLOOP_DIRECT; - cast_struct_to_array(status.InnerLoop, status.InnerLoop.Roll)[t] = STABILIZATIONSTATUS_INNERLOOP_CRUISECONTROL; + StabilizationStatusOuterLoopToArray(status.OuterLoop)[t] = STABILIZATIONSTATUS_OUTERLOOP_DIRECT; + StabilizationStatusInnerLoopToArray(status.InnerLoop)[t] = STABILIZATIONSTATUS_INNERLOOP_CRUISECONTROL; break; } } diff --git a/flight/modules/Stabilization/virtualflybar.c b/flight/modules/Stabilization/virtualflybar.c index cc1a284f1..a815fe034 100644 --- a/flight/modules/Stabilization/virtualflybar.c +++ b/flight/modules/Stabilization/virtualflybar.c @@ -32,7 +32,6 @@ #include "openpilot.h" #include -#include #include "stabilization.h" #include "stabilizationsettings.h" @@ -78,7 +77,7 @@ int stabilization_virtual_flybar(float gyro, float command, float *output, float } // Command signal is composed of stick input added to the gyro and virtual flybar - *output = command * cast_struct_to_array(settings->VbarSensitivity, settings->VbarSensitivity.Roll)[axis] - + *output = command * StabilizationSettingsVbarSensitivityToArray(settings->VbarSensitivity)[axis] - gyro_gain * (vbar_integral[axis] * ki + gyro * kp); return 0; diff --git a/flight/modules/StateEstimation/filterekf.c b/flight/modules/StateEstimation/filterekf.c index 320a9ef35..3931226e4 100644 --- a/flight/modules/StateEstimation/filterekf.c +++ b/flight/modules/StateEstimation/filterekf.c @@ -31,7 +31,6 @@ */ #include "inc/stateestimation.h" -#include #include #include @@ -165,17 +164,17 @@ static int32_t maininit(stateFilter *self) int t; // plausibility check for (t = 0; t < EKFCONFIGURATION_P_NUMELEM; t++) { - if (invalid_var(cast_struct_to_array(this->ekfConfiguration.P, this->ekfConfiguration.P.AttitudeQ1)[t])) { + if (invalid_var(EKFConfigurationPToArray(this->ekfConfiguration.P)[t])) { return 2; } } for (t = 0; t < EKFCONFIGURATION_Q_NUMELEM; t++) { - if (invalid_var(cast_struct_to_array(this->ekfConfiguration.Q, this->ekfConfiguration.Q.AccelX)[t])) { + if (invalid_var(EKFConfigurationQToArray(this->ekfConfiguration.Q)[t])) { return 2; } } for (t = 0; t < EKFCONFIGURATION_R_NUMELEM; t++) { - if (invalid_var(cast_struct_to_array(this->ekfConfiguration.R, this->ekfConfiguration.R.BaroZ)[t])) { + if (invalid_var(EKFConfigurationRToArray(this->ekfConfiguration.R)[t])) { return 2; } } @@ -295,7 +294,7 @@ static filterResult filter(stateFilter *self, stateEstimation *state) INSSetState(this->work.pos, (float *)zeros, this->work.attitude, (float *)zeros, (float *)zeros); - INSResetP(cast_struct_to_array(this->ekfConfiguration.P, this->ekfConfiguration.P.AttitudeQ1)); + INSResetP(EKFConfigurationPToArray(this->ekfConfiguration.P)); } else { // Run prediction a bit before any corrections @@ -422,12 +421,12 @@ static filterResult filter(stateFilter *self, stateEstimation *state) EKFStateVarianceData vardata; EKFStateVarianceGet(&vardata); - INSGetP(cast_struct_to_array(vardata.P, vardata.P.AttitudeQ1)); + INSGetP(EKFStateVariancePToArray(vardata.P)); EKFStateVarianceSet(&vardata); int t; for (t = 0; t < EKFSTATEVARIANCE_P_NUMELEM; t++) { - if (!IS_REAL(cast_struct_to_array(vardata.P, vardata.P.AttitudeQ1)[t]) || cast_struct_to_array(vardata.P, vardata.P.AttitudeQ1)[t] <= 0.0f) { - INSResetP(cast_struct_to_array(this->ekfConfiguration.P, this->ekfConfiguration.P.AttitudeQ1)); + if (!IS_REAL(EKFStateVariancePToArray(vardata.P)[t]) || EKFStateVariancePToArray(vardata.P)[t] <= 0.0f) { + INSResetP(EKFConfigurationPToArray(this->ekfConfiguration.P)); this->init_stage = -1; break; } diff --git a/flight/modules/System/systemmod.c b/flight/modules/System/systemmod.c index aa6a8f54d..97ff55f3d 100644 --- a/flight/modules/System/systemmod.c +++ b/flight/modules/System/systemmod.c @@ -39,7 +39,6 @@ */ #include -#include // private includes #include "inc/systemmod.h" #include "notification.h" @@ -420,9 +419,9 @@ static void taskMonitorForEachCallback(uint16_t task_id, const struct pios_task_ // By convention, there is a direct mapping between task monitor task_id's and members // of the TaskInfoXXXXElem enums PIOS_DEBUG_Assert(task_id < TASKINFO_RUNNING_NUMELEM); - cast_struct_to_array(taskData->Running, taskData->Running.System)[task_id] = task_info->is_running ? TASKINFO_RUNNING_TRUE : TASKINFO_RUNNING_FALSE; - ((uint16_t *)&taskData->StackRemaining)[task_id] = task_info->stack_remaining; - ((uint8_t *)&taskData->RunningTime)[task_id] = task_info->running_time_percentage; + TaskInfoRunningToArray(taskData->Running)[task_id] = task_info->is_running ? TASKINFO_RUNNING_TRUE : TASKINFO_RUNNING_FALSE; + ((uint16_t *)&taskData->StackRemaining)[task_id] = task_info->stack_remaining; + ((uint8_t *)&taskData->RunningTime)[task_id] = task_info->running_time_percentage; } static void callbackSchedulerForEachCallback(int16_t callback_id, const struct pios_callback_info *callback_info, void *context) diff --git a/flight/modules/TxPID/txpid.c b/flight/modules/TxPID/txpid.c index f356558e8..3ade671eb 100644 --- a/flight/modules/TxPID/txpid.c +++ b/flight/modules/TxPID/txpid.c @@ -51,7 +51,6 @@ */ #include "openpilot.h" -#include #include "txpidsettings.h" #include "accessorydesired.h" #include "manualcontrolcommand.h" @@ -195,26 +194,26 @@ static void updatePIDs(UAVObjEvent *ev) // Loop through every enabled instance for (uint8_t i = 0; i < TXPIDSETTINGS_PIDS_NUMELEM; i++) { - if (cast_struct_to_array(inst.PIDs, inst.PIDs.Instance1)[i] != TXPIDSETTINGS_PIDS_DISABLED) { + if (TxPIDSettingsPIDsToArray(inst.PIDs)[i] != TXPIDSETTINGS_PIDS_DISABLED) { float value; - if (cast_struct_to_array(inst.Inputs, inst.Inputs.Instance1)[i] == TXPIDSETTINGS_INPUTS_THROTTLE) { + if (TxPIDSettingsInputsToArray(inst.Inputs)[i] == TXPIDSETTINGS_INPUTS_THROTTLE) { ManualControlCommandThrottleGet(&value); value = scale(value, inst.ThrottleRange.Min, inst.ThrottleRange.Max, - cast_struct_to_array(inst.MinPID, inst.MinPID.Instance1)[i], - cast_struct_to_array(inst.MaxPID, inst.MaxPID.Instance1)[i]); + TxPIDSettingsMinPIDToArray(inst.MinPID)[i], + TxPIDSettingsMaxPIDToArray(inst.MaxPID)[i]); } else if (AccessoryDesiredInstGet( - cast_struct_to_array(inst.Inputs, inst.Inputs.Instance1)[i] - TXPIDSETTINGS_INPUTS_ACCESSORY0, + TxPIDSettingsInputsToArray(inst.Inputs)[i] - TXPIDSETTINGS_INPUTS_ACCESSORY0, &accessory) == 0) { value = scale(accessory.AccessoryVal, -1.0f, 1.0f, - cast_struct_to_array(inst.MinPID, inst.MinPID.Instance1)[i], - cast_struct_to_array(inst.MaxPID, inst.MaxPID.Instance1)[i]); + TxPIDSettingsMinPIDToArray(inst.MinPID)[i], + TxPIDSettingsMaxPIDToArray(inst.MaxPID)[i]); } else { continue; } - switch (cast_struct_to_array(inst.PIDs, inst.PIDs.Instance1)[i]) { + switch (TxPIDSettingsPIDsToArray(inst.PIDs)[i]) { case TXPIDSETTINGS_PIDS_ROLLRATEKP: needsUpdateBank |= update(&bank.RollRatePID.Kp, value); break; diff --git a/flight/pios/inc/pios_struct_helper.h b/flight/pios/inc/pios_struct_helper.h index 65bae405a..1198b6fb1 100644 --- a/flight/pios/inc/pios_struct_helper.h +++ b/flight/pios/inc/pios_struct_helper.h @@ -17,13 +17,4 @@ (type *)((char *)__mptr - offsetof(type, member)); } \ ) -/** - * cast_struct_to_array casts an homogeneous structure instance to an array - * of typeof(struct_field). struct_field need to be any of the fields - * containing inside the struct - * @instance: homogeneous structure to cast - * @struct_field: a field contained inside the structure - */ -#define cast_struct_to_array(instance, struct_field) \ - ((typeof(struct_field) *) & (instance)) #endif /* PIOS_STRUCT_HELPER_H */ diff --git a/flight/uavobjects/inc/uavobjectmanager.h b/flight/uavobjects/inc/uavobjectmanager.h index bbd3fc5b7..63be8b17c 100644 --- a/flight/uavobjects/inc/uavobjectmanager.h +++ b/flight/uavobjects/inc/uavobjectmanager.h @@ -51,6 +51,11 @@ typedef void *UAVObjHandle; #define MetaObjectId(id) ((id) + 1) +#define UAVObjectFieldToArray(type, var) \ + ({ type *const dummy = &(var); \ + (((type##Array *)dummy)->array); } \ + ) + /** * Object update mode, used by multiple modules (e.g. telemetry and logger) */ diff --git a/ground/uavobjgenerator/generators/flight/uavobjectgeneratorflight.cpp b/ground/uavobjgenerator/generators/flight/uavobjectgeneratorflight.cpp index 72d4c1a63..1cc41bbf7 100644 --- a/ground/uavobjgenerator/generators/flight/uavobjectgeneratorflight.cpp +++ b/ground/uavobjgenerator/generators/flight/uavobjectgeneratorflight.cpp @@ -131,7 +131,11 @@ bool UAVObjectGeneratorFlight::process_object(ObjectInfo *info) for (int f = 0; f < info->fields[n]->elementNames.count(); f++) { structType.append(QString(" %1 %2;\n").arg(type).arg(info->fields[n]->elementNames[f])); } - structType.append(QString("} %1 ;\n\n").arg(structTypeName)); + structType.append(QString("} %1 ;\n").arg(structTypeName)); + structType.append(QString("typedef struct __attribute__ ((__packed__)) {\n")); + structType.append(QString(" %1 array[%2];\n").arg(type).arg(info->fields[n]->elementNames.count())); + structType.append(QString("} %1Array ;\n").arg(structTypeName)); + structType.append(QString("#define %1%2ToArray( var ) UAVObjectFieldToArray( %3, var )\n\n").arg(info->name).arg(info->fields[n]->name).arg(structTypeName)); dataStructures.append(structType);