Skip to content

Commit 87a1aa9

Browse files
chore: lqr simulator (#95)
* lqr simulator * improve code * fix copilot comments * fix comments * process comments
1 parent b40e5b9 commit 87a1aa9

33 files changed

Lines changed: 1785 additions & 6 deletions

‎.github/copilot-instructions.md‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,42 @@ When implementing or modifying algorithms:
7373
- Document numerical properties and limitations
7474
- Keep documentation in sync with code changes
7575

76+
## Design Principles
77+
78+
These are the most important architectural directives in this codebase. Every new class, function, and CMake target must follow them.
79+
80+
### SOLID Principles
81+
82+
- **Single Responsibility (SRP)**: Each class and struct owns exactly one concern. Separate configuration into sub-structs by concern (e.g., `WeightsConfig`, `SimulationConfig`, `PlantParameters`) rather than flat mega-structs. Each source file should address a single topic.
83+
- **Open/Closed (OCP)**: Prefer templates and compile-time polymorphism for extending algorithms to new numeric types (`float`, `Q15`, `Q31`) without modifying existing code.
84+
- **Liskov Substitution (LSP)**: When using abstract base classes (e.g., `Plant`), all derived classes must be fully substitutable. Override every pure virtual method; do not weaken preconditions or strengthen postconditions.
85+
- **Interface Segregation (ISP)**: Keep interfaces small and focused. Do not force implementers to depend on methods they do not use.
86+
- **Dependency Inversion (DIP)**: High-level modules (simulators, controllers) depend on abstractions, not concrete implementations. Use constructor injection for dependencies. Pass interfaces, not concrete types, when multiple implementations exist.
87+
88+
### DRY (Don't Repeat Yourself)
89+
90+
- **NEVER duplicate logic**. If two functions share more than a few lines of identical code, extract a common helper, template, or base class.
91+
- Use templates to eliminate per-type or per-size code duplication (e.g., DOF-parameterized dynamics instead of copy-pasted `StepDof2()` / `StepDof3()`).
92+
- Use `std::numbers::pi_v<float>` consistently — never hardcode `3.14159265f` or similar constants.
93+
- Share configuration structs across layers instead of re-declaring equivalent types.
94+
- Reuse existing utility components (e.g., `TimeSeriesChartWidget`, `SignalGenerator`) rather than reimplementing equivalent widgets.
95+
96+
### Small Functions
97+
98+
- **Every function should do one thing and fit on one screen** (~30 lines max, hard limit ~50 lines).
99+
- Extract named helpers from long methods — the name acts as self-documentation.
100+
- Rendering code should delegate to focused `Draw*()` helpers (e.g., `DrawTrack()`, `DrawCart()`, `DrawPole()`).
101+
- Prefer returning values over mutating shared state — this makes functions easier to test and reason about.
102+
103+
### CMake Reuse
104+
105+
- **Simulator structure**: Every simulator follows the three-layer pattern: `application/` (logic) → `view/` (Qt GUI) → `Main.cpp` (entry point), each with its own `CMakeLists.txt` and library target.
106+
- **Shared utilities**: Link against `numerical.simulator.utils` for common widgets (`TimeSeriesChartWidget`, `SignalGenerator`) — do not duplicate them.
107+
- **Shared abstractions**: Link against `numerical.simulator.controllers.common` for shared controller interfaces (e.g., `Plant`).
108+
- **Naming convention**: Targets follow `numerical.simulator.<domain>.<algorithm>.<layer>` (e.g., `numerical.simulator.controllers.lqr.application`).
109+
- **Library code reuse**: Use `numerical_add_header_library()` and `numerical_add_coverage_sources()` from `cmake/NumericalHeaderLibrary.cmake` for all `numerical/` targets.
110+
- **Compiler options blocks**: Every simulator target repeats the same MSVC/GCC warning suppression — keep this consistent across all targets.
111+
76112
## Coding Style and Patterns
77113

78114
### Dependency Injection

‎.mega-linter.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ PRINT_ALPACA: false
2424
SARIF_REPORTER: true
2525
SHOW_SKIPPED_LINTERS: false
2626
JSON_JSONLINT_ARGUMENTS: --mode cjson
27-
CPP_CPPCHECK_ARGUMENTS: --suppress=normalCheckLevelMaxBranches
27+
CPP_CPPCHECK_ARGUMENTS: --suppress=normalCheckLevelMaxBranches --library=qt

‎.vscode/launch.json‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,26 @@
6161
}
6262
]
6363
},
64+
{
65+
"name": "LQR Cart-Pole Simulator",
66+
"type": "cppdbg",
67+
"request": "launch",
68+
"program": "${workspaceFolder}/build/host/simulator/controllers/LqrCartPole/Debug/numerical.simulator.controllers.lqr_cart_pole",
69+
"args": [],
70+
"stopAtEntry": false,
71+
"cwd": "${workspaceFolder}",
72+
"environment": [],
73+
"externalConsole": false,
74+
"MIMode": "gdb",
75+
"miDebuggerPath": "/usr/bin/gdb",
76+
"setupCommands": [
77+
{
78+
"description": "Enable pretty-printing for gdb",
79+
"text": "-enable-pretty-printing",
80+
"ignoreFailures": true
81+
}
82+
]
83+
},
6484
{
6585
"name": "Robot Arm Simulator",
6686
"type": "cppdbg",

‎numerical/controllers/implementations/Lqr.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace controllers
66
template class Lqr<float, 1, 1>;
77
template class Lqr<float, 2, 1>;
88
template class Lqr<float, 3, 1>;
9+
template class Lqr<float, 4, 1>;
910
template class Lqr<float, 2, 2>;
1011

1112
template class Lqr<math::Q15, 1, 1>;

‎numerical/controllers/implementations/Lqr.hpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ namespace controllers
9090
extern template class Lqr<float, 1, 1>;
9191
extern template class Lqr<float, 2, 1>;
9292
extern template class Lqr<float, 3, 1>;
93+
extern template class Lqr<float, 4, 1>;
9394
extern template class Lqr<float, 2, 2>;
9495

9596
extern template class Lqr<math::Q15, 1, 1>;

‎numerical/solvers/DiscreteAlgebraicRiccatiEquation.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ namespace solvers
44
{
55
template class DiscreteAlgebraicRiccatiEquation<float, 1, 1>;
66
template class DiscreteAlgebraicRiccatiEquation<float, 2, 1>;
7+
template class DiscreteAlgebraicRiccatiEquation<float, 4, 1>;
78
}

‎numerical/solvers/DiscreteAlgebraicRiccatiEquation.hpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,6 @@ namespace solvers
8787
#ifdef NUMERICAL_TOOLBOX_COVERAGE_BUILD
8888
extern template class DiscreteAlgebraicRiccatiEquation<float, 1, 1>;
8989
extern template class DiscreteAlgebraicRiccatiEquation<float, 2, 1>;
90+
extern template class DiscreteAlgebraicRiccatiEquation<float, 4, 1>;
9091
#endif
9192
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
add_subdirectory(common)
2+
add_subdirectory(LqrCartPole)
13
add_subdirectory(Mpc)
24
add_subdirectory(PidController)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
find_package(Qt6 REQUIRED COMPONENTS Widgets)
2+
3+
add_subdirectory(application)
4+
add_subdirectory(view)
5+
6+
add_executable(numerical.simulator.controllers.lqr_cart_pole)
7+
emil_build_for(numerical.simulator.controllers.lqr_cart_pole HOST All PREREQUISITE_BOOL NUMERICAL_TOOLBOX_BUILD_SIMULATOR)
8+
9+
target_link_libraries(numerical.simulator.controllers.lqr_cart_pole PRIVATE
10+
numerical.simulator.controllers.lqr.application
11+
numerical.simulator.controllers.lqr.view
12+
)
13+
14+
target_sources(numerical.simulator.controllers.lqr_cart_pole PRIVATE
15+
Main.cpp
16+
)
17+
18+
if(MSVC)
19+
target_compile_options(numerical.simulator.controllers.lqr_cart_pole PRIVATE /W4 /wd4244 /wd4100)
20+
else()
21+
target_compile_options(numerical.simulator.controllers.lqr_cart_pole PRIVATE
22+
-Wno-error
23+
)
24+
endif()
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include "simulator/controllers/LqrCartPole/view/LqrMainWindow.hpp"
2+
#include <QApplication>
3+
4+
int main(int argc, char* argv[])
5+
{
6+
QApplication app(argc, argv);
7+
8+
simulator::controllers::lqr::view::LqrMainWindow window;
9+
window.show();
10+
11+
return app.exec();
12+
}

0 commit comments

Comments
 (0)