Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PedalsRead returning signed int #419

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
4 changes: 2 additions & 2 deletions Apps/Src/DebugDump.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions BSP/Inc/BSP_ADC.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ 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
* index
* @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

Expand Down
10 changes: 5 additions & 5 deletions BSP/STM32F413/Src/BSP_ADC.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,22 +108,22 @@ 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;
}

/**
* @brief Gets converted ADC value in units of mV.
* @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);
}
2 changes: 1 addition & 1 deletion Drivers/Inc/Pedals.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
24 changes: 11 additions & 13 deletions Drivers/Src/Pedals.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,24 @@ 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);

int8_t percentage = 0;
uint16_t millivolts_pedal =
BspAdcGetMillivoltage((pedal == kAccelerator) ? kCh10 : kCh11);
diyarajon marked this conversation as resolved.
Show resolved Hide resolved

if (millivolts_pedal >= kLowerBound[pedal]) {
percentage = (int8_t)((int32_t)(millivolts_pedal - kLowerBound[pedal]) *
100 / (kUpperBound[pedal] - kLowerBound[pedal]));
}

if (percentage > 100) {
// Handle case if above bound
if (millivolts_pedal >= kUpperBound[pedal]) {
return 100;
}
if (percentage < 0) {
return 0;
}

uint8_t percentage = 0;

// Converts from millivolts to percent using pedal bounds
percentage = (uint8_t)(((millivolts_pedal - kLowerBound[pedal]) * 100) /
(kUpperBound[pedal] - kLowerBound[pedal]));

return percentage;
}
Loading