Skip to content

Fix lack of test coverage for angle_meas scaling in subaru.h#3084

Closed
jacobwaller wants to merge 4 commits intocommaai:masterfrom
jacobwaller:mutant-fix
Closed

Fix lack of test coverage for angle_meas scaling in subaru.h#3084
jacobwaller wants to merge 4 commits intocommaai:masterfrom
jacobwaller:mutant-fix

Conversation

@jacobwaller
Copy link
Copy Markdown
Contributor

Validation

  • Dongle ID:
  • Route:

@github-actions github-actions bot added the car safety vehicle-specific safety code label Jan 29, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Testing 79 segments for: SUBARU_ASCENT, SUBARU_OUTBACK, SUBARU_LEGACY, SUBARU_IMPREZA, SUBARU_IMPREZA_2020, SUBARU_FORESTER, SUBARU_OUTBACK_PREGLOBAL, SUBARU_OUTBACK_PREGLOBAL_2018, SUBARU_FORESTER_2022

Results: 79 passed, 0 with diffs, 0 errors

int angle_meas_new = (GET_BYTES(msg, 4, 2) & 0xFFFFU);
// convert Steering_Torque -> Steering_Angle to centidegrees, to match the ES_LKAS_ANGLE angle request units
angle_meas_new = ROUND(to_signed(angle_meas_new, 16) * -2.17);
angle_meas_new = ROUND(-2.17 * to_signed(angle_meas_new, 16));
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This change is not needed, but is there to demonstrate that the tests added cover the mutant tests for this line

Feel free to accept this suggestion to undo the change:

Suggested change
angle_meas_new = ROUND(-2.17 * to_signed(angle_meas_new, 16));
angle_meas_new = ROUND(to_signed(angle_meas_new, 16) * -2.17);

@jacobwaller jacobwaller changed the title Fix laack of test coverage for angle_meas scaling in subaru.h Fix lack of test coverage for angle_meas scaling in subaru.h Jan 29, 2026
values = {"Cruise_Activated": enable}
return self.packer.make_can_msg_safety("CruiseControl", self.ALT_MAIN_BUS, values)

def test_angle_meas_scaling(self):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Don't we have a test to make sure angle_meas_new is parsed properly in common.py?

Copy link
Copy Markdown
Contributor Author

@jacobwaller jacobwaller Jan 29, 2026

Choose a reason for hiding this comment

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

I'm not really sure if that's the intention of those tests as they don't catch the mutation:
Changing only one line to trigger a diff in opendbc master:

❯ git diff
diff --git a/opendbc/safety/modes/subaru.h b/opendbc/safety/modes/subaru.h
index 9b01d39d..6871cf06 100644
--- a/opendbc/safety/modes/subaru.h
+++ b/opendbc/safety/modes/subaru.h
@@ -101,7 +101,7 @@ static void subaru_rx_hook(const CANPacket_t *msg) {
 
     int angle_meas_new = (GET_BYTES(msg, 4, 2) & 0xFFFFU);
     // convert Steering_Torque -> Steering_Angle to centidegrees, to match the ES_LKAS_ANGLE angle request units
-    angle_meas_new = ROUND(to_signed(angle_meas_new, 16) * -2.17);
+    angle_meas_new = ROUND(-2.17 * to_signed(angle_meas_new, 16));
     update_sample(&angle_meas, angle_meas_new);
   }

Results in the mutation tests failing as before:

/Users/jacobwaller/Documents/personal/comma/opendbc/opendbc/safety/modes/subaru.h:104:34: warning: Survived: Replaced * with / [cxx_mul_to_div]
    angle_meas_new = ROUND(-2.17 * to_signed(angle_meas_new, 16));
                                 ^

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Oh I see, that steering angle test is only being done for angle-based cars

class AngleSteeringSafetyTest(VehicleSpeedSafetyTest):

... other code

  def test_steering_angle_measurements(self):
    self._common_measurement_test(self._angle_meas_msg, -self.STEER_ANGLE_MAX, self.STEER_ANGLE_MAX, self.DEG_TO_CAN,
                                  self.safety.get_angle_meas_min, self.safety.get_angle_meas_max)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Alright @sshane I updated to use the helper in common.py

One note -- I wanted to just add this to all the cars, but not everything has an easily accessible STEER_ANGLE_MAX or a way to grab the angle_meas. In fact, from what I can see, some cars don't even have angle_meas in safety (hyundai.h for example)? I might be missing something there though.

As a workaround, I just added it in test_subaru.py

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If the safety test/mode is for angle, it should subclass from that base angle test

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The case we're looking at here is for torque-based, I'm hesitant to subclass from that because subarus don't support angle based steering yet.

Copy link
Copy Markdown
Contributor Author

@jacobwaller jacobwaller Jan 31, 2026

Choose a reason for hiding this comment

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

Hey @sshane , sorry to ping you again :). I refactored the scaling check into a dedicated test (AngleScalingSafetyTest) and made both AngleSteeringSafetyTest and TestSubaruSafetyBase inherit from it.

This should keep these tests a little more DRY, let me know if this is kind of what you were thinking

@jacobwaller jacobwaller requested a review from sshane January 29, 2026 23:03
Jacob Waller added 2 commits January 29, 2026 17:48
@sshane
Copy link
Copy Markdown
Contributor

sshane commented Jan 31, 2026

I see we merged a PR to add angle-based signals before we actually added angle safety... commaai/panda#1528 Then we never merged commaai/panda#1533

@sshane
Copy link
Copy Markdown
Contributor

sshane commented Jan 31, 2026

AngleSteeringSafetyTest should catch this in #2864. I'll check out why it doesn't

@sshane sshane closed this Jan 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

car safety vehicle-specific safety code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants