From 77904e3560c5e4ca941db5a8e2cba694a2667d81 Mon Sep 17 00:00:00 2001 From: Ari Wasch Date: Wed, 2 Feb 2022 23:33:34 -0800 Subject: [PATCH 1/6] Created BMS module --- CMakeLists.txt | 1 + rover-apps/bms_2022/CMakeLists.txt | 25 +++++++ rover-apps/bms_2022/include/AppConfig.h | 12 ++++ rover-apps/bms_2022/include/BMSMonitoring.h | 32 +++++++++ rover-apps/bms_2022/src/BMSMonitoring.cpp | 68 +++++++++++++++++++ supported_build_configurations.yaml | 3 + .../TARGET_PDB_REV2_2021/include/PinNames.h | 15 ++-- 7 files changed, 152 insertions(+), 4 deletions(-) create mode 100644 rover-apps/bms_2022/CMakeLists.txt create mode 100644 rover-apps/bms_2022/include/AppConfig.h create mode 100644 rover-apps/bms_2022/include/BMSMonitoring.h create mode 100644 rover-apps/bms_2022/src/BMSMonitoring.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 0a747dc2..3be5b3b4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -91,6 +91,7 @@ endfunction() ## Include Rover Apps add_subdirectory(${ROVER_APPS_DIR}/common) add_app_subdirectory(${ROVER_APPS_DIR}/arm_2021) +add_app_subdirectory(${ROVER_APPS_DIR}/bms_2022) add_app_subdirectory(${ROVER_APPS_DIR}/gamepad_2021) add_app_subdirectory(${ROVER_APPS_DIR}/gimbal_2021) add_app_subdirectory(${ROVER_APPS_DIR}/pdb_2021) diff --git a/rover-apps/bms_2022/CMakeLists.txt b/rover-apps/bms_2022/CMakeLists.txt new file mode 100644 index 00000000..06f893b2 --- /dev/null +++ b/rover-apps/bms_2022/CMakeLists.txt @@ -0,0 +1,25 @@ +add_library(BMS_Monitoring STATIC) +target_sources(BMS_Monitoring PRIVATE src/BMSMonitoring.cpp) +target_include_directories(BMS_Monitoring PUBLIC + include + ${ROVER_APPS_DIR}/common/include + ) +target_link_libraries(BMS_Monitoring + PRIVATE + Logger + mbed-os + ) + +add_executable(bms_2022) +target_sources(bms_2022 PRIVATE ${ROVER_APPS_DIR}/common/src/main.cpp) +target_include_directories(bms_2022 PUBLIC include ${ROVER_APPS_DIR}/common/include) +target_link_libraries(bms_2022 + PRIVATE + CANBus + CANMsg + Logger + uwrt-mars-rover-hw-bridge + #Modules + BMS_Monitoring + ) +mbed_set_post_build(bms_2022) diff --git a/rover-apps/bms_2022/include/AppConfig.h b/rover-apps/bms_2022/include/AppConfig.h new file mode 100644 index 00000000..b1b66d8a --- /dev/null +++ b/rover-apps/bms_2022/include/AppConfig.h @@ -0,0 +1,12 @@ +#pragma once + +#include + +#include "Module.h" +#include "BMSMonitoring.h" + +BMSMonitoring BMS_Monitoring; + +std::vector gModules = { + &BMS_Monitoring, +}; diff --git a/rover-apps/bms_2022/include/BMSMonitoring.h b/rover-apps/bms_2022/include/BMSMonitoring.h new file mode 100644 index 00000000..434c7bd5 --- /dev/null +++ b/rover-apps/bms_2022/include/BMSMonitoring.h @@ -0,0 +1,32 @@ +#pragma once + +#include "Module.h" +#include "mbed.h" + +/*This PDB module is for load, rail and temperature monitoring.*/ + +class BMSMonitoring final : public Module { + public: + /* Sets the Load DIAG_EN pins */ + BMSMonitoring(); + + void periodic_1s(void) override; + void periodic_10s(void) override {} + void periodic_100ms(void) override {} + void periodic_10ms(void) override {} + void periodic_1ms(void) override; + + private: + void current_monitoring(); + void cell_monitoring(); + + SPI spi; + DigitalOut chip_select; + int cell_voltages[12]; + DigitalOut buzzer_en; + AnalogIn currentADC; + bool is_cell_balancing = false; + int coloumb_count = 0; + const int battery_capacity = 22000; // mAh + int battery_percent; +}; \ No newline at end of file diff --git a/rover-apps/bms_2022/src/BMSMonitoring.cpp b/rover-apps/bms_2022/src/BMSMonitoring.cpp new file mode 100644 index 00000000..8f0138d3 --- /dev/null +++ b/rover-apps/bms_2022/src/BMSMonitoring.cpp @@ -0,0 +1,68 @@ +#include "BMSMonitoring.h" + +#include + +#include "Logger.h" + +BMSMonitoring::BMSMonitoring() { + buzzer_en(BUZZER_EN); + currentADC(CURR_ANLG_IN); + spi(SDI, SDO, CLK); // mosi, miso, sclk + chip_select(SAMPL); + chip_select = 1; + spi.format(24,3); + spi.frequency(1000000); +} + +void BMSMonitoring::cell_monitoring() { + chip_select = 0; + //is_cell_balancing = getRXSignal + int spi_message = 0b000000000000000000000000; + if(is_cell_balancing){ + spi_message = 0b111111111111111100000000; + } + + for(int i = 0; i < 16; i++){ + cell_voltages[i] = spi.write(spi_message); + spi_message += 0b10000; + } + //setTXSignalValue(HWBRIDGE::CANID msgID, HWBRIDGE::CANSIGNAL signalName, HWBRIDGE::CANSignalValue_t signalValue); + + + //if voltage is too low on one cell, enable balancing on that cell + //only way to balance is to keep all fets on so cells always have same voltage +} + +void BMSMonitoring::current_monitoring() { + + float current = (currentADC.read_voltage() * 20) -33; + if(current < 0){ + current = 0; + }else if(current > 29){ + current = 29.1; + } + current = current * 1000; //converts Amps to MilliAmps + coloumb_count += current * 0.001; //converts to MilliAmp Seconds + int battery_capacity_seconds = battery_capacity * 3600; //converts to MilliAmp Seconds + + battery_percent = (1 - (coloumb_count / battery_capacity_seconds)) * 100; + + if(battery_percent < 20){ + buzzer_en = true; + }else{ + buzzer_en = false; + } +} + +void BMSMonitoring::periodic_1s() { + +} + +void BMSMonitoring::periodic_1ms() { + current_monitoring(); +} + + +//TODO: +// make it work with CAN +// voltage look-up table \ No newline at end of file diff --git a/supported_build_configurations.yaml b/supported_build_configurations.yaml index 0a385884..3ff066c1 100644 --- a/supported_build_configurations.yaml +++ b/supported_build_configurations.yaml @@ -16,6 +16,9 @@ pdb_2021: science_2021: - SCIENCE_REV2_2021 +bms_2022: + - PDB_REV2_2021 + # Test Apps test-aeat-6012: - UWRT_NUCLEO diff --git a/targets/TARGET_PDB_REV2_2021/include/PinNames.h b/targets/TARGET_PDB_REV2_2021/include/PinNames.h index 94e25d90..e947bb5e 100644 --- a/targets/TARGET_PDB_REV2_2021/include/PinNames.h +++ b/targets/TARGET_PDB_REV2_2021/include/PinNames.h @@ -169,7 +169,6 @@ typedef enum { /**** Voltage Measurement ****/ RAIL_5V_ANLG_IN = PA_4, RAIL_17V_ANLG_IN = PA_5, - RAIL_24V_ANLG_IN = PB_0, RAIL_BATTERY_ANLG_IN = PB_1, RAIL_24V_PGOOD_N = PC_6, @@ -192,9 +191,17 @@ typedef enum { LOAD5_5V_DIAG_EN = PB_8, LOAD5_5V_FAULT = PB_9, - LOAD_17V_DIAG_EN = PC_4, - LOAD_17V_FAULT = PC_5, - + // LOAD_17V_DIAG_EN = PC_4, + // LOAD_17V_FAULT = PC_5, + + /**** BMS ****/ + BUZZER_EN = PC_5, + CURR_ANLG_IN = PC_8, + CLK = PB_7, + SDI = PB_8, + SDO = PB_9, + SAMPL = PB_10, + /**** LED Matrix ****/ LED_MATRIX_R_CHANNEL = PB_4, LED_MATRIX_G_CHANNEL = PB_5, From ac465023527e677a4bd1da75fcd29c9a80060a0e Mon Sep 17 00:00:00 2001 From: Ari Wasch Date: Thu, 3 Feb 2022 20:20:02 -0800 Subject: [PATCH 2/6] Made BMS code compile --- rover-apps/bms_2022/include/BMSMonitoring.h | 4 ---- rover-apps/bms_2022/src/BMSMonitoring.cpp | 22 ++++++++++++--------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/rover-apps/bms_2022/include/BMSMonitoring.h b/rover-apps/bms_2022/include/BMSMonitoring.h index 434c7bd5..bb713101 100644 --- a/rover-apps/bms_2022/include/BMSMonitoring.h +++ b/rover-apps/bms_2022/include/BMSMonitoring.h @@ -20,11 +20,7 @@ class BMSMonitoring final : public Module { void current_monitoring(); void cell_monitoring(); - SPI spi; - DigitalOut chip_select; int cell_voltages[12]; - DigitalOut buzzer_en; - AnalogIn currentADC; bool is_cell_balancing = false; int coloumb_count = 0; const int battery_capacity = 22000; // mAh diff --git a/rover-apps/bms_2022/src/BMSMonitoring.cpp b/rover-apps/bms_2022/src/BMSMonitoring.cpp index 8f0138d3..26ac37f6 100644 --- a/rover-apps/bms_2022/src/BMSMonitoring.cpp +++ b/rover-apps/bms_2022/src/BMSMonitoring.cpp @@ -3,19 +3,22 @@ #include #include "Logger.h" +#include "mbed.h" + +SPI spi(SDI, SDO, CLK); // mosi, miso, sclk +AnalogIn currentADC(CURR_ANLG_IN); +DigitalOut buzzer_en(BUZZER_EN); +DigitalOut chip_select(SAMPL); BMSMonitoring::BMSMonitoring() { - buzzer_en(BUZZER_EN); - currentADC(CURR_ANLG_IN); - spi(SDI, SDO, CLK); // mosi, miso, sclk - chip_select(SAMPL); - chip_select = 1; - spi.format(24,3); - spi.frequency(1000000); + buzzer_en.write(0); + chip_select.write(1); + spi.format(24,3); + spi.frequency(1000000); } void BMSMonitoring::cell_monitoring() { - chip_select = 0; + chip_select.write(0); //is_cell_balancing = getRXSignal int spi_message = 0b000000000000000000000000; if(is_cell_balancing){ @@ -28,6 +31,7 @@ void BMSMonitoring::cell_monitoring() { } //setTXSignalValue(HWBRIDGE::CANID msgID, HWBRIDGE::CANSIGNAL signalName, HWBRIDGE::CANSignalValue_t signalValue); + chip_select.write(1); //if voltage is too low on one cell, enable balancing on that cell //only way to balance is to keep all fets on so cells always have same voltage @@ -35,7 +39,7 @@ void BMSMonitoring::cell_monitoring() { void BMSMonitoring::current_monitoring() { - float current = (currentADC.read_voltage() * 20) -33; + float current = (currentADC.read() * 20) -33; if(current < 0){ current = 0; }else if(current > 29){ From 9d0cd0d43dbba4b87b9f92ae69ba528b19455034 Mon Sep 17 00:00:00 2001 From: Ari Wasch Date: Sat, 5 Feb 2022 20:56:39 -0800 Subject: [PATCH 3/6] Added voltage lookup, statusing, and LED matrix --- rover-apps/bms_2022/include/BMSMonitoring.h | 10 +- rover-apps/bms_2022/src/BMSMonitoring.cpp | 93 ++++++++++++--- rover-apps/pdb_2021/include/PDBMonitoring.h | 15 +-- rover-apps/pdb_2021/src/PDBMonitoring.cpp | 125 ++++++++++++++------ 4 files changed, 177 insertions(+), 66 deletions(-) diff --git a/rover-apps/bms_2022/include/BMSMonitoring.h b/rover-apps/bms_2022/include/BMSMonitoring.h index bb713101..fa74ee1a 100644 --- a/rover-apps/bms_2022/include/BMSMonitoring.h +++ b/rover-apps/bms_2022/include/BMSMonitoring.h @@ -9,7 +9,6 @@ class BMSMonitoring final : public Module { public: /* Sets the Load DIAG_EN pins */ BMSMonitoring(); - void periodic_1s(void) override; void periodic_10s(void) override {} void periodic_100ms(void) override {} @@ -19,10 +18,15 @@ class BMSMonitoring final : public Module { private: void current_monitoring(); void cell_monitoring(); - + void update_voltage_percent(); + void low_battery_warning(); + void send_can_data(); + int cell_voltages[12]; bool is_cell_balancing = false; int coloumb_count = 0; const int battery_capacity = 22000; // mAh - int battery_percent; + int battery_percent_couloumb; + int battery_percent_voltage; + int battery_voltage; }; \ No newline at end of file diff --git a/rover-apps/bms_2022/src/BMSMonitoring.cpp b/rover-apps/bms_2022/src/BMSMonitoring.cpp index 26ac37f6..44030674 100644 --- a/rover-apps/bms_2022/src/BMSMonitoring.cpp +++ b/rover-apps/bms_2022/src/BMSMonitoring.cpp @@ -4,6 +4,7 @@ #include "Logger.h" #include "mbed.h" +#include SPI spi(SDI, SDO, CLK); // mosi, miso, sclk AnalogIn currentADC(CURR_ANLG_IN); @@ -19,26 +20,77 @@ BMSMonitoring::BMSMonitoring() { void BMSMonitoring::cell_monitoring() { chip_select.write(0); + + //TODO: recieve data from can //is_cell_balancing = getRXSignal + // can.getRXSignalValue(HWBRIDGE::CANID::BMS, HWBRIDGE::CANSIGNAL::BMS_STATE, is_cell_balancing); + + //if voltage is too low on one cell, enable balancing on that cell + //only way to balance is to keep all fets on so cells always have same voltage int spi_message = 0b000000000000000000000000; if(is_cell_balancing){ spi_message = 0b111111111111111100000000; } + battery_voltage = 0; for(int i = 0; i < 16; i++){ cell_voltages[i] = spi.write(spi_message); + battery_voltage += cell_voltages[i]; spi_message += 0b10000; } - //setTXSignalValue(HWBRIDGE::CANID msgID, HWBRIDGE::CANSIGNAL signalName, HWBRIDGE::CANSignalValue_t signalValue); - chip_select.write(1); - //if voltage is too low on one cell, enable balancing on that cell - //only way to balance is to keep all fets on so cells always have same voltage + update_voltage_percent(); } -void BMSMonitoring::current_monitoring() { +void BMSMonitoring::update_voltage_percent() { + if(battery_voltage >= 50.40){ + battery_percent_voltage = 100; + }else if(battery_voltage >= 49.80){ + battery_percent_voltage = 95; + }else if(battery_voltage >= 49.32){ + battery_percent_voltage = 90; + }else if(battery_voltage >= 48.96){ + battery_percent_voltage = 85; + }else if(battery_voltage >= 48.24){ + battery_percent_voltage = 80; + }else if(battery_voltage >= 47.76){ + battery_percent_voltage = 75; + }else if(battery_voltage >= 47.40){ + battery_percent_voltage = 70; + }else if(battery_voltage >= 46.92){ + battery_percent_voltage = 65; + }else if(battery_voltage >= 46.44){ + battery_percent_voltage = 60; + }else if(battery_voltage >= 46.20){ + battery_percent_voltage = 55; + }else if(battery_voltage >= 46.08){ + battery_percent_voltage = 50; + }else if(battery_voltage >= 45.84){ + battery_percent_voltage = 45; + }else if(battery_voltage >= 45.60){ + battery_percent_voltage = 40; + }else if(battery_voltage >= 45.48){ + battery_percent_voltage = 35; + }else if(battery_voltage >= 45.24){ + battery_percent_voltage = 30; + }else if(battery_voltage >= 45.00){ + battery_percent_voltage = 25; + }else if(battery_voltage >= 44.76){ + battery_percent_voltage = 20; + }else if(battery_voltage >= 44.52){ + battery_percent_voltage = 15; + }else if(battery_voltage >= 44.28){ + battery_percent_voltage = 10; + }else if(battery_voltage >= 43.32){ + battery_percent_voltage = 5; + }else{ + battery_percent_voltage = 0; + } +} + +void BMSMonitoring::current_monitoring() { float current = (currentADC.read() * 20) -33; if(current < 0){ current = 0; @@ -48,25 +100,34 @@ void BMSMonitoring::current_monitoring() { current = current * 1000; //converts Amps to MilliAmps coloumb_count += current * 0.001; //converts to MilliAmp Seconds int battery_capacity_seconds = battery_capacity * 3600; //converts to MilliAmp Seconds + battery_percent_couloumb = (1 - (coloumb_count / battery_capacity_seconds)) * 100; +} - battery_percent = (1 - (coloumb_count / battery_capacity_seconds)) * 100; - - if(battery_percent < 20){ - buzzer_en = true; +void BMSMonitoring::low_battery_warning(){ + if(battery_percent_couloumb <= 15 || battery_percent_voltage <= 15){ + buzzer_en.write(1); }else{ - buzzer_en = false; + buzzer_en.write(0); } } +void BMSMonitoring::send_can_data(){ + //TODO: Send data over CAN + // Things to send over CAN + // - cell voltages for 12 cells: cell_voltages + // - percent battery: battery_percent_couloumb and battery_percent_couloumb + // - current: current + //setTXSignalValue(HWBRIDGE::CANID msgID, HWBRIDGE::CANSIGNAL signalName, HWBRIDGE::CANSignalValue_t signalValue); + +} + void BMSMonitoring::periodic_1s() { - + low_battery_warning(); + send_can_data(); } void BMSMonitoring::periodic_1ms() { current_monitoring(); -} + cell_monitoring(); - -//TODO: -// make it work with CAN -// voltage look-up table \ No newline at end of file +} diff --git a/rover-apps/pdb_2021/include/PDBMonitoring.h b/rover-apps/pdb_2021/include/PDBMonitoring.h index fc97ce0b..8cf5c60e 100644 --- a/rover-apps/pdb_2021/include/PDBMonitoring.h +++ b/rover-apps/pdb_2021/include/PDBMonitoring.h @@ -20,15 +20,12 @@ class PDBMonitoring final : public Module { void load_monitoring(); void rail_monitoring(); void temperature_monitoring(); + void LED_matrix(); static const float PDB_VBAT_RAIL_NOMINAL_VOLTAGE; static const float PDB_VBAT_RAIL_MIN_THRESHOLD; static const float PDB_VBAT_RAIL_MAX_THRESHOLD; - static const float PDB_24V_RAIL_NOMINAL_VOLTAGE; - static const float PDB_24V_RAIL_MIN_THRESHOLD; - static const float PDB_24V_RAIL_MAX_THRESHOLD; - static const float PDB_17V_RAIL_NOMINAL_VOLTAGE; static const float PDB_17V_RAIL_MIN_THRESHOLD; static const float PDB_17V_RAIL_MAX_THRESHOLD; @@ -44,7 +41,6 @@ class PDBMonitoring final : public Module { static const bool PDB_5V_LOAD2_DIAG_EN; static const bool PDB_5V_LOAD3_DIAG_EN; static const bool PDB_5V_LOAD4_DIAG_EN; - static const bool PDB_5V_LOAD5_DIAG_EN; static const bool PDB_17V_LOAD_DIAG_EN; /* Pins configuration for Load Monitoring */ @@ -60,18 +56,17 @@ class PDBMonitoring final : public Module { DigitalOut load4_5V_diag_en; DigitalIn load4_5V_fault_n; - DigitalOut load5_5V_diag_en; - DigitalIn load5_5V_fault_n; - DigitalOut load_17V_diag_en; DigitalIn load_17V_fault_n; + DigitalOut LED_matrix_red; + PwmOut LED_matrix_green; + DigitalOut LED_matrix_blue; + /* Pins configuration for Rail Monitoring */ AnalogIn railBattery; AnalogIn rail5V; AnalogIn rail17V; - AnalogIn rail24V; - DigitalIn rail24V_pgood_n; /* Pins configuration for Temperature Monitoring */ AnalogIn temperatureADC; diff --git a/rover-apps/pdb_2021/src/PDBMonitoring.cpp b/rover-apps/pdb_2021/src/PDBMonitoring.cpp index 21fc5a82..a77ea00a 100644 --- a/rover-apps/pdb_2021/src/PDBMonitoring.cpp +++ b/rover-apps/pdb_2021/src/PDBMonitoring.cpp @@ -3,15 +3,13 @@ #include #include "Logger.h" +#include "CANConfig.h" +#include "hw_bridge.h" -const float PDBMonitoring::PDB_VBAT_RAIL_NOMINAL_VOLTAGE = 24.0; +const float PDBMonitoring::PDB_VBAT_RAIL_NOMINAL_VOLTAGE = 48.0; const float PDBMonitoring::PDB_VBAT_RAIL_MIN_THRESHOLD = 18.0; const float PDBMonitoring::PDB_VBAT_RAIL_MAX_THRESHOLD = 25.2; -const float PDBMonitoring::PDB_24V_RAIL_NOMINAL_VOLTAGE = 24.0; -const float PDBMonitoring::PDB_24V_RAIL_MIN_THRESHOLD = 22.0; -const float PDBMonitoring::PDB_24V_RAIL_MAX_THRESHOLD = 26.0; - const float PDBMonitoring::PDB_17V_RAIL_NOMINAL_VOLTAGE = 17.0; const float PDBMonitoring::PDB_17V_RAIL_MIN_THRESHOLD = 16.0; const float PDBMonitoring::PDB_17V_RAIL_MAX_THRESHOLD = 18.0; @@ -23,12 +21,9 @@ const float PDBMonitoring::PDB_5V_RAIL_MAX_THRESHOLD = 6.0; const float PDBMonitoring::PDB_TEMPERATURE_MIN_THRESHOLD = 10.0; const float PDBMonitoring::PDB_TEMPERATURE_MAX_THRESHOLD = 50.0; -const bool PDBMonitoring::PDB_5V_LOAD1_DIAG_EN = 1; -const bool PDBMonitoring::PDB_5V_LOAD2_DIAG_EN = 0; -const bool PDBMonitoring::PDB_5V_LOAD3_DIAG_EN = 0; -const bool PDBMonitoring::PDB_5V_LOAD4_DIAG_EN = 0; -const bool PDBMonitoring::PDB_5V_LOAD5_DIAG_EN = 0; -const bool PDBMonitoring::PDB_17V_LOAD_DIAG_EN = 1; +HWBRIDGE::PDB_LED_MATRIX_STATE_VALUES LED_matrix_status = HWBRIDGE::PDB_LED_MATRIX_STATE_VALUES::False; + +CANInterface can(CANConfig::config); PDBMonitoring::PDBMonitoring() : load1_5V_diag_en(LOAD1_5V_DIAG_EN), @@ -39,8 +34,6 @@ PDBMonitoring::PDBMonitoring() load3_5V_fault_n(LOAD3_5V_FAULT), load4_5V_diag_en(LOAD4_5V_DIAG_EN), load4_5V_fault_n(LOAD4_5V_FAULT), - load5_5V_diag_en(LOAD5_5V_DIAG_EN), - load5_5V_fault_n(LOAD5_5V_FAULT), load_17V_diag_en(LOAD_17V_DIAG_EN), load_17V_fault_n(LOAD_17V_FAULT), railBattery(RAIL_BATTERY_ANLG_IN), @@ -48,13 +41,16 @@ PDBMonitoring::PDBMonitoring() rail17V(RAIL_17V_ANLG_IN), rail24V(RAIL_24V_ANLG_IN), rail24V_pgood_n(RAIL_24V_PGOOD_N), - temperatureADC(TEMPERATURE_ADC_IN) { - load1_5V_diag_en = PDB_5V_LOAD1_DIAG_EN; - load2_5V_diag_en = PDB_5V_LOAD2_DIAG_EN; - load3_5V_diag_en = PDB_5V_LOAD3_DIAG_EN; - load4_5V_diag_en = PDB_5V_LOAD4_DIAG_EN; - load5_5V_diag_en = PDB_5V_LOAD5_DIAG_EN; - load_17V_diag_en = PDB_17V_LOAD_DIAG_EN; + temperatureADC(TEMPERATURE_ADC_IN), + LED_matrix_red(LED_MATRIX_R_CHANNEL), + LED_matrix_blue(LED_MATRIX_B_CHANNEL), + LED_matrix_green(LED_MATRIX_G_CHANNEL), + { + load1_5V_diag_en.write(1); + load2_5V_diag_en.write(1); + load3_5V_diag_en.write(1); + load4_5V_diag_en.write(1); + load_17V_diag_en.write(1); } /*TODO:Replace the logger statements with CAN logs @@ -62,61 +58,116 @@ PDBMonitoring::PDBMonitoring() void PDBMonitoring::load_monitoring() { if (load1_5V_diag_en && !load1_5V_fault_n) { + load1_5V_diag_en.write(0); Utility::logger << "Fault on 5V load 1\n"; + }else{ + load1_5V_diag_en.write(1); } if (load2_5V_diag_en && !load2_5V_fault_n) { Utility::logger << "Fault on 5V load 2\n"; + load2_5V_diag_en.write(0); + }else{ + load2_5V_diag_en.write(1); } if (load3_5V_diag_en && !load3_5V_fault_n) { Utility::logger << "Fault on 5V load 3\n"; + load3_5V_diag_en.write(0); + }else{ + load3_5V_diag_en.write(1); } if (load4_5V_diag_en && !load4_5V_fault_n) { Utility::logger << "Fault on 5V load 4\n"; - } - if (load5_5V_diag_en && !load5_5V_fault_n) { - Utility::logger << "Fault on 5V load 5\n"; + load4_5V_diag_en.write(0); + }else{ + load4_5V_diag_en.write(1); } if (load_17V_diag_en && !load_17V_fault_n) { Utility::logger << "Fault on 17V load\n"; + load_17V_diag_en.write(0); + }else{ + load_17V_diag_en.write(1); } } void PDBMonitoring::rail_monitoring() { float rail_vbat_voltage = railBattery.read_voltage() / 3 * PDB_VBAT_RAIL_NOMINAL_VOLTAGE; - float rail_24V_voltage = rail24V.read_voltage() / 3 * PDB_24V_RAIL_NOMINAL_VOLTAGE; float rail_17V_voltage = rail17V.read_voltage() / 3 * PDB_17V_RAIL_NOMINAL_VOLTAGE; float rail_5V_voltage = rail5V.read_voltage() / 3 * PDB_5V_RAIL_NOMINAL_VOLTAGE; if (rail_vbat_voltage < PDB_VBAT_RAIL_MIN_THRESHOLD || rail_vbat_voltage > PDB_VBAT_RAIL_MAX_THRESHOLD) { Utility::logger << "!!! VBAT RAIL VOLTAGE: %.3fV !!!\n", rail_vbat_voltage; } - if (rail_24V_voltage < PDB_24V_RAIL_MIN_THRESHOLD || rail_24V_voltage > PDB_24V_RAIL_MAX_THRESHOLD) { - Utility::logger << "!!! 24V RAIL VOLTAGE: %.3fV !!!\n", rail_24V_voltage; - } if (rail_17V_voltage < PDB_17V_RAIL_MIN_THRESHOLD || rail_17V_voltage > PDB_17V_RAIL_MAX_THRESHOLD) { Utility::logger << "!!! 17V RAIL VOLTAGE: %.3fV !!!\n", rail_17V_voltage; } if (rail_5V_voltage < PDB_5V_RAIL_MIN_THRESHOLD || rail_5V_voltage > PDB_5V_RAIL_MAX_THRESHOLD) { Utility::logger << "!!! 5V RAIL VOLTAGE: %.3fV !!!\n", rail_5V_voltage; } -} -/*https://www.ti.com/lit/ds/symlink/lmt87-q1.pdf?ts=1627158177761&ref_url=https%253A%252F%252Fwww.google.com%252F*/ + // Sends can status messaging + //TODO: Make CAN sending work + if(rail_17V_voltage < PDB_17V_RAIL_MIN_THRESHOLD){ + can.setTXSignalValue(HWBRIDGE::CANID::PDB_REPORT_FAULTS, HWBRIDGE::CANSIGNAL::PDB_17_V_RAIL_STATUS_VALUES, + HWBRIDGE::CANSIGNAL::PDB_17_V_RAIL_STATUS_VALUES::UNDERVOLTAGE); + }else if(rail_17V_voltage > PDB_17V_RAIL_MAX_THRESHOLD){ + can.setTXSignalValue(HWBRIDGE::CANID::PDB_REPORT_FAULTS, HWBRIDGE::CANSIGNAL::PDB_17_V_RAIL_STATUS_VALUES, + HWBRIDGE::CANSIGNAL::PDB_17_V_RAIL_STATUS_VALUES::OVERVOLTAGE); + }else{ + can.setTXSignalValue(HWBRIDGE::CANID::PDB_REPORT_FAULTS, HWBRIDGE::CANSIGNAL::PDB_17_V_RAIL_STATUS_VALUES, + HWBRIDGE::CANSIGNAL::PDB_17_V_RAIL_STATUS_VALUES::OPERATIONAL); + } -void PDBMonitoring::temperature_monitoring() { - float temperature_celsius = - (13.582 - sqrt(pow(-13.582, 2) + 4 * 0.00433 * (2230.8 - temperatureADC.read_voltage() * 1000))) / - (2 * (-0.00433)) + - 30; + if(rail_5V_voltage < PDB_5V_RAIL_MIN_THRESHOLD){ + can.setTXSignalValue(HWBRIDGE::CANID::PDB_REPORT_FAULTS, HWBRIDGE::CANSIGNAL::PDB_5_V_RAIL_STATUS_VALUES, + HWBRIDGE::CANSIGNAL::PDB_5_V_RAIL_STATUS_VALUES::UNDERVOLTAGE); + }else if(rail_5V_voltage > PDB_5V_RAIL_MAX_THRESHOLD){ + can.setTXSignalValue(HWBRIDGE::CANID::PDB_REPORT_FAULTS, HWBRIDGE::CANSIGNAL::PDB_5_V_RAIL_STATUS_VALUES, + HWBRIDGE::CANSIGNAL::PDB_5_V_RAIL_STATUS_VALUES::OVERVOLTAGE); + }else{ + can.setTXSignalValue(HWBRIDGE::CANID::PDB_REPORT_FAULTS, HWBRIDGE::CANSIGNAL::PDB_5_V_RAIL_STATUS_VALUES, + HWBRIDGE::CANSIGNAL::PDB_5_V_RAIL_STATUS_VALUES::OPERATIONAL); - if (temperature_celsius < PDB_TEMPERATURE_MIN_THRESHOLD || temperature_celsius > PDB_TEMPERATURE_MAX_THRESHOLD) { - Utility::logger << "!!! TEMPERATURE FAULT:" << temperature_celsius << "degrees Celsius\n"; } } +void PDBMonitoring::LED_matrix(){ + //TODO: Make CAN receive work + can.getRXSignalValue(HWBRIDGE::CANID::PDB_SET_LED_MATRIX, HWBRIDGE::CANSIGNAL::PDB_LED_MATRIX_STATE, LED_matrix_status); + if(LED_matrix_status == HWBRIDGE::PDB_LED_MATRIX_STATE_VALUES::SOLID_RED){ + LED_matrix_red.write(1); + LED_matrix_green.write(0); + LED_matrix_blue.write(0); + }else if(LED_matrix_status == HWBRIDGE::PDB_LED_MATRIX_STATE_VALUES::FLASHING_GREEN){ + LED_matrix_green.write(0.50f); + LED_matrix_blue.write(0); + LED_matrix_red.write(0); + }else if(LED_matrix_status == HWBRIDGE::PDB_LED_MATRIX_STATE_VALUES::SOLID_BLUE){ + LED_matrix_blue.write(1); + LED_matrix_red.write(0); + LED_matrix_green.write(0); + }else{ + LED_matrix_blue.write(0); + LED_matrix_red.write(0); + LED_matrix_green.write(0); + } +} + +/*https://www.ti.com/lit/ds/symlink/lmt87-q1.pdf?ts=1627158177761&ref_url=https%253A%252F%252Fwww.google.com%252F*/ + +// void PDBMonitoring::temperature_monitoring() { +// float temperature_celsius = +// (13.582 - sqrt(pow(-13.582, 2) + 4 * 0.00433 * (2230.8 - temperatureADC.read_voltage() * 1000))) / +// (2 * (-0.00433)) + +// 30; + +// if (temperature_celsius < PDB_TEMPERATURE_MIN_THRESHOLD || temperature_celsius > PDB_TEMPERATURE_MAX_THRESHOLD) { +// Utility::logger << "!!! TEMPERATURE FAULT:" << temperature_celsius << "degrees Celsius\n"; +// } +// } void PDBMonitoring::periodic_1s() { - temperature_monitoring(); + // temperature_monitoring(); load_monitoring(); + LED_matrix(); } void PDBMonitoring::periodic_1ms() { From 991b04623d7e9731f7257f8be4baebb1d4b055b0 Mon Sep 17 00:00:00 2001 From: Ari Wasch Date: Sat, 5 Feb 2022 21:03:24 -0800 Subject: [PATCH 4/6] Clang format fix --- rover-apps/bms_2022/include/BMSMonitoring.h | 6 +- rover-apps/bms_2022/src/BMSMonitoring.cpp | 89 ++++++++++----------- rover-apps/pdb_2021/src/PDBMonitoring.cpp | 46 +++++------ 3 files changed, 69 insertions(+), 72 deletions(-) diff --git a/rover-apps/bms_2022/include/BMSMonitoring.h b/rover-apps/bms_2022/include/BMSMonitoring.h index fa74ee1a..2942c226 100644 --- a/rover-apps/bms_2022/include/BMSMonitoring.h +++ b/rover-apps/bms_2022/include/BMSMonitoring.h @@ -23,9 +23,9 @@ class BMSMonitoring final : public Module { void send_can_data(); int cell_voltages[12]; - bool is_cell_balancing = false; - int coloumb_count = 0; - const int battery_capacity = 22000; // mAh + bool is_cell_balancing = false; + int coloumb_count = 0; + const int battery_capacity = 22000; // mAh int battery_percent_couloumb; int battery_percent_voltage; int battery_voltage; diff --git a/rover-apps/bms_2022/src/BMSMonitoring.cpp b/rover-apps/bms_2022/src/BMSMonitoring.cpp index 44030674..73844e0d 100644 --- a/rover-apps/bms_2022/src/BMSMonitoring.cpp +++ b/rover-apps/bms_2022/src/BMSMonitoring.cpp @@ -1,12 +1,12 @@ #include "BMSMonitoring.h" #include +#include #include "Logger.h" #include "mbed.h" -#include -SPI spi(SDI, SDO, CLK); // mosi, miso, sclk +SPI spi(SDI, SDO, CLK); // mosi, miso, sclk AnalogIn currentADC(CURR_ANLG_IN); DigitalOut buzzer_en(BUZZER_EN); DigitalOut chip_select(SAMPL); @@ -14,26 +14,26 @@ DigitalOut chip_select(SAMPL); BMSMonitoring::BMSMonitoring() { buzzer_en.write(0); chip_select.write(1); - spi.format(24,3); + spi.format(24, 3); spi.frequency(1000000); } void BMSMonitoring::cell_monitoring() { chip_select.write(0); - //TODO: recieve data from can - //is_cell_balancing = getRXSignal + // TODO: recieve data from can + // is_cell_balancing = getRXSignal // can.getRXSignalValue(HWBRIDGE::CANID::BMS, HWBRIDGE::CANSIGNAL::BMS_STATE, is_cell_balancing); - //if voltage is too low on one cell, enable balancing on that cell - //only way to balance is to keep all fets on so cells always have same voltage + // if voltage is too low on one cell, enable balancing on that cell + // only way to balance is to keep all fets on so cells always have same voltage int spi_message = 0b000000000000000000000000; - if(is_cell_balancing){ + if (is_cell_balancing) { spi_message = 0b111111111111111100000000; } battery_voltage = 0; - for(int i = 0; i < 16; i++){ + for (int i = 0; i < 16; i++) { cell_voltages[i] = spi.write(spi_message); battery_voltage += cell_voltages[i]; spi_message += 0b10000; @@ -44,81 +44,79 @@ void BMSMonitoring::cell_monitoring() { } void BMSMonitoring::update_voltage_percent() { - if(battery_voltage >= 50.40){ + if (battery_voltage >= 50.40) { battery_percent_voltage = 100; - }else if(battery_voltage >= 49.80){ + } else if (battery_voltage >= 49.80) { battery_percent_voltage = 95; - }else if(battery_voltage >= 49.32){ + } else if (battery_voltage >= 49.32) { battery_percent_voltage = 90; - }else if(battery_voltage >= 48.96){ + } else if (battery_voltage >= 48.96) { battery_percent_voltage = 85; - }else if(battery_voltage >= 48.24){ + } else if (battery_voltage >= 48.24) { battery_percent_voltage = 80; - }else if(battery_voltage >= 47.76){ + } else if (battery_voltage >= 47.76) { battery_percent_voltage = 75; - }else if(battery_voltage >= 47.40){ + } else if (battery_voltage >= 47.40) { battery_percent_voltage = 70; - }else if(battery_voltage >= 46.92){ + } else if (battery_voltage >= 46.92) { battery_percent_voltage = 65; - }else if(battery_voltage >= 46.44){ + } else if (battery_voltage >= 46.44) { battery_percent_voltage = 60; - }else if(battery_voltage >= 46.20){ + } else if (battery_voltage >= 46.20) { battery_percent_voltage = 55; - }else if(battery_voltage >= 46.08){ + } else if (battery_voltage >= 46.08) { battery_percent_voltage = 50; - }else if(battery_voltage >= 45.84){ + } else if (battery_voltage >= 45.84) { battery_percent_voltage = 45; - }else if(battery_voltage >= 45.60){ + } else if (battery_voltage >= 45.60) { battery_percent_voltage = 40; - }else if(battery_voltage >= 45.48){ + } else if (battery_voltage >= 45.48) { battery_percent_voltage = 35; - }else if(battery_voltage >= 45.24){ + } else if (battery_voltage >= 45.24) { battery_percent_voltage = 30; - }else if(battery_voltage >= 45.00){ + } else if (battery_voltage >= 45.00) { battery_percent_voltage = 25; - }else if(battery_voltage >= 44.76){ + } else if (battery_voltage >= 44.76) { battery_percent_voltage = 20; - }else if(battery_voltage >= 44.52){ + } else if (battery_voltage >= 44.52) { battery_percent_voltage = 15; - }else if(battery_voltage >= 44.28){ + } else if (battery_voltage >= 44.28) { battery_percent_voltage = 10; - }else if(battery_voltage >= 43.32){ + } else if (battery_voltage >= 43.32) { battery_percent_voltage = 5; - }else{ + } else { battery_percent_voltage = 0; } - } void BMSMonitoring::current_monitoring() { - float current = (currentADC.read() * 20) -33; - if(current < 0){ + float current = (currentADC.read() * 20) - 33; + if (current < 0) { current = 0; - }else if(current > 29){ + } else if (current > 29) { current = 29.1; } - current = current * 1000; //converts Amps to MilliAmps - coloumb_count += current * 0.001; //converts to MilliAmp Seconds - int battery_capacity_seconds = battery_capacity * 3600; //converts to MilliAmp Seconds - battery_percent_couloumb = (1 - (coloumb_count / battery_capacity_seconds)) * 100; + current = current * 1000; // converts Amps to MilliAmps + coloumb_count += current * 0.001; // converts to MilliAmp Seconds + int battery_capacity_seconds = battery_capacity * 3600; // converts to MilliAmp Seconds + battery_percent_couloumb = (1 - (coloumb_count / battery_capacity_seconds)) * 100; } -void BMSMonitoring::low_battery_warning(){ - if(battery_percent_couloumb <= 15 || battery_percent_voltage <= 15){ +void BMSMonitoring::low_battery_warning() { + if (battery_percent_couloumb <= 15 || battery_percent_voltage <= 15) { buzzer_en.write(1); - }else{ + } else { buzzer_en.write(0); } } -void BMSMonitoring::send_can_data(){ - //TODO: Send data over CAN +void BMSMonitoring::send_can_data() { + // TODO: Send data over CAN // Things to send over CAN // - cell voltages for 12 cells: cell_voltages // - percent battery: battery_percent_couloumb and battery_percent_couloumb // - current: current - //setTXSignalValue(HWBRIDGE::CANID msgID, HWBRIDGE::CANSIGNAL signalName, HWBRIDGE::CANSignalValue_t signalValue); - + // setTXSignalValue(HWBRIDGE::CANID msgID, HWBRIDGE::CANSIGNAL signalName, HWBRIDGE::CANSignalValue_t signalValue); } void BMSMonitoring::periodic_1s() { @@ -129,5 +127,4 @@ void BMSMonitoring::periodic_1s() { void BMSMonitoring::periodic_1ms() { current_monitoring(); cell_monitoring(); - } diff --git a/rover-apps/pdb_2021/src/PDBMonitoring.cpp b/rover-apps/pdb_2021/src/PDBMonitoring.cpp index a77ea00a..22091fd8 100644 --- a/rover-apps/pdb_2021/src/PDBMonitoring.cpp +++ b/rover-apps/pdb_2021/src/PDBMonitoring.cpp @@ -2,8 +2,8 @@ #include -#include "Logger.h" #include "CANConfig.h" +#include "Logger.h" #include "hw_bridge.h" const float PDBMonitoring::PDB_VBAT_RAIL_NOMINAL_VOLTAGE = 48.0; @@ -21,7 +21,7 @@ const float PDBMonitoring::PDB_5V_RAIL_MAX_THRESHOLD = 6.0; const float PDBMonitoring::PDB_TEMPERATURE_MIN_THRESHOLD = 10.0; const float PDBMonitoring::PDB_TEMPERATURE_MAX_THRESHOLD = 50.0; -HWBRIDGE::PDB_LED_MATRIX_STATE_VALUES LED_matrix_status = HWBRIDGE::PDB_LED_MATRIX_STATE_VALUES::False; +HWBRIDGE::PDB_LED_MATRIX_STATE_VALUES LED_matrix_status = HWBRIDGE::PDB_LED_MATRIX_STATE_VALUES::False; CANInterface can(CANConfig::config); @@ -45,7 +45,7 @@ PDBMonitoring::PDBMonitoring() LED_matrix_red(LED_MATRIX_R_CHANNEL), LED_matrix_blue(LED_MATRIX_B_CHANNEL), LED_matrix_green(LED_MATRIX_G_CHANNEL), - { +{ load1_5V_diag_en.write(1); load2_5V_diag_en.write(1); load3_5V_diag_en.write(1); @@ -60,31 +60,31 @@ void PDBMonitoring::load_monitoring() { if (load1_5V_diag_en && !load1_5V_fault_n) { load1_5V_diag_en.write(0); Utility::logger << "Fault on 5V load 1\n"; - }else{ + } else { load1_5V_diag_en.write(1); } if (load2_5V_diag_en && !load2_5V_fault_n) { Utility::logger << "Fault on 5V load 2\n"; load2_5V_diag_en.write(0); - }else{ + } else { load2_5V_diag_en.write(1); } if (load3_5V_diag_en && !load3_5V_fault_n) { Utility::logger << "Fault on 5V load 3\n"; load3_5V_diag_en.write(0); - }else{ + } else { load3_5V_diag_en.write(1); } if (load4_5V_diag_en && !load4_5V_fault_n) { Utility::logger << "Fault on 5V load 4\n"; load4_5V_diag_en.write(0); - }else{ + } else { load4_5V_diag_en.write(1); } if (load_17V_diag_en && !load_17V_fault_n) { Utility::logger << "Fault on 17V load\n"; load_17V_diag_en.write(0); - }else{ + } else { load_17V_diag_en.write(1); } } @@ -105,46 +105,46 @@ void PDBMonitoring::rail_monitoring() { } // Sends can status messaging - //TODO: Make CAN sending work - if(rail_17V_voltage < PDB_17V_RAIL_MIN_THRESHOLD){ + // TODO: Make CAN sending work + if (rail_17V_voltage < PDB_17V_RAIL_MIN_THRESHOLD) { can.setTXSignalValue(HWBRIDGE::CANID::PDB_REPORT_FAULTS, HWBRIDGE::CANSIGNAL::PDB_17_V_RAIL_STATUS_VALUES, HWBRIDGE::CANSIGNAL::PDB_17_V_RAIL_STATUS_VALUES::UNDERVOLTAGE); - }else if(rail_17V_voltage > PDB_17V_RAIL_MAX_THRESHOLD){ + } else if (rail_17V_voltage > PDB_17V_RAIL_MAX_THRESHOLD) { can.setTXSignalValue(HWBRIDGE::CANID::PDB_REPORT_FAULTS, HWBRIDGE::CANSIGNAL::PDB_17_V_RAIL_STATUS_VALUES, HWBRIDGE::CANSIGNAL::PDB_17_V_RAIL_STATUS_VALUES::OVERVOLTAGE); - }else{ + } else { can.setTXSignalValue(HWBRIDGE::CANID::PDB_REPORT_FAULTS, HWBRIDGE::CANSIGNAL::PDB_17_V_RAIL_STATUS_VALUES, HWBRIDGE::CANSIGNAL::PDB_17_V_RAIL_STATUS_VALUES::OPERATIONAL); } - if(rail_5V_voltage < PDB_5V_RAIL_MIN_THRESHOLD){ + if (rail_5V_voltage < PDB_5V_RAIL_MIN_THRESHOLD) { can.setTXSignalValue(HWBRIDGE::CANID::PDB_REPORT_FAULTS, HWBRIDGE::CANSIGNAL::PDB_5_V_RAIL_STATUS_VALUES, HWBRIDGE::CANSIGNAL::PDB_5_V_RAIL_STATUS_VALUES::UNDERVOLTAGE); - }else if(rail_5V_voltage > PDB_5V_RAIL_MAX_THRESHOLD){ + } else if (rail_5V_voltage > PDB_5V_RAIL_MAX_THRESHOLD) { can.setTXSignalValue(HWBRIDGE::CANID::PDB_REPORT_FAULTS, HWBRIDGE::CANSIGNAL::PDB_5_V_RAIL_STATUS_VALUES, HWBRIDGE::CANSIGNAL::PDB_5_V_RAIL_STATUS_VALUES::OVERVOLTAGE); - }else{ + } else { can.setTXSignalValue(HWBRIDGE::CANID::PDB_REPORT_FAULTS, HWBRIDGE::CANSIGNAL::PDB_5_V_RAIL_STATUS_VALUES, HWBRIDGE::CANSIGNAL::PDB_5_V_RAIL_STATUS_VALUES::OPERATIONAL); - } } -void PDBMonitoring::LED_matrix(){ - //TODO: Make CAN receive work - can.getRXSignalValue(HWBRIDGE::CANID::PDB_SET_LED_MATRIX, HWBRIDGE::CANSIGNAL::PDB_LED_MATRIX_STATE, LED_matrix_status); - if(LED_matrix_status == HWBRIDGE::PDB_LED_MATRIX_STATE_VALUES::SOLID_RED){ +void PDBMonitoring::LED_matrix() { + // TODO: Make CAN receive work + can.getRXSignalValue(HWBRIDGE::CANID::PDB_SET_LED_MATRIX, HWBRIDGE::CANSIGNAL::PDB_LED_MATRIX_STATE, + LED_matrix_status); + if (LED_matrix_status == HWBRIDGE::PDB_LED_MATRIX_STATE_VALUES::SOLID_RED) { LED_matrix_red.write(1); LED_matrix_green.write(0); LED_matrix_blue.write(0); - }else if(LED_matrix_status == HWBRIDGE::PDB_LED_MATRIX_STATE_VALUES::FLASHING_GREEN){ + } else if (LED_matrix_status == HWBRIDGE::PDB_LED_MATRIX_STATE_VALUES::FLASHING_GREEN) { LED_matrix_green.write(0.50f); LED_matrix_blue.write(0); LED_matrix_red.write(0); - }else if(LED_matrix_status == HWBRIDGE::PDB_LED_MATRIX_STATE_VALUES::SOLID_BLUE){ + } else if (LED_matrix_status == HWBRIDGE::PDB_LED_MATRIX_STATE_VALUES::SOLID_BLUE) { LED_matrix_blue.write(1); LED_matrix_red.write(0); LED_matrix_green.write(0); - }else{ + } else { LED_matrix_blue.write(0); LED_matrix_red.write(0); LED_matrix_green.write(0); From 71b8cec10b4a56ad57d3fe27e0b591d2afb4c83f Mon Sep 17 00:00:00 2001 From: Ari Wasch Date: Sat, 5 Feb 2022 21:08:53 -0800 Subject: [PATCH 5/6] Clang format fix --- rover-apps/bms_2022/include/AppConfig.h | 2 +- targets/TARGET_PDB_REV2_2021/include/PinNames.h | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/rover-apps/bms_2022/include/AppConfig.h b/rover-apps/bms_2022/include/AppConfig.h index b1b66d8a..8aa572c8 100644 --- a/rover-apps/bms_2022/include/AppConfig.h +++ b/rover-apps/bms_2022/include/AppConfig.h @@ -2,8 +2,8 @@ #include -#include "Module.h" #include "BMSMonitoring.h" +#include "Module.h" BMSMonitoring BMS_Monitoring; diff --git a/targets/TARGET_PDB_REV2_2021/include/PinNames.h b/targets/TARGET_PDB_REV2_2021/include/PinNames.h index e947bb5e..10a8ed20 100644 --- a/targets/TARGET_PDB_REV2_2021/include/PinNames.h +++ b/targets/TARGET_PDB_REV2_2021/include/PinNames.h @@ -195,13 +195,13 @@ typedef enum { // LOAD_17V_FAULT = PC_5, /**** BMS ****/ - BUZZER_EN = PC_5, - CURR_ANLG_IN = PC_8, - CLK = PB_7, - SDI = PB_8, - SDO = PB_9, - SAMPL = PB_10, - + BUZZER_EN = PC_5, + CURR_ANLG_IN = PC_8, + CLK = PB_7, + SDI = PB_8, + SDO = PB_9, + SAMPL = PB_10, + /**** LED Matrix ****/ LED_MATRIX_R_CHANNEL = PB_4, LED_MATRIX_G_CHANNEL = PB_5, From 6ea8cd9058b3298cfd0ab30c510741f79e9917cf Mon Sep 17 00:00:00 2001 From: Ari Wasch Date: Wed, 16 Mar 2022 20:47:12 -0700 Subject: [PATCH 6/6] Fixed errors --- CMakeLists.txt | 1 + libs/can/include/CANInterface.h | 6 +- rover-apps/bms_2022/include/BMSMonitoring.h | 26 ++- rover-apps/bms_2022/src/BMSMonitoring.cpp | 143 ++++++++------- rover-apps/pdb_2021/include/PDBMonitoring.h | 15 +- rover-apps/pdb_2021/src/PDBMonitoring.cpp | 125 ++++--------- rover-apps/pdb_2022/CMakeLists.txt | 25 +++ rover-apps/pdb_2022/include/AppConfig.h | 12 ++ rover-apps/pdb_2022/include/PDBMonitoring.h | 73 ++++++++ rover-apps/pdb_2022/src/PDBMonitoring.cpp | 183 ++++++++++++++++++++ rover-apps/pdb_2022/src/main.cpp | 51 ++++++ supported_build_configurations.yaml | 3 + 12 files changed, 486 insertions(+), 177 deletions(-) create mode 100644 rover-apps/pdb_2022/CMakeLists.txt create mode 100644 rover-apps/pdb_2022/include/AppConfig.h create mode 100644 rover-apps/pdb_2022/include/PDBMonitoring.h create mode 100644 rover-apps/pdb_2022/src/PDBMonitoring.cpp create mode 100644 rover-apps/pdb_2022/src/main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 3be5b3b4..0e5fbd69 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -95,6 +95,7 @@ add_app_subdirectory(${ROVER_APPS_DIR}/bms_2022) add_app_subdirectory(${ROVER_APPS_DIR}/gamepad_2021) add_app_subdirectory(${ROVER_APPS_DIR}/gimbal_2021) add_app_subdirectory(${ROVER_APPS_DIR}/pdb_2021) +add_app_subdirectory(${ROVER_APPS_DIR}/pdb_2022) add_app_subdirectory(${ROVER_APPS_DIR}/science_2021) # Include Test Apps diff --git a/libs/can/include/CANInterface.h b/libs/can/include/CANInterface.h index 0c0bf4f4..9b67f2f6 100644 --- a/libs/can/include/CANInterface.h +++ b/libs/can/include/CANInterface.h @@ -30,10 +30,10 @@ class CANInterface { bool sendOneShotMessage(CANMsg &msg, Kernel::Clock::duration_u32 timeout); // Update a TX CAN signal - bool setTXSignalValue(HWBRIDGE::CANID msgID, HWBRIDGE::CANSIGNAL signalName, HWBRIDGE::CANSignalValue_t signalValue); - + bool setTXSignalValue(HWBRIDGE::CANID msgID, HWBRIDGE::CANSIGNAL signalName, HWBRIDGE::CANSignalValue_t signalValue); //here +// PDB_setLEDMatrix. // Read a RX CAN signal - bool getRXSignalValue(HWBRIDGE::CANID msgID, HWBRIDGE::CANSIGNAL signalName, HWBRIDGE::CANSignalValue_t &signalValue); + bool getRXSignalValue(HWBRIDGE::CANID msgID, HWBRIDGE::CANSIGNAL signalName, HWBRIDGE::CANSignalValue_t &signalValue); //here // Switch CAN bus bool switchCANBus(HWBRIDGE::CANBUSID canBusID); diff --git a/rover-apps/bms_2022/include/BMSMonitoring.h b/rover-apps/bms_2022/include/BMSMonitoring.h index 2942c226..8185e9f1 100644 --- a/rover-apps/bms_2022/include/BMSMonitoring.h +++ b/rover-apps/bms_2022/include/BMSMonitoring.h @@ -1,8 +1,15 @@ #pragma once +#include +#include +#include + +#include "Logger.h" #include "Module.h" +#include "PinNames.h" +#include "PinNamesTypes.h" +#include "cmsis.h" #include "mbed.h" - /*This PDB module is for load, rail and temperature monitoring.*/ class BMSMonitoring final : public Module { @@ -22,11 +29,14 @@ class BMSMonitoring final : public Module { void low_battery_warning(); void send_can_data(); - int cell_voltages[12]; - bool is_cell_balancing = false; - int coloumb_count = 0; - const int battery_capacity = 22000; // mAh - int battery_percent_couloumb; - int battery_percent_voltage; - int battery_voltage; + int m_cell_voltages[12]; + bool m_is_cell_balancing = false; + int m_coloumb_count = 0; + const int m_battery_capacity = 22000; // mAh + int m_battery_percent_couloumb; + int m_battery_percent_voltage; + int m_battery_voltage; + int const m_spi_frequency = 1000000; + int const m_spi_bits = 24; + int const m_spi_mode = 3; }; \ No newline at end of file diff --git a/rover-apps/bms_2022/src/BMSMonitoring.cpp b/rover-apps/bms_2022/src/BMSMonitoring.cpp index 73844e0d..fbb5a5c2 100644 --- a/rover-apps/bms_2022/src/BMSMonitoring.cpp +++ b/rover-apps/bms_2022/src/BMSMonitoring.cpp @@ -1,25 +1,30 @@ #include "BMSMonitoring.h" +#include #include #include #include "Logger.h" #include "mbed.h" +#include "PinNames.h" -SPI spi(SDI, SDO, CLK); // mosi, miso, sclk -AnalogIn currentADC(CURR_ANLG_IN); -DigitalOut buzzer_en(BUZZER_EN); -DigitalOut chip_select(SAMPL); +#include "PinNamesTypes.h" +#include "cmsis.h" + +SPI m_spi(SDI, SDO, CLK); // mosi, miso, sclk +AnalogIn m_currentADC(CURR_ANLG_IN); +DigitalOut m_buzzer_en(BUZZER_EN); +DigitalOut m_chip_select(SAMPL); BMSMonitoring::BMSMonitoring() { - buzzer_en.write(0); - chip_select.write(1); - spi.format(24, 3); - spi.frequency(1000000); + m_buzzer_en.write(0); + m_chip_select.write(1); + m_spi.format(m_spi_bits, m_spi_mode); + m_spi.frequency(m_spi_frequency); } void BMSMonitoring::cell_monitoring() { - chip_select.write(0); + m_chip_select.write(0); // TODO: recieve data from can // is_cell_balancing = getRXSignal @@ -27,86 +32,78 @@ void BMSMonitoring::cell_monitoring() { // if voltage is too low on one cell, enable balancing on that cell // only way to balance is to keep all fets on so cells always have same voltage - int spi_message = 0b000000000000000000000000; - if (is_cell_balancing) { - spi_message = 0b111111111111111100000000; - } + int spi_message = (m_is_cell_balancing ? 0b111111111111111100000000 : 0b000000000000000000000000); - battery_voltage = 0; - for (int i = 0; i < 16; i++) { - cell_voltages[i] = spi.write(spi_message); - battery_voltage += cell_voltages[i]; + m_battery_voltage = 0; + for (int i = 0; i < 12; i++) { + m_cell_voltages[i] = m_spi.write(spi_message); + m_battery_voltage += m_cell_voltages[i]; spi_message += 0b10000; } - chip_select.write(1); - + m_chip_select.write(1); update_voltage_percent(); } void BMSMonitoring::update_voltage_percent() { - if (battery_voltage >= 50.40) { - battery_percent_voltage = 100; - } else if (battery_voltage >= 49.80) { - battery_percent_voltage = 95; - } else if (battery_voltage >= 49.32) { - battery_percent_voltage = 90; - } else if (battery_voltage >= 48.96) { - battery_percent_voltage = 85; - } else if (battery_voltage >= 48.24) { - battery_percent_voltage = 80; - } else if (battery_voltage >= 47.76) { - battery_percent_voltage = 75; - } else if (battery_voltage >= 47.40) { - battery_percent_voltage = 70; - } else if (battery_voltage >= 46.92) { - battery_percent_voltage = 65; - } else if (battery_voltage >= 46.44) { - battery_percent_voltage = 60; - } else if (battery_voltage >= 46.20) { - battery_percent_voltage = 55; - } else if (battery_voltage >= 46.08) { - battery_percent_voltage = 50; - } else if (battery_voltage >= 45.84) { - battery_percent_voltage = 45; - } else if (battery_voltage >= 45.60) { - battery_percent_voltage = 40; - } else if (battery_voltage >= 45.48) { - battery_percent_voltage = 35; - } else if (battery_voltage >= 45.24) { - battery_percent_voltage = 30; - } else if (battery_voltage >= 45.00) { - battery_percent_voltage = 25; - } else if (battery_voltage >= 44.76) { - battery_percent_voltage = 20; - } else if (battery_voltage >= 44.52) { - battery_percent_voltage = 15; - } else if (battery_voltage >= 44.28) { - battery_percent_voltage = 10; - } else if (battery_voltage >= 43.32) { - battery_percent_voltage = 5; + if (m_battery_voltage >= 50.40) { + m_battery_percent_voltage = 100; + } else if (m_battery_voltage >= 49.80) { + m_battery_percent_voltage = 95; + } else if (m_battery_voltage >= 49.32) { + m_battery_percent_voltage = 90; + } else if (m_battery_voltage >= 48.96) { + m_battery_percent_voltage = 85; + } else if (m_battery_voltage >= 48.24) { + m_battery_percent_voltage = 80; + } else if (m_battery_voltage >= 47.76) { + m_battery_percent_voltage = 75; + } else if (m_battery_voltage >= 47.40) { + m_battery_percent_voltage = 65; + } else if (m_battery_voltage >= 46.44) { + m_battery_percent_voltage = 60; + } else if (m_battery_voltage >= 46.20) { + m_battery_percent_voltage = 55; + } else if (m_battery_voltage >= 46.08) { + m_battery_percent_voltage = 50; + } else if (m_battery_voltage >= 45.84) { + m_battery_percent_voltage = 45; + } else if (m_battery_voltage >= 45.60) { + m_battery_percent_voltage = 40; + } else if (m_battery_voltage >= 45.48) { + m_battery_percent_voltage = 35; + } else if (m_battery_voltage >= 45.24) { + m_battery_percent_voltage = 30; + } else if (m_battery_voltage >= 45.00) { + m_battery_percent_voltage = 25; + } else if (m_battery_voltage >= 44.76) { + m_battery_percent_voltage = 20; + } else if (m_battery_voltage >= 44.52) { + m_battery_percent_voltage = 15; + } else if (m_battery_voltage >= 44.28) { + m_battery_percent_voltage = 10; + } else if (m_battery_voltage >= 43.32) { + m_battery_percent_voltage = 5; } else { - battery_percent_voltage = 0; + m_battery_percent_voltage = 0; } } void BMSMonitoring::current_monitoring() { - float current = (currentADC.read() * 20) - 33; - if (current < 0) { - current = 0; - } else if (current > 29) { - current = 29.1; - } - current = current * 1000; // converts Amps to MilliAmps - coloumb_count += current * 0.001; // converts to MilliAmp Seconds - int battery_capacity_seconds = battery_capacity * 3600; // converts to MilliAmp Seconds - battery_percent_couloumb = (1 - (coloumb_count / battery_capacity_seconds)) * 100; + float current = (m_currentADC.read() * 20) - 33; + + current = std::clamp(current, 0.0f, 29.0f); + + current *= 1000; // converts Amps to MilliAmps + m_coloumb_count += current * 0.001; // converts to MilliAmp Seconds + float battery_capacity_seconds = m_battery_capacity * 3600; // converts to MilliAmp Seconds + m_battery_percent_couloumb = (1 - (m_coloumb_count / battery_capacity_seconds)) * 100; } void BMSMonitoring::low_battery_warning() { - if (battery_percent_couloumb <= 15 || battery_percent_voltage <= 15) { - buzzer_en.write(1); + if (m_battery_percent_couloumb <= 15 || m_battery_percent_voltage <= 15) { + m_buzzer_en.write(1); } else { - buzzer_en.write(0); + m_buzzer_en.write(0); } } diff --git a/rover-apps/pdb_2021/include/PDBMonitoring.h b/rover-apps/pdb_2021/include/PDBMonitoring.h index 8cf5c60e..fc97ce0b 100644 --- a/rover-apps/pdb_2021/include/PDBMonitoring.h +++ b/rover-apps/pdb_2021/include/PDBMonitoring.h @@ -20,12 +20,15 @@ class PDBMonitoring final : public Module { void load_monitoring(); void rail_monitoring(); void temperature_monitoring(); - void LED_matrix(); static const float PDB_VBAT_RAIL_NOMINAL_VOLTAGE; static const float PDB_VBAT_RAIL_MIN_THRESHOLD; static const float PDB_VBAT_RAIL_MAX_THRESHOLD; + static const float PDB_24V_RAIL_NOMINAL_VOLTAGE; + static const float PDB_24V_RAIL_MIN_THRESHOLD; + static const float PDB_24V_RAIL_MAX_THRESHOLD; + static const float PDB_17V_RAIL_NOMINAL_VOLTAGE; static const float PDB_17V_RAIL_MIN_THRESHOLD; static const float PDB_17V_RAIL_MAX_THRESHOLD; @@ -41,6 +44,7 @@ class PDBMonitoring final : public Module { static const bool PDB_5V_LOAD2_DIAG_EN; static const bool PDB_5V_LOAD3_DIAG_EN; static const bool PDB_5V_LOAD4_DIAG_EN; + static const bool PDB_5V_LOAD5_DIAG_EN; static const bool PDB_17V_LOAD_DIAG_EN; /* Pins configuration for Load Monitoring */ @@ -56,17 +60,18 @@ class PDBMonitoring final : public Module { DigitalOut load4_5V_diag_en; DigitalIn load4_5V_fault_n; + DigitalOut load5_5V_diag_en; + DigitalIn load5_5V_fault_n; + DigitalOut load_17V_diag_en; DigitalIn load_17V_fault_n; - DigitalOut LED_matrix_red; - PwmOut LED_matrix_green; - DigitalOut LED_matrix_blue; - /* Pins configuration for Rail Monitoring */ AnalogIn railBattery; AnalogIn rail5V; AnalogIn rail17V; + AnalogIn rail24V; + DigitalIn rail24V_pgood_n; /* Pins configuration for Temperature Monitoring */ AnalogIn temperatureADC; diff --git a/rover-apps/pdb_2021/src/PDBMonitoring.cpp b/rover-apps/pdb_2021/src/PDBMonitoring.cpp index 22091fd8..21fc5a82 100644 --- a/rover-apps/pdb_2021/src/PDBMonitoring.cpp +++ b/rover-apps/pdb_2021/src/PDBMonitoring.cpp @@ -2,14 +2,16 @@ #include -#include "CANConfig.h" #include "Logger.h" -#include "hw_bridge.h" -const float PDBMonitoring::PDB_VBAT_RAIL_NOMINAL_VOLTAGE = 48.0; +const float PDBMonitoring::PDB_VBAT_RAIL_NOMINAL_VOLTAGE = 24.0; const float PDBMonitoring::PDB_VBAT_RAIL_MIN_THRESHOLD = 18.0; const float PDBMonitoring::PDB_VBAT_RAIL_MAX_THRESHOLD = 25.2; +const float PDBMonitoring::PDB_24V_RAIL_NOMINAL_VOLTAGE = 24.0; +const float PDBMonitoring::PDB_24V_RAIL_MIN_THRESHOLD = 22.0; +const float PDBMonitoring::PDB_24V_RAIL_MAX_THRESHOLD = 26.0; + const float PDBMonitoring::PDB_17V_RAIL_NOMINAL_VOLTAGE = 17.0; const float PDBMonitoring::PDB_17V_RAIL_MIN_THRESHOLD = 16.0; const float PDBMonitoring::PDB_17V_RAIL_MAX_THRESHOLD = 18.0; @@ -21,9 +23,12 @@ const float PDBMonitoring::PDB_5V_RAIL_MAX_THRESHOLD = 6.0; const float PDBMonitoring::PDB_TEMPERATURE_MIN_THRESHOLD = 10.0; const float PDBMonitoring::PDB_TEMPERATURE_MAX_THRESHOLD = 50.0; -HWBRIDGE::PDB_LED_MATRIX_STATE_VALUES LED_matrix_status = HWBRIDGE::PDB_LED_MATRIX_STATE_VALUES::False; - -CANInterface can(CANConfig::config); +const bool PDBMonitoring::PDB_5V_LOAD1_DIAG_EN = 1; +const bool PDBMonitoring::PDB_5V_LOAD2_DIAG_EN = 0; +const bool PDBMonitoring::PDB_5V_LOAD3_DIAG_EN = 0; +const bool PDBMonitoring::PDB_5V_LOAD4_DIAG_EN = 0; +const bool PDBMonitoring::PDB_5V_LOAD5_DIAG_EN = 0; +const bool PDBMonitoring::PDB_17V_LOAD_DIAG_EN = 1; PDBMonitoring::PDBMonitoring() : load1_5V_diag_en(LOAD1_5V_DIAG_EN), @@ -34,6 +39,8 @@ PDBMonitoring::PDBMonitoring() load3_5V_fault_n(LOAD3_5V_FAULT), load4_5V_diag_en(LOAD4_5V_DIAG_EN), load4_5V_fault_n(LOAD4_5V_FAULT), + load5_5V_diag_en(LOAD5_5V_DIAG_EN), + load5_5V_fault_n(LOAD5_5V_FAULT), load_17V_diag_en(LOAD_17V_DIAG_EN), load_17V_fault_n(LOAD_17V_FAULT), railBattery(RAIL_BATTERY_ANLG_IN), @@ -41,16 +48,13 @@ PDBMonitoring::PDBMonitoring() rail17V(RAIL_17V_ANLG_IN), rail24V(RAIL_24V_ANLG_IN), rail24V_pgood_n(RAIL_24V_PGOOD_N), - temperatureADC(TEMPERATURE_ADC_IN), - LED_matrix_red(LED_MATRIX_R_CHANNEL), - LED_matrix_blue(LED_MATRIX_B_CHANNEL), - LED_matrix_green(LED_MATRIX_G_CHANNEL), -{ - load1_5V_diag_en.write(1); - load2_5V_diag_en.write(1); - load3_5V_diag_en.write(1); - load4_5V_diag_en.write(1); - load_17V_diag_en.write(1); + temperatureADC(TEMPERATURE_ADC_IN) { + load1_5V_diag_en = PDB_5V_LOAD1_DIAG_EN; + load2_5V_diag_en = PDB_5V_LOAD2_DIAG_EN; + load3_5V_diag_en = PDB_5V_LOAD3_DIAG_EN; + load4_5V_diag_en = PDB_5V_LOAD4_DIAG_EN; + load5_5V_diag_en = PDB_5V_LOAD5_DIAG_EN; + load_17V_diag_en = PDB_17V_LOAD_DIAG_EN; } /*TODO:Replace the logger statements with CAN logs @@ -58,116 +62,61 @@ PDBMonitoring::PDBMonitoring() void PDBMonitoring::load_monitoring() { if (load1_5V_diag_en && !load1_5V_fault_n) { - load1_5V_diag_en.write(0); Utility::logger << "Fault on 5V load 1\n"; - } else { - load1_5V_diag_en.write(1); } if (load2_5V_diag_en && !load2_5V_fault_n) { Utility::logger << "Fault on 5V load 2\n"; - load2_5V_diag_en.write(0); - } else { - load2_5V_diag_en.write(1); } if (load3_5V_diag_en && !load3_5V_fault_n) { Utility::logger << "Fault on 5V load 3\n"; - load3_5V_diag_en.write(0); - } else { - load3_5V_diag_en.write(1); } if (load4_5V_diag_en && !load4_5V_fault_n) { Utility::logger << "Fault on 5V load 4\n"; - load4_5V_diag_en.write(0); - } else { - load4_5V_diag_en.write(1); + } + if (load5_5V_diag_en && !load5_5V_fault_n) { + Utility::logger << "Fault on 5V load 5\n"; } if (load_17V_diag_en && !load_17V_fault_n) { Utility::logger << "Fault on 17V load\n"; - load_17V_diag_en.write(0); - } else { - load_17V_diag_en.write(1); } } void PDBMonitoring::rail_monitoring() { float rail_vbat_voltage = railBattery.read_voltage() / 3 * PDB_VBAT_RAIL_NOMINAL_VOLTAGE; + float rail_24V_voltage = rail24V.read_voltage() / 3 * PDB_24V_RAIL_NOMINAL_VOLTAGE; float rail_17V_voltage = rail17V.read_voltage() / 3 * PDB_17V_RAIL_NOMINAL_VOLTAGE; float rail_5V_voltage = rail5V.read_voltage() / 3 * PDB_5V_RAIL_NOMINAL_VOLTAGE; if (rail_vbat_voltage < PDB_VBAT_RAIL_MIN_THRESHOLD || rail_vbat_voltage > PDB_VBAT_RAIL_MAX_THRESHOLD) { Utility::logger << "!!! VBAT RAIL VOLTAGE: %.3fV !!!\n", rail_vbat_voltage; } + if (rail_24V_voltage < PDB_24V_RAIL_MIN_THRESHOLD || rail_24V_voltage > PDB_24V_RAIL_MAX_THRESHOLD) { + Utility::logger << "!!! 24V RAIL VOLTAGE: %.3fV !!!\n", rail_24V_voltage; + } if (rail_17V_voltage < PDB_17V_RAIL_MIN_THRESHOLD || rail_17V_voltage > PDB_17V_RAIL_MAX_THRESHOLD) { Utility::logger << "!!! 17V RAIL VOLTAGE: %.3fV !!!\n", rail_17V_voltage; } if (rail_5V_voltage < PDB_5V_RAIL_MIN_THRESHOLD || rail_5V_voltage > PDB_5V_RAIL_MAX_THRESHOLD) { Utility::logger << "!!! 5V RAIL VOLTAGE: %.3fV !!!\n", rail_5V_voltage; } - - // Sends can status messaging - // TODO: Make CAN sending work - if (rail_17V_voltage < PDB_17V_RAIL_MIN_THRESHOLD) { - can.setTXSignalValue(HWBRIDGE::CANID::PDB_REPORT_FAULTS, HWBRIDGE::CANSIGNAL::PDB_17_V_RAIL_STATUS_VALUES, - HWBRIDGE::CANSIGNAL::PDB_17_V_RAIL_STATUS_VALUES::UNDERVOLTAGE); - } else if (rail_17V_voltage > PDB_17V_RAIL_MAX_THRESHOLD) { - can.setTXSignalValue(HWBRIDGE::CANID::PDB_REPORT_FAULTS, HWBRIDGE::CANSIGNAL::PDB_17_V_RAIL_STATUS_VALUES, - HWBRIDGE::CANSIGNAL::PDB_17_V_RAIL_STATUS_VALUES::OVERVOLTAGE); - } else { - can.setTXSignalValue(HWBRIDGE::CANID::PDB_REPORT_FAULTS, HWBRIDGE::CANSIGNAL::PDB_17_V_RAIL_STATUS_VALUES, - HWBRIDGE::CANSIGNAL::PDB_17_V_RAIL_STATUS_VALUES::OPERATIONAL); - } - - if (rail_5V_voltage < PDB_5V_RAIL_MIN_THRESHOLD) { - can.setTXSignalValue(HWBRIDGE::CANID::PDB_REPORT_FAULTS, HWBRIDGE::CANSIGNAL::PDB_5_V_RAIL_STATUS_VALUES, - HWBRIDGE::CANSIGNAL::PDB_5_V_RAIL_STATUS_VALUES::UNDERVOLTAGE); - } else if (rail_5V_voltage > PDB_5V_RAIL_MAX_THRESHOLD) { - can.setTXSignalValue(HWBRIDGE::CANID::PDB_REPORT_FAULTS, HWBRIDGE::CANSIGNAL::PDB_5_V_RAIL_STATUS_VALUES, - HWBRIDGE::CANSIGNAL::PDB_5_V_RAIL_STATUS_VALUES::OVERVOLTAGE); - } else { - can.setTXSignalValue(HWBRIDGE::CANID::PDB_REPORT_FAULTS, HWBRIDGE::CANSIGNAL::PDB_5_V_RAIL_STATUS_VALUES, - HWBRIDGE::CANSIGNAL::PDB_5_V_RAIL_STATUS_VALUES::OPERATIONAL); - } -} -void PDBMonitoring::LED_matrix() { - // TODO: Make CAN receive work - can.getRXSignalValue(HWBRIDGE::CANID::PDB_SET_LED_MATRIX, HWBRIDGE::CANSIGNAL::PDB_LED_MATRIX_STATE, - LED_matrix_status); - if (LED_matrix_status == HWBRIDGE::PDB_LED_MATRIX_STATE_VALUES::SOLID_RED) { - LED_matrix_red.write(1); - LED_matrix_green.write(0); - LED_matrix_blue.write(0); - } else if (LED_matrix_status == HWBRIDGE::PDB_LED_MATRIX_STATE_VALUES::FLASHING_GREEN) { - LED_matrix_green.write(0.50f); - LED_matrix_blue.write(0); - LED_matrix_red.write(0); - } else if (LED_matrix_status == HWBRIDGE::PDB_LED_MATRIX_STATE_VALUES::SOLID_BLUE) { - LED_matrix_blue.write(1); - LED_matrix_red.write(0); - LED_matrix_green.write(0); - } else { - LED_matrix_blue.write(0); - LED_matrix_red.write(0); - LED_matrix_green.write(0); - } } /*https://www.ti.com/lit/ds/symlink/lmt87-q1.pdf?ts=1627158177761&ref_url=https%253A%252F%252Fwww.google.com%252F*/ -// void PDBMonitoring::temperature_monitoring() { -// float temperature_celsius = -// (13.582 - sqrt(pow(-13.582, 2) + 4 * 0.00433 * (2230.8 - temperatureADC.read_voltage() * 1000))) / -// (2 * (-0.00433)) + -// 30; +void PDBMonitoring::temperature_monitoring() { + float temperature_celsius = + (13.582 - sqrt(pow(-13.582, 2) + 4 * 0.00433 * (2230.8 - temperatureADC.read_voltage() * 1000))) / + (2 * (-0.00433)) + + 30; -// if (temperature_celsius < PDB_TEMPERATURE_MIN_THRESHOLD || temperature_celsius > PDB_TEMPERATURE_MAX_THRESHOLD) { -// Utility::logger << "!!! TEMPERATURE FAULT:" << temperature_celsius << "degrees Celsius\n"; -// } -// } + if (temperature_celsius < PDB_TEMPERATURE_MIN_THRESHOLD || temperature_celsius > PDB_TEMPERATURE_MAX_THRESHOLD) { + Utility::logger << "!!! TEMPERATURE FAULT:" << temperature_celsius << "degrees Celsius\n"; + } +} void PDBMonitoring::periodic_1s() { - // temperature_monitoring(); + temperature_monitoring(); load_monitoring(); - LED_matrix(); } void PDBMonitoring::periodic_1ms() { diff --git a/rover-apps/pdb_2022/CMakeLists.txt b/rover-apps/pdb_2022/CMakeLists.txt new file mode 100644 index 00000000..fd6392d2 --- /dev/null +++ b/rover-apps/pdb_2022/CMakeLists.txt @@ -0,0 +1,25 @@ +add_library(PDB_Monitoring2022 STATIC) +target_sources(PDB_Monitoring2022 PRIVATE src/PDBMonitoring.cpp) +target_include_directories(PDB_Monitoring2022 PUBLIC + include + ${ROVER_APPS_DIR}/common/include + ) +target_link_libraries(PDB_Monitoring2022 + PRIVATE + Logger + mbed-os + ) + +add_executable(pdb_2022) +target_sources(pdb_2022 PRIVATE ${ROVER_APPS_DIR}/common/src/main.cpp) +target_include_directories(pdb_2022 PUBLIC include ${ROVER_APPS_DIR}/common/include) +target_link_libraries(pdb_2022 + PRIVATE + CANBus + CANMsg + Logger + uwrt-mars-rover-hw-bridge + #Modules + PDB_Monitoring2022 + ) +mbed_set_post_build(pdb_2022) diff --git a/rover-apps/pdb_2022/include/AppConfig.h b/rover-apps/pdb_2022/include/AppConfig.h new file mode 100644 index 00000000..0f841d33 --- /dev/null +++ b/rover-apps/pdb_2022/include/AppConfig.h @@ -0,0 +1,12 @@ +#pragma once + +#include + +#include "Module.h" +#include "PDBMonitoring.h" + +PDBMonitoring PDB_monitoring; + +std::vector gModules = { + &PDB_monitoring, +}; diff --git a/rover-apps/pdb_2022/include/PDBMonitoring.h b/rover-apps/pdb_2022/include/PDBMonitoring.h new file mode 100644 index 00000000..8cf5c60e --- /dev/null +++ b/rover-apps/pdb_2022/include/PDBMonitoring.h @@ -0,0 +1,73 @@ +#pragma once + +#include "Module.h" +#include "mbed.h" + +/*This PDB module is for load, rail and temperature monitoring.*/ + +class PDBMonitoring final : public Module { + public: + /* Sets the Load DIAG_EN pins */ + PDBMonitoring(); + + void periodic_1s(void) override; + void periodic_10s(void) override {} + void periodic_100ms(void) override {} + void periodic_10ms(void) override {} + void periodic_1ms(void) override; + + private: + void load_monitoring(); + void rail_monitoring(); + void temperature_monitoring(); + void LED_matrix(); + + static const float PDB_VBAT_RAIL_NOMINAL_VOLTAGE; + static const float PDB_VBAT_RAIL_MIN_THRESHOLD; + static const float PDB_VBAT_RAIL_MAX_THRESHOLD; + + static const float PDB_17V_RAIL_NOMINAL_VOLTAGE; + static const float PDB_17V_RAIL_MIN_THRESHOLD; + static const float PDB_17V_RAIL_MAX_THRESHOLD; + + static const float PDB_5V_RAIL_NOMINAL_VOLTAGE; + static const float PDB_5V_RAIL_MIN_THRESHOLD; + static const float PDB_5V_RAIL_MAX_THRESHOLD; + + static const float PDB_TEMPERATURE_MIN_THRESHOLD; + static const float PDB_TEMPERATURE_MAX_THRESHOLD; + + static const bool PDB_5V_LOAD1_DIAG_EN; + static const bool PDB_5V_LOAD2_DIAG_EN; + static const bool PDB_5V_LOAD3_DIAG_EN; + static const bool PDB_5V_LOAD4_DIAG_EN; + static const bool PDB_17V_LOAD_DIAG_EN; + + /* Pins configuration for Load Monitoring */ + DigitalOut load1_5V_diag_en; + DigitalIn load1_5V_fault_n; + + DigitalOut load2_5V_diag_en; + DigitalIn load2_5V_fault_n; + + DigitalOut load3_5V_diag_en; + DigitalIn load3_5V_fault_n; + + DigitalOut load4_5V_diag_en; + DigitalIn load4_5V_fault_n; + + DigitalOut load_17V_diag_en; + DigitalIn load_17V_fault_n; + + DigitalOut LED_matrix_red; + PwmOut LED_matrix_green; + DigitalOut LED_matrix_blue; + + /* Pins configuration for Rail Monitoring */ + AnalogIn railBattery; + AnalogIn rail5V; + AnalogIn rail17V; + + /* Pins configuration for Temperature Monitoring */ + AnalogIn temperatureADC; +}; \ No newline at end of file diff --git a/rover-apps/pdb_2022/src/PDBMonitoring.cpp b/rover-apps/pdb_2022/src/PDBMonitoring.cpp new file mode 100644 index 00000000..22091fd8 --- /dev/null +++ b/rover-apps/pdb_2022/src/PDBMonitoring.cpp @@ -0,0 +1,183 @@ +#include "PDBMonitoring.h" + +#include + +#include "CANConfig.h" +#include "Logger.h" +#include "hw_bridge.h" + +const float PDBMonitoring::PDB_VBAT_RAIL_NOMINAL_VOLTAGE = 48.0; +const float PDBMonitoring::PDB_VBAT_RAIL_MIN_THRESHOLD = 18.0; +const float PDBMonitoring::PDB_VBAT_RAIL_MAX_THRESHOLD = 25.2; + +const float PDBMonitoring::PDB_17V_RAIL_NOMINAL_VOLTAGE = 17.0; +const float PDBMonitoring::PDB_17V_RAIL_MIN_THRESHOLD = 16.0; +const float PDBMonitoring::PDB_17V_RAIL_MAX_THRESHOLD = 18.0; + +const float PDBMonitoring::PDB_5V_RAIL_NOMINAL_VOLTAGE = 5.0; +const float PDBMonitoring::PDB_5V_RAIL_MIN_THRESHOLD = 4.8; +const float PDBMonitoring::PDB_5V_RAIL_MAX_THRESHOLD = 6.0; + +const float PDBMonitoring::PDB_TEMPERATURE_MIN_THRESHOLD = 10.0; +const float PDBMonitoring::PDB_TEMPERATURE_MAX_THRESHOLD = 50.0; + +HWBRIDGE::PDB_LED_MATRIX_STATE_VALUES LED_matrix_status = HWBRIDGE::PDB_LED_MATRIX_STATE_VALUES::False; + +CANInterface can(CANConfig::config); + +PDBMonitoring::PDBMonitoring() + : load1_5V_diag_en(LOAD1_5V_DIAG_EN), + load1_5V_fault_n(LOAD1_5V_FAULT), + load2_5V_diag_en(LOAD2_5V_DIAG_EN), + load2_5V_fault_n(LOAD2_5V_FAULT), + load3_5V_diag_en(LOAD3_5V_DIAG_EN), + load3_5V_fault_n(LOAD3_5V_FAULT), + load4_5V_diag_en(LOAD4_5V_DIAG_EN), + load4_5V_fault_n(LOAD4_5V_FAULT), + load_17V_diag_en(LOAD_17V_DIAG_EN), + load_17V_fault_n(LOAD_17V_FAULT), + railBattery(RAIL_BATTERY_ANLG_IN), + rail5V(RAIL_5V_ANLG_IN), + rail17V(RAIL_17V_ANLG_IN), + rail24V(RAIL_24V_ANLG_IN), + rail24V_pgood_n(RAIL_24V_PGOOD_N), + temperatureADC(TEMPERATURE_ADC_IN), + LED_matrix_red(LED_MATRIX_R_CHANNEL), + LED_matrix_blue(LED_MATRIX_B_CHANNEL), + LED_matrix_green(LED_MATRIX_G_CHANNEL), +{ + load1_5V_diag_en.write(1); + load2_5V_diag_en.write(1); + load3_5V_diag_en.write(1); + load4_5V_diag_en.write(1); + load_17V_diag_en.write(1); +} + +/*TODO:Replace the logger statements with CAN logs + * after CAN Module is ready */ + +void PDBMonitoring::load_monitoring() { + if (load1_5V_diag_en && !load1_5V_fault_n) { + load1_5V_diag_en.write(0); + Utility::logger << "Fault on 5V load 1\n"; + } else { + load1_5V_diag_en.write(1); + } + if (load2_5V_diag_en && !load2_5V_fault_n) { + Utility::logger << "Fault on 5V load 2\n"; + load2_5V_diag_en.write(0); + } else { + load2_5V_diag_en.write(1); + } + if (load3_5V_diag_en && !load3_5V_fault_n) { + Utility::logger << "Fault on 5V load 3\n"; + load3_5V_diag_en.write(0); + } else { + load3_5V_diag_en.write(1); + } + if (load4_5V_diag_en && !load4_5V_fault_n) { + Utility::logger << "Fault on 5V load 4\n"; + load4_5V_diag_en.write(0); + } else { + load4_5V_diag_en.write(1); + } + if (load_17V_diag_en && !load_17V_fault_n) { + Utility::logger << "Fault on 17V load\n"; + load_17V_diag_en.write(0); + } else { + load_17V_diag_en.write(1); + } +} + +void PDBMonitoring::rail_monitoring() { + float rail_vbat_voltage = railBattery.read_voltage() / 3 * PDB_VBAT_RAIL_NOMINAL_VOLTAGE; + float rail_17V_voltage = rail17V.read_voltage() / 3 * PDB_17V_RAIL_NOMINAL_VOLTAGE; + float rail_5V_voltage = rail5V.read_voltage() / 3 * PDB_5V_RAIL_NOMINAL_VOLTAGE; + + if (rail_vbat_voltage < PDB_VBAT_RAIL_MIN_THRESHOLD || rail_vbat_voltage > PDB_VBAT_RAIL_MAX_THRESHOLD) { + Utility::logger << "!!! VBAT RAIL VOLTAGE: %.3fV !!!\n", rail_vbat_voltage; + } + if (rail_17V_voltage < PDB_17V_RAIL_MIN_THRESHOLD || rail_17V_voltage > PDB_17V_RAIL_MAX_THRESHOLD) { + Utility::logger << "!!! 17V RAIL VOLTAGE: %.3fV !!!\n", rail_17V_voltage; + } + if (rail_5V_voltage < PDB_5V_RAIL_MIN_THRESHOLD || rail_5V_voltage > PDB_5V_RAIL_MAX_THRESHOLD) { + Utility::logger << "!!! 5V RAIL VOLTAGE: %.3fV !!!\n", rail_5V_voltage; + } + + // Sends can status messaging + // TODO: Make CAN sending work + if (rail_17V_voltage < PDB_17V_RAIL_MIN_THRESHOLD) { + can.setTXSignalValue(HWBRIDGE::CANID::PDB_REPORT_FAULTS, HWBRIDGE::CANSIGNAL::PDB_17_V_RAIL_STATUS_VALUES, + HWBRIDGE::CANSIGNAL::PDB_17_V_RAIL_STATUS_VALUES::UNDERVOLTAGE); + } else if (rail_17V_voltage > PDB_17V_RAIL_MAX_THRESHOLD) { + can.setTXSignalValue(HWBRIDGE::CANID::PDB_REPORT_FAULTS, HWBRIDGE::CANSIGNAL::PDB_17_V_RAIL_STATUS_VALUES, + HWBRIDGE::CANSIGNAL::PDB_17_V_RAIL_STATUS_VALUES::OVERVOLTAGE); + } else { + can.setTXSignalValue(HWBRIDGE::CANID::PDB_REPORT_FAULTS, HWBRIDGE::CANSIGNAL::PDB_17_V_RAIL_STATUS_VALUES, + HWBRIDGE::CANSIGNAL::PDB_17_V_RAIL_STATUS_VALUES::OPERATIONAL); + } + + if (rail_5V_voltage < PDB_5V_RAIL_MIN_THRESHOLD) { + can.setTXSignalValue(HWBRIDGE::CANID::PDB_REPORT_FAULTS, HWBRIDGE::CANSIGNAL::PDB_5_V_RAIL_STATUS_VALUES, + HWBRIDGE::CANSIGNAL::PDB_5_V_RAIL_STATUS_VALUES::UNDERVOLTAGE); + } else if (rail_5V_voltage > PDB_5V_RAIL_MAX_THRESHOLD) { + can.setTXSignalValue(HWBRIDGE::CANID::PDB_REPORT_FAULTS, HWBRIDGE::CANSIGNAL::PDB_5_V_RAIL_STATUS_VALUES, + HWBRIDGE::CANSIGNAL::PDB_5_V_RAIL_STATUS_VALUES::OVERVOLTAGE); + } else { + can.setTXSignalValue(HWBRIDGE::CANID::PDB_REPORT_FAULTS, HWBRIDGE::CANSIGNAL::PDB_5_V_RAIL_STATUS_VALUES, + HWBRIDGE::CANSIGNAL::PDB_5_V_RAIL_STATUS_VALUES::OPERATIONAL); + } +} +void PDBMonitoring::LED_matrix() { + // TODO: Make CAN receive work + can.getRXSignalValue(HWBRIDGE::CANID::PDB_SET_LED_MATRIX, HWBRIDGE::CANSIGNAL::PDB_LED_MATRIX_STATE, + LED_matrix_status); + if (LED_matrix_status == HWBRIDGE::PDB_LED_MATRIX_STATE_VALUES::SOLID_RED) { + LED_matrix_red.write(1); + LED_matrix_green.write(0); + LED_matrix_blue.write(0); + } else if (LED_matrix_status == HWBRIDGE::PDB_LED_MATRIX_STATE_VALUES::FLASHING_GREEN) { + LED_matrix_green.write(0.50f); + LED_matrix_blue.write(0); + LED_matrix_red.write(0); + } else if (LED_matrix_status == HWBRIDGE::PDB_LED_MATRIX_STATE_VALUES::SOLID_BLUE) { + LED_matrix_blue.write(1); + LED_matrix_red.write(0); + LED_matrix_green.write(0); + } else { + LED_matrix_blue.write(0); + LED_matrix_red.write(0); + LED_matrix_green.write(0); + } +} + +/*https://www.ti.com/lit/ds/symlink/lmt87-q1.pdf?ts=1627158177761&ref_url=https%253A%252F%252Fwww.google.com%252F*/ + +// void PDBMonitoring::temperature_monitoring() { +// float temperature_celsius = +// (13.582 - sqrt(pow(-13.582, 2) + 4 * 0.00433 * (2230.8 - temperatureADC.read_voltage() * 1000))) / +// (2 * (-0.00433)) + +// 30; + +// if (temperature_celsius < PDB_TEMPERATURE_MIN_THRESHOLD || temperature_celsius > PDB_TEMPERATURE_MAX_THRESHOLD) { +// Utility::logger << "!!! TEMPERATURE FAULT:" << temperature_celsius << "degrees Celsius\n"; +// } +// } + +void PDBMonitoring::periodic_1s() { + // temperature_monitoring(); + load_monitoring(); + LED_matrix(); +} + +void PDBMonitoring::periodic_1ms() { + /*Monitoring Period = 500ms*/ + int rail_counter = 0; + + if (rail_counter % 5 == 0) { + rail_monitoring(); + rail_counter = 0; + } + + rail_counter++; +} diff --git a/rover-apps/pdb_2022/src/main.cpp b/rover-apps/pdb_2022/src/main.cpp new file mode 100644 index 00000000..12607c59 --- /dev/null +++ b/rover-apps/pdb_2022/src/main.cpp @@ -0,0 +1,51 @@ +#include + +#include "CANBus.h" +#include "CANMsg.h" +#include "Logger.h" +#include "hw_bridge.h" + +AnalogIn railBattery(RAIL_BATTERY_ANLG_IN), rail5V(RAIL_5V_ANLG_IN), rail17V(RAIL_17V_ANLG_IN), + rail24V(RAIL_24V_ANLG_IN); // add voltage range (as percentage) to hw bridge also allocate can id for reporting if + // outside range + +CANBus can1(CAN1_RX, CAN1_TX, HWBRIDGE::ROVER_CANBUS_FREQUENCY_HZ); + +void rxCANProcessor(); +void txCANProcessor(); + +// simple switch statement that calls a different function based on contents of CAN msg +static mbed_error_status_t setLEDMatrix(void); + +const static CANMsg::CANMsgHandlerMap canHandlerMap = {{HWBRIDGE::CANID::PDB_SET_LED_MATRIX, &setLEDMatrix}}; + +int main() { + Thread rxCANProcessorThread(osPriorityAboveNormal); + Thread txCANProcessorThread(osPriorityBelowNormal); + while (true) { + // if (std::abs((railBattery.read() - 3) * 100 / 3) < some percent defined in hw bridge) {send to xavier} + // repeat for each digital in + } +} + +void rxCANProcessor() { + CANMsg rxMsg; + while (true) { + if (can1.read(rxMsg)) { + canHandlerMap.at(rxMsg.getID())(); + } + ThisThread::sleep_for(2ms); + } +} + +void txCANProcessor() { + CANMsg txMsg; + while (true) { + // stream ultrasonic data + ThisThread::sleep_for(2ms); // TODO: define the sleep rate in the bridge + } +} + +static mbed_error_status_t setLEDMatrix(void) { + return MBED_ERROR_INVALID_ARGUMENT; +} diff --git a/supported_build_configurations.yaml b/supported_build_configurations.yaml index 3ff066c1..33700644 100644 --- a/supported_build_configurations.yaml +++ b/supported_build_configurations.yaml @@ -13,6 +13,9 @@ gimbal_2021: pdb_2021: - PDB_REV2_2021 +pdb_2022: + - PDB_REV2_2021 + science_2021: - SCIENCE_REV2_2021