From 82dfae7387b4e9b6458ecfdede5b388f42eb08de Mon Sep 17 00:00:00 2001 From: DJohns-droid Date: Mon, 27 Oct 2025 17:46:29 -0400 Subject: [PATCH 1/5] Add PedalPressureInterface files and update VCF code --- include/VCF_Constants.h | 4 +++- lib/interfaces/include/PedalPressureInterface.h | 14 ++++++++++++++ lib/interfaces/src/PedalPressureInterface.cpp | 15 +++++++++++++++ src/VCF_Tasks.cpp | 1 + src/main.cpp | 2 ++ 5 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 lib/interfaces/include/PedalPressureInterface.h create mode 100644 lib/interfaces/src/PedalPressureInterface.cpp diff --git a/include/VCF_Constants.h b/include/VCF_Constants.h index 0f33658..ac1fecb 100644 --- a/include/VCF_Constants.h +++ b/include/VCF_Constants.h @@ -40,7 +40,7 @@ constexpr int STEERING_1_CHANNEL = 5; // constexpr int UNUSED_CHANNEL = 7; /* Channels on ADC_2 */ -// constexpr int UNUSED_CHANNEL = 0; +constexpr int PEDAL_PRESSURE_CHANNEL = 0; // constexpr int UNUSED_CHANNEL = 1; constexpr int ACCEL_1_CHANNEL = 2; constexpr int ACCEL_2_CHANNEL = 3; @@ -81,6 +81,8 @@ constexpr float BRAKE_1_SCALE = 1; constexpr float BRAKE_1_OFFSET = 0; constexpr float BRAKE_2_SCALE = 1; constexpr float BRAKE_2_OFFSET = 0; +constexpr float PEDAL_PRESSURE_SCALE = 1; // TODO: Figure out what these should be from calibration/ datasheet +constexpr float PEDAL_PRESSURE_OFFSET = 0; //TODO: Figure out what these should be from calibration/ datasheet diff --git a/lib/interfaces/include/PedalPressureInterface.h b/lib/interfaces/include/PedalPressureInterface.h new file mode 100644 index 0000000..05d29c7 --- /dev/null +++ b/lib/interfaces/include/PedalPressureInterface.h @@ -0,0 +1,14 @@ +#ifndef PEDALPRESSUREINTERFACE_H +#define PEDALPRESSUREINTERFACE_H + +#include "hytech.h" +#include "VCFCANInterfaceImpl.h" + +class PedalPressureInterface { +public: + void send_pedal_pressure_CAN(uint16_t pedal_pressure); + +private: + PEDAL_PRESSURE_t msg_data; +}; +#endif // PEDALPRESSUREINTERFACE_H \ No newline at end of file diff --git a/lib/interfaces/src/PedalPressureInterface.cpp b/lib/interfaces/src/PedalPressureInterface.cpp new file mode 100644 index 0000000..e301309 --- /dev/null +++ b/lib/interfaces/src/PedalPressureInterface.cpp @@ -0,0 +1,15 @@ +#include "PedalPressureInterface.h" + +void PedalPressureInterface::send_pedal_pressure_CAN(uint16_t pedal_pressure) +{ + msg_data.Pedal_Pressure = pedal_pressure; + + uint8_t buf[8]; + uint8_t len = 0; + uint8_t ide = 0; + + Pack_PEDAL_PRESSURE_hytech(&msg_data, buf, &len, &ide); + + // Push to VCF CAN TX buffer + VCFCANInterfaceImpl::VCFCANInterfaceObjectsInstance::instance().main_can_tx_buffer.push_back(buf, len); +} diff --git a/src/VCF_Tasks.cpp b/src/VCF_Tasks.cpp index d82d12c..9811114 100644 --- a/src/VCF_Tasks.cpp +++ b/src/VCF_Tasks.cpp @@ -45,6 +45,7 @@ HT_TASK::TaskResponse run_read_adc2_task(const unsigned long& sysMicros, const H VCFData_sInstance::instance().interface_data.pedal_sensor_data.accel_2 = ADCsOnVCFInstance::instance().adc_2.data.conversions[ACCEL_2_CHANNEL].conversion; VCFData_sInstance::instance().interface_data.pedal_sensor_data.brake_1 = ADCsOnVCFInstance::instance().adc_2.data.conversions[BRAKE_1_CHANNEL].conversion; VCFData_sInstance::instance().interface_data.pedal_sensor_data.brake_2 = ADCsOnVCFInstance::instance().adc_2.data.conversions[BRAKE_2_CHANNEL].conversion; + VCFData_sInstance::instance().interface_data.pedal_sensor_data.pedal_pressure = ADCsOnVCFInstance::instance().adc_2.data.conversions[PEDAL_PRESSURE_CHANNEL].conversion; return HT_TASK::TaskResponse::YIELD; } diff --git a/src/main.cpp b/src/main.cpp index ddf6a9e..d8edb79 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -160,6 +160,8 @@ void setup() { adc_2_offsets[BRAKE_1_CHANNEL] = BRAKE_1_OFFSET; adc_2_scales[BRAKE_2_CHANNEL] = BRAKE_2_SCALE; adc_2_offsets[BRAKE_2_CHANNEL] = BRAKE_2_OFFSET; + adc_2_offsets[PEDAL_PRESSURE_CHANNEL] = PEDAL_PRESSURE_OFFSET; + adc_2_scales[PEDAL_PRESSURE_CHANNEL] = PEDAL_PRESSURE_SCALE; ADCsOnVCFInstance::create(adc_1_scales, adc_1_offsets, adc_2_scales, adc_2_offsets); EthernetIPDefsInstance::create(); From 82b31db1c2ed9fc81bbe512287b0c37b33a8351a Mon Sep 17 00:00:00 2001 From: DJohns-droid Date: Mon, 27 Oct 2025 20:16:39 -0400 Subject: [PATCH 2/5] Include SharedFirmwareTypes.h in PedalPressureInterface --- include/VCF_Constants.h | 4 +++- lib/interfaces/include/PedalPressureInterface.h | 15 +++++++++++++++ lib/interfaces/src/PedalPressureInterface.cpp | 15 +++++++++++++++ src/VCF_Tasks.cpp | 1 + src/main.cpp | 2 ++ 5 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 lib/interfaces/include/PedalPressureInterface.h create mode 100644 lib/interfaces/src/PedalPressureInterface.cpp diff --git a/include/VCF_Constants.h b/include/VCF_Constants.h index 0f33658..ac1fecb 100644 --- a/include/VCF_Constants.h +++ b/include/VCF_Constants.h @@ -40,7 +40,7 @@ constexpr int STEERING_1_CHANNEL = 5; // constexpr int UNUSED_CHANNEL = 7; /* Channels on ADC_2 */ -// constexpr int UNUSED_CHANNEL = 0; +constexpr int PEDAL_PRESSURE_CHANNEL = 0; // constexpr int UNUSED_CHANNEL = 1; constexpr int ACCEL_1_CHANNEL = 2; constexpr int ACCEL_2_CHANNEL = 3; @@ -81,6 +81,8 @@ constexpr float BRAKE_1_SCALE = 1; constexpr float BRAKE_1_OFFSET = 0; constexpr float BRAKE_2_SCALE = 1; constexpr float BRAKE_2_OFFSET = 0; +constexpr float PEDAL_PRESSURE_SCALE = 1; // TODO: Figure out what these should be from calibration/ datasheet +constexpr float PEDAL_PRESSURE_OFFSET = 0; //TODO: Figure out what these should be from calibration/ datasheet diff --git a/lib/interfaces/include/PedalPressureInterface.h b/lib/interfaces/include/PedalPressureInterface.h new file mode 100644 index 0000000..31697a7 --- /dev/null +++ b/lib/interfaces/include/PedalPressureInterface.h @@ -0,0 +1,15 @@ +#ifndef PEDALPRESSUREINTERFACE_H +#define PEDALPRESSUREINTERFACE_H + +#include "hytech.h" +#include "VCFCANInterfaceImpl.h" +#include "SharedFirmwareTypes.h" + +class PedalPressureInterface { +public: + void send_pedal_pressure_CAN(uint16_t pedal_pressure); + +private: + PEDAL_PRESSURE_t msg_data; +}; +#endif // PEDALPRESSUREINTERFACE_H \ No newline at end of file diff --git a/lib/interfaces/src/PedalPressureInterface.cpp b/lib/interfaces/src/PedalPressureInterface.cpp new file mode 100644 index 0000000..e301309 --- /dev/null +++ b/lib/interfaces/src/PedalPressureInterface.cpp @@ -0,0 +1,15 @@ +#include "PedalPressureInterface.h" + +void PedalPressureInterface::send_pedal_pressure_CAN(uint16_t pedal_pressure) +{ + msg_data.Pedal_Pressure = pedal_pressure; + + uint8_t buf[8]; + uint8_t len = 0; + uint8_t ide = 0; + + Pack_PEDAL_PRESSURE_hytech(&msg_data, buf, &len, &ide); + + // Push to VCF CAN TX buffer + VCFCANInterfaceImpl::VCFCANInterfaceObjectsInstance::instance().main_can_tx_buffer.push_back(buf, len); +} diff --git a/src/VCF_Tasks.cpp b/src/VCF_Tasks.cpp index d82d12c..9811114 100644 --- a/src/VCF_Tasks.cpp +++ b/src/VCF_Tasks.cpp @@ -45,6 +45,7 @@ HT_TASK::TaskResponse run_read_adc2_task(const unsigned long& sysMicros, const H VCFData_sInstance::instance().interface_data.pedal_sensor_data.accel_2 = ADCsOnVCFInstance::instance().adc_2.data.conversions[ACCEL_2_CHANNEL].conversion; VCFData_sInstance::instance().interface_data.pedal_sensor_data.brake_1 = ADCsOnVCFInstance::instance().adc_2.data.conversions[BRAKE_1_CHANNEL].conversion; VCFData_sInstance::instance().interface_data.pedal_sensor_data.brake_2 = ADCsOnVCFInstance::instance().adc_2.data.conversions[BRAKE_2_CHANNEL].conversion; + VCFData_sInstance::instance().interface_data.pedal_sensor_data.pedal_pressure = ADCsOnVCFInstance::instance().adc_2.data.conversions[PEDAL_PRESSURE_CHANNEL].conversion; return HT_TASK::TaskResponse::YIELD; } diff --git a/src/main.cpp b/src/main.cpp index ddf6a9e..d8edb79 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -160,6 +160,8 @@ void setup() { adc_2_offsets[BRAKE_1_CHANNEL] = BRAKE_1_OFFSET; adc_2_scales[BRAKE_2_CHANNEL] = BRAKE_2_SCALE; adc_2_offsets[BRAKE_2_CHANNEL] = BRAKE_2_OFFSET; + adc_2_offsets[PEDAL_PRESSURE_CHANNEL] = PEDAL_PRESSURE_OFFSET; + adc_2_scales[PEDAL_PRESSURE_CHANNEL] = PEDAL_PRESSURE_SCALE; ADCsOnVCFInstance::create(adc_1_scales, adc_1_offsets, adc_2_scales, adc_2_offsets); EthernetIPDefsInstance::create(); From cc32e40e4e752590b7740ce8fba0703bdc2802f9 Mon Sep 17 00:00:00 2001 From: DJohns-droid Date: Tue, 28 Oct 2025 20:42:54 -0400 Subject: [PATCH 3/5] Add PedalPressureInterface to send pedal pressure CAN frame using helper --- .../include/PedalPressureInterface.h | 3 --- lib/interfaces/src/PedalPressureInterface.cpp | 26 ++++++++++++++----- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/lib/interfaces/include/PedalPressureInterface.h b/lib/interfaces/include/PedalPressureInterface.h index 31697a7..6fdc118 100644 --- a/lib/interfaces/include/PedalPressureInterface.h +++ b/lib/interfaces/include/PedalPressureInterface.h @@ -8,8 +8,5 @@ class PedalPressureInterface { public: void send_pedal_pressure_CAN(uint16_t pedal_pressure); - -private: - PEDAL_PRESSURE_t msg_data; }; #endif // PEDALPRESSUREINTERFACE_H \ No newline at end of file diff --git a/lib/interfaces/src/PedalPressureInterface.cpp b/lib/interfaces/src/PedalPressureInterface.cpp index e301309..7ded09c 100644 --- a/lib/interfaces/src/PedalPressureInterface.cpp +++ b/lib/interfaces/src/PedalPressureInterface.cpp @@ -1,15 +1,27 @@ #include "PedalPressureInterface.h" +#include // for memcpy + +#define PEDAL_PRESSURE_CANID 0x0CBU +#define PEDAL_PRESSURE_DLC 2U // 16-bit pedal pressure void PedalPressureInterface::send_pedal_pressure_CAN(uint16_t pedal_pressure) { - msg_data.Pedal_Pressure = pedal_pressure; + // Create the CAN frame + CAN_message_t msg; + msg.id = PEDAL_PRESSURE_CANID; + msg.len = PEDAL_PRESSURE_DLC; + msg.flags.extended = 0; // standard frame + msg.buf[0] = static_cast(pedal_pressure & 0xFF); // low byte + msg.buf[1] = static_cast((pedal_pressure >> 8) & 0xFF); // high byte - uint8_t buf[8]; - uint8_t len = 0; - uint8_t ide = 0; + // Copy the message into a raw byte buffer + uint8_t raw_buf[sizeof(CAN_message_t)]; + memcpy(raw_buf, &msg, sizeof(CAN_message_t)); - Pack_PEDAL_PRESSURE_hytech(&msg_data, buf, &len, &ide); + // Push to the VCF CAN TX buffer + auto &can_objs = VCFCANInterfaceImpl::VCFCANInterfaceObjectsInstance::instance(); + can_objs.main_can_tx_buffer.push_back(raw_buf, sizeof(CAN_message_t)); - // Push to VCF CAN TX buffer - VCFCANInterfaceImpl::VCFCANInterfaceObjectsInstance::instance().main_can_tx_buffer.push_back(buf, len); + // Use the helper to send all messages + VCFCANInterfaceImpl::send_all_CAN_msgs(can_objs.main_can_tx_buffer, can_objs.MAIN_CAN); } From eac96dae8ce9ba645d4d0e99e970a81b7ba443da Mon Sep 17 00:00:00 2001 From: DJohns-droid Date: Tue, 28 Oct 2025 21:03:33 -0400 Subject: [PATCH 4/5] Refactor PedalPressureInterface to use constexpr and fix clang-tidy lint errors --- lib/interfaces/src/PedalPressureInterface.cpp | 36 +++++++++++-------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/lib/interfaces/src/PedalPressureInterface.cpp b/lib/interfaces/src/PedalPressureInterface.cpp index 7ded09c..610c71f 100644 --- a/lib/interfaces/src/PedalPressureInterface.cpp +++ b/lib/interfaces/src/PedalPressureInterface.cpp @@ -1,27 +1,35 @@ #include "PedalPressureInterface.h" -#include // for memcpy +#include "VCFCANInterfaceImpl.h" +#include +#include // for memcpy -#define PEDAL_PRESSURE_CANID 0x0CBU -#define PEDAL_PRESSURE_DLC 2U // 16-bit pedal pressure +// Constants +constexpr uint32_t PEDAL_PRESSURE_CANID = 0x0CBU; +constexpr uint8_t PEDAL_PRESSURE_DLC = 2U; // 16-bit pedal pressure +constexpr uint8_t BYTE_MASK = 0xFF; +constexpr size_t CAN_MSG_SIZE = sizeof(CAN_message_t); void PedalPressureInterface::send_pedal_pressure_CAN(uint16_t pedal_pressure) { - // Create the CAN frame - CAN_message_t msg; + // Construct the CAN message + CAN_message_t msg{}; msg.id = PEDAL_PRESSURE_CANID; msg.len = PEDAL_PRESSURE_DLC; - msg.flags.extended = 0; // standard frame - msg.buf[0] = static_cast(pedal_pressure & 0xFF); // low byte - msg.buf[1] = static_cast((pedal_pressure >> 8) & 0xFF); // high byte + msg.flags.extended = 0; - // Copy the message into a raw byte buffer - uint8_t raw_buf[sizeof(CAN_message_t)]; - memcpy(raw_buf, &msg, sizeof(CAN_message_t)); + // Encode the 16-bit pedal pressure + msg.buf[0] = static_cast(pedal_pressure & BYTE_MASK); // low byte + msg.buf[1] = static_cast((pedal_pressure >> 8) & BYTE_MASK); // high byte - // Push to the VCF CAN TX buffer + // Copy to raw buffer for TX queue + uint8_t raw_buf[CAN_MSG_SIZE]; + std::memcpy(static_cast(raw_buf), &msg, CAN_MSG_SIZE); + + // Push to TX buffer using VCF helper auto &can_objs = VCFCANInterfaceImpl::VCFCANInterfaceObjectsInstance::instance(); - can_objs.main_can_tx_buffer.push_back(raw_buf, sizeof(CAN_message_t)); + can_objs.main_can_tx_buffer.push_back(static_cast(raw_buf), CAN_MSG_SIZE); - // Use the helper to send all messages + // Send all messages on the main CAN interface VCFCANInterfaceImpl::send_all_CAN_msgs(can_objs.main_can_tx_buffer, can_objs.MAIN_CAN); } + From 60d8412f6bdf078a0c63b45732dfad0a15f6bd87 Mon Sep 17 00:00:00 2001 From: DJohns-droid Date: Tue, 28 Oct 2025 22:17:43 -0400 Subject: [PATCH 5/5] Fix clang-tidy magic number: add PEDAL_PRESSURE_SHIFT constant --- lib/interfaces/src/PedalPressureInterface.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/interfaces/src/PedalPressureInterface.cpp b/lib/interfaces/src/PedalPressureInterface.cpp index 610c71f..63ca1f0 100644 --- a/lib/interfaces/src/PedalPressureInterface.cpp +++ b/lib/interfaces/src/PedalPressureInterface.cpp @@ -8,6 +8,8 @@ constexpr uint32_t PEDAL_PRESSURE_CANID = 0x0CBU; constexpr uint8_t PEDAL_PRESSURE_DLC = 2U; // 16-bit pedal pressure constexpr uint8_t BYTE_MASK = 0xFF; constexpr size_t CAN_MSG_SIZE = sizeof(CAN_message_t); +constexpr uint8_t PEDAL_PRESSURE_SHIFT = 8U; + void PedalPressureInterface::send_pedal_pressure_CAN(uint16_t pedal_pressure) { @@ -19,7 +21,7 @@ void PedalPressureInterface::send_pedal_pressure_CAN(uint16_t pedal_pressure) // Encode the 16-bit pedal pressure msg.buf[0] = static_cast(pedal_pressure & BYTE_MASK); // low byte - msg.buf[1] = static_cast((pedal_pressure >> 8) & BYTE_MASK); // high byte + msg.buf[1] = static_cast((pedal_pressure >> PEDAL_PRESSURE_SHIFT) & BYTE_MASK); // high byte // Copy to raw buffer for TX queue uint8_t raw_buf[CAN_MSG_SIZE];