Kaynağa Gözat

通过ENCODER_CC_INVERT定义编码器是否和电机的方向相反

Signed-off-by: huhui <huhui@sharkgulf.com>
huhui 3 yıl önce
ebeveyn
işleme
0c9da426f4

+ 1 - 0
Applications/bsp/board_gd32demo.h

@@ -100,6 +100,7 @@
 
 /* 是否用编码器 */
 #define USE_ENCODER_ABI
+#define ENCODER_CC_INVERT //编码器方向和电机反向
 
 /* 编码器 */
 #define ENC_A_GROUP GPIOA

+ 22 - 7
Applications/foc/motor/encoder.c

@@ -70,14 +70,21 @@ void encoder_lock_position(bool enable) {
 	}
 }
 
+static __INLINE float _pll_over_comp(void) {
+	u8 dir = ENC_DIR_DOWN;
+#ifdef ENCODER_CC_INVERT
+	dir = ENC_DIR_UP;
+#endif
+	if(ENC_Direction() == dir){
+		return -((float)g_encoder.cpr);
+	}
+	return (float)g_encoder.cpr;
+}
+
 static __INLINE void encoder_run_pll(float cnt) {
 	float pll_comp = 0.0f;
 	if (g_encoder.b_timer_ov) {
-		if(ENC_Direction() == ENC_DIR_DOWN){
-			pll_comp = -((float)g_encoder.cpr);
-		}else {
-			pll_comp = g_encoder.cpr;
-		}
+		pll_comp = _pll_over_comp();
 		g_encoder.b_timer_ov = false;
 	}
 	encoder_pll_update_gain();
@@ -85,14 +92,22 @@ static __INLINE void encoder_run_pll(float cnt) {
 	g_encoder.est_angle_counts = g_encoder.est_pll.observer;
 }
 
+static __INLINE u32 _abi_count(void) {
+#ifdef ENCODER_CC_INVERT
+	return (g_encoder.cpr - ENC_COUNT);
+#else
+	return ENC_COUNT;
+#endif
+}
+
 float encoder_get_theta(void) {
 	if (!g_encoder.b_index_found) {
 		return g_encoder.pwm_angle;
 	}
-	u32 cnt = ENC_COUNT;
+	u32 cnt = _abi_count();
 	__NOP();__NOP();__NOP();__NOP();
 	if (ENC_OverFlow()) {
-		cnt = ENC_COUNT;
+		cnt = _abi_count();
 		g_encoder.b_timer_ov = true;
 		ENC_ClearUpFlags();
 	}

+ 0 - 1
Project/GD32_DEMO.uvoptx

@@ -120,7 +120,6 @@
         <SetRegEntry>
           <Number>0</Number>
           <Key>DLGUARM</Key>
-          <Name>?</Name>
         </SetRegEntry>
         <SetRegEntry>
           <Number>0</Number>