fix PWM_control

This commit is contained in:
hamza 2024-07-11 21:13:35 +03:00
parent cc496b1140
commit aea84c620b
2 changed files with 15 additions and 5 deletions

View File

@ -61,5 +61,10 @@ void PWM_powerground_control(uint8_t percent){
__HAL_TIM_SET_COMPARE(powerground, TIM_CHANNEL_4, ccr); __HAL_TIM_SET_COMPARE(powerground, TIM_CHANNEL_4, ccr);
} }
void PWM_powerground_softcontrol(){
int ccr = 2000 + (20 * current_powerground_status);
__HAL_TIM_SET_COMPARE(powerground, TIM_CHANNEL_3, 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

@ -5,6 +5,7 @@
* Author: Hamza * Author: Hamza
*/ */
#include "state_machine.h"
#include "PWM_control.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
@ -56,7 +57,7 @@ void sm_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;
if (state.current_state == STATE_INACTIVE){ 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; CURRENT_MEASUREMENT = (module.auxVoltages[0] > 2494) ? (module.auxVoltages[0] - (2494.0)) * (300.0) : 0;
} else { } else {
CURRENT_MEASUREMENT = (module.auxVoltages[0] > 2477) ? (module.auxVoltages[0] - (2477.0)) * (4600.0) : 0; CURRENT_MEASUREMENT = (module.auxVoltages[0] > 2477) ? (module.auxVoltages[0] - (2477.0)) * (4600.0) : 0;
@ -117,12 +118,12 @@ void sm_handle_ams_in(const uint8_t *data){
break; break;
case 0x02: case 0x02:
if (state.current_state == STATE_READY || state.current_state == STATE_ACTIVE){ if (state.current_state == STATE_READY || state.current_state == STATE_ACTIVE){
target_powerground_status = data[1];
state.target_state = STATE_ACTIVE; // READY -> ACTIVE state.target_state = STATE_ACTIVE; // READY -> ACTIVE
} }
break; break;
case 0xF0: case 0xF0:
if (state.current_state == STATE_INACTIVE){ if (state.current_state == STATE_INACTIVE){
target_powerground_status = data[1];
state.target_state = STATE_CHARGING_PRECHARGE; state.target_state = STATE_CHARGING_PRECHARGE;
} }
break; break;
@ -162,7 +163,9 @@ void sm_precharge_discharge_manager(){
} }
void sm_powerground_manager(){ void sm_powerground_manager(){
if (target_powerground_status > 100){ //something went wrong if (current_powerground_status == target_powerground_status)
return;
if ( current_powerground_status > 100 || target_powerground_status > 100){ //something went wrong
PWM_powerground_control(255); PWM_powerground_control(255);
current_powerground_status = target_powerground_status= 0; current_powerground_status = target_powerground_status= 0;
return; return;
@ -170,10 +173,12 @@ void sm_powerground_manager(){
if (powerground_softstart_timer < HAL_GetTick()){ if (powerground_softstart_timer < HAL_GetTick()){
if (current_powerground_status < target_powerground_status ){ if (current_powerground_status < target_powerground_status ){
PWM_powerground_control(current_powerground_status + 1); current_powerground_status++;
PWM_powerground_softcontrol();
powerground_softstart_timer = HAL_GetTick() + 10; powerground_softstart_timer = HAL_GetTick() + 10;
} else if (current_powerground_status > target_powerground_status) { } else if (current_powerground_status > target_powerground_status) {
PWM_powerground_control(current_powerground_status - 1); current_powerground_status--;
PWM_powerground_softcontrol();
powerground_softstart_timer = HAL_GetTick() + 10; powerground_softstart_timer = HAL_GetTick() + 10;
} }
} }