From 377dbaa35fb2b87394bb424cf0d7b12c60be58a8 Mon Sep 17 00:00:00 2001 From: Laurens Valk Date: Tue, 21 Apr 2020 14:36:56 +0200 Subject: [PATCH] extmod/modrobotics: bind settings by limits The settings() just set lower defaults for straight() and turn(). When they call pbio functions, they are still bound by the control limits. By limiting the values kept as class attributes, this implicit bound is made explicit because it makes it possible to see that very large values are capped. --- extmod/modrobotics.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/extmod/modrobotics.c b/extmod/modrobotics.c index 96e4ca268..1d4303566 100644 --- a/extmod/modrobotics.c +++ b/extmod/modrobotics.c @@ -1,6 +1,8 @@ // SPDX-License-Identifier: MIT // Copyright (c) 2019 Laurens Valk +#include + #include #include @@ -230,11 +232,15 @@ STATIC mp_obj_t robotics_DriveBase_settings(size_t n_args, const mp_obj_t *pos_a pb_assert(PBIO_ERROR_INVALID_OP); } - // If some values are given, set them - self->straight_speed = pb_obj_get_default_int(straight_speed, self->straight_speed); - self->straight_acceleration = pb_obj_get_default_int(straight_acceleration, self->straight_acceleration); - self->turn_rate = pb_obj_get_default_int(turn_rate, self->turn_rate); - self->turn_acceleration = pb_obj_get_default_int(turn_acceleration, self->turn_acceleration); + // If some values are given, set them, bound by the control limits + int32_t straight_speed_limit, straight_acceleration_limit, turn_rate_limit, turn_acceleration_limit, _; + pbio_control_settings_get_limits(&self->db->control_distance.settings, &straight_speed_limit, &straight_acceleration_limit, &_); + pbio_control_settings_get_limits(&self->db->control_heading.settings, &turn_rate_limit, &turn_acceleration_limit, &_); + + self->straight_speed = min(straight_speed_limit, abs(pb_obj_get_default_int(straight_speed, self->straight_speed))); + self->straight_acceleration = min(straight_acceleration_limit, abs(pb_obj_get_default_int(straight_acceleration, self->straight_acceleration))); + self->turn_rate = min(turn_rate_limit, abs(pb_obj_get_default_int(turn_rate, self->turn_rate))); + self->turn_acceleration = min(turn_acceleration_limit, abs(pb_obj_get_default_int(turn_acceleration, self->turn_acceleration))); return mp_const_none; }