Skip to content

Commit

Permalink
Phase current offset read at PWM off
Browse files Browse the repository at this point in the history
  • Loading branch information
stancecoke committed Dec 29, 2020
1 parent 27bd5fa commit 4b501a0
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 101 deletions.
63 changes: 28 additions & 35 deletions LishuiFOC_01 Debug.cfg
Original file line number Diff line number Diff line change
@@ -1,35 +1,28 @@
# This is an LishuiFOC_01 board with a single STM32F103C6Tx chip
#
# Generated by System Workbench for STM32
# Take care that such file, as generated, may be overridden without any early notice. Please have a look to debug launch configuration setup(s)

source [find interface/stlink.cfg]

set WORKAREASIZE 0x2800

transport select "hla_swd"

set CHIPNAME STM32F103C6Tx
set BOARDNAME LishuiFOC_01

# CHIPNAMES state
set CHIPNAME_CPU0_ACTIVATED 1

# Enable debug when in low power modes
set ENABLE_LOW_POWER 1

# Stop Watchdog counters when halt
set STOP_WATCHDOG 1

# STlink Debug clock frequency
set CLOCK_FREQ 8000

# use software system reset
reset_config none
set CONNECT_UNDER_RESET 0

# BCTM CPU variables



source [find target/stm32f1x.cfg]
# This is an LishuiFOC_01 board with a single STM32F103C6Tx chip
#
# Generated by System Workbench for STM32
# Take care that such file, as generated, may be overridden without any early notice. Please have a look to debug launch configuration setup(s)

source [find interface/stlink.cfg]

set WORKAREASIZE 0x2800

transport select "hla_swd"

set CHIPNAME STM32F103C6Tx
set BOARDNAME LishuiFOC_01

# Enable debug when in low power modes
set ENABLE_LOW_POWER 1

# Stop Watchdog counters when halt
set STOP_WATCHDOG 1

# STlink Debug clock frequency
set CLOCK_FREQ 8000

# use software system reset
reset_config none
set CONNECT_UNDER_RESET 0

