34 unsigned short last_angle = 0, positive_counter = 0;
35 float u, v, w, angle = 0;
42 for (
unsigned char counter = 0; counter < 50; ++counter) {
47 if (current_angle > last_angle)
51 last_angle = current_angle;
59 for (
unsigned char counter = 0; counter < 50; ++counter) {
64 if (current_angle < last_angle)
68 last_angle = current_angle;
82 if (positive_counter >= 75)
84 else if (positive_counter < 25)
108 alpha = d * cf - q * sf;
109 beta = q * cf + d * sf;
112 *u = 0.5f - alpha /
VBUS;
113 *v = 0.5f + (alpha * 0.5f - beta * 0.866025404f) /
VBUS;
116 float tmp2, tmp3, Ta, Tb, Tc;
120 alpha = d * cf - q * sf;
121 beta = q * cf + d * sf;
122 tmp2 = beta * 0.5f + alpha * 0.8660254f;
126 vec_sector = ((*(
unsigned int *) &tmp2) >> 31) ? vec_sector : (vec_sector - 1);
127 vec_sector = ((*(
unsigned int *) &tmp3) >> 31) ? vec_sector : (vec_sector - 1);
128 vec_sector = ((*(
unsigned int *) &beta) >> 31) ? (7 - vec_sector) : vec_sector;
131 switch (vec_sector) {
138 case 5:Ta = tmp3 + tmp2;
149 *u = (Ta /
VBUS) * 0.5f + 0.5f;
150 *v = (Tb /
VBUS) * 0.5f + 0.5f;
151 *w = (Tc /
VBUS) * 0.5f + 0.5f;
used to place important parameter configurations for users
#define CALI_TORQUE
set calibrate torque to 0.5
#define VBUS
bus voltage is 8.0V
unsigned short encoder_get_mechanical_angle(void)
read mechanical angle directly from encoder
this is the header file of encoder.c.
float fast_sin(float theta)
fast calculation of sine
this is the header file of fast_math.c, the macros of fast amplitude limiting algorithm and cosine fu...
#define fast_cos(x)
fast calculation of cosine
void foc_calculate_dutycycle(float elect_angle, float d, float q, float *u, float *v, float *w)
calculate the corresponding three-phase PWM duty cycle under the current electrical angle
unsigned char foc_parameter_available_flag
flag variable for FOC parameter availability
volatile FOC_Structure_t FOC_Struct
FOC handler.
void foc_calibrate_phase(void)
automatic phase sequence detection and correction
volatile unsigned char phase_sequence
motor phase sequence flag variable
this is the header file of foc.c, which defines the structure of FOC algorithm and angle conversion f...
structure of FOC algorithm
void delayms(unsigned long count)
millisecond delay function, any time time.
system basic function header file
void update_pwm_dutycycle(float ch0, float ch1, float ch2)
update timer1 ch0 1 2 duty-cycle
this is the header file of timer.c.