This commit is contained in:
hamza 2024-07-19 17:40:53 +03:00
parent a8d8b6d696
commit e33579318e
8 changed files with 82 additions and 71 deletions

View File

@ -81,4 +81,11 @@ V1.13
- moved macros and libraries from eeprom.c to h - moved macros and libraries from eeprom.c to h
- added can.h to state_machine.h - added can.h to state_machine.h
- added balancing to AMS_HighLevel.c (hopefully) - added balancing to AMS_HighLevel.c (hopefully)
- fixed can_handle_dump() - fixed can_handle_dump()
V1.14
- added programming mode to program the ESCs
- removed void status_led_blink_sequence(uint8_t blinks, color color) and added proper blinking
- fixed the bit shift of temperature in the can message
- moved tmp1075_measure() and can_handle_send_status() to the state_machine
-

View File

@ -55,6 +55,8 @@ typedef struct {
typedef enum { RELAY_MAIN, RELAY_PRECHARGE } Relay; typedef enum { RELAY_MAIN, RELAY_PRECHARGE } Relay;
extern StateHandle state; extern StateHandle state;
extern bool programming_mode;
extern int32_t RELAY_BAT_SIDE_VOLTAGE; extern int32_t RELAY_BAT_SIDE_VOLTAGE;
extern int32_t RELAY_ESC_SIDE_VOLTAGE; extern int32_t RELAY_ESC_SIDE_VOLTAGE;
extern int32_t CURRENT_MEASUREMENT; extern int32_t CURRENT_MEASUREMENT;
@ -67,6 +69,7 @@ void sm_powerground_manager();
void sm_calibrate_powerground(); void sm_calibrate_powerground();
void sm_balancing(); void sm_balancing();
void sm_eeprom_write_status(); void sm_eeprom_write_status();
void sm_program_powerground();
void sm_check_errors(); void sm_check_errors();
void sm_set_error_source(); void sm_set_error_source();

View File

@ -25,7 +25,6 @@ typedef enum {
void status_led_init(TIM_HandleTypeDef* r, TIM_HandleTypeDef* g, TIM_HandleTypeDef* b); void status_led_init(TIM_HandleTypeDef* r, TIM_HandleTypeDef* g, TIM_HandleTypeDef* b);
void status_led_update(); void status_led_update();
void status_led_blink_sequence(uint8_t blinks, color color);
void status_led_set_color(color color); void status_led_set_color(color color);
void status_led_set(uint8_t r, uint8_t g, uint8_t b); void status_led_set(uint8_t r, uint8_t g, uint8_t b);

View File

@ -66,5 +66,6 @@ void PWM_powerground_softcontrol(){
__HAL_TIM_SET_COMPARE(powerground, TIM_CHANNEL_3, ccr); __HAL_TIM_SET_COMPARE(powerground, TIM_CHANNEL_3, ccr);
__HAL_TIM_SET_COMPARE(powerground, TIM_CHANNEL_4, ccr); __HAL_TIM_SET_COMPARE(powerground, TIM_CHANNEL_4, ccr);
} }
void PWM_battery_cooling_control(uint8_t percent){} void PWM_battery_cooling_control(uint8_t percent){}
void PWM_esc_cooling(uint8_t percent){} void PWM_esc_cooling(uint8_t percent){}

View File

@ -16,7 +16,7 @@
#define CAN_ID_IN 0x501 #define CAN_ID_IN 0x501
#define CAN_ID_OUT 0x502 #define CAN_ID_OUT 0x502
#define CAN_STATUS_FREQ 100 #define CAN_STATUS_FREQ 1000
#define CAN_DUMP_FREQ 10; #define CAN_DUMP_FREQ 10;
uint8_t last_message[8]; uint8_t last_message[8];
@ -85,7 +85,7 @@ void can_handle_send_status() {
data[4] = ((RELAY_BAT_SIDE_VOLTAGE / 1000)); // 8 bit battery voltage data[4] = ((RELAY_BAT_SIDE_VOLTAGE / 1000)); // 8 bit battery voltage
data[5] = ((RELAY_ESC_SIDE_VOLTAGE / 1000)); // 8 bit Inverter voltage data[5] = ((RELAY_ESC_SIDE_VOLTAGE / 1000)); // 8 bit Inverter voltage
data[6] = ((CURRENT_MEASUREMENT / 1000)); // 8 bit Current data[6] = ((CURRENT_MEASUREMENT / 1000)); // 8 bit Current
data[7] = ((highest_temp) >> 8); // 8 bit highest cell temperature data[7] = ((highest_temp) >> 4); // 8 bit highest cell temperature
//data[6] = (module.cellVoltages[7] >> 8); //data[6] = (module.cellVoltages[7] >> 8);
//data[7] = (module.cellVoltages[7]); //data[7] = (module.cellVoltages[7]);
//data[7] = state.error_source; //data[7] = state.error_source;
@ -158,6 +158,11 @@ it only checks if the id is and datalen is correct thans hands data over to can_
in MXCUBE under CAN NVIC settings "USB low priority or CAN_RX0 interrupts" has to be on in MXCUBE under CAN NVIC settings "USB low priority or CAN_RX0 interrupts" has to be on
*/ */
void ftcan_msg_received_cb(uint16_t id, size_t datalen, const uint8_t *data){ void ftcan_msg_received_cb(uint16_t id, size_t datalen, const uint8_t *data){
if (programming_mode == 1){
can_handle_recieve_command(data);
return;
}
if (id == 0x501 && datalen == 2){ if (id == 0x501 && datalen == 2){
if (last_message[0] != data[0] || last_message[1] != data[1]){ if (last_message[0] != data[0] || last_message[1] != data[1]){
last_message[0] = data[0]; last_message[0] = data[0];

View File

@ -138,7 +138,7 @@ int main(void)
PWM_control_init(&htim3, &htim2, &htim15); PWM_control_init(&htim3, &htim2, &htim15);
soc_init(); soc_init();
status_led_init(&htim4, &htim4, &htim4); status_led_init(&htim4, &htim4, &htim4);
sm_program_powerground();
//AMS_Loop(); //AMS_Loop();
//int ttrgrtd = 2000 + HAL_GetTick(); //int ttrgrtd = 2000 + HAL_GetTick();
//while (ttrgrtd > HAL_GetTick()); //while (ttrgrtd > HAL_GetTick());
@ -153,9 +153,7 @@ int main(void)
/* USER CODE BEGIN 3 */ /* USER CODE BEGIN 3 */
AMS_Loop(); AMS_Loop();
sm_update(); sm_update();
tmp1075_measure();
//sm_test_cycle_states(); //sm_test_cycle_states();
can_handle_send_status();
} }
/* USER CODE END 3 */ /* USER CODE END 3 */
} }

View File

@ -7,6 +7,7 @@
#include <state_machine.h> #include <state_machine.h>
#include "AMS_HighLevel.h" #include "AMS_HighLevel.h"
#include "PWM_control.h"
// Time to wait after reaching 95% of battery voltage before exiting precharge // Time to wait after reaching 95% of battery voltage before exiting precharge
// Set this to 1000 in scruti to demonstrate the voltage on the multimeter // Set this to 1000 in scruti to demonstrate the voltage on the multimeter
@ -22,6 +23,10 @@
// waiting time between to eeprom writes // waiting time between to eeprom writes
#define EEPROM_WRITE_FREQ 1000 #define EEPROM_WRITE_FREQ 1000
#define POWERGROUND_SOFTSTART_INCREMENT_DELAY 3
bool programming_mode;
StateHandle state; StateHandle state;
int32_t RELAY_BAT_SIDE_VOLTAGE; int32_t RELAY_BAT_SIDE_VOLTAGE;
int32_t RELAY_ESC_SIDE_VOLTAGE; int32_t RELAY_ESC_SIDE_VOLTAGE;
@ -49,27 +54,27 @@ void sm_init(){
state.error_source = 0; state.error_source = 0;
precharge_timer = discharge_timer = powerground_calibration_timer; precharge_timer = discharge_timer = powerground_calibration_timer;
CAN_timer = HAL_GetTick() + 5000; CAN_timer = HAL_GetTick() + 5000;
programming_mode = 0;
} }
#warning change amsState here #warning change amsState here
void sm_update(){ void sm_update(){
can_handle_send_status();
sm_check_errors(); sm_check_errors();
sm_precharge_discharge_manager(); sm_precharge_discharge_manager();
sm_calibrate_powerground(); sm_calibrate_powerground();
sm_powerground_manager(); sm_powerground_manager();
tmp1075_measure();
status_led_update();
soc_update(); soc_update();
if (CAN_timer < HAL_GetTick()) //if (CAN_timer < HAL_GetTick())
state.current_state = state.target_state = STATE_ERROR; // state.current_state = state.target_state = STATE_ERROR;
CURRENT_MEASUREMENT = (module.auxVoltages[0] > 2494) ? (module.auxVoltages[0] - (2494.0)) * (300.0) : 0;
if (state.current_state == STATE_INACTIVE || state.current_state == STATE_ERROR){
CURRENT_MEASUREMENT = (module.auxVoltages[0] > 2494) ? (module.auxVoltages[0] - (2494.0)) * (300.0) : 0;
} else {
CURRENT_MEASUREMENT = (module.auxVoltages[0] > 2477) ? (module.auxVoltages[0] - (2477.0)) * (4600.0) : 0;
}
CURRENT_MEASUREMENT_ON = (module.auxVoltages[1] > 2400); CURRENT_MEASUREMENT_ON = (module.auxVoltages[1] > 2400);
RELAY_ESC_SIDE_VOLTAGE = module.auxVoltages[2] * 15.98; RELAY_ESC_SIDE_VOLTAGE = module.auxVoltages[2] * 15.25;
RELAY_BAT_SIDE_VOLTAGE = module.auxVoltages[3] * 15.98; // the calculation says the factor is 11. 11.711 yields the better result RELAY_BAT_SIDE_VOLTAGE = module.auxVoltages[3] * 15.25; // the calculation says the factor is 11. 11.711 yields the better result
switch (state.current_state) { switch (state.current_state) {
@ -104,6 +109,8 @@ void sm_update(){
} }
void sm_handle_ams_in(const uint8_t *data){ void sm_handle_ams_in(const uint8_t *data){
if (programming_mode == 1 && (state.current_state == STATE_READY || state.current_state == STATE_ACTIVE)){ PWM_powerground_control(data[1]); }
CAN_timer = HAL_GetTick() + CAN_TIMEOUT; CAN_timer = HAL_GetTick() + CAN_TIMEOUT;
switch (data[0]) { switch (data[0]) {
case 0x00: case 0x00:
@ -181,11 +188,11 @@ void sm_powerground_manager(){
if (current_powerground_status < target_powerground_status ){ if (current_powerground_status < target_powerground_status ){
current_powerground_status++; current_powerground_status++;
PWM_powerground_softcontrol(); PWM_powerground_softcontrol();
powerground_softstart_timer = HAL_GetTick() + 10; powerground_softstart_timer = HAL_GetTick() + POWERGROUND_SOFTSTART_INCREMENT_DELAY;
} else if (current_powerground_status > target_powerground_status) { } else if (current_powerground_status > target_powerground_status) {
current_powerground_status--; current_powerground_status--;
PWM_powerground_softcontrol(); PWM_powerground_softcontrol();
powerground_softstart_timer = HAL_GetTick() + 10; powerground_softstart_timer = HAL_GetTick() + POWERGROUND_SOFTSTART_INCREMENT_DELAY;
} }
} }
} }
@ -221,17 +228,12 @@ void sm_calibrate_powerground(){
} }
void sm_program_powerground(){ void sm_program_powerground(){
powerground_calibration_timer = HAL_GetTick() + 5000; if (programming_mode == 0)
while (powerground_calibration_timer > HAL_GetTick()){} return;
state.current_state = STATE_PRECHARGE;
sm_update();
powerground_calibration_timer = HAL_GetTick() + PRECHARGE_DURATION + 1000;
PWM_powerground_control(100); PWM_powerground_control(100);
while (powerground_calibration_timer > HAL_GetTick()){} state.current_state = state.target_state = STATE_ACTIVE;
sm_update();
while (1) { while (1) {
can_handle_send_status();
} }
} }
@ -243,6 +245,7 @@ void sm_eeprom_write_status(){
} }
void sm_check_errors(){ void sm_check_errors(){
if (programming_mode == 1) {return;} // to disable error checking
state.error_type.temperature_error = (error_data.error_sources & (1 << 0) || error_data.error_sources & (1 << 1) || error_data.error_sources & (1 << 4)) ? 1 : 0; state.error_type.temperature_error = (error_data.error_sources & (1 << 0) || error_data.error_sources & (1 << 1) || error_data.error_sources & (1 << 4)) ? 1 : 0;
state.error_type.voltage_error = (error_data.error_sources & (1 << 2)|| error_data.error_sources & (1 << 3)|| error_data.error_sources & (1 << 5) || RELAY_BAT_SIDE_VOLTAGE < 30000) ? 1 : 0; state.error_type.voltage_error = (error_data.error_sources & (1 << 2)|| error_data.error_sources & (1 << 3)|| error_data.error_sources & (1 << 5) || RELAY_BAT_SIDE_VOLTAGE < 30000) ? 1 : 0;
state.error_type.bms_timeout = (error_data.error_sources & (1 << 7)) ? 1 : 0; state.error_type.bms_timeout = (error_data.error_sources & (1 << 7)) ? 1 : 0;

View File

@ -25,15 +25,14 @@ Prescaler:
F_CLK = 16 MHz F_CLK = 16 MHz
*/ */
#define STATUS_LED_ARR 255 #define STATUS_LED_ARR 255
#define BLINK_LENGTH 300 #define TIME_BLINK_OFF 100
#define TIME_BETWEEN_BLINKS 200 #define TIME_BLINK_ON 1000
#define TIME_BETWEEN_BLINK_SEQS 500
TIM_HandleTypeDef* red; TIM_HandleTypeDef* red;
TIM_HandleTypeDef* green; TIM_HandleTypeDef* green;
TIM_HandleTypeDef* blue; TIM_HandleTypeDef* blue;
uint8_t blinked;
uint32_t blink_timer; uint32_t blink_timer;
bool blink_state; bool blink_state;
@ -44,7 +43,6 @@ void status_led_init(TIM_HandleTypeDef* r, TIM_HandleTypeDef* g, TIM_HandleTypeD
blink_timer = 0; blink_timer = 0;
blink_state = 0; blink_state = 0;
blinked = 0;
HAL_TIM_PWM_Start(red, TIM_CHANNEL_1); //TIM4CH1 HAL_TIM_PWM_Start(red, TIM_CHANNEL_1); //TIM4CH1
HAL_TIM_PWM_Start(green, TIM_CHANNEL_2); //TIM4CH2 HAL_TIM_PWM_Start(green, TIM_CHANNEL_2); //TIM4CH2
@ -54,6 +52,24 @@ void status_led_init(TIM_HandleTypeDef* r, TIM_HandleTypeDef* g, TIM_HandleTypeD
} }
void status_led_update(){ void status_led_update(){
if (state.current_state == STATE_ERROR){
status_led_set_color(RED);
return;
}
if(blink_timer > HAL_GetTick()){
return;
}
if (blink_state == 1){
blink_timer = HAL_GetTick() + TIME_BLINK_OFF;
blink_state = 0;
status_led_set_color(OFF);
return;
} else {
blink_timer = HAL_GetTick() + TIME_BLINK_ON;
blink_state = 1;
}
switch (state.current_state) { switch (state.current_state) {
case STATE_INACTIVE: case STATE_INACTIVE:
status_led_set_color(GREEN); status_led_set_color(GREEN);
@ -74,52 +90,31 @@ void status_led_update(){
} }
} }
void status_led_blink_sequence(uint8_t blinks, color color){
if (blink_timer < HAL_GetTick())
return;
if (blink_state){
if (blinked != blinks){
blink_timer = HAL_GetTick() + BLINK_LENGTH;
blinks++;
status_led_set_color(GREEN);
} else {
blink_timer = HAL_GetTick() + TIME_BETWEEN_BLINK_SEQS;
blinks = 0;
}
} else {
if (blinked != blinks){
blink_timer = HAL_GetTick() + TIME_BETWEEN_BLINKS;
status_led_set_color(OFF);
}
}
}
void status_led_set_color(color color){ void status_led_set_color(color color){
switch (color) { switch (color) {
case RED: case RED:
status_led_set(255, 0, 0);
break;
case GREEN:
status_led_set(0, 255, 0);
break;
case BLUE:
status_led_set(0, 0, 255);
break;
case YELLOW:
status_led_set(255, 255, 0);
break;
case PINK:
status_led_set(255, 0, 255);
break;
case CYAN:
status_led_set(0, 255, 255); status_led_set(0, 255, 255);
break; break;
case GREEN:
status_led_set(255, 0, 255);
break;
case BLUE:
status_led_set(255, 255, 0);
break;
case YELLOW:
status_led_set(0, 0, 255);
break;
case PINK:
status_led_set(0, 255, 0);
break;
case CYAN:
status_led_set(255, 0, 0);
break;
case WHITE: case WHITE:
status_led_set(255, 255, 255); status_led_set(0, 0, 0);
break; break;
case OFF: case OFF:
status_led_set(0,0,0); status_led_set(255,255,255);
break; break;
} }
} }