Skip to content

Conversation

@cowsed
Copy link
Contributor

@cowsed cowsed commented Jan 26, 2026

Description

Add simple matrix and cmsis-dsp matrix to airbrakes project. Also stub out implementation of 'simple' kalman filter. Subject to change as the model changes but im working to avoid 10000000 line PRs because i love my reviewers

The matrix files could be added as 'library' level files but given the rate of change for airbrakes and the fact that we have no use for the matrices elsewhere, I believe the should live in the app/ for airbrakes. There is also a chance that once the filter design has solidified, it makes sense to implement the entire control system as one function with direct calls to the cmsis matrix library. However, bc readability is important i have the matrix types. If the time comes, the 'optimized' version with direct calls can be implemented with the 'readable' version acting as a reference to verify functionality.

Based on the super cool specs we have been given / drawn up
image

Type of change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

sw_board tests for determining matrix correctness and speeds on arm w/ cmsis
native_sim tests for ability to test independently of hardware
control_freak hardware in case all the other hardware explodes

sw_board

Size Opts
Addition Manual: (4x4 + 4x4) 16 us, 1142 cycles
Addition CMSIS:  (4x4 + 4x4) 16 us, 1179 cycles
Multiply Manual: (4x4 * 4x1) 25 us, 1828 cycles
Multiply CMSIS:  (4x4 * 4x1) 23 us, 1647 cycles

Speed Opts
Addition Manual: (4x4 + 4x4) 15 us, 1107 cycles
Addition CMSIS:  (4x4 + 4x4) 15 us, 1088 cycles
Multiply Manual: (4x4 * 4x1) 24 us, 1708 cycles
Multiply CMSIS:  (4x4 * 4x1) 21 us, 1507 cycles

these dang arm engineers seem to know a thing or two.

Checklist:

  • New functionality is documented in the necessary spots (i.e new functions documented in the header)
  • Unit tests cover any new functionality or edge cases that the PR was meant to resolve (if applicable)
  • The CI checks are passing
  • I reviewed my own code in the GitHub diff and am sure that each change is intentional
  • I feel comfortable about this code flying in a rocket

modernize-*,
performance-*,
portability-*,
-portability-avoid-pragma-once,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pragma once is awesome and ill be really sad if The Powers That Be say we shouldnt use it
(all the compilers we have ever used for our hardware and native sim support it wholly and completely [sdcc doesnt so no stm8s but also none of our code would work on an stm8 lol])

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can do pragma once tbh. I do it in our other codebases lol. I just stuck with the standard for Zephyr when we started out

};

namespace ManualMatrixTests {
constexpr ManualMatrix<2, 2> testMult2x2x2() {
Copy link
Contributor Author

@cowsed cowsed Jan 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can verify that the type checking for matrix dimensions is all good at compile time. hurray for modern c++ versions

Copy link
Contributor Author

@cowsed cowsed Jan 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could also static_assert their results but that was extra work

@cowsed cowsed marked this pull request as ready for review January 26, 2026 21:53
@cowsed cowsed requested a review from a team as a code owner January 26, 2026 21:53
Copy link
Member

@AarC10 AarC10 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks pretty good. Just drop the sample for benchmarking for repro and also I'm curious about your error handling. I'm looking at the return status and maybe we should alert the user that their inputs are bad?
https://arm-software.github.io/CMSIS_5/DSP/html/arm__math__types_8h.html#a5e459c6409dfcd2927bb8a57491d7cf6

@cowsed
Copy link
Contributor Author

cowsed commented Jan 27, 2026

Looks pretty good. Just drop the sample for benchmarking for repro and also I'm curious about your error handling. I'm looking at the return status and maybe we should alert the user that their inputs are bad? https://arm-software.github.io/CMSIS_5/DSP/html/arm__math__types_8h.html#a5e459c6409dfcd2927bb8a57491d7cf6

For the functions implemented here (add, subtract, multiply) the template static_asserts prevent the conditions that would bring about the error codes. Even multiplication and addition by NaN is defined.

For things like decompositions and inverses, there has to be some sort of error handling since they can fail and if/when implemented they will include return codes std::options or something.
Also, trying to get a return code into operator overloads kinda doesnt work and i think the readability of operators is well worth it given the guarantees from the template/type checker.

@cowsed
Copy link
Contributor Author

cowsed commented Jan 27, 2026

Benchmark included in the math directory not as a sample bc of reasons stated in PR description about current narrow-applicability. But it is there for testing as work is done for airbrakes and should lend itself pretty easily to turning into a 'real' sample later

(also im on campus with no hardware so theres a chance that doesn't work in which case don't approve and ill fix it tn but it does compile)

@cowsed cowsed requested a review from AarC10 January 27, 2026 15:39
@cowsed cowsed merged commit 36d8323 into main Jan 29, 2026
6 checks passed
@cowsed cowsed deleted the cowsed/airbrake_kalman_filter branch January 29, 2026 00:01
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.

3 participants