Skip to content

Commit

Permalink
moved objects, setup fan init
Browse files Browse the repository at this point in the history
  • Loading branch information
Dylan Donahue committed Mar 24, 2024
1 parent 77fca4a commit 3e23317
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 44 deletions.
15 changes: 13 additions & 2 deletions Core/Inc/compute.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,21 @@
#define MC_BAUD 1000000U
#define MAX_ADC_RESOLUTION 1023 // 13 bit ADC



typedef enum {
FAN1,
FAN2,
FAN3,
FAN4,
FAN5,
FAN6,
} fan_select_t;

/**
* @brief inits the compute interface
*/
void compute_init();
uint8_t compute_init();

/**
* @brief sets safeguard bool to check whether charging is enabled or disabled
Expand Down Expand Up @@ -62,7 +73,7 @@ bool compute_charger_connected();
*
* @return uint8_t 0 = success, 1 = fan_select is out of range, 2 = PWM channel not able to be configured
*/
uint8_t compute_set_fan_speed(uint8_t new_fan_speed, uint8_t fan_select);
uint8_t compute_set_fan_speed(TIM_HandleTypeDef* pwmhandle, fan_select_t fan_select, uint8_t duty_cycle);

/**
* @brief Returns the pack current sensor reading
Expand Down
9 changes: 9 additions & 0 deletions Core/Inc/stateMachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,13 @@ void sm_handle_state(acc_data_t *bmsdata);
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
*
*/
void calculate_pwm(acc_data_t *bmsdata);


#endif //BMS_STATES_H
78 changes: 40 additions & 38 deletions Core/Src/compute.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@

#define MAX_CAN1_STORAGE 10
#define MAX_CAN2_STORAGE 10
#define NUM_FANS_ON_TIM1 2
#define NUM_FANS_ON_TIM8 4
#define NUM_FANS_TOTAL NUM_FANS_ON_TIM1 + NUM_FANS_ON_TIM8

#define FAN1 TIM_CHANNEL_3
#define FAN2 TIM_CHANNEL_1
#define FAN3 TIM_CHANNEL_4
#define FAN4 TIM_CHANNEL_3
#define FAN5 TIM_CHANNEL_2
#define FAN6 TIM_CHANNEL_1

uint8_t fan_speed;
bool is_charging_enabled;
Expand All @@ -18,14 +22,22 @@ enum { CHARGE_ENABLED, CHARGE_DISABLED };
extern CAN_HandleTypeDef hcan1;
extern CAN_HandleTypeDef hcan2;

extern TIM_HandleTypeDef htim1;
extern TIM_HandleTypeDef htim8;

TIM_OC_InitTypeDef pwm_config;

const uint32_t fan_channels[6] = {TIM_CHANNEL_3, TIM_CHANNEL_1, TIM_CHANNEL_4, TIM_CHANNEL_3, TIM_CHANNEL_2, TIM_CHANNEL_1};

can_t can1; // main can bus, used by most peripherals
can_t can2; // p2p can bus with charger

/* private function defintions */
uint8_t calc_charger_led_state();

void compute_init()
uint8_t compute_init()
{
// TODO throw all of these objects into a compute struct
can1.hcan = &hcan1;
can1.id_list = can1_id_list;
can1.id_list_len = sizeof(can1_id_list) / sizeof(can1_id_list[0]);
Expand All @@ -39,6 +51,22 @@ void compute_init()
can2.callback = can_receive_callback;
can2_rx_queue = ringbuffer_create(MAX_CAN2_STORAGE, sizeof(can_msg_t));
can_init(&can2);

pwm_config.OCMode = TIM_OCMODE_PWM1;
pwm_config.Pulse = 0;
pwm_config.OCPolarity = TIM_OCPOLARITY_HIGH;
pwm_config.OCFastMode = TIM_OCFAST_DISABLE;

if (HAL_TIM_PWM_ConfigChannel(&htim1, &pwm_config, fan_channels[FAN1]) != HAL_OK) return -1;
if (HAL_TIM_PWM_ConfigChannel(&htim8, &pwm_config, fan_channels[FAN2]) != HAL_OK) return -1;

HAL_TIM_PWM_Start(&htim1, fan_channels[FAN1]);
HAL_TIM_PWM_Start(&htim1, fan_channels[FAN2]);
HAL_TIM_PWM_Start(&htim8, fan_channels[FAN3]);
HAL_TIM_PWM_Start(&htim8, fan_channels[FAN4]);
HAL_TIM_PWM_Start(&htim8, fan_channels[FAN5]);
HAL_TIM_PWM_Start(&htim8, fan_channels[FAN6]);

}

void compute_enable_charging(bool enable_charging)
Expand Down Expand Up @@ -103,44 +131,18 @@ bool compute_charger_connected()
// }