source [find target/stm32f1x.cfg]
120 changes: 60 additions & 60 deletions LishuiFOC_01 Debug.launch
Original file line number Diff line number Diff line change
@@ -1,60 +1,60 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="fr.ac6.mcu.debug.config.launchConfigurationType">
<stringAttribute key="DEBBUGER_BOARD_NAME" value="LishuiFOC_01"/>
<stringAttribute key="DEBBUGER_CORE_NAME" value="cortex-m3"/>
<stringAttribute key="DEBBUGER_MCU_NAME" value="STM32F103C6Tx"/>
<stringAttribute key="fr.ac6.mcu.ide.DBG_DEVICE" value="ST-Link"/>
<stringAttribute key="fr.ac6.mcu.ide.DBG_DEVICE_SHAREABLE_ALLOWED" value="false"/>
<stringAttribute key="fr.ac6.mcu.ide.DBG_INTERFACE" value="SWD"/>
<stringAttribute key="fr.ac6.mcu.ide.DBG_INTERFACE_FREQUENCY" value="8000000.0"/>
<stringAttribute key="fr.ac6.mcu.ide.DBG_LOW_POWER_MODE_ALLOWED" value="true"/>
<intAttribute key="fr.ac6.mcu.ide.DBG_NB_CORES" value="1"/>
<stringAttribute key="fr.ac6.mcu.ide.DBG_RESET_MODE" value="soft"/>
<stringAttribute key="fr.ac6.mcu.ide.DBG_STOP_WATCHDOG_THEN_HALTED_ALLOWED" value="true"/>
<booleanAttribute key="fr.ac6.mcu.ide.OPENOCD_GENERATOR_OPTION" value="true"/>
<booleanAttribute key="fr.ac6.mcu.ide.OPENOCD_LAUNCH" value="true"/>
<stringAttribute key="fr.ac6.mcu.ide.OPENOCD_NAME" value="&quot;${openstm32_openocd_path}\openocd.exe&quot;"/>
<stringAttribute key="fr.ac6.mcu.ide.OPENOCD_OTHER_OPTIONS" value=""/>
<stringAttribute key="fr.ac6.mcu.ide.OPENOCD_SCRIPT" value="${ProjDirPath}\LishuiFOC_01 Debug.cfg"/>
<stringAttribute key="fr.ac6.mcu.ide.OPENOCD_SCRIPT_CHOICE" value="automated"/>
<stringAttribute key="fr.ac6.mcu.ide.OPENOCD_SCRIPT_IS_LEGACY" value="false"/>
<booleanAttribute key="org.eclipse.cdt.debug.gdbjtag.core.doHalt" value="false"/>
<booleanAttribute key="org.eclipse.cdt.debug.gdbjtag.core.doReset" value="false"/>
<stringAttribute key="org.eclipse.cdt.debug.gdbjtag.core.imageFileName" value=""/>
<stringAttribute key="org.eclipse.cdt.debug.gdbjtag.core.imageOffset" value=""/>
<stringAttribute key="org.eclipse.cdt.debug.gdbjtag.core.initCommands" value=""/>
<stringAttribute key="org.eclipse.cdt.debug.gdbjtag.core.ipAddress" value="localhost"/>
<stringAttribute key="org.eclipse.cdt.debug.gdbjtag.core.jtagDevice" value="Generic TCP/IP"/>
<booleanAttribute key="org.eclipse.cdt.debug.gdbjtag.core.loadImage" value="true"/>
<booleanAttribute key="org.eclipse.cdt.debug.gdbjtag.core.loadSymbols" value="true"/>
<stringAttribute key="org.eclipse.cdt.debug.gdbjtag.core.pcRegister" value=""/>
<intAttribute key="org.eclipse.cdt.debug.gdbjtag.core.portNumber" value="3333"/>
<stringAttribute key="org.eclipse.cdt.debug.gdbjtag.core.runCommands" value=""/>
<booleanAttribute key="org.eclipse.cdt.debug.gdbjtag.core.setPcRegister" value="false"/>
<booleanAttribute key="org.eclipse.cdt.debug.gdbjtag.core.setResume" value="true"/>
<booleanAttribute key="org.eclipse.cdt.debug.gdbjtag.core.setStopAt" value="true"/>
<stringAttribute key="org.eclipse.cdt.debug.gdbjtag.core.stopAt" value="main"/>
<stringAttribute key="org.eclipse.cdt.debug.gdbjtag.core.symbolsFileName" value=""/>
<stringAttribute key="org.eclipse.cdt.debug.gdbjtag.core.symbolsOffset" value=""/>
<booleanAttribute key="org.eclipse.cdt.debug.gdbjtag.core.useFileForImage" value="false"/>
<booleanAttribute key="org.eclipse.cdt.debug.gdbjtag.core.useFileForSymbols" value="false"/>
<booleanAttribute key="org.eclipse.cdt.debug.gdbjtag.core.useProjBinaryForImage" value="true"/>
<booleanAttribute key="org.eclipse.cdt.debug.gdbjtag.core.useProjBinaryForSymbols" value="true"/>
<booleanAttribute key="org.eclipse.cdt.debug.gdbjtag.core.useRemoteTarget" value="true"/>
<stringAttribute key="org.eclipse.cdt.dsf.gdb.DEBUG_NAME" value="${openstm32_compiler_path}\arm-none-eabi-gdb"/>
<stringAttribute key="org.eclipse.cdt.launch.COREFILE_PATH" value=""/>
<stringAttribute key="org.eclipse.cdt.launch.DEBUGGER_REGISTER_GROUPS" value=""/>
<stringAttribute key="org.eclipse.cdt.launch.PROGRAM_NAME" value="Debug/LishuiFOC_01.elf"/>
<booleanAttribute key="org.eclipse.cdt.launch.PROGRAM_NAME_ACTIVATED" value="true"/>
<stringAttribute key="org.eclipse.cdt.launch.PROJECT_ATTR" value="LishuiFOC_01"/>
<booleanAttribute key="org.eclipse.cdt.launch.PROJECT_BUILD_CONFIG_AUTO_ATTR" value="true"/>
<stringAttribute key="org.eclipse.cdt.launch.PROJECT_BUILD_CONFIG_ID_ATTR" value="fr.ac6.managedbuild.config.gnu.cross.exe.debug.167643868"/>
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
<listEntry value="/LishuiFOC_01"/>
</listAttribute>
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
<listEntry value="4"/>
</listAttribute>
<stringAttribute key="org.eclipse.dsf.launch.MEMORY_BLOCKS" value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;&#13;&#10;&lt;memoryBlockExpressionList context=&quot;reserved-for-future-use&quot;&gt;&#13;&#10;&lt;gdbmemoryBlockExpression address=&quot;134248448&quot; label=&quot;0x8007800&quot;/&gt;&#13;&#10;&lt;/memoryBlockExpressionList&gt;&#13;&#10;"/>
<stringAttribute key="process_factory_id" value="fr.ac6.mcu.debug.launching.MCUGdbProcessFactory"/>
</launchConfiguration>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="fr.ac6.mcu.debug.config.launchConfigurationType">
<stringAttribute key="DEBBUGER_BOARD_NAME" value="LishuiFOC_01"/>
<stringAttribute key="DEBBUGER_CORE_NAME" value="cortex-m3"/>
<stringAttribute key="DEBBUGER_MCU_NAME" value="STM32F103C6Tx"/>
<stringAttribute key="fr.ac6.mcu.ide.DBG_DEVICE" value="ST-Link"/>
<stringAttribute key="fr.ac6.mcu.ide.DBG_DEVICE_SHAREABLE_ALLOWED" value="false"/>
<stringAttribute key="fr.ac6.mcu.ide.DBG_INTERFACE" value="SWD"/>
<stringAttribute key="fr.ac6.mcu.ide.DBG_INTERFACE_FREQUENCY" value="8000000.0"/>
<stringAttribute key="fr.ac6.mcu.ide.DBG_LOW_POWER_MODE_ALLOWED" value="true"/>
<intAttribute key="fr.ac6.mcu.ide.DBG_NB_CORES" value="1"/>
<stringAttribute key="fr.ac6.mcu.ide.DBG_RESET_MODE" value="soft"/>
<stringAttribute key="fr.ac6.mcu.ide.DBG_STOP_WATCHDOG_THEN_HALTED_ALLOWED" value="true"/>
<booleanAttribute key="fr.ac6.mcu.ide.OPENOCD_GENERATOR_OPTION" value="true"/>
<booleanAttribute key="fr.ac6.mcu.ide.OPENOCD_LAUNCH" value="true"/>
<stringAttribute key="fr.ac6.mcu.ide.OPENOCD_NAME" value="&quot;${openstm32_openocd_path}\openocd.exe&quot;"/>
<stringAttribute key="fr.ac6.mcu.ide.OPENOCD_OTHER_OPTIONS" value=""/>
<stringAttribute key="fr.ac6.mcu.ide.OPENOCD_SCRIPT" value="${ProjDirPath}\LishuiFOC_01 Debug.cfg"/>
<stringAttribute key="fr.ac6.mcu.ide.OPENOCD_SCRIPT_CHOICE" value="automated"/>
<stringAttribute key="fr.ac6.mcu.ide.OPENOCD_SCRIPT_IS_LEGACY" value="false"/>
<booleanAttribute key="org.eclipse.cdt.debug.gdbjtag.core.doHalt" value="false"/>
<booleanAttribute key="org.eclipse.cdt.debug.gdbjtag.core.doReset" value="false"/>
<stringAttribute key="org.eclipse.cdt.debug.gdbjtag.core.imageFileName" value=""/>
<stringAttribute key="org.eclipse.cdt.debug.gdbjtag.core.imageOffset" value=""/>
<stringAttribute key="org.eclipse.cdt.debug.gdbjtag.core.initCommands" value=""/>
<stringAttribute key="org.eclipse.cdt.debug.gdbjtag.core.ipAddress" value="localhost"/>
<stringAttribute key="org.eclipse.cdt.debug.gdbjtag.core.jtagDevice" value="Generic TCP/IP"/>
<booleanAttribute key="org.eclipse.cdt.debug.gdbjtag.core.loadImage" value="true"/>
<booleanAttribute key="org.eclipse.cdt.debug.gdbjtag.core.loadSymbols" value="true"/>
<stringAttribute key="org.eclipse.cdt.debug.gdbjtag.core.pcRegister" value=""/>
<intAttribute key="org.eclipse.cdt.debug.gdbjtag.core.portNumber" value="3333"/>
<stringAttribute key="org.eclipse.cdt.debug.gdbjtag.core.runCommands" value=""/>
<booleanAttribute key="org.eclipse.cdt.debug.gdbjtag.core.setPcRegister" value="false"/>
<booleanAttribute key="org.eclipse.cdt.debug.gdbjtag.core.setResume" value="true"/>
<booleanAttribute key="org.eclipse.cdt.debug.gdbjtag.core.setStopAt" value="true"/>
<stringAttribute key="org.eclipse.cdt.debug.gdbjtag.core.stopAt" value="main"/>
<stringAttribute key="org.eclipse.cdt.debug.gdbjtag.core.symbolsFileName" value=""/>
<stringAttribute key="org.eclipse.cdt.debug.gdbjtag.core.symbolsOffset" value=""/>
<booleanAttribute key="org.eclipse.cdt.debug.gdbjtag.core.useFileForImage" value="false"/>
<booleanAttribute key="org.eclipse.cdt.debug.gdbjtag.core.useFileForSymbols" value="false"/>
<booleanAttribute key="org.eclipse.cdt.debug.gdbjtag.core.useProjBinaryForImage" value="true"/>
<booleanAttribute key="org.eclipse.cdt.debug.gdbjtag.core.useProjBinaryForSymbols" value="true"/>
<booleanAttribute key="org.eclipse.cdt.debug.gdbjtag.core.useRemoteTarget" value="true"/>
<stringAttribute key="org.eclipse.cdt.dsf.gdb.DEBUG_NAME" value="${openstm32_compiler_path}\arm-none-eabi-gdb"/>
<stringAttribute key="org.eclipse.cdt.launch.COREFILE_PATH" value=""/>
<stringAttribute key="org.eclipse.cdt.launch.DEBUGGER_REGISTER_GROUPS" value=""/>
<stringAttribute key="org.eclipse.cdt.launch.PROGRAM_NAME" value="Debug/LishuiFOC_01.elf"/>
<booleanAttribute key="org.eclipse.cdt.launch.PROGRAM_NAME_ACTIVATED" value="true"/>
<stringAttribute key="org.eclipse.cdt.launch.PROJECT_ATTR" value="LishuiFOC_01"/>
<booleanAttribute key="org.eclipse.cdt.launch.PROJECT_BUILD_CONFIG_AUTO_ATTR" value="true"/>
<stringAttribute key="org.eclipse.cdt.launch.PROJECT_BUILD_CONFIG_ID_ATTR" value="fr.ac6.managedbuild.config.gnu.cross.exe.debug.167643868"/>
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
<listEntry value="/LishuiFOC_01"/>
</listAttribute>
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
<listEntry value="4"/>
</listAttribute>
<stringAttribute key="org.eclipse.dsf.launch.MEMORY_BLOCKS" value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;&#13;&#10;&lt;memoryBlockExpressionList context=&quot;reserved-for-future-use&quot;&gt;&#13;&#10;&lt;gdbmemoryBlockExpression address=&quot;134248448&quot; label=&quot;0x8007800&quot;/&gt;&#13;&#10;&lt;/memoryBlockExpressionList&gt;&#13;&#10;"/>
<stringAttribute key="process_factory_id" value="fr.ac6.mcu.debug.launching.MCUGdbProcessFactory"/>
</launchConfiguration>
38 changes: 32 additions & 6 deletions Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ uint16_t k=0;
uint8_t ui8_overflow_flag=0;
uint8_t ui8_slowloop_counter=0;
uint8_t ui8_adc_inj_flag=0;
uint8_t ui8_adc_regular_flag=0;
int8_t i8_direction= REVERSE; //for permanent reverse direction
int8_t i8_reverse_flag = 1; //for temporaribly reverse direction

