Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Bms/pdb #415

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,11 @@ 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)
add_app_subdirectory(${ROVER_APPS_DIR}/pdb_2022)
add_app_subdirectory(${ROVER_APPS_DIR}/science_2021)

# Include Test Apps
Expand Down
6 changes: 3 additions & 3 deletions libs/can/include/CANInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
25 changes: 25 additions & 0 deletions rover-apps/bms_2022/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 12 additions & 0 deletions rover-apps/bms_2022/include/AppConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include <vector>

#include "BMSMonitoring.h"
#include "Module.h"

BMSMonitoring BMS_Monitoring;

std::vector<Module*> gModules = {
&BMS_Monitoring,
};
42 changes: 42 additions & 0 deletions rover-apps/bms_2022/include/BMSMonitoring.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#pragma once

#include <cmath>
#include <iostream>
#include <map>

#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 {
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();
void update_voltage_percent();
void low_battery_warning();
void send_can_data();

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;
};
127 changes: 127 additions & 0 deletions rover-apps/bms_2022/src/BMSMonitoring.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#include "BMSMonitoring.h"
#include <iostream>

#include <cmath>
#include <map>

#include "Logger.h"
#include "mbed.h"
#include "PinNames.h"

#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() {
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() {
m_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 = (m_is_cell_balancing ? 0b111111111111111100000000 : 0b000000000000000000000000);

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;
}
m_chip_select.write(1);
update_voltage_percent();
}

void BMSMonitoring::update_voltage_percent() {
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 {
m_battery_percent_voltage = 0;
}
}

void BMSMonitoring::current_monitoring() {
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 (m_battery_percent_couloumb <= 15 || m_battery_percent_voltage <= 15) {
m_buzzer_en.write(1);
} else {
m_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();
}
25 changes: 25 additions & 0 deletions rover-apps/pdb_2022/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 12 additions & 0 deletions rover-apps/pdb_2022/include/AppConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include <vector>

#include "Module.h"
#include "PDBMonitoring.h"

PDBMonitoring PDB_monitoring;

std::vector<Module*> gModules = {
&PDB_monitoring,
};
73 changes: 73 additions & 0 deletions rover-apps/pdb_2022/include/PDBMonitoring.h
Original file line number Diff line number Diff line change
@@ -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;
};
Loading