//? Change timers to not 1 and 8 since they are advanced timers?
uint8_t compute_set_fan_speed(uint8_t new_fan_speed, uint8_t fan_select)
uint8_t compute_set_fan_speed(TIM_HandleTypeDef* pwmhandle, fan_select_t fan_select, uint8_t duty_cycle)
{
// Define variables
TIM_HandleTypeDef htim;
TIM_OC_InitTypeDef PWMConfig;
uint16_t CCR_value = 0;

// Index of array +1 corresponds to which fan the channel controls
uint32_t channels[6] = {TIM_CHANNEL_3, TIM_CHANNEL_1, TIM_CHANNEL_4, TIM_CHANNEL_3, TIM_CHANNEL_2, TIM_CHANNEL_1};

// Select which timer to use based on fan being initialized
// Based on timer parameters, determine what pulse width count would give the correct duty cycle
if (fan_select > 0 && fan_select <= NUM_FANS_ON_TIM1){
htim.Instance = TIM1;
CCR_value = (TIM1->ARR * new_fan_speed) / 100;
}
else if (fan_select > NUM_FANS_ON_TIM1 && fan_select <= NUM_FANS_TOTAL){
htim.Instance = TIM8;
CCR_value = (TIM8->ARR * new_fan_speed) / 100;
}
else{
return 1;
}

// Set object to how PWM channel should be configured
PWMConfig.OCMode = TIM_OCMODE_PWM1;
PWMConfig.Pulse = CCR_value;
PWMConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
PWMConfig.OCFastMode = TIM_OCFAST_DISABLE;
assert(pwmhandle);
assert(fan_select < 6);
assert(duty_cycle <= 100);
uint32_t CCR_value = 0;

// Attempt to configure PWM channel
if (HAL_TIM_PWM_ConfigChannel(&htim, &PWMConfig, channels[fan_select]) != HAL_OK){
return 2;
}
uint32_t channel = fan_channels[fan_select];

// Call PWM start function for specific fan
HAL_TIM_PWM_Start(&htim, channels[fan_select]);
CCR_value = (pwmhandle->Instance->ARR * duty_cycle) / 100;

__HAL_TIM_SET_COMPARE(pwmhandle, channel, CCR_value);
return 0;
}

Expand Down
9 changes: 7 additions & 2 deletions Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,16 @@ int main(void)
/* USER CODE BEGIN 2 */

segment_init();
volatile uint8_t test = compute_init();

/* USER CODE END 2 */

/* Infinite loop */
/* USER CODE BEGIN WHILE */
for(;;) {
/* Create a dynamically allocated structure */

HAL_GPIO_TogglePin(Debug_LEDB11_GPIO_Port, Debug_LEDB11_Pin);
HAL_Delay(500);

//HAL_UART_Transmit(&huart4, (char*)"hello", 5, 1000);

acc_data_t *acc_data = malloc(sizeof(acc_data_t));
Expand All @@ -254,6 +255,10 @@ int main(void)
/* check for inbound CAN */
// get_can1_msg();
// get_can2_msg();

HAL_GPIO_WritePin(Watchdog_Out_GPIO_Port, Watchdog_Out_Pin, GPIO_PIN_SET);
//HAL_Delay(1);
HAL_GPIO_WritePin(Watchdog_Out_GPIO_Port, Watchdog_Out_Pin, GPIO_PIN_RESET);


#ifdef DEBUG_STATS
Expand Down
21 changes: 19 additions & 2 deletions Core/Src/stateMachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ nertimer_t charge_cut_off_timer = { .active = false };

nertimer_t can_msg_timer = { .active = false };

extern TIM_HandleTypeDef htim1;
extern TIM_HandleTypeDef htim8;

bool entered_faulted = false;

nertimer_t charger_message_timer;
Expand Down Expand Up @@ -167,8 +170,7 @@ void sm_handle_state(acc_data_t* bmsdata)
}
// TODO needs testing - (update, seems to work fine)
handler_LUT[current_state](bmsdata);

compute_set_fan_speed(analyzer_calc_fan_pwm());

sm_broadcast_current_limit(bmsdata);
char state_test[1] = "0";
state_test[0] = current_state + '0';
Expand Down Expand Up @@ -393,3 +395,18 @@ void sm_balance_cells(acc_data_t* bms_data)

segment_configure_balancing(balanceConfig);
}

void calculate_pwm(acc_data_t* bmsdata)
{
// todo actually implement algorithm

compute_set_fan_speed(&htim1, FAN1, 85);
compute_set_fan_speed(&htim1, FAN2, 85);
compute_set_fan_speed(&htim8, FAN3, 85);
compute_set_fan_speed(&htim8, FAN4, 85);
compute_set_fan_speed(&htim8, FAN5, 85);
compute_set_fan_speed(&htim8, FAN6, 85);

}


0 comments on commit 3e23317

Please sign in to comment.