Expand Down Expand Up @@ -399,10 +400,16 @@ int main(void)
TIM1->CCR2 = 1023;
TIM1->CCR3 = 1023;



CLEAR_BIT(TIM1->BDTR, TIM_BDTR_MOE);//Disable PWM

HAL_Delay(200); //wait for stable conditions



/*
// get offset values for adc phase currents
// first phase A+B
i=0;
Expand Down Expand Up @@ -437,6 +444,20 @@ int main(void)
ADC1->JSQR=0b00100000000000000000; //ADC1 injected reads phase A JL = 0b00, JSQ4 = 0b00100 (decimal 4 = channel 4)
ADC1->JOFR1 = ui16_ph1_offset;
*/
for(i=0;i<32;i++){
while(!ui8_adc_regular_flag){}
ui16_ph1_offset+=adcData[2];
ui16_ph2_offset+=adcData[3];
ui16_ph3_offset+=adcData[4];
ui8_adc_regular_flag=0;

}
ui16_ph1_offset=ui16_ph1_offset>>5;
ui16_ph2_offset=ui16_ph2_offset>>5;
ui16_ph3_offset=ui16_ph3_offset>>5;

printf_("phase current offsets: %d, %d, %d \n ", ui16_ph1_offset, ui16_ph2_offset, ui16_ph3_offset);

