pbio/dcmotor: Cap max voltage at hardware limit.
@@ -127,6 +127,11 @@ void pbio_dcmotor_get_settings(pbio_dcmotor_t *dcmotor, int32_t *max_voltage) {
}
pbio_error_t pbio_dcmotor_set_settings(pbio_dcmotor_t *dcmotor, int32_t max_voltage) {
+ // New maximum voltage must be positive and at or below hardware limit.
+ if (max_voltage < 0 || max_voltage > pbio_dcmotor_get_max_voltage(dcmotor->id)) {
+ return PBIO_ERROR_INVALID_ARG;
+ }
+ // Set the new value.
dcmotor->max_voltage = max_voltage;
return PBIO_SUCCESS;
}
