Skip to content

Add unit tests for the LQR controller's Riccati solution and gain - #345

Open
Hung1510 wants to merge 1 commit into
ShisatoYano:mainfrom
Hung1510:lqr-riccati-tests
Open

Hung1510 wants to merge 1 commit into
ShisatoYano:mainfrom
Hung1510:lqr-riccati-tests

Conversation

@Hung1510

Copy link
Copy Markdown
Contributor

Part of #103. Closes #341.

What

New test/test_lqr_controller.py, using the A and B that LqrController builds itself (speed/time step: 5 m/s/0.1 s, 10 m/s/0.1 s, 2 m/s/0.05 s):

  • _solve_riccati_equation(A, B) matches scipy.linalg.solve_discrete_are(A, B, Q, R) with the controller's WEIGHT_MAT_Q and WEIGHT_MAT_R
  • the solution is symmetric positive definite
  • _calculate_control_gain(A, B) matches K = (BᵀXB + R)⁻¹ BᵀXA from the scipy solution

Tolerance: 1% of the largest entry. The loop stops once a step changes X by under THRESHOLD = 0.01, which leaves X about 0.1 from the scipy solution (largest entries 160–350). The measured gap is under 0.08% for X and about 0.002 for K.

How I checked it works

  • pytest test/test_lqr_controller.py: 9 passed.
  • Cutting the solver to 1 iteration (MAX_ITERATION = 1) makes the scipy-comparison tests fail, so they catch a wrong solution.
  • . run_test_suites.sh: 212 passed.
  • Only a test file is added; no src/ change.

Check _solve_riccati_equation against scipy.linalg.solve_discrete_are
and _calculate_control_gain against the gain from the scipy solution,
for the A and B the controller builds, within 1% (the iteration stops
at a 0.01 step).

Part of ShisatoYano#103

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unit tests: the Riccati solution of the LQR controller

1 participant