41 #include "altitudeholdsettings.h"
42 #include "baroaltitude.h"
43 #include "flighttelemetrystats.h"
44 #include "flightstatus.h"
45 #include "loitercommand.h"
46 #include "pathdesired.h"
47 #include "positionactual.h"
48 #include "receiveractivity.h"
49 #include "systemsettings.h"
53 #if defined(PIOS_INCLUDE_OPENLRS_RCVR)
58 #define ARMED_THRESHOLD 0.50f
64 #define CONNECTION_OFFSET 250
66 #define RCVR_ACTIVITY_MONITOR_CHANNELS_PER_GROUP 12
67 #define RCVR_ACTIVITY_MONITOR_MIN_RANGE 20
71 #define THRUST_BIDIR_THRESH 0.35f
77 #define MIN_MEANINGFUL_RANGE 40
80 ManualControlSettingsChannelGroupsOptions
group;
88 static ManualControlCommandData
cmd;
104 bool always_fullrange);
122 case MANUALCONTROLSETTINGS_RSSITYPE_PWM:
123 return MANUALCONTROLSETTINGS_CHANNELGROUPS_PWM;
124 case MANUALCONTROLSETTINGS_RSSITYPE_PPM:
125 return MANUALCONTROLSETTINGS_CHANNELGROUPS_PPM;
126 case MANUALCONTROLSETTINGS_RSSITYPE_SBUS:
127 return MANUALCONTROLSETTINGS_CHANNELGROUPS_SBUS;
128 case MANUALCONTROLSETTINGS_RSSITYPE_HOTTSUM:
129 return MANUALCONTROLSETTINGS_CHANNELGROUPS_HOTTSUM;
130 case MANUALCONTROLSETTINGS_RSSITYPE_TBSCROSSFIRE:
131 return MANUALCONTROLSETTINGS_CHANNELGROUPS_TBSCROSSFIRE;
132 case MANUALCONTROLSETTINGS_RSSITYPE_IBUS:
133 return MANUALCONTROLSETTINGS_CHANNELGROUPS_IBUS;
142 if (ManualControlCommandInitialize() == -1
143 || FlightStatusInitialize() == -1
144 || StabilizationDesiredInitialize() == -1
145 || ReceiverActivityInitialize() == -1
146 || LoiterCommandInitialize() == -1
147 || ManualControlSettingsInitialize() == -1) {
168 bool always_fullrange)
171 manual_control_command->Collective :
172 manual_control_command->Throttle;
188 if (!always_fullrange) {
194 thrust = thrust * 2.0f - 1.0f;
202 ManualControlCommandData *manual_control_command)
211 return manual_control_command->Throttle <= 0;
219 if (
settings.ChannelGroups[channel_num] >=
220 MANUALCONTROLSETTINGS_CHANNELGROUPS_NONE) {
224 if (
settings.ChannelNumber[channel_num] == 0) {
235 ManualControlSettingsGet(&
settings);
237 uint8_t thrust_channel;
241 thrust_channel = MANUALCONTROLSETTINGS_CHANNELGROUPS_COLLECTIVE;
244 thrust_channel = MANUALCONTROLSETTINGS_CHANNELGROUPS_THROTTLE;
247 float minval =
settings.ChannelMin[thrust_channel];
248 float maxval =
settings.ChannelMax[thrust_channel];
249 float neutral =
settings.ChannelNeutral[thrust_channel];
251 float throt_span = (neutral - minval) / (maxval - minval);
272 float scaledChannel[MANUALCONTROLSETTINGS_CHANNELGROUPS_NUMELEM];
273 bool validChannel[MANUALCONTROLSETTINGS_CHANNELGROUPS_NUMELEM];
275 static float filter_rssi;
276 bool settings_updated_this_cycle =
false;
287 settings_updated_this_cycle =
true;
292 FlightStatusArmedGet(&arm_status);
294 if (arm_status == FLIGHTSTATUS_ARMED_DISARMED) {
306 if (
settings.RssiType != MANUALCONTROLSETTINGS_RSSITYPE_NONE) {
310 case MANUALCONTROLSETTINGS_RSSITYPE_ADC:
311 #if defined(PIOS_INCLUDE_ADC)
312 if (
settings.RssiChannelNumber > 0) {
324 case MANUALCONTROLSETTINGS_RSSITYPE_OPENLRS:
325 #if defined(PIOS_INCLUDE_OPENLRS_RCVR)
326 value = PIOS_OpenLRS_RSSI_Get();
348 }
else if (value >
settings.RssiMax) {
363 filter_rssi = ((filter_rssi * 6) + rssi) / 7;
365 cmd.Rssi = filter_rssi * 100;
370 bool valid_input_detected =
true;
374 n < MANUALCONTROLSETTINGS_CHANNELGROUPS_NUMELEM && n < MANUALCONTROLCOMMAND_CHANNEL_NUMELEM;
377 if (
settings.ChannelGroups[n] >= MANUALCONTROLSETTINGS_CHANNELGROUPS_NONE) {
379 validChannel[n] =
false;
388 valid_input_detected =
false;
389 validChannel[n] =
false;
397 if (
settings.ChannelGroups[MANUALCONTROLSETTINGS_CHANNELGROUPS_ROLL] >= MANUALCONTROLSETTINGS_CHANNELGROUPS_NONE ||
398 settings.ChannelGroups[MANUALCONTROLSETTINGS_CHANNELGROUPS_PITCH] >= MANUALCONTROLSETTINGS_CHANNELGROUPS_NONE ||
399 settings.ChannelGroups[MANUALCONTROLSETTINGS_CHANNELGROUPS_YAW] >= MANUALCONTROLSETTINGS_CHANNELGROUPS_NONE ||
400 settings.ChannelGroups[MANUALCONTROLSETTINGS_CHANNELGROUPS_THROTTLE] >= MANUALCONTROLSETTINGS_CHANNELGROUPS_NONE ||
412 settings.FlightModeNumber < 1 ||
settings.FlightModeNumber > MANUALCONTROLSETTINGS_FLIGHTMODEPOSITION_NUMELEM ||
414 ((
settings.FlightModeNumber > 1) && (
settings.ChannelGroups[MANUALCONTROLSETTINGS_CHANNELGROUPS_FLIGHTMODE] >= MANUALCONTROLSETTINGS_CHANNELGROUPS_NONE)) ||
416 ((
settings.ChannelGroups[MANUALCONTROLSETTINGS_CHANNELGROUPS_FLIGHTMODE] < MANUALCONTROLSETTINGS_CHANNELGROUPS_NONE) && (
422 cmd.Connected = MANUALCONTROLCOMMAND_CONNECTED_FALSE;
423 ManualControlCommandSet(&
cmd);
431 bool flightmode_valid_input =
settings.ChannelGroups[MANUALCONTROLSETTINGS_CHANNELGROUPS_FLIGHTMODE] >= MANUALCONTROLSETTINGS_CHANNELGROUPS_NONE ||
432 validChannel[MANUALCONTROLSETTINGS_CHANNELGROUPS_FLIGHTMODE];
435 bool arming_valid_input = (
settings.Arming < MANUALCONTROLSETTINGS_ARMING_SWITCH) ||
436 validChannel[MANUALCONTROLSETTINGS_CHANNELGROUPS_ARMING];
439 valid_input_detected &= validChannel[MANUALCONTROLSETTINGS_CHANNELGROUPS_THROTTLE] &&
440 validChannel[MANUALCONTROLSETTINGS_CHANNELGROUPS_ROLL] &&
441 validChannel[MANUALCONTROLSETTINGS_CHANNELGROUPS_YAW] &&
442 validChannel[MANUALCONTROLSETTINGS_CHANNELGROUPS_PITCH] &&
443 flightmode_valid_input &&
448 cmd.Connected = MANUALCONTROLCOMMAND_CONNECTED_TRUE;
452 cmd.Connected = MANUALCONTROLCOMMAND_CONNECTED_FALSE;
457 if (
cmd.Connected == MANUALCONTROLCOMMAND_CONNECTED_FALSE) {
469 bool rx_signal_detected =
cmd.Channel[MANUALCONTROLSETTINGS_CHANNELGROUPS_THROTTLE] < (uint16_t)PIOS_RCVR_NODRIVER &&
470 cmd.Channel[MANUALCONTROLSETTINGS_CHANNELGROUPS_ROLL] < (uint16_t)PIOS_RCVR_NODRIVER &&
471 cmd.Channel[MANUALCONTROLSETTINGS_CHANNELGROUPS_PITCH] < (uint16_t)PIOS_RCVR_NODRIVER &&
472 cmd.Channel[MANUALCONTROLSETTINGS_CHANNELGROUPS_YAW] < (uint16_t)PIOS_RCVR_NODRIVER &&
473 cmd.Channel[MANUALCONTROLSETTINGS_CHANNELGROUPS_ARMING] < (uint16_t)PIOS_RCVR_NODRIVER &&
476 if (rx_signal_detected)
481 }
else if (valid_input_detected) {
485 cmd.Roll = scaledChannel[MANUALCONTROLSETTINGS_CHANNELGROUPS_ROLL];
486 cmd.Pitch = scaledChannel[MANUALCONTROLSETTINGS_CHANNELGROUPS_PITCH];
487 cmd.Yaw = scaledChannel[MANUALCONTROLSETTINGS_CHANNELGROUPS_YAW];
488 cmd.Throttle = scaledChannel[MANUALCONTROLSETTINGS_CHANNELGROUPS_THROTTLE];
489 cmd.ArmSwitch = scaledChannel[MANUALCONTROLSETTINGS_CHANNELGROUPS_ARMING] > 0 ?
490 MANUALCONTROLCOMMAND_ARMSWITCH_ARMED : MANUALCONTROLCOMMAND_ARMSWITCH_DISARMED;
491 flight_mode_value = scaledChannel[MANUALCONTROLSETTINGS_CHANNELGROUPS_FLIGHTMODE];
500 if(
cmd.Channel[MANUALCONTROLSETTINGS_CHANNELGROUPS_COLLECTIVE] != (uint16_t)
PIOS_RCVR_INVALID &&
501 cmd.Channel[MANUALCONTROLSETTINGS_CHANNELGROUPS_COLLECTIVE] != (uint16_t) PIOS_RCVR_NODRIVER &&
503 cmd.Collective = scaledChannel[MANUALCONTROLSETTINGS_CHANNELGROUPS_COLLECTIVE];
507 if (
settings.ChannelGroups[MANUALCONTROLSETTINGS_CHANNELGROUPS_ACCESSORY0] !=
508 MANUALCONTROLSETTINGS_CHANNELGROUPS_NONE) {
509 cmd.Accessory[0] = scaledChannel[MANUALCONTROLSETTINGS_CHANNELGROUPS_ACCESSORY0];
513 if (
settings.ChannelGroups[MANUALCONTROLSETTINGS_CHANNELGROUPS_ACCESSORY1] !=
514 MANUALCONTROLSETTINGS_CHANNELGROUPS_NONE) {
515 cmd.Accessory[1] = scaledChannel[MANUALCONTROLSETTINGS_CHANNELGROUPS_ACCESSORY1];
518 if (
settings.ChannelGroups[MANUALCONTROLSETTINGS_CHANNELGROUPS_ACCESSORY2] !=
519 MANUALCONTROLSETTINGS_CHANNELGROUPS_NONE) {
520 cmd.Accessory[2] = scaledChannel[MANUALCONTROLSETTINGS_CHANNELGROUPS_ACCESSORY2];
528 settings_updated_this_cycle);
531 ManualControlCommandSet(&
cmd);
545 ManualControlCommandGet(&
cmd);
548 FlightStatusFlightModeGet(&flightMode);
551 case FLIGHTSTATUS_FLIGHTMODE_MANUAL:
552 case FLIGHTSTATUS_FLIGHTMODE_ACRO:
553 case FLIGHTSTATUS_FLIGHTMODE_ACROPLUS:
554 case FLIGHTSTATUS_FLIGHTMODE_ACRODYNE:
555 case FLIGHTSTATUS_FLIGHTMODE_LEVELING:
556 case FLIGHTSTATUS_FLIGHTMODE_VIRTUALBAR:
557 case FLIGHTSTATUS_FLIGHTMODE_HORIZON:
558 case FLIGHTSTATUS_FLIGHTMODE_AXISLOCK:
559 case FLIGHTSTATUS_FLIGHTMODE_STABILIZED1:
560 case FLIGHTSTATUS_FLIGHTMODE_STABILIZED2:
561 case FLIGHTSTATUS_FLIGHTMODE_STABILIZED3:
562 case FLIGHTSTATUS_FLIGHTMODE_FAILSAFE:
563 case FLIGHTSTATUS_FLIGHTMODE_AUTOTUNE:
564 case FLIGHTSTATUS_FLIGHTMODE_ALTITUDEHOLD:
565 case FLIGHTSTATUS_FLIGHTMODE_LQG:
566 case FLIGHTSTATUS_FLIGHTMODE_LQGLEVELING:
567 case FLIGHTSTATUS_FLIGHTMODE_FLIPREVERSED:
570 case FLIGHTSTATUS_FLIGHTMODE_POSITIONHOLD:
573 case FLIGHTSTATUS_FLIGHTMODE_RETURNTOHOME:
576 case FLIGHTSTATUS_FLIGHTMODE_PATHPLANNER:
598 pos =
settings.FlightModeNumber - 1;
607 ManualControlSettingsData *
settings,
613 switch(settings->Arming) {
614 case MANUALCONTROLSETTINGS_ARMING_ALWAYSDISARMED:
616 case MANUALCONTROLSETTINGS_ARMING_ROLLLEFTTHROTTLE:
618 case MANUALCONTROLSETTINGS_ARMING_ROLLRIGHTTHROTTLE:
620 case MANUALCONTROLSETTINGS_ARMING_YAWLEFTTHROTTLE:
622 case MANUALCONTROLSETTINGS_ARMING_YAWRIGHTTHROTTLE:
624 case MANUALCONTROLSETTINGS_ARMING_CORNERSTHROTTLE:
625 return low_throt && (
630 case MANUALCONTROLSETTINGS_ARMING_SWITCHTHROTTLE:
631 if (!low_throt)
return false;
632 case MANUALCONTROLSETTINGS_ARMING_SWITCH:
633 return cmd->ArmSwitch == MANUALCONTROLCOMMAND_ARMSWITCH_ARMED;
644 switch(settings->Arming) {
645 case MANUALCONTROLSETTINGS_ARMING_ALWAYSDISARMED:
647 case MANUALCONTROLSETTINGS_ARMING_ROLLLEFTTHROTTLE:
649 case MANUALCONTROLSETTINGS_ARMING_ROLLRIGHTTHROTTLE:
651 case MANUALCONTROLSETTINGS_ARMING_YAWLEFTTHROTTLE:
653 case MANUALCONTROLSETTINGS_ARMING_YAWRIGHTTHROTTLE:
655 case MANUALCONTROLSETTINGS_ARMING_CORNERSTHROTTLE:
661 case MANUALCONTROLSETTINGS_ARMING_SWITCH:
662 case MANUALCONTROLSETTINGS_ARMING_SWITCHTHROTTLE:
663 return cmd->ArmSwitch != MANUALCONTROLCOMMAND_ARMSWITCH_ARMED;
670 ManualControlSettingsData *
settings,
673 static uint32_t start_time;
687 uint16_t disarm_time = 100;
689 if ((settings->Arming != MANUALCONTROLSETTINGS_ARMING_SWITCH) &&
690 (settings->Arming != MANUALCONTROLSETTINGS_ARMING_SWITCHTHROTTLE)) {
691 disarm_time = (settings->DisarmTime == MANUALCONTROLSETTINGS_DISARMTIME_250) ? 250 :
692 (settings->DisarmTime == MANUALCONTROLSETTINGS_DISARMTIME_500) ? 500 :
693 (settings->DisarmTime == MANUALCONTROLSETTINGS_DISARMTIME_1000) ? 1000 :
694 (settings->DisarmTime == MANUALCONTROLSETTINGS_DISARMTIME_2000) ? 2000 : 1000;
697 return (now - start_time) >= disarm_time;
700 #define RECEIVER_TIMER_FIRED 0xffffffff
748 if (settings->Arming == MANUALCONTROLSETTINGS_ARMING_ALWAYSDISARMED) {
756 valid &= cmd->Connected == MANUALCONTROLCOMMAND_CONNECTED_TRUE;
758 if (!valid || (cmd->Connected != MANUALCONTROLCOMMAND_CONNECTED_TRUE)) {
765 uint32_t timeout = 0;
767 if (settings->InvalidRXArmedTimeout && settings->ArmedTimeout) {
768 timeout =
MIN(settings->InvalidRXArmedTimeout,
769 settings->ArmedTimeout);
770 }
else if (settings->InvalidRXArmedTimeout) {
771 timeout = settings->InvalidRXArmedTimeout;
773 timeout = settings->ArmedTimeout;
790 FlightStatusFlightModeGet(&flt_mode);
792 if (flt_mode == FLIGHTSTATUS_FLIGHTMODE_POSITIONHOLD ||
793 flt_mode == FLIGHTSTATUS_FLIGHTMODE_RETURNTOHOME ||
794 flt_mode == FLIGHTSTATUS_FLIGHTMODE_PATHPLANNER ||
795 flt_mode == FLIGHTSTATUS_FLIGHTMODE_ALTITUDEHOLD ||
796 flt_mode == FLIGHTSTATUS_FLIGHTMODE_TABLETCONTROL) {
811 if (settings_updated) {
834 if (settings->Arming == MANUALCONTROLSETTINGS_ARMING_SWITCH ||
835 settings->Arming == MANUALCONTROLSETTINGS_ARMING_SWITCHTHROTTLE) {
837 }
else if (settings->Arming == MANUALCONTROLSETTINGS_ARMING_CORNERSTHROTTLE) {
848 if (settings->Arming ==
849 MANUALCONTROLSETTINGS_ARMING_SWITCHTHROTTLE) {
850 if (cmd->ArmSwitch == MANUALCONTROLCOMMAND_ARMSWITCH_ARMED) {
869 FlightStatusFlightModeOptions cur_mode;
871 FlightStatusFlightModeGet(&cur_mode);
873 if (cur_mode != new_mode) {
874 FlightStatusFlightModeSet(&new_mode);
881 ReceiverActivityData
data;
882 bool updated =
false;
885 ReceiverActivityGet(&data);
886 if (data.ActiveGroup != RECEIVERACTIVITY_ACTIVEGROUP_NONE &&
887 data.ActiveChannel != 255) {
888 data.ActiveGroup = RECEIVERACTIVITY_ACTIVEGROUP_NONE;
889 data.ActiveChannel = 255;
893 ReceiverActivitySet(&data);
902 for (uint8_t channel = 1; channel <= max_channels; channel++) {
910 uint8_t active_channel = 0;
914 int rssi_channel = 0;
922 rssi_channel =
settings.RssiChannelNumber;
926 for (uint8_t channel = 1;
929 if (rssi_channel == channel) {
935 uint16_t
prev = fsm->
prev[channel - 1];
943 if (delta > threshold) {
944 active_channel = channel;
952 if (active_channel) {
954 ReceiverActivityActiveGroupSet((uint8_t *)&fsm->
group);
955 ReceiverActivityActiveChannelSet(&active_channel);
965 bool activity_updated =
false;
967 if (fsm->
group >= MANUALCONTROLSETTINGS_CHANNELGROUPS_NONE) {
989 for (uint8_t
i = 0;
i <= MANUALCONTROLSETTINGS_CHANNELGROUPS_NONE;
i++) {
992 if (fsm->
group >= MANUALCONTROLSETTINGS_CHANNELGROUPS_NONE) {
1012 return (activity_updated);
1016 float cmd, SharedDefsStabilizationModeOptions
mode,
1017 StabilizationDesiredStabilizationModeElem axis) {
1019 case SHAREDDEFS_STABILIZATIONMODE_DISABLED:
1020 case SHAREDDEFS_STABILIZATIONMODE_FAILSAFE:
1021 case SHAREDDEFS_STABILIZATIONMODE_POI:
1023 case SHAREDDEFS_STABILIZATIONMODE_MANUAL:
1024 case SHAREDDEFS_STABILIZATIONMODE_VIRTUALBAR:
1025 case SHAREDDEFS_STABILIZATIONMODE_COORDINATEDFLIGHT:
1027 case SHAREDDEFS_STABILIZATIONMODE_SYSTEMIDENTRATE:
1028 case SHAREDDEFS_STABILIZATIONMODE_RATE:
1029 case SHAREDDEFS_STABILIZATIONMODE_WEAKLEVELING:
1030 case SHAREDDEFS_STABILIZATIONMODE_AXISLOCK:
1031 case SHAREDDEFS_STABILIZATIONMODE_LQG:
1032 cmd =
expoM(cmd, stabSettings->RateExpo[axis],
1033 stabSettings->RateExponent[axis]*0.1f);
1034 return cmd * stabSettings->ManualRate[axis];
1035 case SHAREDDEFS_STABILIZATIONMODE_ACRODYNE:
1038 case SHAREDDEFS_STABILIZATIONMODE_ACROPLUS:
1039 cmd =
expoM(cmd, stabSettings->RateExpo[axis],
1040 stabSettings->RateExponent[axis]*0.1f);
1042 case SHAREDDEFS_STABILIZATIONMODE_SYSTEMIDENT:
1043 case SHAREDDEFS_STABILIZATIONMODE_ATTITUDE:
1044 case SHAREDDEFS_STABILIZATIONMODE_ATTITUDELQG:
1045 return cmd * stabSettings->MaxLevelAngle[axis];
1046 case SHAREDDEFS_STABILIZATIONMODE_HORIZON:
1047 cmd =
expo3(cmd, stabSettings->HorizonExpo[axis]);
1058 StabilizationDesiredData stabilization;
1059 StabilizationDesiredGet(&stabilization);
1061 StabilizationSettingsData stabSettings;
1062 StabilizationSettingsGet(&stabSettings);
1064 const uint8_t MANUAL_SETTINGS[3] = {
1065 STABILIZATIONDESIRED_STABILIZATIONMODE_MANUAL,
1066 STABILIZATIONDESIRED_STABILIZATIONMODE_MANUAL,
1067 STABILIZATIONDESIRED_STABILIZATIONMODE_MANUAL };
1068 const uint8_t RATE_SETTINGS[3] = {
1069 STABILIZATIONDESIRED_STABILIZATIONMODE_RATE,
1070 STABILIZATIONDESIRED_STABILIZATIONMODE_RATE,
1071 STABILIZATIONDESIRED_STABILIZATIONMODE_AXISLOCK};
1072 const uint8_t ATTITUDE_SETTINGS[3] = {
1073 STABILIZATIONDESIRED_STABILIZATIONMODE_ATTITUDE,
1074 STABILIZATIONDESIRED_STABILIZATIONMODE_ATTITUDE,
1075 STABILIZATIONDESIRED_STABILIZATIONMODE_AXISLOCK};
1076 const uint8_t VIRTUALBAR_SETTINGS[3] = {
1077 STABILIZATIONDESIRED_STABILIZATIONMODE_VIRTUALBAR,
1078 STABILIZATIONDESIRED_STABILIZATIONMODE_VIRTUALBAR,
1079 STABILIZATIONDESIRED_STABILIZATIONMODE_AXISLOCK};
1080 const uint8_t HORIZON_SETTINGS[3] = {
1081 STABILIZATIONDESIRED_STABILIZATIONMODE_HORIZON,
1082 STABILIZATIONDESIRED_STABILIZATIONMODE_HORIZON,
1083 STABILIZATIONDESIRED_STABILIZATIONMODE_AXISLOCK};
1084 const uint8_t AXISLOCK_SETTINGS[3] = {
1085 STABILIZATIONDESIRED_STABILIZATIONMODE_AXISLOCK,
1086 STABILIZATIONDESIRED_STABILIZATIONMODE_AXISLOCK,
1087 STABILIZATIONDESIRED_STABILIZATIONMODE_AXISLOCK};
1088 const uint8_t ACROPLUS_SETTINGS[3] = {
1089 STABILIZATIONDESIRED_STABILIZATIONMODE_ACROPLUS,
1090 STABILIZATIONDESIRED_STABILIZATIONMODE_ACROPLUS,
1091 STABILIZATIONDESIRED_STABILIZATIONMODE_RATE};
1092 const uint8_t ACRODYNE_SETTINGS[3] = {
1093 STABILIZATIONDESIRED_STABILIZATIONMODE_ACRODYNE,
1094 STABILIZATIONDESIRED_STABILIZATIONMODE_ACRODYNE,
1095 STABILIZATIONDESIRED_STABILIZATIONMODE_ACRODYNE};
1096 const uint8_t FAILSAFE_SETTINGS[3] = {
1097 STABILIZATIONDESIRED_STABILIZATIONMODE_FAILSAFE,
1098 STABILIZATIONDESIRED_STABILIZATIONMODE_FAILSAFE,
1099 STABILIZATIONDESIRED_STABILIZATIONMODE_FAILSAFE};
1100 const uint8_t SYSTEMIDENT_SETTINGS[3] = {
1101 STABILIZATIONDESIRED_STABILIZATIONMODE_SYSTEMIDENT,
1102 STABILIZATIONDESIRED_STABILIZATIONMODE_SYSTEMIDENT,
1103 STABILIZATIONDESIRED_STABILIZATIONMODE_SYSTEMIDENTRATE };
1104 const uint8_t LQG_SETTINGS[3] = {
1105 STABILIZATIONDESIRED_STABILIZATIONMODE_LQG,
1106 STABILIZATIONDESIRED_STABILIZATIONMODE_LQG,
1107 STABILIZATIONDESIRED_STABILIZATIONMODE_LQG };
1108 const uint8_t ATTITUDELQG_SETTINGS[3] = {
1109 STABILIZATIONDESIRED_STABILIZATIONMODE_ATTITUDELQG,
1110 STABILIZATIONDESIRED_STABILIZATIONMODE_ATTITUDELQG,
1111 STABILIZATIONDESIRED_STABILIZATIONMODE_LQG };
1112 const uint8_t FLIPOVER_SETTINGS[3] = {
1113 STABILIZATIONDESIRED_STABILIZATIONMODE_MANUAL,
1114 STABILIZATIONDESIRED_STABILIZATIONMODE_MANUAL,
1115 STABILIZATIONDESIRED_STABILIZATIONMODE_DISABLED };
1117 const uint8_t * stab_modes = ATTITUDE_SETTINGS;
1119 uint8_t reprojection = STABILIZATIONDESIRED_REPROJECTIONMODE_NONE;
1120 uint8_t thrust_mode = STABILIZATIONDESIRED_THRUSTMODE_DIRECT;
1124 FlightStatusFlightModeGet(&flightMode);
1125 switch(flightMode) {
1126 case FLIGHTSTATUS_FLIGHTMODE_MANUAL:
1127 stab_modes = MANUAL_SETTINGS;
1129 case FLIGHTSTATUS_FLIGHTMODE_ACRO:
1130 stab_modes = RATE_SETTINGS;
1132 case FLIGHTSTATUS_FLIGHTMODE_ACRODYNE:
1133 stab_modes = ACRODYNE_SETTINGS;
1135 case FLIGHTSTATUS_FLIGHTMODE_ACROPLUS:
1136 stab_modes = ACROPLUS_SETTINGS;
1138 case FLIGHTSTATUS_FLIGHTMODE_LEVELING:
1139 stab_modes = ATTITUDE_SETTINGS;
1141 case FLIGHTSTATUS_FLIGHTMODE_VIRTUALBAR:
1142 stab_modes = VIRTUALBAR_SETTINGS;
1144 case FLIGHTSTATUS_FLIGHTMODE_HORIZON:
1145 stab_modes = HORIZON_SETTINGS;
1147 case FLIGHTSTATUS_FLIGHTMODE_AXISLOCK:
1148 stab_modes = AXISLOCK_SETTINGS;
1150 case FLIGHTSTATUS_FLIGHTMODE_FAILSAFE:
1151 stab_modes = FAILSAFE_SETTINGS;
1153 case FLIGHTSTATUS_FLIGHTMODE_AUTOTUNE:
1154 stab_modes = SYSTEMIDENT_SETTINGS;
1156 case FLIGHTSTATUS_FLIGHTMODE_STABILIZED1:
1157 stab_modes = settings->Stabilization1Settings;
1158 reprojection = settings->Stabilization1Reprojection;
1159 thrust_mode = settings->Stabilization1Thrust;
1161 case FLIGHTSTATUS_FLIGHTMODE_STABILIZED2:
1162 stab_modes = settings->Stabilization2Settings;
1163 reprojection = settings->Stabilization2Reprojection;
1164 thrust_mode = settings->Stabilization2Thrust;
1166 case FLIGHTSTATUS_FLIGHTMODE_STABILIZED3:
1167 stab_modes = settings->Stabilization3Settings;
1168 reprojection = settings->Stabilization3Reprojection;
1169 thrust_mode = settings->Stabilization3Thrust;
1171 #ifdef TARGET_MAY_HAVE_BARO
1172 case FLIGHTSTATUS_FLIGHTMODE_ALTITUDEHOLD:
1173 stab_modes = ATTITUDE_SETTINGS;
1175 STABILIZATIONDESIRED_THRUSTMODE_ALTITUDEWITHSTICKSCALING;
1178 case FLIGHTSTATUS_FLIGHTMODE_LQG:
1179 stab_modes = LQG_SETTINGS;
1181 case FLIGHTSTATUS_FLIGHTMODE_LQGLEVELING:
1182 stab_modes = ATTITUDELQG_SETTINGS;
1184 case FLIGHTSTATUS_FLIGHTMODE_FLIPREVERSED:
1185 stab_modes = FLIPOVER_SETTINGS;
1186 thrust_mode = STABILIZATIONDESIRED_THRUSTMODE_FLIPOVERMODETHRUSTREVERSED;
1196 stabilization.StabilizationMode[STABILIZATIONDESIRED_STABILIZATIONMODE_ROLL] = stab_modes[0];
1197 stabilization.StabilizationMode[STABILIZATIONDESIRED_STABILIZATIONMODE_PITCH] = stab_modes[1];
1198 stabilization.StabilizationMode[STABILIZATIONDESIRED_STABILIZATIONMODE_YAW] = stab_modes[2];
1201 manual_control_command->Roll,
1202 stab_modes[STABILIZATIONDESIRED_STABILIZATIONMODE_ROLL],
1203 STABILIZATIONDESIRED_STABILIZATIONMODE_ROLL);
1206 manual_control_command->Pitch,
1207 stab_modes[STABILIZATIONDESIRED_STABILIZATIONMODE_PITCH],
1208 STABILIZATIONDESIRED_STABILIZATIONMODE_PITCH);
1211 manual_control_command->Yaw,
1212 stab_modes[STABILIZATIONDESIRED_STABILIZATIONMODE_YAW],
1213 STABILIZATIONDESIRED_STABILIZATIONMODE_YAW);
1217 (thrust_mode == STABILIZATIONDESIRED_THRUSTMODE_ALTITUDEWITHSTICKSCALING));
1220 stabilization.ReprojectionMode = reprojection;
1221 stabilization.ThrustMode = thrust_mode;
1223 StabilizationDesiredSet(&stabilization);
1228 LoiterCommandData loiterCommand;
1230 loiterCommand.Pitch = cmd->Pitch;
1231 loiterCommand.Roll = cmd->Roll;
1235 loiterCommand.Frame = LOITERCOMMAND_FRAME_BODY;
1237 LoiterCommandSet(&loiterCommand);
1248 int16_t neutral =
settings.ChannelNeutral[n];
1253 if ((max > min && value >= neutral) || (min > max && value <= neutral))
1256 valueScaled = (float)(value - neutral) / (float)(max - neutral);
1263 valueScaled = (float)(value - neutral) / (float)(neutral - min);
1269 if (valueScaled > 1.0
f) valueScaled = 1.0f;
1271 if (valueScaled < -1.0
f) valueScaled = -1.0f;
1277 if (end_time >= start_time)
1278 return end_time - start_time;
1291 int16_t neutral =
settings.ChannelNeutral[n];
1293 bool inverted =
false;
1303 if ((neutral > max) || (neutral < min)) {
1307 if (n == MANUALCONTROLSETTINGS_CHANNELGROUPS_THROTTLE) {
1311 range = neutral -
min;
1313 range = max - neutral;
1316 range =
MIN(max - neutral, neutral - min);
1323 return (value >= (min - offset) && value <= (max + offset));
1333 SystemAlarmsAlarmOptions severity;
1334 switch (error_code) {
1335 case SYSTEMALARMS_MANUALCONTROL_NONE:
1336 severity = SYSTEMALARMS_ALARM_OK;
1338 case SYSTEMALARMS_MANUALCONTROL_NORX:
1339 case SYSTEMALARMS_MANUALCONTROL_CHANNELCONFIGURATION:
1340 case SYSTEMALARMS_MANUALCONTROL_ACCESSORY:
1341 severity = SYSTEMALARMS_ALARM_WARNING;
1343 case SYSTEMALARMS_MANUALCONTROL_SETTINGS:
1344 severity = SYSTEMALARMS_ALARM_CRITICAL;
1346 case SYSTEMALARMS_MANUALCONTROL_ALTITUDEHOLD:
1347 severity = SYSTEMALARMS_ALARM_ERROR;
1349 case SYSTEMALARMS_MANUALCONTROL_UNDEFINED:
1351 severity = SYSTEMALARMS_ALARM_CRITICAL;
1352 error_code = SYSTEMALARMS_MANUALCONTROL_UNDEFINED;
1356 SystemAlarmsManualControlOptions current_error_code;
1357 SystemAlarmsManualControlGet((uint8_t *) ¤t_error_code);
1358 if (current_error_code != error_code) {
1359 SystemAlarmsManualControlSet((uint8_t *) &error_code);
1363 AlarmsSet(SYSTEMALARMS_ALARM_MANUALCONTROL, (uint8_t) severity);
uint32_t PIOS_Thread_Systime(void)
static void set_loiter_command(ManualControlCommandData *cmd)
#define RCVR_ACTIVITY_MONITOR_MIN_RANGE
static bool validInputRange(int n, uint16_t value, uint16_t offset)
Determine if the manual input value is within acceptable limits.
Disconnected timeout has occurred.
float expoM(float x, int32_t g, float exponent)
static bool thrust_is_bidir
static bool disarm_commanded(ManualControlCommandData *cmd, ManualControlSettingsData *settings, bool low_throt)
User requested disarm, or low throt timeout.
static bool collective_is_thrust
uintptr_t pios_rcvr_group_map[]
Process transmitter inputs and use as control source.
static uint32_t timeDifferenceMs(uint32_t start_time, uint32_t end_time)
static struct rcvr_activity_fsm activity_fsm
static bool arming_position(ManualControlCommandData *cmd, ManualControlSettingsData *settings, bool low_throt)
#define PIOS_THREAD_TIMEOUT_MAX
bool ok_to_arm(void)
Determine if the aircraft is safe to arm based on alarms.
static bool is_low_throttle_for_arming(ManualControlCommandData *manual_control_command)
static bool updateRcvrActivity(struct rcvr_activity_fsm *fsm)
void UAVObjCbSetFlag(const UAVObjEvent *objEv, void *ctx, void *obj, int len)
int32_t AlarmsSet(SystemAlarmsAlarmElem alarm, SystemAlarmsAlarmOptions severity)
static bool disarming_position(ManualControlCommandData *cmd, ManualControlSettingsData *settings, bool low_throt)
int32_t transmitter_control_select(bool reset_controller)
Select and use transmitter control.
static float scale_stabilization(StabilizationSettingsData *stabSettings, float cmd, SharedDefsStabilizationModeOptions mode, StabilizationDesiredStabilizationModeElem axis)
int rssitype_to_channelgroup()
Convert a rssi type to the associated channel group.
ManualControlSettingsChannelGroupsOptions group
static void reset_receiver_timer()
uint8_t data[XFER_BYTES_PER_PACKET]
static ManualControlSettingsData settings
static void set_manual_control_error(SystemAlarmsManualControlOptions errorCode)
enum control_status transmitter_control_get_status()
Get any control events.
static void update_stabilization_desired(ManualControlCommandData *manual_control_command, ManualControlSettingsData *settings)
In stabilization mode, set stabilization desired.
void apply_channel_deadband(float *value, float deadband)
Apply deadband to Roll/Pitch/Yaw channels.
static void resetRcvrActivity(struct rcvr_activity_fsm *fsm)
static enum control_status control_status
Something is fundamentally wrong.
#define THRUST_BIDIR_THRESH
static bool settings_updated
#define RCVR_ACTIVITY_MONITOR_CHANNELS_PER_GROUP
static void process_transmitter_events(ManualControlCommandData *cmd, ManualControlSettingsData *settings, bool valid, bool settings_updated)
Process the inputs and determine whether to arm or not.
static uint8_t connected_count
#define MIN_MEANINGFUL_RANGE
#define RECEIVER_TIMER_FIRED
static float get_thrust_source(ManualControlCommandData *manual_control_command, bool always_fullrange)
#define CONNECTION_OFFSET
int32_t PIOS_RCVR_Read(uintptr_t rcvr_id, uint8_t channel)
static uint32_t lastActivityTime
static uint8_t disconnected_count
User requested arm, controls in valid pos.
static void perform_tc_settings_update()
int32_t PIOS_ADC_GetChannelRaw(uint32_t channel)
Includes PiOS and core architecture components.
static uint32_t receiver_timer
int32_t transmitter_control_update()
Process inputs and arming.
uint16_t prev[RCVR_ACTIVITY_MONITOR_CHANNELS_PER_GROUP]
User requested arm, controls in invalid pos.
int32_t transmitter_control_initialize()
Initialize the transmitter control mode.
Implements an OpenLRS driver for the RFM22B.
static void set_flight_mode()
Determine which of N positions the flight mode switch is in and set flight mode accordingly.
static uint32_t lastSysTime
Arming switch is in a state that doesn't.
For corners. Request we be the "opposite" mode.
if(BaroAltitudeHandle()!=NULL)
static bool updateRcvrActivityCompare(uintptr_t rcvr_id, struct rcvr_activity_fsm *fsm)
static ManualControlCommandData cmd
uint8_t transmitter_control_get_flight_mode()
Query what the flight mode would be if this is selected.
#define PIOS_Assert(test)
static float flight_mode_value
static float scaleChannel(int n, int16_t value)
static bool check_receiver_timer(uint32_t threshold)
float expo3(float x, int32_t g)
Approximation an exponential scale curve.
static bool channel_is_configured(int channel_num)
static void updateRcvrActivitySample(uintptr_t rcvr_id, uint16_t samples[], uint8_t max_channels)