Browse Source

工厂测试模式

Signed-off-by: kevin <huhui@sharkgulf.com>
kevin 2 years ago
parent
commit
fb7121b6f3

+ 17 - 10
Applications/app/factory.c

@@ -4,6 +4,7 @@
 #include "prot/can_foc_msg.h"
 #include "foc/samples.h"
 #include "foc/motor/current.h"
+#include "foc/motor/motor.h"
 #include "libs/utils.h"
 #include "libs/logger.h"
 #include "os/os_task.h"
@@ -53,18 +54,16 @@ void can_process_factory_message(can_message_t *can_message){
 			}
 			u8 item = decode_u8(can_message->data);
 			if (item == 1) { //3相驱动测试
-				u8 mask = decode_u8((u8 *)can_message->data + 1);
-				pwm_3phase_sides(false, mask);
+				u8 duty = decode_u8((u8 *)can_message->data + 1);
+				pwm_3phase_sides(true, 0);
+				u16 duty_time = (u16)((float)duty * FOC_PWM_Half_Period / 100.0f);
+				pwm_update_duty(duty_time, duty_time, duty_time);
 			}else if (item == 2) {//获取所有电压的采集值
 				can_response_vols(can_message->src, can_message->key);
 				return;
 			}else if (item == 3) { //读取gpio状态
-				encode_u16(response + 3, GPIOA_VALUE);
-				encode_u16(response + 5, GPIOB_VALUE);
-				encode_u16(response + 7, GPIOC_VALUE);
-				encode_u16(response + 9, GPIOD_VALUE);
-				encode_u16(response + 11, GPIOE_VALUE);
-				rsplen += 10;
+				encode_u16(response + 3, gpio_get_pin_values());
+				rsplen += 4;
 			}else if (item == 4) { // u phase detect
 				int count = 20;
 				float uvw[3] = {0, 0, 0};
@@ -89,15 +88,23 @@ void can_process_factory_message(can_message_t *can_message){
 				rsplen += 6;
 			}else if (item == 5) { //phase current test
 				u8 start = decode_u8((u8 *)can_message->data + 1);
-				if (start) {
+				if (start == 1) {
 					pwm_3phase_sides(true, 0); //use pwm output, disable timer break in
 					if (!start_pwm_adc()) {
 						response[2] = 1;
 						break;
 					}
-				}else {
+				}else if (start == 0){
 					stop_pwm_adc();
 					pwm_3phase_init();
+				}else {
+					s16 ui = (s16)mot_contrl()->foc.in.curr_abc[0];
+					s16 vi = (s16)mot_contrl()->foc.in.curr_abc[1];
+					s16 wi = (s16)mot_contrl()->foc.in.curr_abc[2];
+					encode_s16(response + 3, ui);
+					encode_s16(response + 5, ui);
+					encode_s16(response + 7, ui);
+					rsplen += 6;
 				}
 			}
 			break;

+ 8 - 1
Applications/bsp/gd32/gpio.c

@@ -229,7 +229,14 @@ bool gpio_motor_locked(void) {
 #endif
 }
 
-
+u32 gpio_get_pin_values(void) {
+	u32 value = gpio_input_bit_get(GPIO_MLOCK_IN_GROUP, GPIO_MLOCK_IN_PIN) == SET?1:0;
+	value |= (gpio_input_bit_get(GPIO_BRAKE_IN_GROUP, GPIO_BRAKE_IN_PIN) == SET?1:0)<<1;
+	value |= (gpio_input_bit_get(REPEAR_IN_GROUP, REPEAR_IN_PIN) == SET?1:0) << 2;
+	value |= (gpio_input_bit_get(BOOT_PIN_0_GROUP, BOOT_PIN_0_PIN) == SET?:0) << 3;
+	value |= (gpio_input_bit_get(BOOT_PIN_1_GROUP, BOOT_PIN_1_PIN) == SET?:0) << 4;
+	return value;
+}
 
 void gpio_ir2136_enable(bool enable) {
 #ifdef GD32_FOC_DEMO

+ 1 - 0
Applications/bsp/gd32/gpio.h

@@ -39,5 +39,6 @@ void gpio_led_enable(bool enable);
 void gpio_brk_light_enable(bool enable);
 u8  gpio_board_id(void);
 bool gpio_is_repear_mode(void);
+u32 gpio_get_pin_values(void);
 
 #endif /* _GPIO_PIN_H__ */

+ 6 - 2
Applications/bsp/gd32/pwm.c

@@ -37,18 +37,22 @@ static rcu_periph_enum _rcu_clk(u32 timer) {
     return RCU_TIMER2;      
 }
 
+static bool phase_start = false;
 void pwm_3phase_init(void){
 	_pwm_gpio_config();
     _init_pwm_timer(true);
+	phase_start = false;
 }
 
 void pwm_3phase_sides(bool pwm, u8 mask) {
-	timer_deinit(MOS_PWM_TIMER);
-	if (pwm) {
+	if (pwm && !phase_start) {
+		timer_deinit(MOS_PWM_TIMER);
 		_pwm_gpio_config();
 		_init_pwm_timer(false);
+		phase_start = true;
 		return;
 	}
+	timer_deinit(MOS_PWM_TIMER);
 	rcu_periph_clock_enable(_rcu_clk(MOS_PWM_TIMER));
     gpio_init(PWM_U_P_GROUP,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ,PWM_U_P_PIN);
     gpio_init(PWM_V_P_GROUP,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ,PWM_V_P_PIN);

+ 3 - 3
Applications/prot/can_foc_msg.c

@@ -160,15 +160,15 @@ void can_response_vols(u8 can, u8 key) {
 	u8 data[16];
 	int len;
 	encoder_can_key(data, CMD_2_CAN_KEY(key));
-	len = 2;
-	float thro = get_throttle_float() * 10.0f;
-	encode_s16(data + len, (s16)thro);
 	len += 2;
 	float acc = get_acc_vol() * 10.0f;
 	encode_s16(data + len, (s16)acc);
 	len += 2;
 	float vbus = get_vbus_float() * 10.0f;
 	encode_s16(data + len, (s16)vbus);
+	len = 2;
+	float thro = get_throttle_float() * 10.0f;
+	encode_s16(data + len, (s16)thro);
 	len += 2;
 	float thro_5v = get_thro_5v_float() * 10.0f;
 	encode_s16(data + len, (s16)thro_5v);