-
Notifications
You must be signed in to change notification settings - Fork 119
Fix jerk smoothing: correct finite difference formula and loop bounds #499
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Co-authored-by: rjoomen <[email protected]>
Co-authored-by: rjoomen <[email protected]>
Co-authored-by: rjoomen <[email protected]>
|
@copilot, can you explain the math behind the velocity, acceleration and jerk formulas and what you fixed? |
The formulas use finite difference approximations to compute derivatives from discrete position samples. Velocity (1st derivative) - uses 2 consecutive positions: This is a forward difference approximation. For N timesteps, we get N-1 velocity values. Acceleration (2nd derivative) - uses 3 consecutive positions: This is the central difference formula for the second derivative. For N timesteps, we get N-2 acceleration values. Jerk (3rd derivative) - uses 4 consecutive positions: This is the forward difference formula for the third derivative. For N timesteps, we get N-3 jerk values. What I fixed: The old jerk formula incorrectly used 5 points with coefficients The corrected formula uses the standard binomial coefficients |
|
@copilot what do the lines with comment "offset to center about 0" do? |
The For example, if you want the velocity to be 2.0 m/s (the target), the code computes: By subtracting the target, the error becomes:
This makes it easier to apply constraints (error = 0 for equality) or costs (minimize error²) since the optimization is working with deviations from the desired target rather than absolute values. |
|
@copilot Is it possible to use central differences instead of forward differences? Do not implement, just explain. |
Yes, central differences are possible and generally more accurate. However, there are tradeoffs: Forward difference (current):
Central difference:
Practical considerations:
The forward difference approach is a reasonable engineering choice that prioritizes simplicity and consistent coverage of the trajectory. |
.gitignore
Outdated
| @@ -0,0 +1 @@ | |||
| build/ | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove your edits of this file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed in commit 3061138.
Co-authored-by: rjoomen <[email protected]>
|
@copilot Can you speculate on the reasoning behind the previous jerk formula? |
The old formula
However, this doesn't match any standard finite difference formula. The standard 5-point central difference for the 3rd derivative would be Possible origins:
The |
|
@copilot can you add a unit test to the existing ones that verifies velocity, acceleration, and jerk are correct first, second, and third derivatives? |
Co-authored-by: rjoomen <[email protected]>
Added in commit 4a8855a. The test uses a cubic polynomial trajectory (x=t³) where the analytical derivatives are known:
The test verifies that the finite difference formulas correctly compute these derivatives and that we get the expected number of values (N-1, N-2, N-3) for each derivative order. |
|
@copilot Can you fix the formatting of your work? See the output of the clang_format CI run for details. Also, the repo has a .clang-format file with formatting rules. |
Co-authored-by: rjoomen <[email protected]>
Fixed in commit be8b38a. Ran clang-format on all modified files using the repository's .clang-format configuration. |
Fix Jerk Smoothing Implementation ✅
Analysis Complete
Issues Found and Fixed
Loop bounds bug: Changed
last_step_ - 4tolast_step_ - 3(4 locations)Formula bug: Fixed finite difference coefficients (4 locations)
-1/2, 1, 0, -1, 1/2❌-1, 3, -3, 1✓Variable indices: Use 4 points
(i, i+1, i+2, i+3)instead of 5 ✓Documentation: Updated comments for expected counts (3 locations) ✓
Bug fix: Fixed variable naming in
JointJerkIneqConstraint::value()✓Implementation Complete
JointJerkEqCostconstructorJointJerkIneqCostconstructorJointJerkEqConstraintconstructorJointJerkIneqConstraintconstructorfinite_difference_derivativestest to verify all three finite difference formulasFiles Changed
trajopt/src/trajectory_costs.cpp- Fixed jerk implementationstrajopt/include/trajopt/trajectory_costs.hpp- Updated documentationtrajopt/test/joint_costs_unit.cpp- Added unit test to verify derivative formulasSummary
Fixed two critical bugs in jerk smoothing that made it inconsistent with velocity and acceleration. The implementation now uses the correct mathematical formula and generates the right number of constraints. Added comprehensive unit test that uses a cubic polynomial trajectory (x=t³) to verify that velocity, acceleration, and jerk finite difference formulas correctly compute the 1st, 2nd, and 3rd derivatives with known analytical values.
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.