Forráskód Böngészése

update code

Signed-off-by: huhui <huhui@sharkgulf.com>
huhui 4 éve
szülő
commit
8d15754255

+ 7 - 7
Application/FOC/foc_api.c

@@ -47,8 +47,8 @@ static void foc_defulat_value(void){
 	g_foc.speed_command = -1;
 	memset(&g_foc.phase_time, 0, sizeof(g_foc.phase_time));
 	g_foc.sector = 0;
-	g_foc.dq_v.Id = 0;
-	g_foc.dq_v.Iq = 0;
+	g_foc.dq_last.Id = 0;
+	g_foc.dq_last.Iq = 0;
 	phase_current_init(&g_foc.current_samp);
 	ramp_ctrl_init(&g_foc.voltage_ramp);
 	ramp_ctrl_init(&g_foc.current_ramp);
@@ -104,15 +104,17 @@ void foc_set_dq_command(float d, float q) {
 
 void foc_set_voltage_ramp(float final){
 	printf("voltage %f\n", final);
-	ramp_set_target(&g_foc.voltage_ramp, g_foc.dq_v.Vq, final, START_RAMP_DURATION);
+	ramp_set_target(&g_foc.voltage_ramp, g_foc.dq_last.Vq, final, START_RAMP_DURATION);
+	ramp_exc(&g_foc.voltage_ramp);
 }
 
 void foc_set_speed_ramp(u16 rpm){
 	ramp_set_target(&g_foc.speed_ramp, foc_get_speed(), rpm, SPEED_RAMP_DURATION);
+	ramp_exc(&g_foc.speed_ramp);
 }
 
-void foc_open_loop(void) {
-	g_foc.mode = FOC_MODE_OPEN_LOOP;
+void foc_set_controller_mode(control_mode_t mode) {
+	g_foc.mode = mode;
 }
 /*
 void foc_set_start_ramp(float v) {
@@ -128,8 +130,6 @@ void foc_set_ref_speed(u16 rpm) {
 	g_foc.speed_command = rpm;
 	if (g_foc.mode == FOC_MODE_OPEN_LOOP) {
 		foc_set_voltage_ramp(speed_to_voltage(rpm));
-		ramp_set_target(&g_foc.speed_ramp, rpm, rpm, SPEED_RAMP_DURATION);
-		ramp_exc(&g_foc.voltage_ramp);
 	}
 	normal_task_enable(true);
 }

+ 1 - 1
Application/FOC/foc_api.h

@@ -5,7 +5,7 @@
 
 void foc_init(void);
 void foc_clear(void);
-void foc_open_loop(void);
+void foc_set_controller_mode(control_mode_t mode);
 void set_dq_voltage(float d_v, float q_v);
 void foc_pwm_start(bool start);
 foc_fault_t foc_start_motor(void);

+ 5 - 4
Application/FOC/foc_core.c

@@ -72,8 +72,8 @@ static void __inline Foc_Calc_Voltage(motor_foc_t *foc, dq_t *sampled, dq_t *ref
 		ref_out->Vd = foc->dq_command.Vd;
 		ref_out->Vq = foc->dq_command.Vq;
 	}
-	foc->dq_v.Vd = ref_out->Vd;
-	foc->dq_v.Vq = ref_out->Vq;
+	foc->dq_last.Vd = ref_out->Vd;
+	foc->dq_last.Vq = ref_out->Vq;
 }
 
 static void __inline DeadTime_Compensation(current_samp_t *c_sample, phase_time_t *time) {
@@ -198,9 +198,10 @@ void Foc_Speed_Ramp(motor_foc_t *foc){
 		foc->speed_command = -1;
 		
 		if (ref_rpm + RPM_FOR_CLOSE_LOOP < current_rpm){
-			ramp_set_target(&foc->voltage_ramp, foc->dq_command.Vq, speed_to_voltage(ref_rpm), SPEED_RAMP_DURATION);
-			ramp_exc(&foc->current_ramp);
+			foc_set_voltage_ramp(speed_to_voltage(ref_rpm));
 			foc->mode = FOC_MODE_OPEN_LOOP;
+		}else {
+			foc_set_speed_ramp(ref_rpm);
 		}
 	}
 }

+ 3 - 3
Application/FOC/foc_type.h

@@ -97,7 +97,7 @@ typedef struct foc_s {
 	current_samp_t current_samp; /*三相电流采集 */
 	dq_t dq_command; 
 	int  speed_command; 
-	dq_t dq_v;
+	dq_t dq_last;
 	u8   sector; //svpwm 扇区
 	float vbus; //母线电压
 	motor_param_t motor_param;
@@ -106,8 +106,8 @@ typedef struct foc_s {
 	pi_controller_t    id_controller;
 	pi_controller_t    iq_controller;
 	pi_controller_t    speed_controller;
-	volatile foc_state_t state;
-	volatile control_mode_t mode;
+	foc_state_t state;
+	control_mode_t mode;
 	//override_param_t override;
 	bool mosfec_gate;
 	ramp_t voltage_ramp; //开环情况下直接设置svm的voltage

+ 19 - 19
Application/FOC/hall_sensor.c

@@ -94,15 +94,15 @@ float hall_sensor_get_theta(void){
 	}
 	_hall.est_theta = tick_2_s(delta_ticks(_hall.ticks)) * _hall.degree_per_s + _hall.theta;
 	float est_delta = _hall.est_theta - _hall.theta;
-	if (est_delta > 60) {
-		_hall.est_theta = _hall.theta + 60;
-		if (_hall.est_theta >= 360){
-			_hall.est_theta -= 360;
+	if (est_delta > PHASE_60_DEGREE) {
+		_hall.est_theta = _hall.theta + PHASE_60_DEGREE;
+		if (_hall.est_theta >= PHASE_360_DEGREE){
+			_hall.est_theta -= PHASE_360_DEGREE;
 		}
-	}else if (est_delta < -60){
-		_hall.est_theta = _hall.theta - 60;
-		if (_hall.est_theta <= -360) {
-			_hall.est_theta += 360;
+	}else if (est_delta < -PHASE_60_DEGREE){
+		_hall.est_theta = _hall.theta - PHASE_60_DEGREE;
+		if (_hall.est_theta <= -PHASE_360_DEGREE) {
+			_hall.est_theta += PHASE_360_DEGREE;
 		}
 	}
 	return _hall.est_theta;
@@ -125,7 +125,7 @@ float hall_sensor_avg_speed(void) {
 
 
 int hall_sensor_calibrate(float voltage, u16 *hall_table){
-	foc_open_loop();
+	foc_set_controller_mode(FOC_MODE_OPEN_LOOP);
 	hall_sensor_set_theta(true, 0.0f);
 	foc_set_dq_command(0.0f, 0.0f);
 	foc_pwm_start(true);
@@ -207,34 +207,34 @@ void hall_sensor_handler(void) {
 		case STATE_1:
 			if (state_prev == STATE_5) {
 				_hall.direction = POSITIVE;
-				theta_now = _hall.phase_offset + 60.0f;
+				theta_now = _hall.phase_offset + PHASE_60_DEGREE;
 			}else if (state_prev == STATE_3) {
 				_hall.direction = NEGATIVE;
-				theta_now = _hall.phase_offset + 120.0f;
+				theta_now = _hall.phase_offset + PHASE_120_DEGREE;
 			}
 			break;
 		case STATE_2:
 			if (state_prev == STATE_3) {
 				_hall.direction = POSITIVE;
-				theta_now = _hall.phase_offset + 180.0f;
+				theta_now = _hall.phase_offset + PHASE_180_DEGREE;
 			}else if (state_prev == STATE_6) {
 				_hall.direction = NEGATIVE;
-				theta_now = _hall.phase_offset + 240.0f;
+				theta_now = _hall.phase_offset + PHASE_240_DEGREE;
 			}
 			break;
 		case STATE_3:
 			if (state_prev == STATE_1) {
 				_hall.direction = POSITIVE;
-				theta_now = _hall.phase_offset + 120.0f;
+				theta_now = _hall.phase_offset + PHASE_120_DEGREE;
 			}else if (state_prev == STATE_2) {
 				_hall.direction = NEGATIVE;
-				theta_now = _hall.phase_offset + 180.0f;
+				theta_now = _hall.phase_offset + PHASE_180_DEGREE;
 			}
 			break;
 		case STATE_4:
 			if (state_prev == STATE_6) {
 				_hall.direction = POSITIVE;
-				theta_now = _hall.phase_offset + 300.0f;
+				theta_now = _hall.phase_offset + PHASE_300_DEGREE;
 			}else if (state_prev == STATE_5) {
 				_hall.direction = NEGATIVE;
 				theta_now = _hall.phase_offset;
@@ -246,16 +246,16 @@ void hall_sensor_handler(void) {
 				theta_now = _hall.phase_offset;
 			}else if (state_prev == STATE_1) {
 				_hall.direction = NEGATIVE;
-				theta_now = _hall.phase_offset + 60.0f;
+				theta_now = _hall.phase_offset + PHASE_60_DEGREE;
 			}
 			break;
 		case STATE_6:
 			if (state_prev == STATE_2) {
 				_hall.direction = POSITIVE;
-				theta_now = _hall.phase_offset + 240.0f;
+				theta_now = _hall.phase_offset + PHASE_240_DEGREE;
 			}else if (state_prev == STATE_4) {
 				_hall.direction = NEGATIVE;
-				theta_now = _hall.phase_offset + 300.0f;
+				theta_now = _hall.phase_offset + PHASE_300_DEGREE;
 			}
 			break;
 		default:

+ 7 - 0
Application/FOC/hall_sensor.h

@@ -6,6 +6,13 @@
 #define NEGATIVE          (int8_t)-1
 #define POSITIVE          (int8_t)1
 
+#define PHASE_60_DEGREE (60.0f)
+#define PHASE_120_DEGREE (120.0f)
+#define PHASE_180_DEGREE (180.0f)
+#define PHASE_240_DEGREE (240.0f)
+#define PHASE_300_DEGREE (300.0f)
+#define PHASE_360_DEGREE (360.0f)
+
 #define STATE_0 (uint8_t)0
 #define STATE_1 (uint8_t)1
 #define STATE_2 (uint8_t)2