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.
main.c
Go to the documentation of this file.
1/**************************************************************************/
11#include "main.h"
12
21static volatile unsigned char minifoc_fsm_state = 0;
22
28void mdtp_callback_handler(unsigned char pid, const unsigned char *data) {
29 /* pack0 is the control pack of miniFOC */
30 if (pid == 0) {
31 unsigned int receive_int32;
32 switch (data[0]) {
33 case 0x0F:
34 /* 0x0F is used to calibrate motor phase and sensor offset */
36 break;
37
38 case 0x1E:
40 /* 0x1E used to enable the motor */
42
43 /* configure pid parameters for (torque/speed/angle) loop */
45 pid_config(data[1]);
46 else
48 }
49 break;
50
51 case 0x2D:
52 /* 0x2D used to disable the motor */
54 break;
55
56 case 0x3C:
57 /* 0x3C used to set user expect */
58 receive_int32 = (data[4] << 24) | (data[5] << 16) | (data[6] << 8) | data[7];
60 switch (pid_control_mode_flag) {
61 default:
63 break;
65 break;
67 break;
68 }
69 } else
70 FOC_Struct.user_expect = int32_to_float(receive_int32);
71 break;
72
73 case 0x4B:
74 /* 0x4D used to set speed pid kp */
75 receive_int32 = (data[4] << 24) | (data[5] << 16) | (data[6] << 8) | data[7];
76 speed_pid_handler.kp = int32_to_float(receive_int32);
77 break;
78
79 case 0x5A:
80 /* 0x5A used to set speed pid ki */
81 receive_int32 = (data[4] << 24) | (data[5] << 16) | (data[6] << 8) | data[7];
82 speed_pid_handler.ki = int32_to_float(receive_int32);
83 break;
84
85 case 0x69:
86 /* 0x69 used to set speed pid kd */
87 receive_int32 = (data[4] << 24) | (data[5] << 16) | (data[6] << 8) | data[7];
88 speed_pid_handler.kd = int32_to_float(receive_int32);
89 break;
90
91 case 0x78:
92 /* 0x78 used to set speed pid summary maximum */
93 receive_int32 = (data[4] << 24) | (data[5] << 16) | (data[6] << 8) | data[7];
95 break;
96
97 case 0x87:
98 /* 0x87 used to set angle pid kp */
99 receive_int32 = (data[4] << 24) | (data[5] << 16) | (data[6] << 8) | data[7];
100 angle_pid_handler.kp = int32_to_float(receive_int32);
101 break;
102
103 case 0x96:
104 /* 0x96 used to set angle pid ki */
105 receive_int32 = (data[4] << 24) | (data[5] << 16) | (data[6] << 8) | data[7];
106 angle_pid_handler.ki = int32_to_float(receive_int32);
107 break;
108
109 case 0xA5:
110 /* 0xA5 used to set angle pid kd */
111 receive_int32 = (data[4] << 24) | (data[5] << 16) | (data[6] << 8) | data[7];
112 angle_pid_handler.kd = int32_to_float(receive_int32);
113 break;
114
115 case 0xB4:
116 /* 0xB4 used to set angle pid summary maximum */
117 receive_int32 = (data[4] << 24) | (data[5] << 16) | (data[6] << 8) | data[7];
119 break;
120
121 case 0xC3:
122 /* 0xC3 used to return current value */
124 break;
125
126 case 0xD2:
127 /* 0xD2 used to write byte to flash */
130 break;
131 default:break;
132 }
133 }
134}
135
139int main(void) {
140 /* 4 bits for preemption priority 0 bits for subpriority */
141 nvic_priority_group_set(NVIC_PRIGROUP_PRE4_SUB0);
142
143 /* configure peripherals */
144 led_config();
145 uart_config();
146 pwm_config();
147 spi_config();
148
149 /* configure filter and pid parameters for pid algorithm */
152
153 /* configure systick timer for delay_ms() function */
155
156 /* read all parameters from flash */
158
159 /* correct the mechanical angle zero deviation */
161
162 while (1) {
163 switch (minifoc_fsm_state) {
164 case 1:
165 /* disable timer to stop foc calculate loop */
168
169 /* automatic phase sequence detection and correction */
171
172 /* correct the mechanical angle zero deviation */
174
175 /* re-write the parameters to flash */
178 break;
179
180 case 2:
181 /* delay to wait for the motor to respond */
182 delayms(1000);
183
184 /* configure timer for foc calculate loop */
189 break;
190
191 case 3:
192 /* disable timer to stop foc calculate loop */
197 break;
198
199 case 4:
200 /* transmit current value to monitor */
203 break;
204
205 case 5:
206 /* disable timer to stop foc calculate loop */
209
210 /* write data to flash */
213 break;
214
215 case 0:
216 default:
217 /* transmit angle and speed to monitor */
219 led_toggle();
220 delayms(50);
221 break;
222 }
223 }
224}
void encoder_zeroing(void)
correct the mechanical angle zero deviation between the magnetic encoder and FOC.
Definition: encoder.c:119
void filter_config(void)
configure low-pass filter parameters
Definition: filter.c:45
void flash_write_parameters(void)
program all parameters to flash
Definition: flash.c:26
void flash_read_parameters(void)
read all parameters from flash
Definition: flash.c:46
unsigned char foc_parameter_available_flag
flag variable for FOC parameter availability
Definition: foc.c:28
volatile FOC_Structure_t FOC_Struct
FOC handler.
Definition: foc.c:20
void foc_calibrate_phase(void)
automatic phase sequence detection and correction
Definition: foc.c:33
#define led_config()
configure led periph and its gpios
Definition: led.h:30
#define led_toggle()
flip the LED pin level
Definition: led.h:25
void mdtp_callback_handler(unsigned char pid, const unsigned char *data)
user callback function for unpacking completion of medium capacity transport protocol
Definition: main.c:28
static volatile unsigned char minifoc_fsm_state
used to indicate the current status of miniFOC
Definition: main.c:21
int main(void)
main function
Definition: main.c:139
main function Header File
volatile unsigned char pid_control_mode_flag
flag variable of PID closed loop mode
Definition: pid.c:20
unsigned char pid_parameter_available_flag
flag variable for PID parameter availability
Definition: pid.c:16
void pid_config(unsigned char mode)
configure pid loop parameters
Definition: pid.c:33
volatile PID_Structure_t angle_pid_handler
algorithm handler of PID angle loop
Definition: pid.c:28
volatile PID_Structure_t speed_pid_handler
algorithm handler of PID speed loop
Definition: pid.c:24
#define ANGLE_LOOP_CONTROL
angle loop control mode
Definition: pid.h:18
#define TORQUE_LOOP_CONTROL
torque loop control mode
Definition: pid.h:14
#define SPEED_LOOP_CONTROL
speed loop control mode
Definition: pid.h:16
void report_local_variable(void)
report PID parameters and current user settings
Definition: report.c:15
void report_angle_speed(void)
report angle and speed using medium capacity transport protocol
Definition: report.c:69
void spi_config(void)
configure spi0 periph and its gpios
Definition: spi.c:35
float user_expect
user expect value of miniFOC
Definition: foc.h:20
float ki
integral term coefficient in PID
Definition: pid.h:26
float kp
proportional term coefficient in PID
Definition: pid.h:25
float sum_maximum
maximum of anti saturation integral in PID
Definition: pid.h:32
float expect
user expectations in PID
Definition: pid.h:29
float kd
differential term coefficient in PID
Definition: pid.h:27
void systick_config(void)
initialize systick timer to implementation delay function
Definition: system.c:20
void delayms(unsigned long count)
millisecond delay function, any time time.
Definition: system.c:30
float int32_to_float(unsigned int data0)
convert int32 to floating point numbers type data
Definition: system.c:70
void timer2_disable(void)
disable timer2 periph and timer2 interrupt
Definition: timer.c:44
void pwm_config(void)
configure timer1 periph and its gpios
Definition: timer.c:117
void timer13_disable(void)
disable timer13 periph and timer2 interrupt
Definition: timer.c:30
void timer13_config(void)
configure timer13 periph for timing interrupt
Definition: timer.c:89
void timer2_config(void)
configure timer2 periph for timing interrupt
Definition: timer.c:61
void uart_config(void)
configure uart0 periph and its gpios
Definition: uart.c:143