Skip to content

Commit

Permalink
Merge pull request #123 from Northeastern-Electric-Racing/feature/crc…
Browse files Browse the repository at this point in the history
…-can

Crc over CAN, permanent debug can msg
  • Loading branch information
jr1221 authored Sep 20, 2024
2 parents 479d02e + 218bdf0 commit 31e741d
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 130 deletions.
4 changes: 4 additions & 0 deletions Core/Inc/compute.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ void compute_send_cell_temp_message(acc_data_t *bmsdata);
void compute_send_segment_temp_message(acc_data_t *bmsdata);

void compute_send_fault_message(uint8_t status, int16_t curr, int16_t in_dcl);

void compute_send_debug_message(uint8_t debug0, uint8_t debug1, uint16_t debug2,
uint32_t debug3);

void compute_send_voltage_noise_message(acc_data_t *bmsdata);

#endif // COMPUTE_H
73 changes: 39 additions & 34 deletions Core/Inc/stateMachine.h
Original file line number Diff line number Diff line change
@@ -1,74 +1,79 @@
#ifndef BMS_STATES_H
#define BMS_STATES_H

#include "analyzer.h"
#include "compute.h"
#include "datastructs.h"
#include "segment.h"
#include "compute.h"
#include "analyzer.h"
#include "timer.h"

/* global that can be read for debugging in main */
extern BMSState_t current_state;

/* global defined in segment.c that keeps an eye on the number of crc errors */
extern uint16_t crc_error_check;

#define NUM_FAULTS 8

/**
* @brief Returns if we want to balance cells during a particular frame
*
* @param bmsdata
* @return true
* @return false
*/
* @brief Returns if we want to balance cells during a particular frame
*
* @param bmsdata
* @return true
* @return false
*/
bool sm_balancing_check(acc_data_t *bmsdata);

/**
* @brief Returns if we want to charge cells during a particular frame
*
* @param bmsdata
* @return true
* @return false
*/
* @brief Returns if we want to charge cells during a particular frame
*
* @param bmsdata
* @return true
* @return false
*/
bool sm_charging_check(acc_data_t *bmsdata);

/**
* @brief Returns any new faults or current faults that have come up
* @note Should be bitwise OR'ed with the current fault status
*
* @param accData
* @return uint32_t
*/
* @brief Returns any new faults or current faults that have come up
* @note Should be bitwise OR'ed with the current fault status
*
* @param accData
* @return uint32_t
*/
uint32_t sm_fault_return(acc_data_t *accData);

/**
* @brief Used in parellel to faultReturn(), calculates each fault to append the fault status
*
* @brief Used in parellel to faultReturn(), calculates each fault to append the
* fault status
*
* @param index
* @return fault_code
*/
uint32_t sm_fault_eval(fault_eval_t *index);

/**
* @brief handles the state machine, calls the appropriate handler function and runs every loop functions
*
* @param bmsdata
* @brief handles the state machine, calls the appropriate handler function and
* runs every loop functions
*
* @param bmsdata
*/
void sm_handle_state(acc_data_t *bmsdata);

/**
* @brief Algorithm behind determining which cells we want to balance
* @note Directly interfaces with the segments
*
* @param bms_data
*/
* @brief Algorithm behind determining which cells we want to balance
* @note Directly interfaces with the segments
*
* @param bms_data
*/
void sm_balance_cells(acc_data_t *bms_data);
void sm_broadcast_current_limit(acc_data_t *bmsdata);

/**
* @brief algorithm to calculate and set fan speed based on temperature
*
* @param bmsdata
*
*/
* @param bmsdata
*
*/
void calculate_pwm(acc_data_t *bmsdata);

#endif //BMS_STATES_H
#endif // BMS_STATES_H
89 changes: 63 additions & 26 deletions Core/Src/compute.c
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#include "compute.h"
#include "can_handler.h"
#include "can.h"
#include "c_utils.h"
#include "can.h"
#include "can_handler.h"
#include "main.h"
#include <assert.h>
#include "stm32f405xx.h"
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include <string.h>

#define MAX_CAN1_STORAGE 10
#define MAX_CAN2_STORAGE 10

#define REF_CHANNEL 0
#define VOUT_CHANNEL 1

//#define CHARGING_ENABLED
// #define CHARGING_ENABLED

uint8_t fan_speed;
bool is_charging_enabled;
Expand Down Expand Up @@ -50,14 +50,14 @@ uint8_t compute_init()
can1.hcan = &hcan1;
can1.id_list = can1_id_list;
can1.id_list_len = sizeof(can1_id_list) / sizeof(can1_id_list[0]);
//can1.callback = can_receive_callback;
// can1.callback = can_receive_callback;
can1_rx_queue = ringbuffer_create(MAX_CAN1_STORAGE, sizeof(can_msg_t));
can_init(&can1);

can2.hcan = &hcan2;
can2.id_list = can2_id_list;
can2.id_list_len = sizeof(can2_id_list) / sizeof(can2_id_list[0]);
//can2.callback = can_receive_callback;
// can2.callback = can_receive_callback;
can2_rx_queue = ringbuffer_create(MAX_CAN2_STORAGE, sizeof(can_msg_t));
can_init(&can2);

