Skip to content

Commit

Permalink
add phase current limiting to duty cycle mode
Browse files Browse the repository at this point in the history
  • Loading branch information
stancecoke committed May 8, 2024
1 parent 1afe9f4 commit 2664b9e
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 24 deletions.
4 changes: 2 additions & 2 deletions .settings/language.settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
<provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/>
<provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/>
<provider class="fr.ac6.mcu.ide.build.CrossBuiltinSpecsDetector" console="false" env-hash="-1059628809318420809" id="fr.ac6.mcu.ide.build.CrossBuiltinSpecsDetector" keep-relative-paths="false" name="Ac6 SW4 STM32 MCU Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
<provider class="fr.ac6.mcu.ide.build.CrossBuiltinSpecsDetector" console="false" env-hash="-148003912687801890" id="fr.ac6.mcu.ide.build.CrossBuiltinSpecsDetector" keep-relative-paths="false" name="Ac6 SW4 STM32 MCU Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
<language-scope id="org.eclipse.cdt.core.gcc"/>
<language-scope id="org.eclipse.cdt.core.g++"/>
</provider>
Expand All @@ -16,7 +16,7 @@
<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
<provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/>
<provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/>
<provider class="fr.ac6.mcu.ide.build.CrossBuiltinSpecsDetector" console="false" env-hash="-1059628809318420809" id="fr.ac6.mcu.ide.build.CrossBuiltinSpecsDetector" keep-relative-paths="false" name="Ac6 SW4 STM32 MCU Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
<provider class="fr.ac6.mcu.ide.build.CrossBuiltinSpecsDetector" console="false" env-hash="-148003912687801890" id="fr.ac6.mcu.ide.build.CrossBuiltinSpecsDetector" keep-relative-paths="false" name="Ac6 SW4 STM32 MCU Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
<language-scope id="org.eclipse.cdt.core.gcc"/>
<language-scope id="org.eclipse.cdt.core.g++"/>
</provider>
Expand Down
11 changes: 8 additions & 3 deletions Inc/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,14 @@
#define VOLTAGE_MIN 1320 //33V

// motor current limits for invividual modes in mA, see default settings at https://max.cfw.sh/#
#define PH_CURRENT_MAX_ECO 500
#define PH_CURRENT_MAX_NORMAL 1500
#define PH_CURRENT_MAX_SPORT 2000
#define UQ_MAX_ECO 500
#define UQ_MAX_NORMAL 1500
#define UQ_MAX_SPORT 2000

// motor current limits for invividual modes in mA, see default settings at https://max.cfw.sh/#
#define PH_CURRENT_MAX_ECO 200
#define PH_CURRENT_MAX_NORMAL 600
#define PH_CURRENT_MAX_SPORT 1200

