From 34ce0be2897b839f31d925bec5224365840b523f Mon Sep 17 00:00:00 2001 From: maxda Date: Wed, 2 Apr 2025 19:37:53 -0500 Subject: [PATCH 1/5] CAN for powertrain --- embedded-pio | 2 +- include/canPowertrain.h | 7 +++++++ src/canPowertrain.cpp | 28 +++++++++++++++++++++++++++- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/embedded-pio b/embedded-pio index 584946b..7163822 160000 --- a/embedded-pio +++ b/embedded-pio @@ -1 +1 @@ -Subproject commit 584946b7d3ce0853d22b285396bb2ed8f3bee7a3 +Subproject commit 7163822407f165e2ef49d13a0b9832665a582128 diff --git a/include/canPowertrain.h b/include/canPowertrain.h index 0af4121..6ccca4f 100644 --- a/include/canPowertrain.h +++ b/include/canPowertrain.h @@ -4,6 +4,13 @@ #include "canmanager.h" #include "IOManagement.h" +#define I_OUT_12V_TELEM_ID 0x500 +#define V_OUT_12V_TELEM_ID 0x501 +#define SUPP_I_TELEM_ID 0x502 +#define BATT_I_TELEM_ID 0x503 +#define SUPP_V_TELEM_ID 0x504 +#define POWERTRAIN_BOOLS 0x505 + class CANPowertrain : public CANManager { public: CANPowertrain(CAN_TypeDef* canPort, CAN_PINS pins, int frequency = DEFAULT_CAN_FREQ); diff --git a/src/canPowertrain.cpp b/src/canPowertrain.cpp index 688efa7..1d98534 100644 --- a/src/canPowertrain.cpp +++ b/src/canPowertrain.cpp @@ -2,10 +2,36 @@ CANPowertrain::CANPowertrain(CAN_TypeDef* canPort, CAN_PINS pins, int frequency) : CANManager(canPort, pins, frequency) {}; -void CANPowertrain::readHandler(CAN_message_t msg) { +volatile bool mcu_battery_enable = false; +void CANPowertrain::readHandler(CAN_message_t msg) { + uint8_t *data = msg.buf; + switch(msg.id){ + case POWERTRAIN_BOOLS: + mcu_battery_enable = (data[0] >> 5) & 1; + break; + default: + break; + } } void CANPowertrain::sendPowertrainData() { // TODO: send messages with their respective CAN IDs + this->sendMessage(I_OUT_12V_TELEM_ID, (void*)&i_out_12v, sizeof(float)); + this->sendMessage(V_OUT_12V_TELEM_ID, (void*)&v_out_12v, sizeof(float)); + this->sendMessage(SUPP_I_TELEM_ID, (void*)&supp_i, sizeof(float)); + this->sendMessage(BATT_I_TELEM_ID, (void*)&batt_i, sizeof(float)); + this->sendMessage(SUPP_V_TELEM_ID, (void*)&supp_v, sizeof(float)); + + u_int8_t boolByte = 0; + boolByte |= (batt_neg_cont << 0); + boolByte |= (estop_mcu << 1); + boolByte |= (batt_pos_cont << 2); + boolByte |= (ppc1_supp_invalid << 3); + boolByte |= (ppc1_dcdc_invalid << 4); + boolByte |= (mppt_cont_mcu << 6); + boolByte |= (mc_cont_mcu << 7); + + this->sendMessage(POWERTRAIN_BOOLS, (void*)&boolByte, sizeof(u_int8_t)); + } \ No newline at end of file From 55d584067aab1cf4f2ef11612025351144394e01 Mon Sep 17 00:00:00 2001 From: maxda Date: Thu, 3 Apr 2025 02:53:12 -0500 Subject: [PATCH 2/5] switch to stm32_can --- embedded-pio | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/embedded-pio b/embedded-pio index 7163822..584946b 160000 --- a/embedded-pio +++ b/embedded-pio @@ -1 +1 @@ -Subproject commit 7163822407f165e2ef49d13a0b9832665a582128 +Subproject commit 584946b7d3ce0853d22b285396bb2ed8f3bee7a3 From 56aacceed3128beafa2fbad2ecb3dc1e70ba549b Mon Sep 17 00:00:00 2001 From: Will Wentink Date: Wed, 2 Apr 2025 19:43:04 -0500 Subject: [PATCH 3/5] IOManagement draft --- include/IOManagement.h | 35 ++++++++++++++++++++++++++++++----- src/IOManagement.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 5 deletions(-) diff --git a/include/IOManagement.h b/include/IOManagement.h index 5f22850..400804d 100644 --- a/include/IOManagement.h +++ b/include/IOManagement.h @@ -3,17 +3,42 @@ #include #include "STM32TimerInterrupt_Generic.h" +#include "adc.h" + +// Inputs +#define BATT_NEG_CONT_MCU PB3 +#define ESTOP_MCU PA9 +#define BATT_POS_CONT_MCU PA10 +#define PPC1_SUPP_INVALID PA12 +#define PPC1_DCDC_INVALID PB0 +#define MPPT_CONT_MCU PB5 +#define MC_CONT_MCU PB4 + +// Outputs +#define MCU_BATT_EN PA8 + +#define IO_UPDATE_PERIOD 100000 // us -// TODO: add macros for digital pins struct Digital_Data { - // TODO: add the digital signals - bool batt_neg_cont : 1; + bool batt_neg_cont : 1; // input + bool estop_mcu : 1; // input + bool batt_pos_cont : 1; // input + bool ppc1_supp_invalid : 1; // input + bool ppc1_dcdc_invalid : 1; // input + bool mcu_batt_en : 1; // output + bool mppt_cont_mcu : 1; // input + bool mc_cont_mcu : 1; // input }; extern volatile Digital_Data digital_data; -// TODO: add extern volatile floats for analog signals. +// Analog signals +extern volatile float i_12v; +extern volatile float v_12v; +extern volatile float supp_i; +extern volatile float batt_i; +extern volatile float supp_v; // initialize digital and analog pins void initIO(); @@ -22,6 +47,6 @@ void initIO(); void readIO(); // Set the value of output pins -// TODO: add setter functions for output pins. +void set_mcu_batt_en(bool batt_en); #endif diff --git a/src/IOManagement.cpp b/src/IOManagement.cpp index 54953f8..518fdff 100644 --- a/src/IOManagement.cpp +++ b/src/IOManagement.cpp @@ -2,13 +2,53 @@ volatile Digital_Data digital_data; +volatile float i_12v = 0; +volatile float v_12v = 0; +volatile float supp_i = 0; +volatile float batt_i = 0; +volatile float supp_v = 0; + // Ticker to poll input readings at fixed rate STM32TimerInterrupt IOTimer(TIM2); void initIO() { + // Initalize digital pins + pinMode(BATT_NEG_CONT_MCU, INPUT); + pinMode(ESTOP_MCU, INPUT); + pinMode(BATT_POS_CONT_MCU, INPUT); + pinMode(PPC1_SUPP_INVALID, INPUT); + pinMode(PPC1_DCDC_INVALID, INPUT); + pinMode(MCU_BATT_EN, OUTPUT); + pinMode(MPPT_CONT_MCU, INPUT); + pinMode(MC_CONT_MCU, INPUT); + + // Initialize analog pins + initADC(ADC1); + if (IOTimer.attachInterruptInterval(IO_UPDATE_PERIOD, readIO)) { + printf("IO timer started\n"); + } else { + printf("Failed to start IO timer\n"); + } } void readIO() { + digital_data.batt_neg_cont = digitalRead(BATT_NEG_CONT_MCU); + digital_data.estop_mcu = digitalRead(ESTOP_MCU); + digital_data.batt_pos_cont = digitalRead(BATT_POS_CONT_MCU); + digital_data.ppc1_supp_invalid = digitalRead(PPC1_SUPP_INVALID); + digital_data.ppc1_dcdc_invalid = digitalRead(PPC1_DCDC_INVALID); + digital_data.mppt_cont_mcu = digitalRead(MPPT_CONT_MCU); + digital_data.mc_cont_mcu = digitalRead(MC_CONT_MCU); + + i_12v = readADC(ADC_CHANNEL_12); // PA7 + v_12v = readADC(ADC_CHANNEL_11); // PA6 + supp_i = readADC(ADC_CHANNEL_9); // PA4 + batt_i = readADC(ADC_CHANNEL_6); // PA1 + supp_v = readADC(ADC_CHANNEL_5); // PA0 +} +void set_mcu_batt_en(bool batt_en) { + digitalWrite(MCU_BATT_EN, batt_en); + digital_data.mcu_batt_en = batt_en; } \ No newline at end of file From 08231aeb23511fc8fff2f820487141c3303930eb Mon Sep 17 00:00:00 2001 From: maxda Date: Fri, 4 Apr 2025 00:03:36 -0500 Subject: [PATCH 4/5] simplify code --- include/canPowertrain.h | 7 ------- src/canPowertrain.cpp | 28 +++++++++------------------- 2 files changed, 9 insertions(+), 26 deletions(-) diff --git a/include/canPowertrain.h b/include/canPowertrain.h index 6ccca4f..0af4121 100644 --- a/include/canPowertrain.h +++ b/include/canPowertrain.h @@ -4,13 +4,6 @@ #include "canmanager.h" #include "IOManagement.h" -#define I_OUT_12V_TELEM_ID 0x500 -#define V_OUT_12V_TELEM_ID 0x501 -#define SUPP_I_TELEM_ID 0x502 -#define BATT_I_TELEM_ID 0x503 -#define SUPP_V_TELEM_ID 0x504 -#define POWERTRAIN_BOOLS 0x505 - class CANPowertrain : public CANManager { public: CANPowertrain(CAN_TypeDef* canPort, CAN_PINS pins, int frequency = DEFAULT_CAN_FREQ); diff --git a/src/canPowertrain.cpp b/src/canPowertrain.cpp index 1d98534..a663583 100644 --- a/src/canPowertrain.cpp +++ b/src/canPowertrain.cpp @@ -2,36 +2,26 @@ CANPowertrain::CANPowertrain(CAN_TypeDef* canPort, CAN_PINS pins, int frequency) : CANManager(canPort, pins, frequency) {}; -volatile bool mcu_battery_enable = false; +//volatile bool mcu_battery_enable = false; void CANPowertrain::readHandler(CAN_message_t msg) { - uint8_t *data = msg.buf; + /*uint8_t *data = msg.buf; switch(msg.id){ case POWERTRAIN_BOOLS: mcu_battery_enable = (data[0] >> 5) & 1; break; default: break; - } + }*/ } void CANPowertrain::sendPowertrainData() { // TODO: send messages with their respective CAN IDs - this->sendMessage(I_OUT_12V_TELEM_ID, (void*)&i_out_12v, sizeof(float)); - this->sendMessage(V_OUT_12V_TELEM_ID, (void*)&v_out_12v, sizeof(float)); - this->sendMessage(SUPP_I_TELEM_ID, (void*)&supp_i, sizeof(float)); - this->sendMessage(BATT_I_TELEM_ID, (void*)&batt_i, sizeof(float)); - this->sendMessage(SUPP_V_TELEM_ID, (void*)&supp_v, sizeof(float)); - - u_int8_t boolByte = 0; - boolByte |= (batt_neg_cont << 0); - boolByte |= (estop_mcu << 1); - boolByte |= (batt_pos_cont << 2); - boolByte |= (ppc1_supp_invalid << 3); - boolByte |= (ppc1_dcdc_invalid << 4); - boolByte |= (mppt_cont_mcu << 6); - boolByte |= (mc_cont_mcu << 7); - - this->sendMessage(POWERTRAIN_BOOLS, (void*)&boolByte, sizeof(u_int8_t)); + this->sendMessage(0x500, (void*)&i_out_12v, sizeof(float)); + this->sendMessage(0x501, (void*)&v_out_12v, sizeof(float)); + this->sendMessage(0x502, (void*)&supp_i, sizeof(float)); + this->sendMessage(0x503, (void*)&batt_i, sizeof(float)); + this->sendMessage(0x504, (void*)&supp_v, sizeof(float)); + this->sendMessage(0x505, (void*)&digital_data, sizeof(digital_data)); } \ No newline at end of file From d0bdcc4e621ab3ae791465047d1d14263a40ea0d Mon Sep 17 00:00:00 2001 From: germilla <87410524+Mightyman14386@users.noreply.github.com> Date: Mon, 10 Nov 2025 19:52:47 -0600 Subject: [PATCH 5/5] Updated pins + fixes --- include/IOManagement.h | 4 +--- src/IOManagement.cpp | 2 -- src/canPowertrain.cpp | 4 ++-- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/include/IOManagement.h b/include/IOManagement.h index 400804d..bea9cd4 100644 --- a/include/IOManagement.h +++ b/include/IOManagement.h @@ -6,10 +6,9 @@ #include "adc.h" // Inputs -#define BATT_NEG_CONT_MCU PB3 #define ESTOP_MCU PA9 #define BATT_POS_CONT_MCU PA10 -#define PPC1_SUPP_INVALID PA12 +#define PPC1_SUPP_INVALID PB1 #define PPC1_DCDC_INVALID PB0 #define MPPT_CONT_MCU PB5 #define MC_CONT_MCU PB4 @@ -21,7 +20,6 @@ struct Digital_Data { - bool batt_neg_cont : 1; // input bool estop_mcu : 1; // input bool batt_pos_cont : 1; // input bool ppc1_supp_invalid : 1; // input diff --git a/src/IOManagement.cpp b/src/IOManagement.cpp index 518fdff..88d9824 100644 --- a/src/IOManagement.cpp +++ b/src/IOManagement.cpp @@ -13,7 +13,6 @@ STM32TimerInterrupt IOTimer(TIM2); void initIO() { // Initalize digital pins - pinMode(BATT_NEG_CONT_MCU, INPUT); pinMode(ESTOP_MCU, INPUT); pinMode(BATT_POS_CONT_MCU, INPUT); pinMode(PPC1_SUPP_INVALID, INPUT); @@ -33,7 +32,6 @@ void initIO() { } void readIO() { - digital_data.batt_neg_cont = digitalRead(BATT_NEG_CONT_MCU); digital_data.estop_mcu = digitalRead(ESTOP_MCU); digital_data.batt_pos_cont = digitalRead(BATT_POS_CONT_MCU); digital_data.ppc1_supp_invalid = digitalRead(PPC1_SUPP_INVALID); diff --git a/src/canPowertrain.cpp b/src/canPowertrain.cpp index a663583..e579438 100644 --- a/src/canPowertrain.cpp +++ b/src/canPowertrain.cpp @@ -17,8 +17,8 @@ void CANPowertrain::readHandler(CAN_message_t msg) { void CANPowertrain::sendPowertrainData() { // TODO: send messages with their respective CAN IDs - this->sendMessage(0x500, (void*)&i_out_12v, sizeof(float)); - this->sendMessage(0x501, (void*)&v_out_12v, sizeof(float)); + this->sendMessage(0x500, (void*)&i_12v, sizeof(float)); + this->sendMessage(0x501, (void*)&v_12v, sizeof(float)); this->sendMessage(0x502, (void*)&supp_i, sizeof(float)); this->sendMessage(0x503, (void*)&batt_i, sizeof(float)); this->sendMessage(0x504, (void*)&supp_v, sizeof(float));