miniFOC 1.0.3
This open-source project aims to accomplish a FOC(Field Oriented Control) scheme that is operatable with minimum costs in China.
encoder.c
Go to the documentation of this file.
1/**************************************************************************/
10#include "encoder.h"
11#include "spi.h"
12#include "gd32f1x0.h"
13#include "foc.h"
14#include "timer.h"
15#include "system.h"
16#include "config.h"
17#include "filter.h"
18
23volatile unsigned short machine_angle_offset = 0;
29volatile static unsigned short last_mechanical_angle = 0;
34volatile static long long total_machine_angle = 0;
39volatile static long long systick_mechanical_angle_last = 0;
40
44void encoder_delay(void) {
45 /* use loop functions to delay time */
46 unsigned char delay_counter = 0xff;
47 while (delay_counter)
48 delay_counter--;
49}
50
56unsigned short encoder_read_data(unsigned short TxData) {
57 unsigned short data;
58
59 /* pull down the CS pin and prepare to send data */
60 gpio_bit_write(GPIOA, GPIO_PIN_4, RESET);
62
63 /* call SPI related functions to send and receive data */
64 data = spi_readwrite_halfworld(TxData);
66
67 /* pull up CS pin to end sending data */
68 gpio_bit_write(GPIOA, GPIO_PIN_4, SET);
69 return data;
70}
71
76unsigned short encoder_get_mechanical_angle(void) {
77 /* read back register raw data */
78 unsigned short angle = encoder_read_data(0x0000) >> 4;
79
80 /* statistical total rotation angle */
83
84 /* returns the result of subtracting the offset. */
86}
87
94 /* read back the mechanical angle directly from the magnetic encoder */
95 unsigned short tmp_mechanical_angle = encoder_get_mechanical_angle();
96
97 /* calculate and update the mechanical angle and electric angle */
98 FOC_Struct.mechanical_angle = (float) tmp_mechanical_angle * MECHANGLE_COEFFICIENT;
99 float electric_angle = (float) (tmp_mechanical_angle % (ENCODER_RESO / POLAR_PAIRS)) * ELECANGLE_COEFFICIENT;
100 return electric_angle;
101}
102
107 /* calculate the difference between this angle and the last angle */
108 short tmp_mechanical_angle_velocity = (short) (total_machine_angle - systick_mechanical_angle_last);
109
110 /* send it to low-pass filter for filtering to prevent PID high-frequency oscillation */
112 filter_update_value((Filter_Structure_t *) &velocity_filter, tmp_mechanical_angle_velocity);
114}
115
119void encoder_zeroing(void) {
120 float u, v, w;
121 /* delay to wait for the power supply voltage to be normal */
122 delayms(1000);
123
124 /* set that there is only a magnetic field on the straight axis. */
125 foc_calculate_dutycycle(0, CALI_TORQUE, 0, &u, &v, &w);
126 update_pwm_dutycycle(u, v, w);
127 delayms(300);
130
131 /* read the angle at this time as the offset angle */
133
134 /* zero the torque in all directions to release the motor */
135 foc_calculate_dutycycle(0, 0, 0, &u, &v, &w);
136 update_pwm_dutycycle(u, v, w);
137 delayms(300);
138}
used to place important parameter configurations for users
#define CALI_TORQUE
set calibrate torque to 0.5
Definition: config.h:19
#define ENCODER_RESO
SC60228 resolution is 2^8 = 4096.
Definition: config.h:17
#define POLAR_PAIRS
set BLDC polar pairs to 7
Definition: config.h:15
unsigned short encoder_get_mechanical_angle(void)
read mechanical angle directly from encoder
Definition: encoder.c:76
void encoder_delay(void)
delay function for magnetic encoder
Definition: encoder.c:44
volatile unsigned short machine_angle_offset
mechanical angle offset, which is used to align the mechanical angle with the zero point of the elect...
Definition: encoder.c:23
void encoder_update_speed(void)
called every 2 milliseconds to calculate the speed.
Definition: encoder.c:106
float encoder_get_electronic_angle(void)
according to the electrical angle calculated from the mechanical angle, this function will call encod...
Definition: encoder.c:93
static volatile long long total_machine_angle
the total mechanical angle since power on is used to calculate the angle integral value and realize t...
Definition: encoder.c:34
unsigned short encoder_read_data(unsigned short TxData)
read data from the register of the magnetic encoder
Definition: encoder.c:56
void encoder_zeroing(void)
correct the mechanical angle zero deviation between the magnetic encoder and FOC.
Definition: encoder.c:119
static volatile long long systick_mechanical_angle_last
the mechanical angle at the last moment is used to calculate the rotation speed of the motor rotor.
Definition: encoder.c:39
static volatile unsigned short last_mechanical_angle
the mechanical angle at the last moment is used to calculate the angle differential value and realize...
Definition: encoder.c:29
this is the header file of encoder.c.
float filter_update_value(Filter_Structure_t *param, short value)
update the output value of the first-order low-pass filter
Definition: filter.c:56
volatile Filter_Structure_t velocity_filter
motor rotation speed low pass filter handle
Definition: filter.c:19
this is the header file of filter.c, which defines the structure of filter algorithm.
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
Definition: foc.c:99
volatile FOC_Structure_t FOC_Struct
FOC handler.
Definition: foc.c:20
this is the header file of foc.c, which defines the structure of FOC algorithm and angle conversion f...
#define ELECANGLE_COEFFICIENT
electric angle conversion factor
Definition: foc.h:26
#define MECHANGLE_COEFFICIENT
mechanical angle conversion factor
Definition: foc.h:24
unsigned short spi_readwrite_halfworld(unsigned short data)
spi0 transmit data for sc60228
Definition: spi.c:19
this is the header file of spi.c.
float rotate_speed
motor rotate speed calculate from timer
Definition: foc.h:19
float mechanical_angle
mechanical angle read form encoder
Definition: foc.h:18
structure of low-pass filter algorithm
Definition: filter.h:16
void delayms(unsigned long count)
millisecond delay function, any time time.
Definition: system.c:30
system basic function header file
void update_pwm_dutycycle(float ch0, float ch1, float ch2)
update timer1 ch0 1 2 duty-cycle
Definition: timer.c:20
this is the header file of timer.c.