[wpilibj] Throw separate exception for constraint misbehavior (#2510)

The most common mistake users (including contributors to WPILib) seem to make while creating new constraints is ignoring some sort of edge case that causes the calculated minimum acceleration to be greater than the calculated maximum acceleration.

This specialized exception, with its detailed error message, should make it easier and quicker for said users to debug and fix bugs within their constraints.

Co-authored-by: Tyler Veness <calcmogul@gmail.com>
This commit is contained in:
Prateek Machiraju
2020-06-08 13:38:47 -04:00
committed by GitHub
parent 4b76adf15b
commit 762347f005
2 changed files with 32 additions and 7 deletions

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2019 FIRST. All Rights Reserved. */
/* Copyright (c) 2019-2020 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
@@ -31,6 +31,8 @@
#include "frc/trajectory/TrajectoryParameterizer.h"
#include <string>
using namespace frc;
Trajectory TrajectoryParameterizer::TimeParameterizeTrajectory(
@@ -186,8 +188,9 @@ Trajectory TrajectoryParameterizer::TimeParameterizeTrajectory(
// delta_x = v * t
dt = ds / v;
} else {
throw std::runtime_error(
"Something went wrong during trajectory generation.");
throw std::runtime_error("Something went wrong at iteration " +
std::to_string(i) +
" of time parameterization.");
}
}
@@ -213,6 +216,14 @@ void TrajectoryParameterizer::EnforceAccelerationLimits(
auto minMaxAccel = constraint->MinMaxAcceleration(
state->pose.first, state->pose.second, state->maxVelocity * factor);
if (minMaxAccel.minAcceleration > minMaxAccel.maxAcceleration) {
throw std::runtime_error(
"The constraint's min acceleration was greater than its max "
"acceleration. To debug this, remove all constraints from the config "
"and add each one individually. If the offending constraint was "
"packaged with WPILib, please file a bug report.");
}
state->minAcceleration = units::math::max(
state->minAcceleration,
reverse ? -minMaxAccel.maxAcceleration : minMaxAccel.minAcceleration);