// motor current limits for invividual modes in mA, see default settings at https://max.cfw.sh/#
#define BAT_CURRENT_MAX_ECO 3000
Expand Down
5 changes: 3 additions & 2 deletions Inc/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ typedef struct
int32_t i_d_setpoint_temp;
q31_t u_d;
q31_t u_q;
q31_t u_q_setpoint_temp;
q31_t u_abs;
q31_t Battery_Current;
uint8_t hall_angle_detect_flag;
Expand Down Expand Up @@ -215,11 +216,11 @@ typedef struct
uint8_t speedLimit;
uint8_t speed_limit;
uint8_t pulses_per_revolution;
uint16_t phase_current_max;
int16_t spec_angle;
uint8_t com_mode;
int16_t regen_current;
int16_t phase_current_limit;
int16_t phase_current_max;
int16_t u_q_max;
int16_t battery_current_max;

}MotorParams_t;
Expand Down
5 changes: 3 additions & 2 deletions Src/M365_Dashboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,9 @@ void process_DashboardMessage(MotorState_t *MS, MotorParams_t *MP, uint8_t *mess
}
else{
if(MS->Speed>2||MS->mode==2){
MS->i_q_setpoint_temp = map(message[Throttle],THROTTLEOFFSET,THROTTLEMAX,0,MP->phase_current_limit);
if(MS->i_q_setpoint_temp)HAL_GPIO_WritePin(LIGHT_GPIO_Port, LIGHT_Pin,SET);
//map u_q to the throttle setting.
MS->u_q_setpoint_temp = map(message[Throttle],THROTTLEOFFSET,THROTTLEMAX,0,MP->u_q_max);
if(MS->u_q_setpoint_temp)HAL_GPIO_WritePin(LIGHT_GPIO_Port, LIGHT_Pin,SET);
}
else MS->i_q_setpoint_temp =0;
MS->brake_active=false;
Expand Down
9 changes: 6 additions & 3 deletions Src/button_processing.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,18 +169,21 @@ void set_mode(MotorParams_t *MP, MotorState_t *MS){

switch( MS->mode & 0x07){ //look only on the lowest 3 bits
case eco :{
MP->phase_current_limit=PH_CURRENT_MAX_ECO;
MP->u_q_max=UQ_MAX_ECO;
MP->phase_current_max=PH_CURRENT_MAX_ECO;
MP->speed_limit=SPEEDLIMIT_ECO;
MP->battery_current_max=BAT_CURRENT_MAX_ECO;

} break ;
case normal :{
MP->phase_current_limit=PH_CURRENT_MAX_NORMAL;
MP->u_q_max=UQ_MAX_NORMAL;
MP->phase_current_max=PH_CURRENT_MAX_NORMAL;
MP->speed_limit=SPEEDLIMIT_NORMAL;
MP->battery_current_max=BAT_CURRENT_MAX_NORMAL;
} break ;
case sport :{
MP->phase_current_limit=PH_CURRENT_MAX_SPORT;
MP->u_q_max=UQ_MAX_SPORT;
MP->phase_current_max=PH_CURRENT_MAX_SPORT;
MP->speed_limit=SPEEDLIMIT_SPORT;
MP->battery_current_max=BAT_CURRENT_MAX_SPORT;
} break ;
Expand Down
27 changes: 15 additions & 12 deletions Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,8 @@ int main(void)
MP.pulses_per_revolution = PULSES_PER_REVOLUTION;
MP.wheel_cirumference = WHEEL_CIRCUMFERENCE;
MP.speedLimit=SPEEDLIMIT;
MP.phase_current_limit=PH_CURRENT_MAX_NORMAL;
MP.u_q_max=UQ_MAX_NORMAL;
MP.phase_current_max=PH_CURRENT_MAX_NORMAL;
MP.regen_current=PH_CURRENT_MAX_NORMAL;
MP.com_mode=Hallsensor;
if(MP.com_mode==Sensorless_openloop||MP.com_mode==Sensorless_startkick)MS.Obs_flag=1;
Expand Down Expand Up @@ -792,7 +793,7 @@ if(MP.com_mode==Sensorless_openloop||MP.com_mode==Sensorless_startkick)MS.Obs_fl
MS.u_q,
q31_u_q_temp,
MP.battery_current_max,
MP.phase_current_limit );
MP.phase_current_max );
// sprintf_(buffer, "%d, %d, %d, %d, %d, %d, %d\r\n",(uint16_t)adcData[0],(uint16_t)adcData[1],(uint16_t)adcData[2],(uint16_t)adcData[3],(uint16_t)(adcData[4]),(uint16_t)(adcData[5]),(uint16_t)(adcData[6])) ;
// sprintf_(buffer, "%d, %d, %d, %d, %d, %d\r\n",tic_array[0],tic_array[1],tic_array[2],tic_array[3],tic_array[4],tic_array[5]) ;
i=0;
Expand Down Expand Up @@ -2170,27 +2171,29 @@ void runPIcontrol(){


if (!ui8_BC_limit_flag){
//normal motor current control
PI_iq.recent_value = MS.i_q;
PI_iq.setpoint = i8_direction*i8_reverse_flag*MS.i_q_setpoint;
}
else{
if(brake_flag==0){
// if(HAL_GPIO_ReadPin(Brake_GPIO_Port, Brake_Pin)){


//battery current control (control battery current to maximum battery current) in motor operation
PI_iq.recent_value= (MS.Battery_Current>>6)*i8_direction*i8_reverse_flag;
PI_iq.setpoint = (BATTERYCURRENT_MAX>>6)*i8_direction*i8_reverse_flag;
q31_u_q_temp = PI_control(&PI_iq);

}
else{
//battery current control (control battery current to maximum battery current) in regen operation
PI_iq.recent_value= (MS.Battery_Current>>6)*i8_direction*i8_reverse_flag;
PI_iq.setpoint = (-REGEN_CURRENT_MAX>>6)*i8_direction*i8_reverse_flag;
}
}
#if (CONTROL_MODE == DUTYCYCLE)
q31_u_q_temp = MS.i_q_setpoint_temp*i8_direction*i8_reverse_flag;
//directly set the u_q from the throttle information
q31_u_q_temp = MS.u_q_setpoint_temp*i8_direction*i8_reverse_flag;
//ramp down u_q at maximum battery current
q31_u_q_temp = map(MS.Battery_Current, MP.battery_current_max-1000, MP.battery_current_max, q31_u_q_temp,0);
//ramp down u_q at maximum motor current
q31_u_q_temp = map(MS.i_q, MP.phase_current_max-100, MP.phase_current_max, q31_u_q_temp,0);

#else

Expand All @@ -2210,10 +2213,10 @@ void runPIcontrol(){
MS.u_abs = (MS.u_abs>>16)+1;


if (MS.u_abs > PI_iq.limit_output){
MS.u_q = (q31_u_q_temp*PI_iq.limit_output)/MS.u_abs; //division!
MS.u_d = (q31_u_d_temp*PI_iq.limit_output)/MS.u_abs; //division!
MS.u_abs = PI_iq.limit_output;
if (MS.u_abs > _U_MAX){
MS.u_q = (q31_u_q_temp*_U_MAX)/MS.u_abs; //division!
MS.u_d = (q31_u_d_temp*_U_MAX)/MS.u_abs; //division!
MS.u_abs = _U_MAX;
}
else{
MS.u_q=q31_u_q_temp;
Expand Down

0 comments on commit 2664b9e

Please sign in to comment.