From b56598b847181dab5a0ad678239d4770c1e983e1 Mon Sep 17 00:00:00 2001 From: diyarajon Date: Wed, 28 Feb 2024 17:24:06 -0600 Subject: [PATCH 01/10] Adjusted return for PedalsRead and case handling for above and below thresholds --- Drivers/Inc/Pedals.h | 2 +- Drivers/Src/Pedals.c | 25 ++++++++++++++----------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/Drivers/Inc/Pedals.h b/Drivers/Inc/Pedals.h index 45232b924..f0f9e04ed 100644 --- a/Drivers/Inc/Pedals.h +++ b/Drivers/Inc/Pedals.h @@ -32,7 +32,7 @@ void PedalsInit(void); * @param pedal_t pedal, ACCELERATOR or BRAKE as defined in enum * @return distance the pedal has been pressed in percentage */ -int8_t PedalsRead(Pedal pedal); +uint8_t PedalsRead(Pedal pedal); #endif diff --git a/Drivers/Src/Pedals.c b/Drivers/Src/Pedals.c index 770e3fa9a..d0f4d828d 100644 --- a/Drivers/Src/Pedals.c +++ b/Drivers/Src/Pedals.c @@ -37,26 +37,29 @@ void PedalsInit() { BspAdcInit(); } * @param Pedal, ACCELERATOR or BRAKE as defined in enum * @return percent amount the pedal has been pressed in percentage */ -int8_t PedalsRead(Pedal pedal) { +uint8_t PedalsRead(Pedal pedal) { if (pedal >= kNumberOfPedals) { return 0; } + int16_t millivolts_pedal = - (int16_t)BspAdcGetMillivoltage((pedal == kAccelerator) ? kCh10 : kCh11); + BspAdcGetMillivoltage((pedal == kAccelerator) ? kCh10 : kCh11); - int8_t percentage = 0; - - if (millivolts_pedal >= kLowerBound[pedal]) { - percentage = (int8_t)((int32_t)(millivolts_pedal - kLowerBound[pedal]) * - 100 / (kUpperBound[pedal] - kLowerBound[pedal])); - } - - if (percentage > 100) { + // Handle cases above and below bounds + if (millivolts_pedal >= kUpperBound[pedal]){ return 100; } - if (percentage < 0) { + + if (millivolts_pedal <= kLowerBound[pedal]){ return 0; } + uint8_t percentage = 0; + + if (millivolts_pedal > kLowerBound[pedal]) { + percentage = (uint8_t)((millivolts_pedal - kLowerBound[pedal]) * + 100) / (kUpperBound[pedal] - kLowerBound[pedal]); + } + return percentage; } From bf53fceebc2b72df2320e170219d79f9afd2a607 Mon Sep 17 00:00:00 2001 From: diyarajon Date: Wed, 28 Feb 2024 17:56:00 -0600 Subject: [PATCH 02/10] Ran make tidy and format, updated DebugDump variables that use PedalsRead --- Apps/Src/DebugDump.c | 4 ++-- Drivers/Src/Pedals.c | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Apps/Src/DebugDump.c b/Apps/Src/DebugDump.c index 06abe5922..839ff80b4 100644 --- a/Apps/Src/DebugDump.c +++ b/Apps/Src/DebugDump.c @@ -25,10 +25,10 @@ void TaskDebugDump(void *p_arg) { while (1) { // Get pedal information - int8_t accel_pedal = PedalsRead(kAccelerator); + uint8_t accel_pedal = PedalsRead(kAccelerator); printf("kAccelerator: %d\n\r", accel_pedal); - int8_t brake_pedal = PedalsRead(kBrake); + uint8_t brake_pedal = PedalsRead(kBrake); printf("kBrake: %d\n\r", brake_pedal); // Get minion information diff --git a/Drivers/Src/Pedals.c b/Drivers/Src/Pedals.c index d0f4d828d..89fe42fe5 100644 --- a/Drivers/Src/Pedals.c +++ b/Drivers/Src/Pedals.c @@ -41,24 +41,24 @@ uint8_t PedalsRead(Pedal pedal) { if (pedal >= kNumberOfPedals) { return 0; } - + int16_t millivolts_pedal = BspAdcGetMillivoltage((pedal == kAccelerator) ? kCh10 : kCh11); // Handle cases above and below bounds - if (millivolts_pedal >= kUpperBound[pedal]){ + if (millivolts_pedal >= kUpperBound[pedal]) { return 100; } - if (millivolts_pedal <= kLowerBound[pedal]){ + if (millivolts_pedal <= kLowerBound[pedal]) { return 0; } uint8_t percentage = 0; if (millivolts_pedal > kLowerBound[pedal]) { - percentage = (uint8_t)((millivolts_pedal - kLowerBound[pedal]) * - 100) / (kUpperBound[pedal] - kLowerBound[pedal]); + percentage = (uint8_t)((millivolts_pedal - kLowerBound[pedal]) * 100) / + (kUpperBound[pedal] - kLowerBound[pedal]); } return percentage; From 4afa7bd7e4dcf205d4fc11cbb06836ad0279b0b1 Mon Sep 17 00:00:00 2001 From: diyarajon Date: Wed, 28 Feb 2024 18:01:45 -0600 Subject: [PATCH 03/10] Added comments to clarify code --- Drivers/Src/Pedals.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Drivers/Src/Pedals.c b/Drivers/Src/Pedals.c index 89fe42fe5..fa44cd0b5 100644 --- a/Drivers/Src/Pedals.c +++ b/Drivers/Src/Pedals.c @@ -56,6 +56,7 @@ uint8_t PedalsRead(Pedal pedal) { uint8_t percentage = 0; + // Converts from millivolts to percent using pedal bounds if (millivolts_pedal > kLowerBound[pedal]) { percentage = (uint8_t)((millivolts_pedal - kLowerBound[pedal]) * 100) / (kUpperBound[pedal] - kLowerBound[pedal]); From e29cb6f33b034172b5b8d88fc4c43a8e6c884b9c Mon Sep 17 00:00:00 2001 From: diyarajon Date: Wed, 28 Feb 2024 18:21:14 -0600 Subject: [PATCH 04/10] Removed a condition that checked if percent is greater than millivolts read to get the percent --- Drivers/Src/Pedals.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Drivers/Src/Pedals.c b/Drivers/Src/Pedals.c index fa44cd0b5..bb6338382 100644 --- a/Drivers/Src/Pedals.c +++ b/Drivers/Src/Pedals.c @@ -57,10 +57,8 @@ uint8_t PedalsRead(Pedal pedal) { uint8_t percentage = 0; // Converts from millivolts to percent using pedal bounds - if (millivolts_pedal > kLowerBound[pedal]) { - percentage = (uint8_t)((millivolts_pedal - kLowerBound[pedal]) * 100) / - (kUpperBound[pedal] - kLowerBound[pedal]); - } + percentage = (uint8_t)((millivolts_pedal - kLowerBound[pedal]) * 100) / + (kUpperBound[pedal] - kLowerBound[pedal]); return percentage; } From fff821b07c7fc2ce65f094ba19de5971eb9d2e04 Mon Sep 17 00:00:00 2001 From: diyarajon Date: Wed, 28 Feb 2024 18:34:04 -0600 Subject: [PATCH 05/10] Needed another set of parenthesis --- Drivers/Src/Pedals.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Drivers/Src/Pedals.c b/Drivers/Src/Pedals.c index bb6338382..0b7cb4efc 100644 --- a/Drivers/Src/Pedals.c +++ b/Drivers/Src/Pedals.c @@ -57,8 +57,8 @@ uint8_t PedalsRead(Pedal pedal) { uint8_t percentage = 0; // Converts from millivolts to percent using pedal bounds - percentage = (uint8_t)((millivolts_pedal - kLowerBound[pedal]) * 100) / - (kUpperBound[pedal] - kLowerBound[pedal]); + percentage = (uint8_t)(((millivolts_pedal - kLowerBound[pedal]) * 100) / + (kUpperBound[pedal] - kLowerBound[pedal])); return percentage; } From 3c2a87faf851c04643b6fd108f45efd090a3ec62 Mon Sep 17 00:00:00 2001 From: diyarajon Date: Wed, 28 Feb 2024 18:37:48 -0600 Subject: [PATCH 06/10] Make tidy and format --- Drivers/Src/Pedals.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Drivers/Src/Pedals.c b/Drivers/Src/Pedals.c index 0b7cb4efc..1dd98872f 100644 --- a/Drivers/Src/Pedals.c +++ b/Drivers/Src/Pedals.c @@ -58,7 +58,7 @@ uint8_t PedalsRead(Pedal pedal) { // Converts from millivolts to percent using pedal bounds percentage = (uint8_t)(((millivolts_pedal - kLowerBound[pedal]) * 100) / - (kUpperBound[pedal] - kLowerBound[pedal])); + (kUpperBound[pedal] - kLowerBound[pedal])); return percentage; } From 891d3641f272bb54a68092d62e25cb10e083706f Mon Sep 17 00:00:00 2001 From: diyarajon Date: Wed, 28 Feb 2024 18:58:07 -0600 Subject: [PATCH 07/10] Addressed review comments - Update BSP_ADC getters to return unsigned ints --- BSP/Inc/BSP_ADC.h | 4 ++-- BSP/STM32F413/Src/BSP_ADC.c | 10 +++++----- Drivers/Src/Pedals.c | 8 ++------ 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/BSP/Inc/BSP_ADC.h b/BSP/Inc/BSP_ADC.h index ccedb41a5..26c5fb909 100644 --- a/BSP/Inc/BSP_ADC.h +++ b/BSP/Inc/BSP_ADC.h @@ -29,7 +29,7 @@ void BspAdcInit(void); * @param hardwareDevice pedal enum that represents the specific device * @return Raw ADC value without conversion */ -int16_t BspAdcGetValue(Adc hardware_device); +uint16_t BspAdcGetValue(Adc hardware_device); /** * @brief Provides the ADC value in millivolts of the channel at the specified @@ -37,7 +37,7 @@ int16_t BspAdcGetValue(Adc hardware_device); * @param hardwareDevice pedal enum that represents the specific device * @return ADC value in millivolts */ -int16_t BspAdcGetMillivoltage(Adc hardware_device); +uint16_t BspAdcGetMillivoltage(Adc hardware_device); #endif diff --git a/BSP/STM32F413/Src/BSP_ADC.c b/BSP/STM32F413/Src/BSP_ADC.c index 2cc562700..b89abed6d 100644 --- a/BSP/STM32F413/Src/BSP_ADC.c +++ b/BSP/STM32F413/Src/BSP_ADC.c @@ -108,11 +108,11 @@ void BspAdcInit(void) { * @param None * @return millivoltage value ADC measurement */ -int16_t BspAdcGetValue(Adc hardware_device) { +uint16_t BspAdcGetValue(Adc hardware_device) { // Get ADC raw data uint16_t data = adc_results[hardware_device]; - return (int16_t)data; + return data; } /** @@ -120,10 +120,10 @@ int16_t BspAdcGetValue(Adc hardware_device) { * @param None * @return millivoltage value ADC measurement */ -int16_t BspAdcGetMillivoltage(Adc hardware_device) { +uint16_t BspAdcGetMillivoltage(Adc hardware_device) { // Get ADC raw data - int16_t data = (int16_t)adc_results[hardware_device]; + uint16_t data = adc_results[hardware_device]; // Convert to millivoltage - return (int16_t)((ADC_RANGE_MILLIVOLTS * data) >> BSP_ADC_PRECISION_BITS); + return (uint16_t)((ADC_RANGE_MILLIVOLTS * data) >> BSP_ADC_PRECISION_BITS); } diff --git a/Drivers/Src/Pedals.c b/Drivers/Src/Pedals.c index 1dd98872f..23d287909 100644 --- a/Drivers/Src/Pedals.c +++ b/Drivers/Src/Pedals.c @@ -42,18 +42,14 @@ uint8_t PedalsRead(Pedal pedal) { return 0; } - int16_t millivolts_pedal = + uint16_t millivolts_pedal = BspAdcGetMillivoltage((pedal == kAccelerator) ? kCh10 : kCh11); - // Handle cases above and below bounds + // Handle case if above bound if (millivolts_pedal >= kUpperBound[pedal]) { return 100; } - if (millivolts_pedal <= kLowerBound[pedal]) { - return 0; - } - uint8_t percentage = 0; // Converts from millivolts to percent using pedal bounds From a9189acc4b8a21bebad8cca7e8ac6d0918dc68b4 Mon Sep 17 00:00:00 2001 From: diyarajon Date: Sat, 2 Mar 2024 13:47:54 -0600 Subject: [PATCH 08/10] Pedals enum brief updated --- Drivers/Inc/Pedals.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Drivers/Inc/Pedals.h b/Drivers/Inc/Pedals.h index f0f9e04ed..5953df20c 100644 --- a/Drivers/Inc/Pedals.h +++ b/Drivers/Inc/Pedals.h @@ -14,8 +14,7 @@ #include "BSP_ADC.h" /** - * @brief Stuff - * + * @brief Pedal enums */ typedef enum { kAccelerator, kBrake, kNumberOfPedals } Pedal; From 8b4014452e3f8eda9e1552c54f1fcbd871c41415 Mon Sep 17 00:00:00 2001 From: diyarajon Date: Sat, 2 Mar 2024 13:49:00 -0600 Subject: [PATCH 09/10] Updated the lower threshold to output 0 and wrote updated test file for pedals --- Drivers/Src/Pedals.c | 4 ++++ Tests/Test_Driver_Pedal_.c | 25 ------------------------- 2 files changed, 4 insertions(+), 25 deletions(-) delete mode 100644 Tests/Test_Driver_Pedal_.c diff --git a/Drivers/Src/Pedals.c b/Drivers/Src/Pedals.c index 23d287909..b53905b03 100644 --- a/Drivers/Src/Pedals.c +++ b/Drivers/Src/Pedals.c @@ -50,6 +50,10 @@ uint8_t PedalsRead(Pedal pedal) { return 100; } + if (millivolts_pedal <= kLowerBound[pedal]) { + return 0; + } + uint8_t percentage = 0; // Converts from millivolts to percent using pedal bounds diff --git a/Tests/Test_Driver_Pedal_.c b/Tests/Test_Driver_Pedal_.c deleted file mode 100644 index 1bf737e02..000000000 --- a/Tests/Test_Driver_Pedal_.c +++ /dev/null @@ -1,25 +0,0 @@ -/** - * Test file for library to interact with the pedal driver file - * - * Run this test in conjunction with the simulator - * GUI. As you move the accelerator and brake on the GUI, the respective - * pressed/slided percentage will change from '0' to '100' on the terminal and display - * to show that sliding the pedals is read by the BSP - * - */ - -#include "common.h" -#include "config.h" -#include "Pedals.h" -#include - - -int main() { - Pedals_Init(); - - while(1) { - printf("Accelerator: %5.1d%%\tBrake: %5.1d%%\r", - Pedals_Read(ACCELERATOR),Pedals_Read(BRAKE)); - } -} - From b3084b46958404373f68397ae67ccd507b07d21f Mon Sep 17 00:00:00 2001 From: diyarajon Date: Sat, 2 Mar 2024 13:49:57 -0600 Subject: [PATCH 10/10] Added comments to clarify bounds --- Drivers/Src/Pedals.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Drivers/Src/Pedals.c b/Drivers/Src/Pedals.c index b53905b03..8e1fa7690 100644 --- a/Drivers/Src/Pedals.c +++ b/Drivers/Src/Pedals.c @@ -45,7 +45,7 @@ uint8_t PedalsRead(Pedal pedal) { uint16_t millivolts_pedal = BspAdcGetMillivoltage((pedal == kAccelerator) ? kCh10 : kCh11); - // Handle case if above bound + // Handle cases if above and below bounds if (millivolts_pedal >= kUpperBound[pedal]) { return 100; }