Compare commits
	
		
			3 Commits
		
	
	
		
			dae660d8f2
			...
			79f69333a5
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 79f69333a5 | |||
| b86d165c41 | |||
|  | 537641a53a | 
| @ -20,14 +20,12 @@ CCR * DUTY_CYCLE | ||||
| CCR: 1/20 -> 500, 2/20 -> 1000 | ||||
| */ | ||||
| #define POWERGROUND_FREQ            50 | ||||
| #define POWERGROUND_MAX_DUTY_CYCLE  0.1 | ||||
| #define POWERGROUND_MIN_DUTY_CYCLE  0.05 | ||||
| 
 | ||||
| //#define BATTERY_COOLING_FREQ        20000
 | ||||
| 
 | ||||
| void PWM_control_init(TIM_HandleTypeDef* pg, TIM_HandleTypeDef* bat_cool, TIM_HandleTypeDef* esc_cool); | ||||
| void PWM_powerground_control(uint8_t percent); | ||||
| void PWM_battery_cooling_control(uint8_t percent); | ||||
| void PWM_esc_cooling(uint8_t percent); | ||||
| void PWM_set_throttle(); | ||||
| 
 | ||||
| #endif /* INC_CHANNEL_CONTROL_H */ | ||||
|  | ||||
| @ -37,7 +37,6 @@ typedef enum {              //  states -> 3 bit. valid transitions: (all could t | ||||
| } State; | ||||
|    | ||||
| typedef struct {   | ||||
| 
 | ||||
|   uint16_t bms_timeout : 1; | ||||
|   uint16_t bms_fault : 1; | ||||
|   uint16_t temperature_error : 1; | ||||
| @ -46,7 +45,8 @@ typedef struct { | ||||
|   uint16_t voltage_error : 1; | ||||
|   uint16_t voltage_missing : 1; | ||||
|   uint16_t state_transition_fail : 1; | ||||
| 
 | ||||
|   uint16_t eeprom_error : 1; | ||||
|   uint16_t : 7; // padding
 | ||||
| } ErrorKind; | ||||
| 
 | ||||
| //typedef enum {} WarningKind;
 | ||||
| @ -59,8 +59,8 @@ typedef struct { | ||||
| } StateHandle; | ||||
| 
 | ||||
| extern StateHandle state; | ||||
| static bool relay_closed = 0; | ||||
| static bool precharge_closed = 0; | ||||
| static bool relay_closed = 0; //NOTE: unused?
 | ||||
| static bool precharge_closed = 0; //NOTE: unused?
 | ||||
| extern int16_t RELAY_BAT_SIDE_VOLTAGE; | ||||
| extern int16_t RELAY_ESC_SIDE_VOLTAGE; | ||||
| extern int16_t CURRENT_MEASUREMENT; | ||||
|  | ||||
| @ -1,4 +1,6 @@ | ||||
| #include "PWM_control.h" | ||||
| #include "stm32f3xx_hal.h" | ||||
| #include <stdint.h> | ||||
| 
 | ||||
| //uint32_t powerground1_CCR, powerground2_CCR, battery_cooling_CCR;
 | ||||
| 
 | ||||
| @ -11,32 +13,51 @@ TIM_HandleTypeDef *powerground, *battery_cooling, *esc_cooling; | ||||
| 
 | ||||
| void PWM_control_init(TIM_HandleTypeDef* pg, TIM_HandleTypeDef* bat_cool, TIM_HandleTypeDef* esc_cool){ | ||||
|   powerground_status = 0; | ||||
|   //battery_cooling_status = 0;
 | ||||
|    | ||||
|   powerground = pg; | ||||
|   battery_cooling = bat_cool; | ||||
| 
 | ||||
|   HAL_TIM_PWM_Start(pg, TIM_CHANNEL_1);          //TIM15CH1
 | ||||
|   HAL_TIM_PWM_Start(pg, TIM_CHANNEL_2);          //TIM15CH2
 | ||||
|   HAL_TIM_PWM_Start(bat_cool, TIM_CHANNEL_3);     //TIM1CH3
 | ||||
| 
 | ||||
|   powerground = pg; | ||||
|   battery_cooling = bat_cool; | ||||
|   esc_cooling = esc_cool; | ||||
|    | ||||
|   __HAL_TIM_SET_COMPARE(powerground, TIM_CHANNEL_1, 2000); | ||||
|   __HAL_TIM_SET_COMPARE(powerground, TIM_CHANNEL_2, 2000); | ||||
|   __HAL_TIM_SET_COMPARE(powerground, TIM_CHANNEL_1, 0); | ||||
|   __HAL_TIM_SET_COMPARE(powerground, TIM_CHANNEL_2, 0); | ||||
| 
 | ||||
|   //PWM_powerground_control(0);
 | ||||
|   //__HAL_TIM_SET_COMPARE(battery_cooling, TIM_CHANNEL_3, 2000);
 | ||||
| }               | ||||
| 
 | ||||
| /*
 | ||||
|   controls the duty cycle of the fans by setting the CCR of the channel percent/100 = x/ARR | ||||
|   DUTYCYCLE = 40000 * X/100 | ||||
| */ | ||||
| void PWM_powerground_control(uint8_t percent){ | ||||
|   if (percent > 100) //something went wrong
 | ||||
|     return; | ||||
|   powerground_status = percent; | ||||
| 
 | ||||
|   int ccr = 2000 + ((2000) * (percent/100.0)); | ||||
|   __HAL_TIM_SET_COMPARE(powerground, TIM_CHANNEL_1, ccr); | ||||
|   __HAL_TIM_SET_COMPARE(powerground, TIM_CHANNEL_2, 2000 + ((2000) * (percent/100.0))); | ||||
|   __HAL_TIM_SET_COMPARE(powerground, TIM_CHANNEL_2, ccr); | ||||
|   //TIM15->CCR1 = (TIM15->ARR*POWERGROUND_MAX_DUTY_CYCLE-TIM15->ARR*POWERGROUND_MIN_DUTY_CYCLE) * (percent/100.0) + TIM15->ARR*POWERGROUND_MIN_DUTY_CYCLE;
 | ||||
| } | ||||
| 
 | ||||
| void PWM_set_throttle(){ | ||||
|   uint32_t timestamp = HAL_GetTick() + 5000; | ||||
|   while (timestamp > HAL_GetTick()) {} | ||||
| 
 | ||||
|   __HAL_TIM_SET_COMPARE(powerground, TIM_CHANNEL_1, 4000); | ||||
|   __HAL_TIM_SET_COMPARE(powerground, TIM_CHANNEL_2, 4000); | ||||
|   timestamp = HAL_GetTick() + 2000; | ||||
|   while (timestamp > HAL_GetTick()) {} | ||||
| 
 | ||||
|   __HAL_TIM_SET_COMPARE(powerground, TIM_CHANNEL_1, 2000); | ||||
|   __HAL_TIM_SET_COMPARE(powerground, TIM_CHANNEL_2, 2000); | ||||
|   timestamp = HAL_GetTick() + 1000; | ||||
|   while (timestamp > HAL_GetTick()) {} | ||||
| } | ||||
| 
 | ||||
| void PWM_battery_cooling_control(uint8_t percent){} | ||||
| void PWM_esc_cooling(uint8_t percent){} | ||||
|  | ||||
| @ -8,7 +8,7 @@ | ||||
| 
 | ||||
| //#define CAN_ID_IN   0x501
 | ||||
| //#define CAN_ID_OUT  0x502
 | ||||
| int can_delay_manager = 0; | ||||
| static uint32_t can_delay_manager = 0; | ||||
| void can_init(CAN_HandleTypeDef* hcan) {  | ||||
|   ftcan_init(hcan);  | ||||
|   ftcan_add_filter(CAN_ID_IN, 0xFFF); | ||||
|  | ||||
| @ -5,7 +5,6 @@ | ||||
| #include "main.h" | ||||
| #include "stm32f3xx_hal.h" | ||||
| #include <stdint.h> | ||||
| #include <stdio.h> | ||||
| 
 | ||||
| StateHandle state; | ||||
| int16_t RELAY_BAT_SIDE_VOLTAGE; | ||||
| @ -13,7 +12,7 @@ int16_t RELAY_ESC_SIDE_VOLTAGE; | ||||
| int16_t CURRENT_MEASUREMENT; | ||||
| uint8_t powerground_status; | ||||
| 
 | ||||
| uint32_t timestamp; | ||||
| static uint32_t timestamp; | ||||
| 
 | ||||
| void sm_init(){ | ||||
|   state.current_state = STATE_INACTIVE; | ||||
| @ -75,9 +74,11 @@ State sm_update_precharge(){ | ||||
|     case STATE_INACTIVE:          // if CAN Signal 0000 0000 then immidiete shutdown
 | ||||
|       return STATE_DISCHARGE; | ||||
|     case STATE_PRECHARGE: | ||||
|       if (RELAY_BAT_SIDE_VOLTAGE-RELAY_ESC_SIDE_VOLTAGE < 100) | ||||
|       if (RELAY_BAT_SIDE_VOLTAGE-RELAY_ESC_SIDE_VOLTAGE < 100){ | ||||
|         PWM_set_throttle(); | ||||
|         return STATE_READY; | ||||
|       break; | ||||
|       } | ||||
|       return STATE_PRECHARGE; | ||||
|     case STATE_DISCHARGE: | ||||
|       return STATE_DISCHARGE; | ||||
|     default: | ||||
| @ -110,8 +111,7 @@ State sm_update_active(){ | ||||
| State sm_update_discharge(){ | ||||
|   switch (state.target_state) { | ||||
|     case STATE_DISCHARGE: | ||||
|       if (RELAY_ESC_SIDE_VOLTAGE < 5000) | ||||
|         return STATE_INACTIVE; | ||||
|       return (RELAY_ESC_SIDE_VOLTAGE < 5000) ? STATE_INACTIVE : STATE_DISCHARGE; | ||||
|     case STATE_PRECHARGE:       // if CAN Signal 1000 0000 then get ready
 | ||||
|       return STATE_PRECHARGE; | ||||
|     default:  | ||||
| @ -229,21 +229,18 @@ void sm_handle_ams_in(const uint8_t *data){ | ||||
|   switch (data[0]) { | ||||
|     case 0x00: | ||||
|       if (state.current_state != STATE_INACTIVE){ | ||||
|         PWM_powerground_control(0); | ||||
|         state.target_state = STATE_DISCHARGE; | ||||
|       } | ||||
|       break; | ||||
|     case 0x01: | ||||
|       if (state.target_state == STATE_INACTIVE || state.target_state == STATE_DISCHARGE){ | ||||
|         PWM_powerground_control(0); | ||||
|         state.target_state = STATE_PRECHARGE; | ||||
|       } else if (state.target_state == STATE_ACTIVE){ | ||||
|         PWM_powerground_control(0); | ||||
|         state.target_state = STATE_READY; | ||||
|       } | ||||
|       break; | ||||
|     case 0x02: | ||||
|       if (state.current_state == STATE_READY){ | ||||
|       if (state.current_state == STATE_READY || state.current_state == STATE_ACTIVE){ | ||||
|         PWM_powerground_control(data[1]); | ||||
|         state.target_state = STATE_ACTIVE;        // READY -> ACTIVE
 | ||||
|       } | ||||
| @ -260,12 +257,18 @@ void sm_check_errors(){ | ||||
|     case SEK_UNDERTEMP: | ||||
|     case SEK_TOO_FEW_TEMPS: | ||||
|       state.error_type.temperature_error = 1; | ||||
|       break; | ||||
|     case SEK_OVERVOLT: | ||||
|     case SEK_UNDERVOLT: | ||||
|     case SEK_OPENWIRE: | ||||
|       state.error_type.voltage_error = 1; | ||||
|       break; | ||||
|     case SEK_EEPROM_ERR: | ||||
|       state.error_type.eeprom_error = 1; | ||||
|       break; | ||||
|     case SEK_INTERNAL_BMS_TIMEOUT: | ||||
|       state.error_type.bms_timeout = 1; | ||||
|       break; | ||||
|     case SEK_INTERNAL_BMS_CHECKSUM_FAIL: | ||||
|     case SEK_INTERNAL_BMS_OVERTEMP: | ||||
|     case SEK_INTERNAL_BMS_FAULT: | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user