Expand Down Expand Up @@ -95,8 +95,10 @@ int compute_send_charging_message(uint16_t voltage_to_set,
uint16_t current_to_set, acc_data_t *bms_data)
{
struct __attribute__((__packed__)) {
uint16_t charger_voltage; // Note the charger voltage sent over should be 10*desired voltage
uint16_t charger_current; // Note the charge current sent over should be 10*desired current
uint16_t charger_voltage; // Note the charger voltage sent over should be
// 10*desired voltage
uint16_t charger_current; // Note the charge current sent over should be
// 10*desired current
uint8_t charger_control;
uint8_t reserved_1;
uint16_t reserved_23;
Expand All @@ -106,7 +108,7 @@ int compute_send_charging_message(uint16_t voltage_to_set,
charger_msg_data.charger_current = current_to_set * 10;

if (is_charging_enabled) {
charger_msg_data.charger_control = 0x00; //0:Start charging.
charger_msg_data.charger_control = 0x00; // 0:Start charging.
} else {
charger_msg_data.charger_control =
0xFF; // 1:battery protection, stop charging
Expand Down Expand Up @@ -139,15 +141,15 @@ int compute_send_charging_message(uint16_t voltage_to_set,

bool compute_charger_connected()
{
//TODO need to set up CAN msg that actually toggles this bool
return false; //bmsdata->is_charger_connected;
// TODO need to set up CAN msg that actually toggles this bool
return false; // bmsdata->is_charger_connected;
}

//TODO add this back
// void compute_charger_callback(const CAN_message_t& msg)
// {
// return;
// }
// TODO add this back
// void compute_charger_callback(const CAN_message_t& msg)
// {
// return;
// }

uint8_t compute_set_fan_speed(TIM_HandleTypeDef *pwmhandle,
fan_select_t fan_select, uint8_t duty_cycle)
Expand All @@ -170,9 +172,9 @@ uint8_t compute_set_fan_speed(TIM_HandleTypeDef *pwmhandle,

void compute_set_fault(int fault_state)
{
//TODO work with charger fw on this
// TODO work with charger fw on this
HAL_GPIO_WritePin(GPIOA, Fault_Output_Pin, !fault_state);
//if (true) digitalWrite(CHARGE_SAFETY_RELAY, 1);
// if (true) digitalWrite(CHARGE_SAFETY_RELAY, 1);
}

int16_t compute_get_pack_current()
Expand All @@ -192,13 +194,13 @@ int16_t compute_get_pack_current()
// int16_t current = (vout - ref_voltage - OFFSET) / (GAIN); // convert to V

// /* Low Pass Filter of Current*/
// current = ((current_accumulator * (num_samples - 1)) + current) / num_samples;
// current_accumulator = current;
// current = ((current_accumulator * (num_samples - 1)) + current) /
// num_samples; current_accumulator = current;

// return current;

static const float CURRENT_LOWCHANNEL_MAX = 75.0; //Amps
static const float CURRENT_LOWCHANNEL_MIN = -75.0; //Amps
static const float CURRENT_LOWCHANNEL_MAX = 75.0; // Amps
static const float CURRENT_LOWCHANNEL_MIN = -75.0; // Amps
// static const float CURRENT_SUPPLY_VOLTAGE = 5.038;
static const float CURRENT_ADC_RESOLUTION = 5.0 / MAX_ADC_RESOLUTION;

Expand Down Expand Up @@ -250,15 +252,16 @@ int16_t compute_get_pack_current()
(1000 * CURRENT_LOWCHANNEL_OFFSET)) *
(1 / 26.7); //* (LOWCHANNEL_GAIN/100.0f))/1000;

// If the current is scoped within the range of the low channel, use the low channel
// If the current is scoped within the range of the low channel, use the low
// channel

if ((low_current < CURRENT_LOWCHANNEL_MAX - 5.0 && low_current >= 0) ||
(low_current > CURRENT_LOWCHANNEL_MIN + 5.0 && low_current < 0)) {
//printf("\rLow Current: %d\n", -low_current);
// printf("\rLow Current: %d\n", -low_current);
return -low_current;
}

//printf("\rHigh Current: %d\n", -high_current);
// printf("\rHigh Current: %d\n", -high_current);
return -high_current;
}

Expand Down Expand Up @@ -666,6 +669,40 @@ void compute_send_voltage_noise_message(acc_data_t *bmsdata)

can_send_msg(line, &acc_msg);
}

void compute_send_debug_message(uint8_t debug0, uint8_t debug1, uint16_t debug2,
uint32_t debug3)
{
struct __attribute__((__packed__)) {
uint8_t debug0;
uint8_t debug1;
uint16_t debug2;
uint32_t debug3;
} debug_msg_data;

debug_msg_data.debug0 = debug0;
debug_msg_data.debug1 = debug1;
debug_msg_data.debug2 = debug2;
debug_msg_data.debug3 = debug3;

can_msg_t debug_msg;
debug_msg.id = 0x702;
debug_msg.len = 8; // yaml decodes this msg as 8 bytes

endian_swap(&debug_msg_data.debug2, sizeof(debug_msg_data.debug2));
endian_swap(&debug_msg_data.debug3, sizeof(debug_msg_data.debug3));

memcpy(debug_msg.data, &debug_msg_data, 8);

#ifdef CHARGING_ENABLED
can_t *line = &can2;
#else
can_t *line = &can1;
#endif

can_send_msg(line, &debug_msg);
}

void change_adc1_channel(uint8_t channel)
{
ADC_ChannelConfTypeDef sConfig = { 0 };
Expand Down
Loading

0 comments on commit 31e741d

Please sign in to comment.