ソースを参照

对3.3和5v进行实时校准,温度变化3.3和5v会有明显变化

Signed-off-by: huhui <huhui@sharkgulf.com>
huhui 3 年 前
コミット
5ed8eb8078

+ 57 - 5
Applications/bsp/adc.c

@@ -3,6 +3,7 @@
 #include "libs/utils.h"
 #include "os/os_task.h"
 #include "libs/logger.h"
+#include "math/fast_math.h"
 
 #define REG_CHAN_DMA 1
 
@@ -11,11 +12,15 @@
 #define ADC01_NUM 7
 #define ADC2_NUM 0
 #else
-#define ADC01_NUM (6)
+#define ADC01_NUM (8)
 #define ADC2_NUM 4
 #endif
 #define REG_CHAN_NUM (ADC01_NUM + ADC2_NUM)
 s16 adc_buffer[REG_CHAN_NUM];
+float vref_adc = 1408.0f;
+float vref_5v_adc = 2047.0f;
+
+#define VREF_ADC_DATA 1509.0F //1498, 1.21/3.3*4095
 
 static void adc01_dma_init(void)
 {
@@ -123,6 +128,10 @@ static void adc0_init(void){
 	adc_regular_channel_config(ADC0, 3, U_VOL_ADC_CHAN, ADC_REGCHAN_SAMPLE_TIME);
 	adc_regular_channel_config(ADC0, 4, V_VOL_ADC_CHAN, ADC_REGCHAN_SAMPLE_TIME);
 	adc_regular_channel_config(ADC0, 5, W_VOL_ADC_CHAN, ADC_REGCHAN_SAMPLE_TIME);
+	adc_regular_channel_config(ADC0, 6, ADC_CHANNEL_10, ADC_REGCHAN_SAMPLE_TIME);
+	adc_regular_channel_config(ADC0, 7, ADC_CHANNEL_17, ADC_REGCHAN_SAMPLE_TIME);
+	adc_tempsensor_vrefint_enable();
+	adc_buffer[7] = VREF_ADC_DATA; //1.21/3.3*4095
 #endif
 #endif
     /* configure ADC regular channel trigger */
@@ -326,9 +335,44 @@ void adc_init(void) {
 	adc_current_sample_config(0);
 }
 
+void adc_set_vref_calc(float v) {
+	vref_adc = v;
+}
+
+void adc_set_5vref_calc(float v) {
+	vref_5v_adc = v;
+}
+
+static float vref_compestion_filter = 1.0f;
+#define VREF_COMPESTION() (vref_adc/(float)adc_buffer[7])
+void adc_3v3ref_filter(void) {
+	float value = VREF_COMPESTION();
+	LowPass_Filter(vref_compestion_filter, value, 0.01f);
+}
+
+float adc_vref_compesion(void) {
+	return vref_compestion_filter;
+}
+
+static float vref_5v_compestion_filter = 1.0f;
+#define VREF_5V_COMPESTION() (vref_5v_adc/(float)adc_buffer[1])
+void adc_5vref_filter(void) {
+	float value = VREF_5V_COMPESTION();
+	LowPass_Filter(vref_5v_compestion_filter, value, 0.01f);
+}
+
+float adc_5vref_compesion(void) {
+	return vref_5v_compestion_filter;
+}
+
+void adc_vref_filter(void) {
+	adc_3v3ref_filter();
+	adc_5vref_filter();
+}
+
 u16 adc_get_vbus(void) {
 #ifdef MC100_HW_V1
-	return adc_buffer[ADC01_NUM + 0];
+	return (float)adc_buffer[ADC01_NUM + 0] * VREF_COMPESTION();
 #else
 	return adc_buffer[0];
 #endif
@@ -336,7 +380,7 @@ u16 adc_get_vbus(void) {
 
 u16 adc_get_acc(void) {
 #ifdef MC100_HW_V1
-	return adc_buffer[ADC01_NUM + 1];
+	return (float)adc_buffer[ADC01_NUM + 1] * VREF_COMPESTION();
 #else
 	return adc_get_vbus();
 #endif
@@ -344,7 +388,7 @@ u16 adc_get_acc(void) {
 
 u16 adc_get_ibus(void) {
 #ifdef MC100_HW_V1
-	return adc_buffer[2];
+	return (float)adc_buffer[2] * VREF_5V_COMPESTION();
 #else
 	return 0;
 #endif
@@ -352,7 +396,7 @@ u16 adc_get_ibus(void) {
 
 u16 adc_get_throttle(void) {
 #ifdef MC100_HW_V1
-	return adc_buffer[ADC01_NUM + 2];
+	return adc_buffer[ADC01_NUM + 2] * VREF_COMPESTION();
 #else
 	return adc_buffer[1];
 #endif
@@ -393,6 +437,14 @@ u16 adc_get_motor_temp(void) {
 #endif
 }
 
+u16 adc_get_vref(void) {
+	return adc_buffer[7];
+}
+
+u16 adc_get_5v_ref(void) {
+	return adc_buffer[1];
+}
+
 void adc_start_convert(void) {
 	int drop = 2;
     /* clear the ADC flag */

+ 13 - 4
Applications/bsp/adc.h

@@ -3,6 +3,9 @@
 #include "bsp/bsp.h"
 #include "os/os_types.h"
 
+float adc_vref_compesion(void);
+float adc_5vref_compesion(void);
+
 /*
 inserted ADC 由timer0 ch3触发,
 注意:adc所有外部触发都是下降沿触发 
@@ -77,16 +80,16 @@ static void __inline adc_phase_current_read(u8 phases, s32 *v1, s32 *v2) {
 	v[1] = ADC_IDATA1(ADC1);
 	v[2] = ADC_IDATA2(ADC0);
 	v[3] = ADC_IDATA3(ADC1);
-	*v1 = _adc_avg(v);
+	*v1 = (s32)((float)_adc_avg(v) * * adc_5vref_compesion());
 
 	v[0] = ADC_IDATA0(ADC1);
 	v[1] = ADC_IDATA1(ADC0);
 	v[2] = ADC_IDATA2(ADC1);
 	v[3] = ADC_IDATA3(ADC0);
-	*v2 = _adc_avg(v);
+	*v2 = (s32)((float)_adc_avg(v) * * adc_5vref_compesion());
 #else
-	*v1 = ADC_IDATA0(ADC0);
-	*v2 = ADC_IDATA0(ADC1);
+	*v1 = (s32)((float)ADC_IDATA0(ADC0) * adc_5vref_compesion());
+	*v2 = (s32)((float)ADC_IDATA0(ADC1) * adc_5vref_compesion());
 #endif
 #else
 	*v1 = (s32)(*adc_phase_reg1[phases]) ;
@@ -170,4 +173,10 @@ u16 adc_get_mos_temp(void);
 u16 adc_get_mos_temp2(void);
 u16 adc_get_motor_temp(void);
 u16 adc_get_ibus(void);
+u16 adc_get_vref(void);
+void adc_set_vref_calc(float v);
+void adc_vref_filter(void);
+u16 adc_get_5v_ref(void);
+void adc_set_5vref_calc(float v);
+
 #endif /* _ADC_H__ */

+ 2 - 2
Applications/bsp/board_mc100_v1.h

@@ -24,7 +24,7 @@
 #define CONFIG_UNDER_VOL_RPM     1000
 #define CONFIG_UNDER_VOL_PHASE_CURR 100.0F
 #define CONFIG_UNDER_VOL_DC_CURR 15.0F
-#define CONFIG_MAX_FW_D_CURR     100.0F //d轴最大的退磁电流
+#define CONFIG_MAX_FW_D_CURR     150.0F //d轴最大的退磁电流
 
 #define CONFIG_CURR_LP_WC (600.0F)
 
@@ -52,7 +52,7 @@
 #define TSampleBefore (TDead + TRise) //采样开始前需要等待的时间
 
 #define ADC_REFERENCE_VOLTAGE  (3.3F)
-#define ADC_FULL_MAX          (4096.0F)
+#define ADC_FULL_MAX          (4095.0F)
 
 /* MOS驱动 */
 #define pwm_timer TIMER0

+ 1 - 1
Applications/bsp/bsp.h

@@ -27,7 +27,7 @@
 
 #define FOC_CTRL_US (1.0f/(float)FOC_PWM_FS)
 
-#define ADC_REGCHAN_SAMPLE_TIME ADC_SAMPLETIME_55POINT5
+#define ADC_REGCHAN_SAMPLE_TIME ADC_SAMPLETIME_239POINT5
 #define ADC_TRIG_CONV_LATENCY_CYCLES 12.5f
 #define ADC_SAMPLING_CYCLES 13.5f
 

+ 25 - 7
Applications/foc/motor/motor.c

@@ -17,6 +17,7 @@
 #include "foc/core/e_ctrl.h"
 #include "foc/samples.h"
 #include "foc/motor/motor_param.h"
+#include "foc/core/foc_observer.h"
 #include "foc/core/torque.h"
 #include "app/nv_storage.h"
 #include "foc/core/torque.h"
@@ -41,23 +42,33 @@ static motor_t motor = {
 };
 
 static void MC_Check_MosVbusThrottle(void) {
-	static bool _ibus_offset = false;
 	int count = 1000;
 	float ibus_adc = 0;
+	float vref_adc = 0;
+	float vref_5v_adc = 0;
 	gpio_phase_u_detect(true);
 	while(count-- > 0) {
 		task_udelay(20);
 		sample_uvw_phase();
 		sample_throttle();
 		sample_vbus();
-		ibus_adc += adc_get_ibus();
+		vref_adc += adc_get_vref();
+		vref_5v_adc += adc_get_5v_ref();
 	}
-	if (!_ibus_offset) {
-		u16 offset = ((float)ibus_adc)/1000.0f;
-		sys_debug("ibus offset %d\n", offset);
-		sample_ibus_offset(offset);
-		_ibus_offset = true;
+
+	adc_set_vref_calc(vref_adc/1000.0f);
+	adc_set_5vref_calc(vref_5v_adc/1000.0f);
+	sys_debug("5v ref %f\n", vref_5v_adc/1000.0f);
+	count = 50;
+	while(count-- >0) {
+		task_udelay(300);
+		ibus_adc += adc_get_ibus();
 	}
+
+	u16 offset = ((float)ibus_adc)/50.0f;
+	sys_debug("ibus offset %d\n", offset);
+	sample_ibus_offset(offset);
+
 	gpio_phase_u_detect(false);
 	float abc[3];
 	get_phase_vols(abc);
@@ -749,6 +760,10 @@ static bool mc_can_stop_foc(void) {
 			return true;
 		}
 	}
+	/* 启用无感观测器,同时速度低于观测器允许的最小速度,关闭输出,滑行 */
+	if (!foc_observer_is_encoder() && PMSM_FOC_GetSpeed() < CONFIG_SMO_MIN_SPEED) {
+		return true;
+	}
 	return false;
 }
 
@@ -844,6 +859,9 @@ measure_time_t g_meas_MCTask;
 #define mc_TaskEnd time_measure_end(&g_meas_MCTask)
 void Sched_MC_mTask(void) {
 	mc_TaskStart;
+
+	adc_vref_filter();
+
 	bool is96v_prev = motor.b_is96Mode;
 	mc_detect_vbus_mode();