#include "../src/agvvelocityramp.h" #include #include #include #include namespace { int checks = 0; void expect(bool condition, const char *message) { ++checks; if (!condition) { std::cerr << "FAIL: " << message << '\n'; std::exit(1); } } bool near(double actual, double expected, double tolerance = 1e-10) { return std::abs(actual - expected) <= tolerance; } bool finite(const AgvManualVelocity &value) { return std::isfinite(value.vx) && std::isfinite(value.vy) && std::isfinite(value.wz); } double length(const AgvManualVelocity &value) { return std::hypot(value.vx, value.vy); } void positiveAndNegativeAcceleration() { const AgvRampLimits limits; AgvManualVelocity value; const AgvManualVelocity target{0.2, 0.0, 0.0}; for (int tick = 0; tick != 10; ++tick) { const auto next = AgvVelocityRamp::step(value, target, limits, 0.1); expect(near(next.vx - value.vx, 0.02), "positive acceleration bound"); expect(next.vx <= target.vx && next.vx >= value.vx, "positive no overshoot"); value = next; } expect(near(value.vx, target.vx), "positive target reached"); expect(near(AgvVelocityRamp::step(value, target, limits, 0.1).vx, 0.2), "target stable"); value = {}; for (int tick = 0; tick != 20; ++tick) { const auto next = AgvVelocityRamp::step(value, {-0.37, 0.0, 0.0}, limits, 0.1); expect(next.vx <= value.vx && next.vx >= -0.37, "negative no overshoot"); expect(std::abs(next.vx - value.vx) <= 0.02 + 1e-10, "negative slope bound"); value = next; } expect(near(value.vx, -0.37), "negative target reached exactly"); } void stoppingAndMagnitudeDecrease() { const AgvRampLimits limits; auto value = AgvVelocityRamp::step({0.5, 0.0, 0.0}, {0.2, 0.0, 0.0}, limits, 0.1); expect(near(value.vx, 0.47), "same direction lowering uses deceleration"); value = {-0.2, 0.0, -0.3}; int tick = 0; while (!AgvVelocityRamp::isZero(value) && tick++ < 10) { const auto next = AgvVelocityRamp::step(value, {}, limits, 0.1); expect(next.vx >= value.vx && next.vx <= 0.0, "stop never flips negative linear sign"); expect(next.wz >= value.wz && next.wz <= 0.0, "stop never flips negative angular sign"); value = next; } expect(tick == 7, "stop reaches zero in finite expected time"); expect(value.vx == 0.0 && value.vy == 0.0 && value.wz == 0.0, "stop exact zero"); } void reversalsAndTurns() { const AgvRampLimits limits; auto value = AgvManualVelocity{0.2, 0.0, 0.0}; bool seenZero = false; for (int tick = 0; tick != 20; ++tick) { const auto next = AgvVelocityRamp::step(value, {-0.2, 0.0, 0.0}, limits, 0.1); if (next.vx < 0.0) expect(seenZero, "linear reversal has an explicit zero sample"); if (value.vx > 0.0) expect(near(value.vx - next.vx, std::min(0.03, value.vx)), "reverse brakes first"); seenZero = seenZero || AgvVelocityRamp::isZero(next); value = next; } expect(near(value.vx, -0.2), "linear reversal reaches target"); value = {0.12, 0.0, 0.0}; seenZero = false; for (int tick = 0; tick != 12; ++tick) { const auto next = AgvVelocityRamp::step(value, {0.0, 0.12, 0.0}, limits, 0.1); if (next.vy > 0.0) expect(seenZero && next.vx == 0.0, "ninety degree turn stops before new axis"); seenZero = seenZero || AgvVelocityRamp::isZero(next); value = next; } expect(near(value.vy, 0.12), "ninety degree turn reaches target"); value = {0.08, 0.0, 0.0}; seenZero = false; for (int tick = 0; tick != 12; ++tick) { const auto next = AgvVelocityRamp::step(value, {0.0, 0.0, 0.25}, limits, 0.1); if (next.wz > 0.0) expect(seenZero && next.vx == 0.0, "linear to angular stops before rotation"); seenZero = seenZero || AgvVelocityRamp::isZero(next); value = next; } expect(near(value.wz, 0.25), "linear to angular reaches target"); value = {0.0, 0.0, -0.17}; seenZero = false; for (int tick = 0; tick != 12; ++tick) { const auto next = AgvVelocityRamp::step(value, {0.14, 0.0, 0.0}, limits, 0.1); if (next.vx > 0.0) expect(seenZero && next.wz == 0.0, "angular to linear stops first"); seenZero = seenZero || AgvVelocityRamp::isZero(next); value = next; } expect(near(value.vx, 0.14), "angular to linear reaches target"); } void vectorAndAngularLimits() { const AgvRampLimits limits; auto value = AgvManualVelocity{}; const AgvManualVelocity target{0.3, 0.4, 0.0}; for (int tick = 0; tick != 25; ++tick) { const auto next = AgvVelocityRamp::step(value, target, limits, 0.1); expect(near(length(next) - length(value), 0.02), "XY acceleration uses vector magnitude"); expect(near(next.vx / next.vy, 0.75), "XY vector direction preserved"); value = next; } expect(near(value.vx, 0.3) && near(value.vy, 0.4), "diagonal target reached"); value = AgvVelocityRamp::step(value, {0.06, 0.08, 0.0}, limits, 0.1); expect(near(length(value), 0.47), "diagonal deceleration magnitude bound"); value = AgvVelocityRamp::step(value, {}, limits, 0.1); expect(near(length(value), 0.44), "diagonal stop magnitude bound"); value = AgvVelocityRamp::step({}, {0.0, 0.0, 0.3}, limits, 0.1); expect(near(value.wz, 0.05), "angular acceleration separate limit"); value = AgvVelocityRamp::step({0.0, 0.0, 0.3}, {0.0, 0.0, -0.3}, limits, 0.1); expect(near(value.wz, 0.22), "angular reversal brakes at deceleration rate"); bool seenZero = false; for (int tick = 0; tick != 12; ++tick) { const auto next = AgvVelocityRamp::step(value, {0.0, 0.0, -0.3}, limits, 0.1); if (next.wz < 0.0) expect(seenZero, "angular reversal has zero sample"); seenZero = seenZero || AgvVelocityRamp::isZero(next); value = next; } expect(near(value.wz, -0.3), "angular reverse target reached"); // In a combined reversal the faster component must wait for the slower // component to finish braking before any new component starts. value = {0.3, 0.0, 0.05}; seenZero = false; for (int tick = 0; tick != 30; ++tick) { const auto next = AgvVelocityRamp::step(value, {-0.2, 0.0, -0.3}, limits, 0.1); if (next.vx < 0.0 || next.wz < 0.0) expect(seenZero, "combined reversal waits for all motion to stop"); seenZero = seenZero || AgvVelocityRamp::isZero(next); value = next; } expect(near(value.vx, -0.2) && near(value.wz, -0.3), "combined reverse target reached"); } void elapsedTimeAndInvalidInputs() { const AgvRampLimits limits; expect(near(AgvVelocityRamp::step({}, {1.0, 0.0, 0.0}, limits, 1.0).vx, 0.03), "long GUI delay capped at 150 ms"); expect(near(AgvVelocityRamp::step({}, {1.0, 0.0, 0.0}, limits, 0.025).vx, 0.005), "fractional elapsed time respected"); const AgvManualVelocity current{0.2, 0.0, 0.1}; const auto nan = std::numeric_limits::quiet_NaN(); const auto infinity = std::numeric_limits::infinity(); for (double dt : {0.0, -1.0, nan, infinity}) { const auto value = AgvVelocityRamp::step(current, {}, limits, dt); expect(near(value.vx, current.vx) && near(value.wz, current.wz), "invalid elapsed time does not move ramp"); } AgvManualVelocity a, b; for (int tick = 0; tick != 10; ++tick) a = AgvVelocityRamp::step(a, {0.5, 0.0, 0.0}, limits, 0.1); for (int tick = 0; tick != 40; ++tick) b = AgvVelocityRamp::step(b, {0.5, 0.0, 0.0}, limits, 0.025); expect(near(a.vx, b.vx), "same elapsed time at different tick rates"); const auto invalidTarget = AgvVelocityRamp::step({0.2, 0.0, 0.0}, {nan, infinity, nan}, limits, 0.1); expect(near(invalidTarget.vx, 0.17) && finite(invalidTarget), "invalid target brakes safely"); expect(AgvVelocityRamp::isZero(AgvVelocityRamp::step({nan, 0.0, 0.0}, {0.2, 0.0, 0.0}, limits, 0.1)), "invalid current never extrapolates"); expect(!AgvVelocityRamp::isZero({nan, 0.0, 0.0}), "invalid velocity is not reported stationary"); AgvRampLimits bad; bad.linearAcceleration = nan; bad.linearDeceleration = -1.0; bad.angularAcceleration = infinity; bad.angularDeceleration = 0.0; const auto start = AgvVelocityRamp::step({}, {0.2, 0.0, 0.3}, bad, 0.1); expect(near(start.vx, 0.02) && near(start.wz, 0.05), "invalid acceleration uses bounded defaults"); const auto stop = AgvVelocityRamp::step({0.2, 0.0, 0.3}, {}, bad, 0.1); expect(near(stop.vx, 0.17) && near(stop.wz, 0.22), "invalid deceleration uses bounded defaults"); const auto huge = AgvVelocityRamp::step({1e308, 1e308, 0.0}, {}, limits, 0.1); expect(finite(huge), "extreme finite velocities never overflow output"); expect(finite(AgvVelocityRamp::step({}, {infinity, 0.0, 0.0}, limits, 0.1)), "infinite target never becomes output"); AgvManualVelocity value{0.3, 0.4, -0.3}; const double ticks[] = {0.03, 0.1, 0.05, 0.14, 0.01}; for (int tick = 0; tick != 60; ++tick) { const auto next = AgvVelocityRamp::step(value, {}, limits, ticks[tick % 5]); expect(length(next) <= length(value) + 1e-10 && finite(next), "irregular ticks stop monotonically"); value = next; } expect(AgvVelocityRamp::isZero(value), "irregular ticks reach exact zero"); } } // namespace int main() { positiveAndNegativeAcceleration(); stoppingAndMagnitudeDecrease(); reversalsAndTurns(); vectorAndAngularLimits(); elapsedTimeAndInvalidInputs(); std::cout << "PASS AGV velocity ramp: " << checks << " checks (vector limits, start/stop/reversal, elapsed time, invalid inputs)\n"; return 0; }