Skip to content

Commit

Permalink
Forgot to update update_low_pass function calls to call alpha using p…
Browse files Browse the repository at this point in the history
…ass-by-reference
  • Loading branch information
taisirhassan committed Nov 14, 2024
1 parent a3a5a48 commit ae58fc5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
5 changes: 3 additions & 2 deletions include/low_pass_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ static inline double low_pass_alpha(double TR, double time_diff_ms) {
return (freq * TR / 5.0) / (1.0 + freq * TR / 5.0);
}

// Initializes the low-pass filter by calculating and storing the alpha value based on the given response time.
// Initializes the low-pass filter by calculating and storing the alpha value based on the given
// response time.
w_status_t low_pass_filter_init(double *alpha, double response_time);

// Updates the low-pass filter with a new value and returns the operation status
w_status_t update_low_pass(double *alpha, uint16_t new_input_value, double *low_pass_value);
w_status_t update_low_pass(double alpha, uint16_t new_input_value, double *low_pass_value);

#endif /* ROCKETLIB_LOW_PASS_FILTER_H */
18 changes: 9 additions & 9 deletions unit_tests/low_pass_filter_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,32 @@ void test_update_low_pass() {
for (int i = 1; i <= iterations; ++i) {
// Store the previous low_pass_value before updating
double previous_low_pass = low_pass_value;

// Calculate the expected value based on the low-pass filter formula
double expected = (alpha * new_input_value) + ((1.0 - alpha) * previous_low_pass);

// Update the low-pass filter with the fixed input value
result = update_low_pass(&alpha, new_input_value, &low_pass_value);
result = update_low_pass(alpha, new_input_value, &low_pass_value);
w_assert(result == W_SUCCESS);

// Print the current iteration and values
printf(
"Iteration %2d: Low-Pass Value = %.4f | Expected ~ %.4f\n", i, low_pass_value, expected
);

// Verify the correctness of the low-pass update
w_assert(fabs(low_pass_value - expected) < EPSILON);
}

printf("\nAll iterations completed successfully.\n\n");

// Test with NULL alpha
result = update_low_pass(NULL, new_input_value, &low_pass_value);
// Test with alpha out of range
result = update_low_pass(-0.1, new_input_value, &low_pass_value);
w_assert(result == W_INVALID_PARAM);
printf("Invalid alpha (negative) test passed.\n");

result = update_low_pass(1.1, new_input_value, &low_pass_value);
w_assert(result == W_INVALID_PARAM);
printf("Null alpha test passed.\n");
printf("Invalid alpha (greater than 1) test passed.\n");

// Test with NULL low_pass_value
result = update_low_pass(&alpha, new_input_value, NULL);
result = update_low_pass(alpha, new_input_value, NULL);
w_assert(result == W_INVALID_PARAM);
printf("Null low_pass_value test passed.\n");
}
Expand Down

0 comments on commit ae58fc5

Please sign in to comment.