Skip to content

Commit ca5bb8a

Browse files
feat: followingMode=follow, dynamic constraints & relative speed profiles (#84)
Closes the p2-s2 deferrals ADR-0011 left behind. followingMode=follow was accepted and executed as position, the jerk limits (maxAccelerationRate / maxDecelerationRate) were parsed and never read, and SpeedProfileAction had neither its entityRef nor its DynamicConstraints. Follow mode is now a shape substitution plus a duration stretch (ADR-0024). The realised shape has zero acceleration at both endpoints — Cubic, or Sinusoidal when the author asked for it — because §SpeedProfileAction says so outright; the duration then grows until the peak acceleration and the peak jerk both fit the effective limits. Constraints only ever slow a transition down: the authored dynamics are a floor. A real driver-model control loop would be more faithful and is not reproducible across platforms, so the construction is closed-form and documented instead. A SpeedProfileAction's DynamicConstraints override the Performance envelope, including when looser — that clause states precedence, and the distance actions' does not, so they keep tighter-of-both. entityRef entries are deltas on the reference's speed, resolved once at start: a profile is a series of authored targets over time, not a tracking controller (that is what a continuous RelativeTargetSpeed is for). The two maxSpeed rules are 'should', so they warn with their rule ids and the target is clamped, as position mode has always clamped it. Additive throughout: the original SpeedProfileAction constructor and scn_engine_add_speed_profile_action are untouched, with scn_engine_add_speed_profile_action_ex alongside. 24 new gtest cases (899) + 6 pytest (164), a follow_mode.py example, the motion guide, two coverage rows and ADR-0024. Closes #62
1 parent 7ebc201 commit ca5bb8a

18 files changed

Lines changed: 1552 additions & 55 deletions

File tree

‎capi/include/scena/capi.h‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,25 @@ SCN_API scn_status scn_engine_add_speed_profile_action(scn_engine* engine, const
477477
double at_time, scn_event_priority priority,
478478
int maximum_execution_count);
479479

480+
/* Adds a SpeedProfileAction with the full §SpeedProfileAction surface.
481+
*
482+
* `reference_entity_id` may be NULL or empty for an absolute profile; when it
483+
* names an entity, each entry's speed is a delta on that entity's speed,
484+
* resolved once when the action starts. `constraints` may be NULL; when
485+
* present it limits acceleration, deceleration, jerk and speed and takes
486+
* precedence over the actor's Performance envelope.
487+
*
488+
* With SCN_FOLLOWING_MODE_FOLLOW the profile is realised with a smooth shape
489+
* whose acceleration is zero at the start and end of each segment, and each
490+
* segment is stretched until its peak acceleration and peak jerk fit inside the
491+
* effective limits; the entry times act as lower bounds. Rejects the same
492+
* arguments as scn_engine_add_speed_profile_action. */
493+
SCN_API scn_status scn_engine_add_speed_profile_action_ex(
494+
scn_engine* engine, const char* entity_id, const char* reference_entity_id,
495+
const scn_speed_profile_entry* entries, size_t entry_count, scn_following_mode following_mode,
496+
const scn_dynamic_constraints* constraints, double at_time, scn_event_priority priority,
497+
int maximum_execution_count);
498+
480499
/* Adds a SpeedAction whose target is relative to `reference_entity_id`
481500
* (§RelativeTargetSpeed): the target is that entity's speed combined with
482501
* `value` per `value_type` (delta ⇒ reference + value, factor ⇒ reference *

‎capi/src/capi.cpp‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,46 @@ scn_status scn_engine_add_speed_profile_action(scn_engine* engine, const char* e
475475
}
476476
}
477477

478+
scn_status scn_engine_add_speed_profile_action_ex(
479+
scn_engine* engine, const char* entity_id, const char* reference_entity_id,
480+
const scn_speed_profile_entry* entries, size_t entry_count, scn_following_mode following_mode,
481+
const scn_dynamic_constraints* constraints, double at_time, scn_event_priority priority,
482+
int maximum_execution_count) {
483+
scena::ir::EventPriority ir_priority = scena::ir::EventPriority::Parallel;
484+
if (engine == nullptr || entity_id == nullptr || entries == nullptr || entry_count == 0 ||
485+
maximum_execution_count < 0 || !to_ir_priority(priority, ir_priority) ||
486+
static_cast<unsigned>(following_mode) > static_cast<unsigned>(SCN_FOLLOWING_MODE_FOLLOW)) {
487+
return SCN_ERROR_INVALID_ARGUMENT;
488+
}
489+
try {
490+
std::vector<scena::ir::SpeedProfileEntry> ir_entries;
491+
ir_entries.reserve(entry_count);
492+
for (size_t i = 0; i < entry_count; ++i) {
493+
scena::ir::SpeedProfileEntry entry;
494+
entry.speed = entries[i].speed;
495+
// A negative time means "unspecified" across the ABI.
496+
if (entries[i].time >= 0.0) {
497+
entry.time = entries[i].time;
498+
}
499+
ir_entries.push_back(entry);
500+
}
501+
std::optional<scena::ir::DynamicConstraints> ir_constraints;
502+
if (constraints != nullptr) {
503+
ir_constraints = from_c_constraints(*constraints);
504+
}
505+
append_storyboard_event(
506+
engine, at_time, ir_priority, maximum_execution_count,
507+
std::make_shared<scena::ir::SpeedProfileAction>(
508+
entity_id, std::move(ir_entries),
509+
static_cast<scena::ir::FollowingMode>(following_mode),
510+
reference_entity_id != nullptr ? std::string(reference_entity_id) : std::string(),
511+
std::move(ir_constraints)));
512+
return SCN_OK;
513+
} catch (...) {
514+
return SCN_ERROR_INTERNAL;
515+
}
516+
}
517+
478518
scn_status scn_engine_add_relative_speed_action(
479519
scn_engine* engine, const char* entity_id, const char* reference_entity_id, double value,
480520
scn_speed_target_value_type value_type, int continuous, const scn_transition_dynamics* dynamics,

‎core/include/scena/ir/action.h‎

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,20 @@ class SpeedAction final : public Action {
144144
TransitionDynamics dynamics_;
145145
};
146146

147+
/// Limits on the acceleration, deceleration, jerk and speed an action may use,
148+
/// per §DynamicConstraints. Every field is optional and "missing value is
149+
/// interpreted as 'inf'" — an absent limit does not constrain. The rate limits
150+
/// are jerk bounds [m/s^3] and were added in 1.2. Where an action carries these
151+
/// constraints they take "precedence over any Performance settings"
152+
/// (§SpeedProfileAction, §DynamicConstraints).
153+
struct DynamicConstraints {
154+
std::optional<double> max_acceleration; ///< m/s^2, Range [0..inf[.
155+
std::optional<double> max_acceleration_rate; ///< m/s^3, Range [0..inf[ [1.2].
156+
std::optional<double> max_deceleration; ///< m/s^2, Range [0..inf[.
157+
std::optional<double> max_deceleration_rate; ///< m/s^3, Range [0..inf[ [1.2].
158+
std::optional<double> max_speed; ///< m/s, Range [0..inf[.
159+
};
160+
147161
/// One speed target of a SpeedProfileAction. Per ASAM OpenSCENARIO XML 1.4.0
148162
/// §SpeedProfileEntry.
149163
struct SpeedProfileEntry {
@@ -159,16 +173,29 @@ struct SpeedProfileEntry {
159173
/// Changes an entity's speed through a series of speed targets over time. Per
160174
/// ASAM OpenSCENARIO XML 1.4.0 §SpeedProfileAction.
161175
///
162-
/// Scena models `FollowingMode::Position`: strictly linear interpolation
163-
/// between successive targets, starting from the entity's current speed. An
164-
/// entry with no time is reached as fast as the Performance envelope allows.
165-
/// The entityRef-relative profile and the jerk/DynamicConstraints of
166-
/// followingMode=follow are deferred (ADR-0011).
176+
/// `FollowingMode::Position` interpolates strictly linearly between successive
177+
/// targets, starting from the entity's current speed. `FollowingMode::Follow`
178+
/// applies jerk and the other dynamic constraints instead, producing a smoother
179+
/// curve whose acceleration is zero at the start and end of the profile
180+
/// (§SpeedProfileAction); Scena realises that with the Cubic shape (ADR-0024).
181+
///
182+
/// With `entity_ref` set, each entry's `speed` is a delta on the referenced
183+
/// entity's speed rather than an absolute value (§SpeedProfileAction
184+
/// `entityRef`); it is resolved once, when the action starts.
185+
///
186+
/// `constraints` limits acceleration, deceleration, jerk and speed, and takes
187+
/// precedence over the actor's `Performance` envelope (§DynamicConstraints).
167188
class SpeedProfileAction final : public Action {
168189
public:
169190
SpeedProfileAction(std::string entity_id, std::vector<SpeedProfileEntry> entries,
170191
FollowingMode following_mode = FollowingMode::Position);
171192

193+
/// Full form: an optionally entity-relative profile with optional dynamic
194+
/// constraints. An empty `entity_ref` means an absolute profile.
195+
SpeedProfileAction(std::string entity_id, std::vector<SpeedProfileEntry> entries,
196+
FollowingMode following_mode, std::string entity_ref,
197+
std::optional<DynamicConstraints> constraints);
198+
172199
[[nodiscard]] const std::string& entity_id() const override;
173200

174201
[[nodiscard]] std::string_view kind() const noexcept override;
@@ -179,10 +206,22 @@ class SpeedProfileAction final : public Action {
179206
/// Interpolation behavior between targets (§FollowingMode).
180207
[[nodiscard]] FollowingMode following_mode() const;
181208

209+
/// Name of the entity the entries are relative to, empty for an absolute
210+
/// profile (§SpeedProfileAction `entityRef`).
211+
[[nodiscard]] const std::string& entity_ref() const;
212+
213+
/// True when the entries are deltas on `entity_ref`'s speed.
214+
[[nodiscard]] bool is_relative() const noexcept;
215+
216+
/// Dynamic constraints for the profile, if any (§DynamicConstraints).
217+
[[nodiscard]] const std::optional<DynamicConstraints>& constraints() const;
218+
182219
private:
183220
std::string entity_id_;
184221
std::vector<SpeedProfileEntry> entries_;
185222
FollowingMode following_mode_;
223+
std::string entity_ref_;
224+
std::optional<DynamicConstraints> constraints_;
186225
};
187226

188227
/// Teleports an entity to a target position. Per ASAM OpenSCENARIO XML 1.4.0
@@ -225,19 +264,6 @@ enum class LongitudinalDisplacement {
225264
LeadingReferencedEntity,
226265
};
227266

228-
/// Limits on the acceleration, deceleration and speed a distance controller may
229-
/// use, per §DynamicConstraints. Every field is optional and "missing value is
230-
/// interpreted as 'inf'" — an absent limit does not constrain. The rate limits
231-
/// (jerk) were added in 1.2; Scena stores them but does not yet clamp jerk
232-
/// (deferred with the followingMode=follow jerk model, #62).
233-
struct DynamicConstraints {
234-
std::optional<double> max_acceleration; ///< m/s^2, Range [0..inf[.
235-
std::optional<double> max_acceleration_rate; ///< m/s^3, Range [0..inf[ [1.2].
236-
std::optional<double> max_deceleration; ///< m/s^2, Range [0..inf[.
237-
std::optional<double> max_deceleration_rate; ///< m/s^3, Range [0..inf[ [1.2].
238-
std::optional<double> max_speed; ///< m/s, Range [0..inf[.
239-
};
240-
241267
/// Keeps a longitudinal distance or time gap to a reference entity, per
242268
/// §LongitudinalDistanceAction: "activates a controller for the longitudinal
243269
/// behavior of an entity in a way that a given distance or time gap to the

‎core/include/scena/runtime/longitudinal.h‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,55 @@ namespace scena::runtime {
5454
/// `factor * |delta| / L`.
5555
[[nodiscard]] double shape_peak_gradient_factor(ir::DynamicsShape shape) noexcept;
5656

57+
/// Peak absolute second derivative of a unit-span (span 1, delta 1) transition
58+
/// of this shape — the factor that turns a jerk limit into a duration. The
59+
/// duration whose peak jerk equals a limit J over a value change |delta| is
60+
/// `sqrt(factor * |delta| / J)`.
61+
///
62+
/// Cubic: 6 (|g''| = |6 - 12p|, peak at both endpoints). Sinusoidal: pi^2/2
63+
/// (|g''| is pi^2/2 times the absolute cosine of pi*p, peak at both endpoints,
64+
/// and the shape itself routes through det_cos). Linear and Step are
65+
/// infinity: both hold the gradient constant and then drop it to zero in no
66+
/// time at all, so no finite duration bounds their second derivative, and no
67+
/// jerk limit can be honoured by stretching them. `follow_shape` is what keeps
68+
/// that infinity out of the arithmetic.
69+
///
70+
/// `lateral.h`'s `shape_peak_curvature_factor` is the same quantity for Cubic
71+
/// and Sinusoidal, and deliberately differs for Linear and Step: there it
72+
/// stands in as a minimum-time rest-to-rest bound for a lane offset that has no
73+
/// authored duration at all, where "infinite" would be useless. Here the
74+
/// infinity is the point — it says a jerk limit is unsatisfiable for that
75+
/// shape.
76+
[[nodiscard]] double shape_peak_jerk_factor(ir::DynamicsShape shape) noexcept;
77+
78+
/// The shape a transition is realised with under
79+
/// `FollowingMode::Follow`. Sinusoidal is returned unchanged; every other shape
80+
/// becomes Cubic.
81+
///
82+
/// §SpeedProfileAction is explicit that with `followingMode=follow` "the
83+
/// acceleration is zero at the start and end of the profile", which excludes
84+
/// the Linear and Step shapes by construction — their acceleration steps at the
85+
/// endpoints. §FollowingMode describes `follow` as tracking the target "as good
86+
/// as possible by observing the dynamic constraints of the entity", so the
87+
/// authored shape is a request rather than a contract, and Scena honours it
88+
/// with the closest shape that satisfies the endpoint requirement. Cubic is
89+
/// that shape: it is the lowest-order polynomial with a zero gradient at both
90+
/// ends, and it is what a Linear ramp becomes once its corners are rounded.
91+
/// `FollowingMode::Position` keeps the authored shape exactly. See ADR-0024.
92+
[[nodiscard]] ir::DynamicsShape follow_shape(ir::DynamicsShape shape) noexcept;
93+
94+
/// The shortest duration [s] in which a `delta`-sized value change of this
95+
/// shape stays inside every supplied limit, never shorter than `authored`.
96+
///
97+
/// `acceleration_limit` bounds the peak first derivative [unit/s] and
98+
/// `jerk_limit` the peak second derivative [unit/s^2]; either may be infinity
99+
/// for "unconstrained" (§DynamicConstraints reads a missing value as 'inf').
100+
/// A non-positive or non-finite `delta` needs no time, and an unconstrained
101+
/// transition simply keeps its authored duration.
102+
[[nodiscard]] double constrained_duration(ir::DynamicsShape shape, double delta,
103+
double acceleration_limit, double jerk_limit,
104+
double authored) noexcept;
105+
57106
/// Resolved duration, in seconds, of a Time- or Rate-dimensioned transition
58107
/// from `from` to `to`. Returns 0 for a transition that consumes no time —
59108
/// Step shape, zero value span (`to == from`), or a non-positive/non-finite

0 commit comments

Comments
 (0)