Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable silent compare of parameter #12850

Merged
merged 9 commits into from
Oct 7, 2019
4 changes: 2 additions & 2 deletions ROMFS/px4fmu_common/init.d-posix/rcS
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,12 @@ commander start
navigator start


if ! param compare MNT_MODE_IN -1
if ! param compare -s MNT_MODE_IN -1
then
vmount start
fi

if param greater TRIG_MODE 0
if param greater -s TRIG_MODE 0
then
camera_trigger start
camera_feedback start
Expand Down
4 changes: 2 additions & 2 deletions ROMFS/px4fmu_common/init.d/rc.interface
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ set OUTPUT_DEV none
# If mount (gimbal) control is enabled and output mode is AUX, set the aux
# mixer to mount (override the airframe-specific MIXER_AUX setting).
#
if ! param compare MNT_MODE_IN -1
if ! param compare -s MNT_MODE_IN -1
then
if param compare MNT_MODE_OUT 0
if param compare -s MNT_MODE_OUT 0
then
set MIXER_AUX mount
fi
Expand Down
2 changes: 1 addition & 1 deletion ROMFS/px4fmu_common/init.d/rc.logging
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# NOTE: Script variables are declared/initialized/unset in the rcS script.
#

if param greater UAVCAN_ENABLE 1
if param greater -s UAVCAN_ENABLE 1
then
# Reduce logger buffer to free up some RAM for UAVCAN servers.
set LOGGER_BUF 6
Expand Down
20 changes: 10 additions & 10 deletions ROMFS/px4fmu_common/init.d/rc.sensors
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ then
fi
fi

if param compare SENS_EN_BATT 1
if param compare -s SENS_EN_BATT 1
then
batt_smbus start -X
fi

# Sensors on the PWM interface bank
if param compare SENS_EN_LL40LS 1
if param compare -s SENS_EN_LL40LS 1
then
if pwm_input start
then
Expand All @@ -62,49 +62,49 @@ then
fi

# Lidar-Lite on I2C
if param compare SENS_EN_LL40LS 2
if param compare -s SENS_EN_LL40LS 2
then
ll40ls start i2c -a
fi

# mappydot lidar sensor
if param compare SENS_EN_MPDT 1
if param compare -s SENS_EN_MPDT 1
then
mappydot start -a
fi

# mb12xx sonar sensor
if param greater SENS_EN_MB12XX 0
if param greater -s SENS_EN_MB12XX 0
then
mb12xx start -a
fi

# pga460 sonar sensor
if param greater SENS_EN_PGA460 0
if param greater -s SENS_EN_PGA460 0
then
pga460 start
fi

# Lightware i2c lidar sensor
if param greater SENS_EN_SF1XX 0
if param greater -s SENS_EN_SF1XX 0
then
sf1xx start -a
fi

# Heater driver for temperature regulated IMUs.
if param compare SENS_EN_THERMAL 1
if param compare -s SENS_EN_THERMAL 1
then
heater start
fi

# Teraranger one tof sensor
if param greater SENS_EN_TRANGER 0
if param greater -s SENS_EN_TRANGER 0
then
teraranger start -a
fi

# Possible pmw3901 optical flow sensor
if param greater SENS_EN_PMW3901 0
if param greater -s SENS_EN_PMW3901 0
then
pmw3901 start
fi
Expand Down
8 changes: 4 additions & 4 deletions ROMFS/px4fmu_common/init.d/rcS
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ else
#
# Set USE_IO flag.
#
if param compare SYS_USE_IO 1
if param compare -s SYS_USE_IO 1
then
set USE_IO yes
fi
Expand Down Expand Up @@ -347,7 +347,7 @@ else
fi

# Sensors on the PWM interface bank.
if param compare SENS_EN_LL40LS 1
if param compare -s SENS_EN_LL40LS 1
then
# Clear pins 5 and 6.
set FMU_MODE pwm4
Expand All @@ -374,7 +374,7 @@ else
#
# Check if UAVCAN is enabled, default to it for ESCs.
#
if param greater UAVCAN_ENABLE 0
if param greater -s UAVCAN_ENABLE 0
then
# Start core UAVCAN module.
if uavcan start
Expand Down Expand Up @@ -426,7 +426,7 @@ else
sh /etc/init.d/rc.vehicle_setup

# Camera capture driver
if param greater CAM_CAP_FBACK 0
if param greater -s CAM_CAP_FBACK 0
then
if camera_capture start
then
Expand Down
48 changes: 32 additions & 16 deletions src/systemcmds/param/param.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,17 @@ __BEGIN_DECLS
__EXPORT int param_main(int argc, char *argv[]);
__END_DECLS

enum COMPARE_OPERATOR {
COMPARE_OPERATOR_EQUAL = 0,
COMPARE_OPERATOR_GREATER = 1,
enum class COMPARE_OPERATOR {
EQUAL = 0,
GREATER = 1,
};

enum class COMPARE_ERROR_LEVEL {
DO_ERROR = 0,
SILENT = 1,
};


#ifdef __PX4_QURT
#define PARAM_PRINT PX4_INFO
#else
Expand All @@ -86,7 +92,8 @@ static int do_show_quiet(const char *param_name);
static int do_show_index(const char *index, bool used_index);
static void do_show_print(void *arg, param_t param);
static int do_set(const char *name, const char *val, bool fail_on_not_found);
static int do_compare(const char *name, char *vals[], unsigned comparisons, enum COMPARE_OPERATOR cmd_op);
static int do_compare(const char *name, char *vals[], unsigned comparisons, enum COMPARE_OPERATOR cmd_op,
enum COMPARE_ERROR_LEVEL err_level);
static int do_reset(const char *excludes[], int num_excludes);
static int do_touch(const char *params[], int num_params);
static int do_reset_nostart(const char *excludes[], int num_excludes);
Expand Down Expand Up @@ -143,10 +150,14 @@ Change the airframe and make sure the airframe's default parameters are loaded:
PRINT_MODULE_USAGE_ARG("fail", "If provided, let the command fail if param is not found", true);

PRINT_MODULE_USAGE_COMMAND_DESCR("compare", "Compare a param with a value. Command will succeed if equal");
PRINT_MODULE_USAGE_PARAM_FLAG('s', "If provided, silent errors if parameter doesn't exists", true);
PRINT_MODULE_USAGE_ARG("<param_name> <value>", "Parameter name and value to compare", false);

PRINT_MODULE_USAGE_COMMAND_DESCR("greater",
"Compare a param with a value. Command will succeed if param is greater than the value");
PRINT_MODULE_USAGE_PARAM_FLAG('s', "If provided, silent errors if parameter doesn't exists", true);
PRINT_MODULE_USAGE_ARG("<param_name> <value>", "Parameter name and value to compare", false);

PRINT_MODULE_USAGE_ARG("<param_name> <value>", "Parameter name and value to compare", false);

PRINT_MODULE_USAGE_COMMAND_DESCR("touch", "Mark a parameter as used");
Expand Down Expand Up @@ -270,19 +281,21 @@ param_main(int argc, char *argv[])
}

if (!strcmp(argv[1], "compare")) {
if (argc >= 4) {
return do_compare(argv[2], &argv[3], argc - 3, COMPARE_OPERATOR_EQUAL);

if(argc >= 5 && !strcmp(argv[2], "-s")) {
return do_compare(argv[3], &argv[4], argc - 4, COMPARE_OPERATOR::EQUAL, COMPARE_ERROR_LEVEL::SILENT);
} else if (argc >= 4) {
return do_compare(argv[2], &argv[3], argc - 3, COMPARE_OPERATOR::EQUAL, COMPARE_ERROR_LEVEL::DO_ERROR);
} else {
PX4_ERR("not enough arguments.\nTry 'param compare PARAM_NAME 3'");
return 1;
}
}

if (!strcmp(argv[1], "greater")) {
if (argc >= 4) {
return do_compare(argv[2], &argv[3], argc - 3, COMPARE_OPERATOR_GREATER);

if(argc >= 5 && !strcmp(argv[2], "-s")) {
return do_compare(argv[3], &argv[4], argc - 4, COMPARE_OPERATOR::GREATER, COMPARE_ERROR_LEVEL::SILENT);
} else if (argc >= 4) {
return do_compare(argv[2], &argv[3], argc - 3, COMPARE_OPERATOR::GREATER, COMPARE_ERROR_LEVEL::DO_ERROR);
} else {
PX4_ERR("not enough arguments.\nTry 'param greater PARAM_NAME 3'");
return 1;
Expand Down Expand Up @@ -708,7 +721,7 @@ do_set(const char *name, const char *val, bool fail_on_not_found)
}

static int
do_compare(const char *name, char *vals[], unsigned comparisons, enum COMPARE_OPERATOR cmp_op)
do_compare(const char *name, char *vals[], unsigned comparisons, enum COMPARE_OPERATOR cmp_op, enum COMPARE_ERROR_LEVEL err_level)
{
int32_t i;
float f;
Expand All @@ -717,7 +730,10 @@ do_compare(const char *name, char *vals[], unsigned comparisons, enum COMPARE_OP
/* set nothing if parameter cannot be found */
if (param == PARAM_INVALID) {
/* param not found */
PX4_DEBUG("Parameter %s not found", name);
if(err_level == COMPARE_ERROR_LEVEL::DO_ERROR)
{
PX4_ERR("Parameter %s not found", name);
}
return 1;
}

Expand All @@ -738,8 +754,8 @@ do_compare(const char *name, char *vals[], unsigned comparisons, enum COMPARE_OP

int j = strtol(vals[k], &end, 10);

if (((cmp_op == COMPARE_OPERATOR_EQUAL) && (i == j)) ||
((cmp_op == COMPARE_OPERATOR_GREATER) && (i > j))) {
if (((cmp_op == COMPARE_OPERATOR::EQUAL) && (i == j)) ||
((cmp_op == COMPARE_OPERATOR::GREATER) && (i > j))) {
PX4_DEBUG(" %ld: ", (long)i);
ret = 0;
}
Expand All @@ -758,8 +774,8 @@ do_compare(const char *name, char *vals[], unsigned comparisons, enum COMPARE_OP

float g = strtod(vals[k], &end);

if (((cmp_op == COMPARE_OPERATOR_EQUAL) && (fabsf(f - g) < 1e-7f)) ||
((cmp_op == COMPARE_OPERATOR_GREATER) && (f > g))) {
if (((cmp_op == COMPARE_OPERATOR::EQUAL) && (fabsf(f - g) < 1e-7f)) ||
((cmp_op == COMPARE_OPERATOR::GREATER) && (f > g))) {
PX4_DEBUG(" %4.4f: ", (double)f);
ret = 0;
}
Expand Down