Sfoglia il codice sorgente

update hall

Signed-off-by: kevin <huhui@sharkgulf.com>
kevin 2 anni fa
parent
commit
40173ea793

+ 6 - 4
Applications/app/app.c

@@ -100,7 +100,8 @@ static void app_print_log(void) {
 	sys_debug("FOC time err %d %d %d %d\n", g_meas_foc.intval_time_h_error, g_meas_foc.intval_time_l_error, g_meas_foc.exec_max_error_time, g_meas_foc.exec_time_error);
 	//sys_debug("acc vol %d, bid %d\n", get_acc_vol(), gpio_board_id());
 	//sys_debug("throttle %f\n", get_throttle_float());
-	//sys_debug("ADC Vref %f, %f\n", get_adc_vref(), adc_5vref_compesion());
+	sys_debug("ADC Vref %f, %f\n", get_adc_vref(), adc_5vref_compesion());
+	sys_debug("curr A:%f,B:%f,C:%f\n", foc()->in.curr_abc[0], foc()->in.curr_abc[1], foc()->in.curr_abc[2]);
 	//sys_debug("dead time 0x%x\n", get_deadtime());
 	//thro_torque_log();
 	//sys_debug("_>%f, %f, %f\n", ladrc_observer_get()->ld, ladrc_observer_get()->lq, ladrc_observer_get()->poles);
@@ -110,7 +111,7 @@ static void app_print_log(void) {
 	//sample_log();
 	//throttle_log();
 	//sys_debug("Trq: %f-%f-%f\n", motor.controller.ramp_input_torque.target, motor.controller.ramp_input_torque.interpolation, motor.controller.ramp_input_torque.step);
-	//sys_debug("FOC is %s, %d,%d\n", mot_contrl_is_start(mot_contrl())?"start":"stop", motor.foc_start_cnt, motor.foc_stop_cnt);
+	sys_debug("FOC is %s, %d,%d\n", mot_contrl_is_start(mot_contrl())?"start":"stop", motor.foc_start_cnt, motor.foc_stop_cnt);
 	//F_debug();
 	//eCtrl_debug_log();
 	//sys_debug("enc err %d, %d\n", foc_observer_enc_errcount(), foc_observer_sensorless_stable());
@@ -159,7 +160,7 @@ static u32 app_report_task(void *p) {
 int plot_type = 0;
 static void plot_smo_angle(void) {
 	u32 mask = cpu_enter_critical();
-	float smo_angle = foc_observer_sensorless_angle();
+	float smo_angle = hall_get_elec_angle();
 	float mot_angle = foc()->in.mot_angle;
 	cpu_exit_critical(mask);
 	float delta = smo_angle - mot_angle;
@@ -181,7 +182,8 @@ static u32 app_plot_task(void * args) {
 		}
 		can_plot2(plot_arg1, plot_arg2);
 	}else if (plot_type == 2) {
-		can_plot2(mot_contrl_get_final_torque(&motor.controller), mot_contrl()->target_torque);
+		//can_plot2(mot_contrl_get_final_torque(&motor.controller), mot_contrl()->target_torque);
+		can_plot2(hall_get()->state, hall_get()->low_res_pos);
 	}else if (plot_type == 3) {
 		plot_smo_angle();
 	}else if (plot_type == 4) {

+ 9 - 3
Applications/bsp/gd32/adc.c

@@ -16,7 +16,7 @@
 #define VBUS_I_BUFF_IDX 5
 #define V_VOL_BUFF_IDX 6
 #define THROTTLE_BUFF_IDX 7
-#define W_VOL_BUFF_IDX 8
+#define W_VOL_BUFF_IDX 9
 #define VREF_BUFF_IDX 10
 #define MOTOR_TEMP_BUFF_IDX 11
 #define REG_CHAN_NUM (ADC01_NUM + ADC01_NUM)
@@ -103,7 +103,7 @@ static void adc01_init(u32 reg_sampletime) {
 	adc_regular_channel_config(ADC0, 1, MOS_TEMP1_ADC_CHAN, reg_sampletime);
 	adc_regular_channel_config(ADC0, 2, U_VOL_ADC_CHAN, reg_sampletime);
 	adc_regular_channel_config(ADC0, 3, V_VOL_ADC_CHAN, reg_sampletime);
-	adc_regular_channel_config(ADC0, 4, W_VOL_ADC_CHAN, reg_sampletime);
+	adc_regular_channel_config(ADC0, 4, ADC_CHANNEL_17, reg_sampletime); //dumy
 	adc_regular_channel_config(ADC0, 5, ADC_CHANNEL_17, reg_sampletime);//3.3vref
 
 	adc_tempsensor_vrefint_enable();
@@ -113,7 +113,7 @@ static void adc01_init(u32 reg_sampletime) {
 	adc_regular_channel_config(ADC1, 1, ACC_V_CHAN, reg_sampletime);
 	adc_regular_channel_config(ADC1, 2, VBUS_I_CHAN, reg_sampletime);
 	adc_regular_channel_config(ADC1, 3, THROTTLE_CHAN, reg_sampletime);
-	adc_regular_channel_config(ADC1, 4, ADC_CHANNEL_10, reg_sampletime); //dumy
+	adc_regular_channel_config(ADC1, 4, W_VOL_ADC_CHAN, reg_sampletime);
 	adc_regular_channel_config(ADC1, 5, MOTOR_TEMP_ADC_CHAN, reg_sampletime);
 #else
 	adc_channel_length_config(ADC0, ADC_REGULAR_CHANNEL, ADC01_NUM);
@@ -328,12 +328,18 @@ float adc_vref_compesion(void) {
 }
 
 static float vref_5v_compestion_filter = 1.0f;
+#if CONFIG_MC105_HW_VERSION==3
 #define VREF_5V_COMPESTION() (vref_5v_adc/(float)adc_buffer[VREF5v_BUFF_IDX])
 void adc_5vref_filter(void) {
 	float value = VREF_5V_COMPESTION();
 	LowPass_Filter(vref_5v_compestion_filter, value, VREF_COMP_LFP_CEOF);
 }
+#else
+#define VREF_5V_COMPESTION() VREF_3V3_COMPESTION()
+void adc_5vref_filter(void) {
 
+}
+#endif
 float adc_5vref_compesion(void) {
 	return vref_5v_compestion_filter;
 }

+ 5 - 5
Applications/bsp/gd32/board_mc100_v1.h

@@ -201,7 +201,7 @@
 
 #define PHASE_VOL_R (40*1000.0f)
 #define PHASE_VOL_R1 (1*1000.0f)
-#define PHASE_VOL_C1 (470e-9f) //470nF
+#define PHASE_VOL_C1 (10e-9f) //470nF
 #define PHASE_VOL_Gain  ((PHASE_VOL_R + PHASE_VOL_R1)/PHASE_VOL_R1)
 #define Phase_Vol_LPF_R  ((PHASE_VOL_R * PHASE_VOL_R1)/(PHASE_VOL_R + PHASE_VOL_R1))
 #define PHASE_VOL_LPF_BAND  (1/(2*3.14F*Phase_Vol_LPF_R*PHASE_VOL_C1))
@@ -297,7 +297,7 @@
 #define HALL_B_MODE GPIO_MODE_IN_FLOATING
 
 #define HALL_C_GROUP GPIOB
-#define HALL_C_PIN GPIO_PIN_6
+#define HALL_C_PIN GPIO_PIN_8
 #define HALL_C_RCU RCU_GPIOB
 #define HALL_C_MODE GPIO_MODE_IN_FLOATING
 
@@ -315,11 +315,11 @@
 #define HALL_C_EXTI 				EXTI_8
 #define HALL_C_EXIT_SRC_GROUP 		GPIO_PORT_SOURCE_GPIOB
 #define HALL_C_EXIT_SRC_PIN 		GPIO_PIN_SOURCE_8
-
-#define USE_ENCODER_ABI
-#define ENCODER_TYPE ENCODER_MT
 #else
 /* 编码器 */
+#define USE_ENCODER_ABI
+#define ENCODER_TYPE ENCODER_MT
+
 #define ENC_A_GROUP GPIOB
 #define ENC_A_PIN GPIO_PIN_4
 #define ENC_A_RCU RCU_GPIOB

+ 4 - 1
Applications/bsp/gd32/bsp.h

@@ -18,8 +18,11 @@
 #define FOC_PWM_Half_Period (FOC_PWM_period/2)
 
 #define FOC_CTRL_US (1.0f/(float)FOC_PWM_FS)
-
+#ifdef GD32F30X_HD
+#define ADC_REGCHAN_SAMPLE_TIME ADC_SAMPLETIME_239POINT5
+#else
 #define ADC_REGCHAN_SAMPLE_TIME ADC_SAMPLETIME_71POINT5
+#endif
 #define ADC_REGCHAN_MOT_IND_SAMPLE_TIME ADC_SAMPLETIME_13POINT5
 
 #define ADC_TRIG_CONV_LATENCY_CYCLES 12.5f

+ 5 - 6
Applications/bsp/gd32/mc_irqs.c

@@ -154,7 +154,7 @@ void EXTI4_IRQHandler(void)
 	if(RESET != exti_interrupt_flag_get(EXTI_4)){
 		exti_interrupt_flag_clear(EXTI_4);
 #ifdef CONFIG_USE_ENCODER_HALL
-	#if (HALL_A_EXTI==EXTI_4)
+	#if (HALL_A_EXTI==EXTI_4) || (HALL_B_EXTI==EXTI_4) || (HALL_C_EXTI==EXTI_4)
 		HALL_IRQHandler();
 	#endif
 #endif
@@ -165,11 +165,10 @@ void EXTI5_9_IRQHandler(void){
 	if(RESET != exti_interrupt_flag_get(EXTI_5)){
 		exti_interrupt_flag_clear(EXTI_5);
 #ifdef CONFIG_USE_ENCODER_HALL
-	#if (HALL_A_EXTI==EXTI_5)
+#if (HALL_A_EXTI==EXTI_5) || (HALL_B_EXTI==EXTI_5) || (HALL_C_EXTI==EXTI_5)
 		HALL_IRQHandler();
-	#endif
 #endif
-
+#endif
 	}
 	if(RESET != exti_interrupt_flag_get(EXTI_6)){
 		exti_interrupt_flag_clear(EXTI_6);
@@ -183,9 +182,9 @@ void EXTI5_9_IRQHandler(void){
 	if(RESET != exti_interrupt_flag_get(EXTI_8)){
 		exti_interrupt_flag_clear(EXTI_8);
 #ifdef CONFIG_USE_ENCODER_HALL
-	#if (HALL_A_EXTI==EXTI_8)
+#if (HALL_A_EXTI==EXTI_8) || (HALL_B_EXTI==EXTI_8) || (HALL_C_EXTI==EXTI_8)
 		HALL_IRQHandler();
-	#endif
+#endif
 #elif (ENC_I_EXTI == EXTI_8)
 		ABI_I_IRQHandler();
 #endif

+ 43 - 30
Applications/foc/motor/hall.c

@@ -12,20 +12,21 @@
 
 //#define USE_DETECTED_ANGLE 1
 
-#define HALL_READ_TIMES 9
+#define HALL_READ_TIMES 5
 #define SMOOTH_COUNT 10.0F
 
 /* 
-4,5,1,3,2,6,4
+1,5,4,6,2,3
+0,1,2,3,4,5
 */
-static s8 hall_2_pos[] = {7,5,1,0,3,4,2,7};
+static s8 hall_2_pos[] = {7,0,4,5,2,1,3,7};
 static hall_t g_hall;
 
 measure_time_t g_meas_hall = {.exec_max_time = 6,};
 
 //#define read_hall(h,t) {h = get_hall_stat(HALL_READ_TIMES); t = _hall_table[h];}
 #define us_2_s(tick) ((float)tick / 1000000.0f) //s32q14
-#define HALL_TIMEOUT_US (1*1000000L)
+#define HALL_TIMEOUT_US (1000000L/4)
 
 
 static u8 __INLINE hall_read_state(void) {
@@ -58,12 +59,12 @@ static void hall_init_low_pos(void) {
 	u8 state = hall_read_state();
 	s16 pos = hall_2_pos[state];
 	if (pos == 7) {
-		g_hall.errors ++;
+		g_hall.sig_errors ++;
 		return;
 	}
 	g_hall.state = state;
 	g_hall.prev_dir = g_hall.dir = POSITIVE;
-	g_hall.low_res_pos = pos;
+	g_hall.low_res_pos = pos + 0.5f - g_hall.phase_offset/PHASE_60_DEGREE;
 }
 
 
@@ -92,13 +93,14 @@ static float __INLINE hall_elec_angle_vel(void){
 }
 
 void hall_debug_log(void) {
-	sys_debug("angle dir %d, stat %d, lowres %d, err %d\n", g_hall.dir, g_hall.state, g_hall.low_res_pos, g_hall.errors);
+	sys_debug("angle dir %d, stat %d, lowres %f, err %d,%d\n", g_hall.dir, g_hall.state, g_hall.low_res_pos, g_hall.sig_errors, g_hall.noise_errors);
 }
 
 static u32 hall_timeout_task(void *args) {
 	hall_t *phall = (hall_t *)args;
 	if (phall->velocity_raw != 0) {
 		if (time_delta_us(phall->edge_ticks, NULL) >= HALL_TIMEOUT_US) {
+			phall->elec_angle_vel = 0;
 			phall->velocity_raw = phall->velocity_filted = 0;
 		}
 	}
@@ -110,12 +112,16 @@ void hall_init(void) {
 	g_hall.mot_poles = mc_conf()->m.poles;
 	g_hall.b_trns_det = false;
 	g_hall.angle_smooth_cnt = SMOOTH_COUNT + 1;
+	g_hall.angle_smooth_step = 0;
 	g_hall.samples.ticks_sum = 0;
+	g_hall.samples.angles_sum = 0;
 	g_hall.position = 0;
+	g_hall.samples.full = false;
+	g_hall.samples.index = 0;
+	g_hall.elec_angle_vel = 0;
 	for (int i = 0; i < SAMPLE_MAX_COUNT; i++) {
-		g_hall.samples.ticks[i] = 120*1000000*1;
+		g_hall.samples.ticks[i] = 0;
 		g_hall.samples.angles[i] = 0;
-		g_hall.samples.ticks_sum += g_hall.samples.ticks[i];
 	}
 	if (!g_hall.inited) {
 		g_hall.inited = true;
@@ -139,19 +145,21 @@ static bool hall_update_low_pos(void) {
 	u8 state = hall_read_state();
 	s16 pos = hall_2_pos[state];
 	if (pos == 7) {
-		g_hall.errors ++;
+		g_hall.sig_errors ++;
 		return false;
 	}
 	g_hall.state = state;
 	s16 delta_pos = pos - g_hall.low_res_pos;
 	g_hall.low_res_pos = pos;
-	s8  prev_dir = g_hall.dir;
-	if (delta_pos == 1 || delta_pos == -5) {
-		g_hall.dir = POSITIVE;
-	}else{
-		g_hall.dir = NEGATIVE;
-	}
 	if (delta_pos != 0) {
+		s8  prev_dir = g_hall.dir;
+		if (g_hall.samples.full || g_hall.samples.index != 0) {
+			if (delta_pos == 1 || delta_pos == -5) {
+				g_hall.dir = POSITIVE;
+			}else{
+				g_hall.dir = NEGATIVE;
+			}
+		}
 		g_hall.edge_ticks = task_ticks_abs();
 		g_hall.prev_dir = prev_dir;
 	}
@@ -171,14 +179,13 @@ float hall_get_elec_angle(void) {
 float hall_update_elec_angle(void) {
 	float delta_ticks = (float)time_delta_us(g_hall.edge_ticks, NULL);//上次hall变换到目前的时间
 	float low_res = g_hall.low_res_pos;
-	if (g_hall.dir == NEGATIVE) {
-		low_res += 1.0f;
-	}
 	float delta_pos = g_hall.elec_angle_vel / PHASE_60_DEGREE * us_2_s(delta_ticks) * g_hall.dir;//上次hall变换到目前走过的角度(对60度的比值,小于1),通过速度插值
-	float high_res_pos = delta_pos  + low_res;
-	if (high_res_pos < 0) {
-		high_res_pos = 0;
+	if (delta_pos > 1.0f) {
+		delta_pos = 1.0f;
+	}else if (delta_pos < -1.0f) {
+		delta_pos = -1.0f;
 	}
+	float high_res_pos = delta_pos  + low_res;
 	float elec_angle = high_res_pos * PHASE_60_DEGREE;
 	float delta_angle = delta_pos * PHASE_60_DEGREE;
 	float elec_smooth_angle;
@@ -209,11 +216,16 @@ float hall_get_position(void) {
 
 static void hall_calc_mot_velocity(u32 prev_ticks) {
 	u32 delta_cnt = time_delta_us(prev_ticks, NULL);
-	hall_put_sample(PHASE_60_DEGREE, delta_cnt);
+	if (!g_hall.samples.full && g_hall.samples.index == 0) {
+		if (delta_cnt <= 1000) {
+			delta_cnt = 1000;
+		}
+	}
+	hall_put_sample(delta_cnt, PHASE_60_DEGREE);
 	float elec_vel;
 	if (g_hall.b_trns_det) {
 		elec_vel = PHASE_60_DEGREE/(us_2_s(delta_cnt));
-		LowPass_Filter(g_hall.elec_angle_vel, elec_vel, 0.8f);
+		LowPass_Filter(g_hall.elec_angle_vel, elec_vel, 1.0f);
 	}else {
 		g_hall.elec_angle_vel = hall_elec_angle_vel();
 	}
@@ -225,7 +237,7 @@ static void hall_calc_mot_velocity(u32 prev_ticks) {
 		g_hall.b_trns_det = false;
 	}
 	g_hall.velocity_raw = velocity_raw;
-	LowPass_Filter(g_hall.velocity_filted, velocity_raw, 0.5f);
+	LowPass_Filter(g_hall.velocity_filted, velocity_raw, 1.0f);
 }
 
 
@@ -238,15 +250,16 @@ void HALL_IRQHandler(void) {
 	if (!hall_update_low_pos()) {
 		return;
 	}
+	if (time_delta_us(prev_ticks, NULL) == 0) {
+		g_hall.noise_errors++;
+		return;
+	}
 	g_hall.elec_angle_edge = g_hall.elec_angle;
 	float low_res = g_hall.low_res_pos;
-	if (g_hall.dir == NEGATIVE) {
-		low_res += 1.0f;
-	}
 	g_hall.delta_angle_edge = get_angle_diff(low_res * PHASE_60_DEGREE, g_hall.elec_angle_edge);
 	if (ABS(g_hall.delta_angle_edge) >= 2.0f) {
-		g_hall.angle_smooth_step = g_hall.delta_angle_edge/SMOOTH_COUNT;
-		g_hall.angle_smooth_cnt = 1;
+		g_hall.angle_smooth_step = 0;//g_hall.delta_angle_edge/SMOOTH_COUNT;
+		g_hall.angle_smooth_cnt = SMOOTH_COUNT + 1;
 	}else {
 		g_hall.angle_smooth_step = 0;
 		g_hall.angle_smooth_cnt = SMOOTH_COUNT + 1;

+ 3 - 2
Applications/foc/motor/hall.h

@@ -31,7 +31,7 @@ typedef struct {
 	float   		phase_offset;
 	float			mot_poles;
 	u8				state; // state = A <<2+ B <<1 + C
-	s8				low_res_pos; //每个霍尔中断更新一次,60度
+	float			low_res_pos; //每个霍尔中断更新一次,60度
 	s8				dir;
 	s8				prev_dir;
 	u32				edge_ticks;
@@ -46,7 +46,8 @@ typedef struct {
 	float			position;
 	hsample_t		samples;
 	bool			b_trns_det; //速度突变
-	u32				errors;
+	u32				sig_errors;
+	u32				noise_errors;
 }hall_t;
 
 void hall_init(void);

+ 14 - 4
Applications/foc/motor/motor.c

@@ -95,7 +95,7 @@ static void MC_Check_MosVbusThrottle(void) {
 	gpio_phase_u_detect(false);
 	float abc[3];
 	get_phase_vols_filtered(abc);
-	int vbus_vol = get_vbus_int();
+	int vbus_vol = sample_vbus_raw();
 	if (vbus_vol > mc_conf()->c.max_dc_vol) {
 		mc_set_critical_error(FOC_CRIT_OV_Vol_Err);
 	}
@@ -103,7 +103,7 @@ static void MC_Check_MosVbusThrottle(void) {
 		mc_set_critical_error(FOC_CRIT_UN_Vol_Err);
 	}
 
-	vbus_vol = get_acc_vol();
+	vbus_vol = sample_acc_vol_raw();
 	sys_debug("acc vol %d\n", vbus_vol);
 	if (vbus_vol >= mc_conf()->c.max_dc_vol) {
 		mc_set_critical_error(FOC_CRIT_ACC_OV_Err);
@@ -820,6 +820,7 @@ u16 mc_get_running_status2(void) {
 
 static float _force_angle = 0.0f;
 static int   _force_wait = 2000;
+static int   _force_invert = false;
 /* 开环,强制给定电角度和DQ的电压 */
 void mc_force_run_open(s16 vd, s16 vq, bool align) {
 	if (motor.b_start || motor.b_force_run) {
@@ -861,6 +862,11 @@ void mc_force_run_open(s16 vd, s16 vq, bool align) {
 	}else {
 		_force_wait = 2000;
 	}
+	if (vd < 0) {
+		_force_invert = true;
+	}else {
+		_force_invert = false;
+	}
 	motor.b_force_run = true;
 }
 
@@ -1245,7 +1251,7 @@ static void motor_vbus_crit_check(s16 curr_vbus) {
 void TIMER_UP_IRQHandler(void){
 	if (!motor.b_start && !mot_contrl_is_start(&motor.controller)) {
 		motor_encoder_update(false);
-		motor_vbus_crit_check((s16)get_vbus_int());
+		motor_vbus_crit_check((s16)sample_vbus_raw());
 	}
 }
 
@@ -1463,7 +1469,11 @@ static bool mc_process_force_running(void) {
 			if (_force_wait > 0) {
 				--_force_wait;
 			}else {
-				_force_angle += 1.5f;
+				float step = 2.5f;
+				if (_force_invert) {
+					step = -step;
+				}
+				_force_angle += step;
 				norm_angle_deg(_force_angle);
 				mot_contrl_set_angle(&motor.controller, _force_angle);
 			}

+ 4 - 0
Applications/foc/samples.c

@@ -278,7 +278,11 @@ s16 sample_motor_temp(void) {
 	if (r > 65535) {
 		r = 65535;
 	}
+#ifdef CONFIG_USE_ENCODER_HALL
+	motor_temp.value = 25;
+#else
 	motor_temp.value = ntc_get_motor_temp(r);
+#endif
 	LowPass_Filter(motor_temp.filted_value, motor_temp.value, motor_temp.lowpass);
 	return (s16)motor_temp.value;
 #else

+ 1 - 1
Project/MC100_HALL.uvoptx

@@ -77,7 +77,7 @@
         <tvExpOptDlg>0</tvExpOptDlg>
         <IsCurrentTarget>1</IsCurrentTarget>
       </OPTFL>
-      <CpuCode>0</CpuCode>
+      <CpuCode>255</CpuCode>
       <DebugOpt>
         <uSim>0</uSim>
         <uTrg>1</uTrg>