Skip to content

Commit

Permalink
moved read brake and throttle into other file
Browse files Browse the repository at this point in the history
  • Loading branch information
AnjiniVerdia committed Nov 3, 2024
1 parent 20758d9 commit 34a5d20
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 32 deletions.
5 changes: 5 additions & 0 deletions PowerBoard/include/main.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <mbed.h>
#include <rtos.h>

extern AnalogIn throttle_pedal;
extern AnalogIn brake_pedal;
3 changes: 2 additions & 1 deletion PowerBoard/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
add_library(PowerBoard-lib
src/PowerCANInterface.cpp
src/MotorInterface.cpp
src/ReadFiles.cpp
)

target_include_directories(PowerBoard-lib PUBLIC include)

target_link_libraries(PowerBoard-lib mbed-os-lib Common-lib)
target_link_libraries(PowerBoard-lib mbed-os-lib Common-lib)
5 changes: 5 additions & 0 deletions PowerBoard/lib/include/ReadFiles.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <mbed.h>
#include <rtos.h>

uint16_t read_throttle();
uint16_t read_brake();
42 changes: 42 additions & 0 deletions PowerBoard/lib/src/ReadFiles.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <mbed.h>
#include <rtos.h>
#include "main.h"

#define THROTTLE_LOW_VOLTAGE 0.66
#define THROTTLE_LOW_VOLTAGE_BUFFER 0.20
#define THROTTLE_HIGH_VOLTAGE 3.08
#define THROTTLE_HIGH_VOLTAGE_BUFFER 0.10



// Reads the throttle pedal value and returns a uint16_t
uint16_t read_throttle() {
float adjusted_throttle_input =
((throttle_pedal.read_voltage() - THROTTLE_LOW_VOLTAGE -
THROTTLE_LOW_VOLTAGE_BUFFER) /
(THROTTLE_HIGH_VOLTAGE - THROTTLE_HIGH_VOLTAGE_BUFFER -
THROTTLE_LOW_VOLTAGE - THROTTLE_LOW_VOLTAGE_BUFFER));
if (adjusted_throttle_input <= 0.0f) {
return 0;
} else if (adjusted_throttle_input >= 1.0f) {
return 256;
} else {
return (uint16_t)(adjusted_throttle_input * 256.0);
}
}

// Reads the brake pedal value and returns a uint16_t
uint16_t read_brake() {
float adjusted_brake_input =
((brake_pedal.read_voltage() - THROTTLE_LOW_VOLTAGE -
THROTTLE_LOW_VOLTAGE_BUFFER) /
(THROTTLE_HIGH_VOLTAGE - THROTTLE_HIGH_VOLTAGE_BUFFER -
THROTTLE_LOW_VOLTAGE - THROTTLE_LOW_VOLTAGE_BUFFER));
if (adjusted_brake_input <= 0.0f) {
return 0;
} else if (adjusted_brake_input >= 1.0f) {
return 256;
} else {
return (uint16_t)(adjusted_brake_input * 256.0);
}
}
33 changes: 2 additions & 31 deletions PowerBoard/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <rtos.h>
#include <MotorInterface.h>
#include <events/EventQueue.h>
#include "ReadFiles.h"
#include "main.h"

#define LOG_LEVEL LOG_ERROR

Expand Down Expand Up @@ -89,37 +91,6 @@ void signal_flash_handler() {
}
}

// Reads the throttle pedal value and returns a uint16_t
uint16_t read_throttle() {
float adjusted_throttle_input =
((throttle_pedal.read_voltage() - THROTTLE_LOW_VOLTAGE -
THROTTLE_LOW_VOLTAGE_BUFFER) /
(THROTTLE_HIGH_VOLTAGE - THROTTLE_HIGH_VOLTAGE_BUFFER -
THROTTLE_LOW_VOLTAGE - THROTTLE_LOW_VOLTAGE_BUFFER));
if (adjusted_throttle_input <= 0.0f) {
return 0;
} else if (adjusted_throttle_input >= 1.0f) {
return 256;
} else {
return (uint16_t)(adjusted_throttle_input * 256.0);
}
}

// Reads the brake pedal value and returns a uint16_t
uint16_t read_brake() {
float adjusted_brake_input =
((brake_pedal.read_voltage() - THROTTLE_LOW_VOLTAGE -
THROTTLE_LOW_VOLTAGE_BUFFER) /
(THROTTLE_HIGH_VOLTAGE - THROTTLE_HIGH_VOLTAGE_BUFFER -
THROTTLE_LOW_VOLTAGE - THROTTLE_LOW_VOLTAGE_BUFFER));
if (adjusted_brake_input <= 0.0f) {
return 0;
} else if (adjusted_brake_input >= 1.0f) {
return 256;
} else {
return (uint16_t)(adjusted_brake_input * 256.0);
}
}

/**
* Sets the throttle and regen values based on the regen and throttle formula
Expand Down

0 comments on commit 34a5d20

Please sign in to comment.