Skip to content

Commit

Permalink
Brake Pedal Position and Brake Pedal Light Logic (#288)
Browse files Browse the repository at this point in the history
* wrote brake_pedal_light

* Implemented recommended edits to functions and split brake_pedal_lights into two functions

* Changed line 14, the return statement, as requested

* removed unneeded import
  • Loading branch information
jabrap1 authored Nov 5, 2024
1 parent eec7ed6 commit 790ce02
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
24 changes: 24 additions & 0 deletions firmware/shared/controls/brake_pedal_lights.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once
#include <algorithm>

namespace ctrl {

template <typename T>
T CalculateBrakePedalPosition(bool rear_brake_pedal_error,
bool front_brake_pedal_error,
T rear_brake_pedal_position,
T front_brake_pedal_position) {
// Brake Error Handling
if (rear_brake_pedal_error || front_brake_pedal_error) {
return static_cast<T>(0);
} else {
return std::max(front_brake_pedal_position, rear_brake_pedal_position);
}
}

template <typename T>
bool DetectBrakeLight(T brake_pedal_position) {
return (brake_pedal_position > 0.1);
}

} // namespace ctrl
40 changes: 40 additions & 0 deletions firmware/shared/controls/brake_pedal_lights_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "brake_pedal_lights.h"

#include <iostream>

#include "testing.h"

int main() {
using namespace ctrl;

// Test Case 1: CalculateBrakePedalPosition with No Errors and Edge Values
ASSERT_CLOSE(CalculateBrakePedalPosition(false, false, 0.0f, 0.0f), 0.0f);

ASSERT_CLOSE(CalculateBrakePedalPosition(false, false, 1.0f, 0.0f), 1.0f);

ASSERT_CLOSE(CalculateBrakePedalPosition(false, false, 0.5f, 0.7f), 0.7f);

// Test Case 2: CalculateBrakePedalPosition with Brake Error
ASSERT_CLOSE(CalculateBrakePedalPosition(true, false, 0.8f, 0.9f), 0.0f);

ASSERT_CLOSE(CalculateBrakePedalPosition(false, true, 0.8f, 0.9f), 0.0f);

ASSERT_CLOSE(CalculateBrakePedalPosition(true, true, 0.8f, 0.9f), 0.0f);

// Test Case 3: DetectBrakeLight with Values Just Below and Above the
// Threshold
ASSERT_FALSE(DetectBrakeLight(0.05f)); // Below threshold

ASSERT_FALSE(DetectBrakeLight(0.09f)); // Near threshold

ASSERT_TRUE(DetectBrakeLight(0.11f)); // Just above threshold

// Test Case 4: DetectBrakeLight with High Pedal Positions
ASSERT_TRUE(DetectBrakeLight(0.5f));

ASSERT_TRUE(DetectBrakeLight(1.0f));

std::cout << "All test cases passed!" << std::endl;

return 0;
}

0 comments on commit 790ce02

Please sign in to comment.