ui8_adc_offset_done_flag=1;

Expand Down Expand Up @@ -662,14 +683,17 @@ int main(void)
MS.Voltage=adcData[0];
if(uint32_SPEED_counter>127999)MS.Speed =128000;

if((uint16_full_rotation_counter>3999||uint16_half_rotation_counter>3999)&&READ_BIT(TIM1->BDTR, TIM_BDTR_MOE))CLEAR_BIT(TIM1->BDTR, TIM_BDTR_MOE); //Disable PWM if motor is not turning
if((uint16_full_rotation_counter>7999||uint16_half_rotation_counter>7999)&&READ_BIT(TIM1->BDTR, TIM_BDTR_MOE)){
CLEAR_BIT(TIM1->BDTR, TIM_BDTR_MOE); //Disable PWM if motor is not turning
q31_rotorposition_absolute = q31_rotorposition_hall;
}

#if (DISPLAY_TYPE == DISPLAY_TYPE_DEBUG && !defined(FAST_LOOP_LOG))
//print values for debugging


sprintf_(buffer, "%d, %d, %d, %d, %d, %d, %d\r\n", MS.i_q,int16_current_target, ui8_hall_case,(uint16_t) temp2, (uint16_t) temp3, (uint16_t)uint32_PAS, (uint16_t) (ui16_reg_adc_value-THROTTLE_OFFSET));//((q31_i_q_fil*q31_u_abs)>>14)*
// sprintf_(buffer, "%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])) ;
//sprintf_(buffer, "%d, %d, %d, %d, %d, %d, %d\r\n", MS.i_q,int16_current_target, ui16_ph1_offset,ui16_ph2_offset, ui16_ph3_offset, ui8_hall_state, (uint16_t) (ui16_reg_adc_value-THROTTLE_OFFSET));//((q31_i_q_fil*q31_u_abs)>>14)*
sprintf_(buffer, "%d, %d, %d, %d, %d, %d\r\n",ui8_hall_state,(uint16_t)adcData[1],(uint16_t)adcData[2],(uint16_t)adcData[3],(uint16_t)(adcData[4]),(uint16_t)(adcData[5])) ;

