From 75fb4ef739c4c57cad13ec47eb37e9dec7a0870e Mon Sep 17 00:00:00 2001 From: Xia Chao Date: Tue, 24 Nov 2026 21:07:33 +0800 Subject: [PATCH] Add timed trajectory waypoints and duration controls --- README.md | 6 +- src/FasTimed.h | 73 ++++++++++++++++++++-- tests/host/test_35.cpp | 136 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 208 insertions(+), 7 deletions(-) create mode 100644 tests/host/test_35.cpp diff --git a/README.md b/README.md index 6a5ca59..e32e0cf 100644 --- a/README.md +++ b/README.md @@ -53,8 +53,10 @@ Arduino project, install this checkout as a local library named driver queue and manages steppers. - [StepperMotion](src/StepperMotion.h) sets speed and acceleration, starts point-to-point and continuous moves, and queues step or pause commands. -- [FasTimed](src/FasTimed.h) queues coordinated segments with caller-supplied - timing. [FasNAxis](src/FasNAxis.h) plans coordinated motion along waypoints. +- [FasTimed](src/FasTimed.h) queues coordinated deltas, absolute waypoints, + and shared dwells. It can require exact tick durations and report the + committed duration of long streams. [FasNAxis](src/FasNAxis.h) plans + coordinated motion along waypoints. - [Examples](examples/) include single-axis, timed, and multi-axis sketches. Backend availability and usable pins depend on the board and SDK. diff --git a/src/FasTimed.h b/src/FasTimed.h index 66f3cbc..664938c 100644 --- a/src/FasTimed.h +++ b/src/FasTimed.h @@ -42,7 +42,9 @@ enum class TimedStatus : uint8_t { // The feeder emits one or two addQueueEntry commands per axis whose tick // sums are equal. When the requested duration is not an exact legal command // split, the shared sum moves to the nearest duration that is, and no -// farther than the longest |delta| of the call. actualTicks() is that sum. +// farther than the longest |delta| of the call. addDeltaExact() requires the +// requested duration to be representable. actualTicks64() reports the total +// issued duration for the plan. // // HORIZON is only the number of chunks waiting to be queued. Each chunk is // already one command, so the ring stays small (default 8). A longer path @@ -114,6 +116,64 @@ class FasTimed { // shape. On Ok the planned position advances by steps and actualTicks() // grows by the duration that will be issued. TimedAdd addDelta(const int16_t steps[NAXES], uint16_t ticks) { + return appendDelta(steps, ticks, false); + } + + // Queue a delta only when every axis can emit exactly ticks. + TimedAdd addDeltaExact(const int16_t steps[NAXES], uint16_t ticks) { + return appendDelta(steps, ticks, true); + } + + // Queue an absolute waypoint relative to the last committed position. + // The same per-axis travel and timing limits as addDelta() apply. + TimedAdd addWaypoint(const int32_t position[NAXES], uint16_t ticks) { + int16_t steps[NAXES]; + for (uint8_t i = 0; i < NAXES; ++i) { + const int64_t delta = (int64_t)position[i] - _p[i]; + if (delta < -128 || delta > 128) { + return TimedAdd::Rejected; + } + steps[i] = delta; + } + return addDelta(steps, ticks); + } + + // Queue a shared dwell at rest. The whole duration must fit in the free + // horizon. Long dwells consume multiple chunks and keep the current DIR. + TimedAdd addDwellTicks(uint32_t ticks) { + if (_fault || _error || !_synced || !all_registered()) { + return TimedAdd::Rejected; + } + if (ticks == 0) { + return TimedAdd::Ok; + } + uint32_t chunks = 1 + (ticks - 1) / UINT16_MAX; + if (ticks < MIN_CMD_TICKS || chunks > (uint32_t)(HORIZON - _count)) { + return TimedAdd::Rejected; + } + for (uint8_t i = 0; i < NAXES; ++i) { + if (_ramp_p[i] != 0) { + return TimedAdd::TimingNotAchievable; + } + } + + // Balanced pieces avoid leaving a final pause shorter than MIN_CMD_TICKS. + uint32_t remaining = ticks; + for (; chunks > 0; --chunks) { + const uint16_t part = remaining / chunks; + Chunk& chunk = _chunk[_count++]; + clear_chunk(&chunk); + for (uint8_t i = 0; i < NAXES; ++i) { + fill_axis(&chunk, i, 0, 0, part); + } + remaining -= part; + } + _actual += ticks; + return TimedAdd::Ok; + } + + private: + TimedAdd appendDelta(const int16_t steps[NAXES], uint16_t ticks, bool exact) { if (_fault || _error || !_synced || !all_registered()) { return TimedAdd::Rejected; } @@ -143,8 +203,8 @@ class FasTimed { if (!speed_ok(mag, ticks)) { return TimedAdd::TimingNotAchievable; } - uint16_t issued = 0; - if (!find_duration(mag, ticks, &issued)) { + uint16_t issued = ticks; + if (exact ? !realizable_all(mag, ticks) : !find_duration(mag, ticks, &issued)) { return TimedAdd::TimingNotAchievable; } uint32_t p_act[NAXES]; @@ -193,6 +253,7 @@ class FasTimed { return TimedAdd::Ok; } + public: int32_t plannedPosition(uint8_t i) const { if (i >= NAXES) { return 0; @@ -201,7 +262,9 @@ class FasTimed { } // Sum of the durations actually committed, in driver ticks. - uint32_t actualTicks() const { return _actual; } + uint64_t actualTicks64() const { return _actual; } + // 32-bit view for clients that track the sum modulo 2^32. + uint32_t actualTicks() const { return (uint32_t)_actual; } bool isBusy() const { if (_count > 0 || any_waiting()) { @@ -281,7 +344,7 @@ class FasTimed { bool _count_up[NAXES]; Chunk _chunk[HORIZON]; uint16_t _count; - uint32_t _actual; + uint64_t _actual; bool _synced; bool _kicked; bool _underrun; diff --git a/tests/host/test_35.cpp b/tests/host/test_35.cpp new file mode 100644 index 0000000..5befa98 --- /dev/null +++ b/tests/host/test_35.cpp @@ -0,0 +1,136 @@ +#include +#include + +#include "FasTimed.h" +#include "naxis_sim_port.h" + +void inject_fill_interrupt(int) {} +void noInterrupts() {} +void interrupts() {} + +namespace { +int failures = 0; + +void check(bool condition, const char* message) { + if (!condition) { + printf("FAIL: %s\n", message); + ++failures; + } +} + +template +struct Fixture { + TestStepperMotionEngine engine; + SimPort x{80, 64}; + SimPort y{80, 64}; + FasTimed<2, Horizon, SimPort, TestStepperMotionEngine> plan{engine}; + + Fixture() { + x.setAcceleration(100000000); + y.setAcceleration(100000000); + check(plan.addAxis(0, &x) && plan.addAxis(1, &y), "register both axes"); + plan.syncFromSteppers(); + } +}; + +void absolute_waypoints_use_the_planned_coordinates() { + Fixture<> f; + const int32_t origin[2] = {100, -50}; + const int32_t first[2] = {104, -52}; + const int32_t next[2] = {106, -53}; + const int32_t overflow[2] = {INT32_MIN, INT32_MAX}; + f.plan.setCurrentPosition(origin); + const auto a = f.plan.addWaypoint(first, 48000); + const auto b = f.plan.addWaypoint(next, 24000); + const auto invalid = f.plan.addWaypoint(overflow, 48000); + f.plan.pump(); + f.x.drain(); + f.y.drain(); + printf("absolute targets: rc=%d/%d/%d planned=(%" PRId32 ",%" PRId32 + ") pulses=(%" PRId32 ",%" PRId32 ") ticks=(%" PRIu32 ",%" PRIu32 ")\n", + static_cast(a), static_cast(b), static_cast(invalid), + f.plan.plannedPosition(0), f.plan.plannedPosition(1), f.x.position(), + f.y.position(), f.x.clock(), f.y.clock()); + check(a == TimedAdd::Ok && b == TimedAdd::Ok && invalid == TimedAdd::Rejected, + "waypoints accept in-range travel and reject excessive deltas"); + check(f.plan.plannedPosition(0) == 106 && f.plan.plannedPosition(1) == -53 && + f.x.position() == 6 && f.y.position() == -3 && + f.x.clock() == 72000 && f.y.clock() == 72000, + "waypoints emit displacement from the committed coordinates"); +} + +void exact_ticks_are_never_changed() { + Fixture<> f; + const int16_t steps[2] = {10, 0}; + const auto invalid = f.plan.addDeltaExact(steps, 20001); + const auto ticks_after_rejection = f.plan.actualTicks(); + const auto position_after_rejection = f.plan.plannedPosition(0); + const auto exact = f.plan.addDeltaExact(steps, 20000); + f.plan.pump(); + f.x.drain(); + f.y.drain(); + printf("exact ticks 20001/20000: rc=%d/%d after rejection=%" PRIu32 + " position=%" PRId32 " emitted=(%" PRIu32 ",%" PRIu32 ")\n", + static_cast(invalid), static_cast(exact), ticks_after_rejection, + position_after_rejection, f.x.clock(), f.y.clock()); + check(invalid == TimedAdd::TimingNotAchievable && ticks_after_rejection == 0 && + position_after_rejection == 0, + "an unrepresentable exact span leaves the plan untouched"); + check(exact == TimedAdd::Ok && f.x.clock() == 20000 && f.y.clock() == 20000, + "a representable exact span keeps both axes on the requested clock"); +} + +void long_dwell_is_admitted_as_a_unit() { + Fixture<2> f; + check(f.plan.addDwellTicks(3200) == TimedAdd::Ok, "one dwell fits in the horizon"); + const auto busy = f.plan.addDwellTicks(100000); + const auto planned_while_busy = f.plan.actualTicks(); + f.plan.pump(); + f.x.drain(); + f.y.drain(); + f.plan.pump(); + const auto accepted = f.plan.addDwellTicks(100000); + f.plan.pump(); + f.x.drain(); + f.y.drain(); + printf("100000-tick dwell: rc=%d/%d busy planned=%" PRIu32 + " total clocks=(%" PRIu32 ",%" PRIu32 ")\n", + static_cast(busy), static_cast(accepted), planned_while_busy, + f.x.clock(), f.y.clock()); + check(busy == TimedAdd::Rejected && planned_while_busy == 3200, + "a dwell that exceeds available horizon commits no partial chunks"); + check(accepted == TimedAdd::Ok && f.x.clock() == 103200 && + f.y.clock() == 103200 && f.x.position() == 0 && f.y.position() == 0, + "a long dwell emits its full shared duration and no pulses"); +} + +void time_accounting_tracks_long_streams() { + Fixture<> f; + const int16_t zero[2] = {0, 0}; + uint64_t emitted = 0; + for (unsigned i = 0; i < 65538; ++i) { + if (f.plan.addDelta(zero, 65535) != TimedAdd::Ok) { + check(false, "streamed dwell is accepted"); + return; + } + f.plan.pump(); + emitted += f.x.drain(); + f.y.drain(); + f.plan.pump(); + } + printf("streamed time: emitted=%" PRIu64 " full=%" PRIu64 + " legacy=%" PRIu32 " ticks\n", + emitted, f.plan.actualTicks64(), f.plan.actualTicks()); + check(emitted == 4295032830ULL && f.plan.actualTicks64() == emitted && + f.plan.actualTicks() == 65534, + "wide tick reporting preserves the clock beyond the legacy range"); +} +} // namespace + +int main() { + absolute_waypoints_use_the_planned_coordinates(); + exact_ticks_are_never_changed(); + long_dwell_is_admitted_as_a_unit(); + time_accounting_tracks_long_streams(); + return failures != 0; +}