Rewrite Inertia Estimation Script with Global Regression#110
Open
theelims wants to merge 1 commit intomjbots:mainfrom
Open
Rewrite Inertia Estimation Script with Global Regression#110theelims wants to merge 1 commit intomjbots:mainfrom
theelims wants to merge 1 commit intomjbots:mainfrom
Conversation
measure_inertia: rewrite inertia estimation with global regression - Bidirectional (CW + CCW) torque sweeps so friction cancels in regression - Replace two-point accel estimate with sliding best-R² window (50% of trace) that automatically skips planetary gear backlash transients - Single global OLS fit τ = J·α + τ_friction across all traces replaces per-trace median; intercept gives friction bias estimate - Bootstrap 95% CI reported alongside J estimate - Starting torque set to 1% of torque limit - Restore servo.pid_dq.max_desired_rate via conf load after measurement - Add matplotlib summary: per-trace v(t) with best window highlighted, global τ vs α scatter with fit line and CI band
|
Thoughts on adding a note to the output indicating that when rotor_to_output_ratio ≠ 1, the reported inertia is the effective inertia seen from the output rather than the rotor? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Rewrite inertia estimation using global regression
The original
measure_inertia.pyhad several issues that inflated the variance of the result. This PR replaces the estimation strategy with a more statistically robust approach while keeping the same CLI interface and moteus communication pattern.Problems with the original
Incorrect acceleration estimate. The two-point formula used
end_velocity / delta_time, implicitly assuming the motor started from rest at the 25% mark. The correct formula isΔv / Δt. This bias varied with torque level and was a significant contributor to variance.Friction ignored. The model
τ = J·αtreats all applied torque as going into acceleration. In realityτ = J·α + τ_friction. By ignoring friction, each per-trace J estimate is biased byτ_friction / α, and since that bias is larger at low torques (small α), the estimates are inconsistent across the sweep.Per-trace median is statistically weak. Computing J independently for each trace and taking a median discards the relationship between traces and amplifies the effect of individual noisy estimates.
Fixed 25–75% window captures backlash. For motors with a planetary gearbox the initial samples are dominated by backlash and stiction, not clean acceleration. A fixed window start cannot account for how long backlash takes to clear, which varies with torque level.
Config not restored.
servo.pid_dq.max_desired_ratewas overridden for the measurement but never restored on exit.Changes
Bidirectional sweep. Each torque magnitude is applied in both CW and CCW directions. Friction opposes motion, so it appears as a consistent offset in the global fit rather than corrupting J directly. Both directions contribute points to the regression, and the intercept of the fit gives a direct estimate of the friction torque.
Sliding best-R² window. Instead of a fixed 25–75% window or a velocity threshold, a window of 50% of the trace slides across the full recording. The position that maximises R² of a linear fit to
v(t)is selected. Because the backlash region is inherently non-linear, the post-backlash clean acceleration phase wins the search automatically - no threshold tuning required.Global OLS regression. All
(α, τ)pairs from both directions and all torque levels are fed into a singleτ = J·α + τ_frictionleast-squares fit. This uses all the data jointly, so no information is thrown away and outlier traces have proportionally less influence.Bootstrap CI. 4000 bootstrap resamples of the
(α, τ)pairs give a proper 95% confidence interval on J.Starting torque scaled to motor. The sweep now starts at 1% of
--torque-limitrather than a hardcoded 10 mN·m, making the script sensible for a wider range of motors out of the box.Config restore.
servo.pid_dq.max_desired_rateis restored on exit viaconf load, which discards all in-RAM config changes since the lastconf write. This works correctly on normal exit, cancellation, and exceptions.Visualisation. A matplotlib summary is shown after the sweep: the left panel shows every
v(t)trace with the selected window highlighted and the linear fit overlaid; the right panel shows theτ vs αscatter plot with the global fit line and 95% CI band, coloured consistently with the left panel.