MAVLink: constrain variable changes to the datatype range

if someone tries to set a AP_Int16 to a value of 300000, they now get
32767 instead of -27678
This commit is contained in:
Andrew Tridgell 2012-03-19 17:20:06 +11:00
parent fbe8592d3c
commit dc71191f25
2 changed files with 18 additions and 6 deletions

View File

@ -1470,19 +1470,25 @@ void GCS_MAVLINK::handleMessage(mavlink_message_t* msg)
Log_Write_Data(1, ((AP_Int32 *)vp)->get()); Log_Write_Data(1, ((AP_Int32 *)vp)->get());
#endif #endif
if (packet.param_value < 0) rounding_addition = -rounding_addition; if (packet.param_value < 0) rounding_addition = -rounding_addition;
((AP_Int32 *)vp)->set_and_save(packet.param_value+rounding_addition); float v = packet.param_value+rounding_addition;
v = constrain(v, -2147483648, 2147483647);
((AP_Int32 *)vp)->set_and_save(v);
} else if (var_type == AP_PARAM_INT16) { } else if (var_type == AP_PARAM_INT16) {
#if LOGGING_ENABLED == ENABLED #if LOGGING_ENABLED == ENABLED
Log_Write_Data(3, (int32_t)((AP_Int16 *)vp)->get()); Log_Write_Data(3, (int32_t)((AP_Int16 *)vp)->get());
#endif #endif
if (packet.param_value < 0) rounding_addition = -rounding_addition; if (packet.param_value < 0) rounding_addition = -rounding_addition;
((AP_Int16 *)vp)->set_and_save(packet.param_value+rounding_addition); float v = packet.param_value+rounding_addition;
v = constrain(v, -32768, 32767);
((AP_Int16 *)vp)->set_and_save(v);
} else if (var_type == AP_PARAM_INT8) { } else if (var_type == AP_PARAM_INT8) {
#if LOGGING_ENABLED == ENABLED #if LOGGING_ENABLED == ENABLED
Log_Write_Data(4, (int32_t)((AP_Int8 *)vp)->get()); Log_Write_Data(4, (int32_t)((AP_Int8 *)vp)->get());
#endif #endif
if (packet.param_value < 0) rounding_addition = -rounding_addition; if (packet.param_value < 0) rounding_addition = -rounding_addition;
((AP_Int8 *)vp)->set_and_save(packet.param_value+rounding_addition); float v = packet.param_value+rounding_addition;
v = constrain(v, -128, 127);
((AP_Int8 *)vp)->set_and_save(v);
} else { } else {
// we don't support mavlink set on this parameter // we don't support mavlink set on this parameter
break; break;

View File

@ -1791,13 +1791,19 @@ void GCS_MAVLINK::handleMessage(mavlink_message_t* msg)
((AP_Float *)vp)->set_and_save(packet.param_value); ((AP_Float *)vp)->set_and_save(packet.param_value);
} else if (var_type == AP_PARAM_INT32) { } else if (var_type == AP_PARAM_INT32) {
if (packet.param_value < 0) rounding_addition = -rounding_addition; if (packet.param_value < 0) rounding_addition = -rounding_addition;
((AP_Int32 *)vp)->set_and_save(packet.param_value+rounding_addition); float v = packet.param_value+rounding_addition;
v = constrain(v, -2147483648, 2147483647);
((AP_Int32 *)vp)->set_and_save(v);
} else if (var_type == AP_PARAM_INT16) { } else if (var_type == AP_PARAM_INT16) {
if (packet.param_value < 0) rounding_addition = -rounding_addition; if (packet.param_value < 0) rounding_addition = -rounding_addition;
((AP_Int16 *)vp)->set_and_save(packet.param_value+rounding_addition); float v = packet.param_value+rounding_addition;
v = constrain(v, -32768, 32767);
((AP_Int16 *)vp)->set_and_save(v);
} else if (var_type == AP_PARAM_INT8) { } else if (var_type == AP_PARAM_INT8) {
if (packet.param_value < 0) rounding_addition = -rounding_addition; if (packet.param_value < 0) rounding_addition = -rounding_addition;
((AP_Int8 *)vp)->set_and_save(packet.param_value+rounding_addition); float v = packet.param_value+rounding_addition;
v = constrain(v, -128, 127);
((AP_Int8 *)vp)->set_and_save(v);
} else { } else {
// we don't support mavlink set on this parameter // we don't support mavlink set on this parameter
break; break;