Skip to content

Improve efficiency of HysteresisDeadband::is_in_deadband #196

@rhaschke

Description

@rhaschke

The average over last errors, this class uses a std::deque to maintain a sliding window of previous errors:

last_errors.push_back(error);

While this approach is semantically correct, it is highly inefficient, copying 50 values around in each call.
There are much more efficient ways to just a compute a sliding average:

  • Use a circular buffer, maintain the average sum, and just subtract the popped and add the pushed value (all others values remain the same).
  • Use estimated sliding average (which should be sufficient here):
    avg_error = (1.0 - lambda) * avg_error + lambda * error
    where lambda adjusts the smoothing and should be chosen lambda=1.0/nb_errors_for_avg here.

I strongly suggest the latter method and storing lambda once in the class (nb_errors_for_avg shouldn't change over time anyway!) As this requires API changes, I leave it to you to decide on this and implement it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions