110 for(std::size_t i = 0; i < preflight_count; ++i) {
111 if(!filters_[i].can_configure(config)) {
116 const rt::ErrorCode configured = filters_[i].configure(config);
121 initialize_configuration(SessionKind::legacy_independent,
joint_count);
129 if(!valid_frame_config(config)) {
133 const std::size_t preflight_count =
135 for(std::size_t i = 0; i < preflight_count; ++i) {
138 if(!filters_[i].can_configure(filter)) {
142 for(std::size_t i = 0; i < config.
joint_count; ++i) {
149 frame_mode_ = config.
mode;
152 for(std::size_t i = 0; i < config.
joint_count; ++i) {
153 frame_configs_[i] = config.
joints[i];
158 initialize_configuration(SessionKind::frame, config.
joint_count);
164 for(std::size_t i = 0; i < joint_count_; ++i) {
165 filters_[i].end_session();
166 member_reset_[i] =
false;
168 clear_frame_runtime();
173 if(
joint >= joint_count_ ||
174 (session_kind_ == SessionKind::frame && (have_accepted_frame_ || local_cycle_ != 0))) {
181 if(session_kind_ == SessionKind::frame) {
182 member_reset_[
joint] =
true;
185 mixed_[
joint].tau_ff = 0.0;
186 mixed_[
joint].kp = frame_configs_[
joint].safe_kp;
187 mixed_[
joint].kd = frame_configs_[
joint].safe_kd;
188 mixed_[
joint].gain_start_kp = mixed_[
joint].kp;
189 mixed_[
joint].gain_start_kd = mixed_[
joint].kd;
190 mixed_[
joint].gain_target_kp = mixed_[
joint].kp;
191 mixed_[
joint].gain_target_kd = mixed_[
joint].kd;
200 if(session_kind_ != SessionKind::legacy_independent ||
joint >= joint_count_) {
203 return filters_[
joint].push_target(target);
213 if(session_kind_ != SessionKind::frame) {
216 if(!all_members_reset() || !valid_command_frame(frame) ||
217 (have_accepted_frame_ && frame.
timestamp_cycles <= latest_accepted_timestamp_)) {
222 pending_frame_ = frame;
224 have_accepted_frame_ =
true;
225 have_pending_frame_ =
true;
226 pending_frame_sequence_ = ++accepted_frame_sequence_;
234 if(session_kind_ == SessionKind::legacy_independent) {
235 for(std::size_t i = 0; i < joint_count_; ++i) {
240 if(session_kind_ != SessionKind::frame || !all_members_reset()) {
243 if(local_cycle_ < INT64_MAX) {
247 const bool activated = have_pending_frame_;
249 activate_pending_frame();
251 for(std::size_t i = 0; i < joint_count_; ++i) {
252 direct_states_[i].acceleration = 0.0;
256 if(have_active_frame_ && !dropping_ &&
257 positive_cycle_delta(local_cycle_, last_activation_cycle_) >
258 static_cast<std::uint64_t
>(group_timeout_cycles_)) {
259 begin_group_dropout();
263 cycle_upsample_filters();
264 }
else if(dropping_) {
265 cycle_direct_dropout();
268 advance_mixed_fields();
274 if(
joint >= joint_count_) {
277 if(session_kind_ == SessionKind::frame) {
281 return filters_[
joint].state();
302 std::size_t count = 0;
303 for(std::size_t i = 0; i < joint_count_; ++i) {
304 if(filters_[i].replan_pending()) {
314 enum class SessionKind
324 double tau_start = 0.0;
327 double gain_start_kp = 0.0;
328 double gain_start_kd = 0.0;
329 double gain_target_kp = 0.0;
330 double gain_target_kd = 0.0;
331 std::int64_t gain_tick = 0;
332 std::int64_t gain_duration = 0;
335 static std::uint64_t positive_cycle_delta(std::int64_t later, std::int64_t earlier)
337 return static_cast<std::uint64_t
>(later) -
static_cast<std::uint64_t
>(earlier);
340 static bool finite_bounds(
double minimum,
double maximum)
342 return std::isfinite(minimum) && std::isfinite(maximum) && minimum <= maximum;
345 bool valid_frame_config(
const JointStreamGroupConfig &config)
const
347 if(config.joint_count < 1 || config.joint_count >
MaxJoints ||
348 config.gain_ramp_cycles < 1 ||
352 for(std::size_t i = 0; i < config.joint_count; ++i) {
353 const JointStreamConfig &member = config.joints[i];
354 if(!filters_[i].can_configure(member.filter) || !std::isfinite(member.max_abs_tau_ff) ||
355 member.max_abs_tau_ff < 0.0 || !finite_bounds(member.min_kp, member.max_kp) ||
356 !finite_bounds(member.min_kd, member.max_kd) || !std::isfinite(member.safe_kp) ||
357 !std::isfinite(member.safe_kd) || member.safe_kp < member.min_kp ||
358 member.safe_kp > member.max_kp || member.safe_kd < member.min_kd ||
359 member.safe_kd > member.max_kd) {
366 bool valid_command_frame(
const JointCommandFrame &frame)
const
368 if(frame.joint_count != joint_count_) {
371 for(std::size_t i = 0; i < joint_count_; ++i) {
372 const JointCommand &
command = frame.joints[i];
373 const JointStreamConfig &config = frame_configs_[i];
374 if(!std::isfinite(
command.q_des) || !std::isfinite(
command.dq_des) ||
377 (config.filter.position_envelope_enabled &&
378 (
command.q_des < config.filter.min_position ||
379 command.q_des > config.filter.max_position)) ||
380 std::fabs(
command.dq_des) > config.filter.limits.max_velocity ||
381 std::fabs(
command.tau_ff) > config.max_abs_tau_ff ||
command.kp < config.min_kp ||
390 bool all_members_reset()
const
392 for(std::size_t i = 0; i < joint_count_; ++i) {
393 if(!member_reset_[i]) {
400 void initialize_configuration(SessionKind kind, std::size_t
joint_count)
402 session_kind_ = kind;
404 for(std::size_t i = 0; i <
MaxJoints; ++i) {
405 member_reset_[i] =
false;
406 direct_states_[i] = {};
409 clear_frame_runtime();
413 void clear_frame_runtime()
417 snapshot_.joint_count = joint_count_;
419 latest_accepted_timestamp_ = 0;
420 last_activation_cycle_ = 0;
421 accepted_frame_sequence_ = 0;
422 pending_frame_sequence_ = 0;
423 active_frame_sequence_ = 0;
424 active_producer_timestamp_ = 0;
425 deferred_replans_ = 0;
426 rejected_frames_ = 0;
427 group_dropout_count_ = 0;
428 slow_replan_start_ = 0;
429 have_accepted_frame_ =
false;
430 have_pending_frame_ =
false;
431 have_active_frame_ =
false;
435 void start_gain_ramp(std::size_t
joint,
double kp,
double kd, std::int64_t duration)
437 MixedState &mixed = mixed_[
joint];
438 mixed.gain_start_kp = mixed.kp;
439 mixed.gain_start_kd = mixed.kd;
440 mixed.gain_target_kp = kp;
441 mixed.gain_target_kd = kd;
443 mixed.gain_duration = duration;
446 void activate_pending_frame()
448 have_pending_frame_ =
false;
449 have_active_frame_ =
true;
451 last_activation_cycle_ = local_cycle_;
452 active_frame_sequence_ = pending_frame_sequence_;
453 active_producer_timestamp_ = pending_frame_.timestamp_cycles;
455 for(std::size_t i = 0; i < joint_count_; ++i) {
456 const JointCommand &
command = pending_frame_.joints[i];
457 mixed_[i].tau_ff =
command.tau_ff;
460 const double previous_velocity = direct_states_[i].velocity;
461 direct_states_[i] = {
464 command.dq_des - previous_velocity,
467 StreamTarget target{};
468 target.position =
command.q_des;
469 target.velocity =
command.dq_des;
470 target.has_velocity =
true;
471 target.timestamp_cycles =
472 filters_[i].now_cycles() < INT64_MAX ? filters_[i].now_cycles() + 1 : INT64_MAX;
473 filters_[i].push_target(target);
478 void begin_group_dropout()
481 ++group_dropout_count_;
482 for(std::size_t i = 0; i < joint_count_; ++i) {
483 const otg::State1D from =
485 filters_[i].begin_dropout_from(from);
486 mixed_[i].tau_start = mixed_[i].tau_ff;
487 start_gain_ramp(i, frame_configs_[i].safe_kp, frame_configs_[i].safe_kd,
488 frame_configs_[i].filter.extrapolation_cycles);
492 void cycle_upsample_filters()
494 std::size_t slow_replans = 0;
495 for(std::size_t offset = 0; offset < joint_count_; ++offset) {
496 const std::size_t
joint = (slow_replan_start_ + offset) % joint_count_;
508 void cycle_direct_dropout()
510 for(std::size_t i = 0; i < joint_count_; ++i) {
512 direct_states_[i] = filters_[i].cycle_with_replan_budget(
true, result);
516 void advance_mixed_fields()
518 for(std::size_t i = 0; i < joint_count_; ++i) {
519 MixedState &mixed = mixed_[i];
521 const std::int64_t duration = frame_configs_[i].filter.extrapolation_cycles;
524 }
else if(mixed.gain_tick < duration) {
525 const double remaining = 1.0 -
static_cast<double>(mixed.gain_tick + 1) /
526 static_cast<double>(duration);
527 mixed.tau_ff = remaining > 0.0 ? mixed.tau_start * remaining : 0.0;
531 if(mixed.gain_tick < mixed.gain_duration) {
533 const double fraction =
534 static_cast<double>(mixed.gain_tick) /
static_cast<double>(mixed.gain_duration);
536 mixed.gain_start_kp + (mixed.gain_target_kp - mixed.gain_start_kp) * fraction;
538 mixed.gain_start_kd + (mixed.gain_target_kd - mixed.gain_start_kd) * fraction;
540 mixed.kp = mixed.gain_target_kp;
541 mixed.kd = mixed.gain_target_kd;
546 void publish_snapshot()
548 snapshot_.joint_count = joint_count_;
549 snapshot_.cycle_timestamp = local_cycle_;
550 snapshot_.producer_timestamp_cycles = active_producer_timestamp_;
551 snapshot_.frame_sequence = active_frame_sequence_;
552 for(std::size_t i = 0; i < joint_count_; ++i) {
553 const otg::State1D
state =
555 snapshot_.joints[i] = {
557 mixed_[i].tau_ff, mixed_[i].kp, mixed_[i].kd,
563 JointStreamConfig frame_configs_[
MaxJoints]{};
564 otg::State1D direct_states_[
MaxJoints]{};
566 JointCommandFrame pending_frame_{};
567 JointSetpointFrame snapshot_{};
568 std::size_t joint_count_ = 0;
569 std::size_t slow_replan_start_ = 0;
570 std::int64_t gain_ramp_cycles_ = 1;
571 std::int64_t group_timeout_cycles_ = 1;
572 std::int64_t local_cycle_ = 0;
573 std::int64_t latest_accepted_timestamp_ = 0;
574 std::int64_t last_activation_cycle_ = 0;
575 std::int64_t active_producer_timestamp_ = 0;
576 std::uint64_t accepted_frame_sequence_ = 0;
577 std::uint64_t pending_frame_sequence_ = 0;
578 std::uint64_t active_frame_sequence_ = 0;
579 std::uint64_t deferred_replans_ = 0;
580 std::uint32_t rejected_frames_ = 0;
581 std::uint32_t group_dropout_count_ = 0;
582 SessionKind session_kind_ = SessionKind::unconfigured;
585 bool have_accepted_frame_ =
false;
586 bool have_pending_frame_ =
false;
587 bool have_active_frame_ =
false;
588 bool dropping_ =
false;