i=0;
while (buffer[i] != '\0')
Expand Down Expand Up @@ -854,7 +878,7 @@ _Error_Handler(__FILE__, __LINE__);
}
/**Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_9;
sConfig.Channel = ADC_CHANNEL_4;
sConfig.Rank = ADC_REGULAR_RANK_3;
sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;//ADC_SAMPLETIME_239CYCLES_5;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
Expand Down Expand Up @@ -1197,8 +1221,8 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
if(HAL_GPIO_ReadPin(PAS_GPIO_Port, PAS_Pin))uint32_PAS_HIGH_counter++;
}
if (uint32_SPEED_counter<128000)uint32_SPEED_counter++; //counter for external Speedsensor
if(uint16_full_rotation_counter<4000)uint16_full_rotation_counter++; //full rotation counter for motor standstill detection
if(uint16_half_rotation_counter<4000)uint16_half_rotation_counter++; //half rotation counter for motor standstill detection
if(uint16_full_rotation_counter<8000)uint16_full_rotation_counter++; //full rotation counter for motor standstill detection
if(uint16_half_rotation_counter<8000)uint16_half_rotation_counter++; //half rotation counter for motor standstill detection

}
}
Expand All @@ -1212,6 +1236,8 @@ void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
ui32_reg_adc_value_filter += adcData[1]; //HAL_ADC_GetValue(hadc);
ui16_reg_adc_value = ui32_reg_adc_value_filter>>4;

ui8_adc_regular_flag=1;



}
Expand Down

0 comments on commit 4b501a0

Please sign in to comment.