forked from uLipe/foc_rtthread_sample
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathesp_foc.c
More file actions
239 lines (201 loc) · 8.14 KB
/
Copy pathesp_foc.c
File metadata and controls
239 lines (201 loc) · 8.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include <string.h>
#include <math.h>
#include "include/esp_foc/esp_foc.h"
static const float ALIGN_ANGLE_CONSTANT = ((3.0f * M_PI) / 2.0f) + (2.0f * M_PI);
static inline float esp_foc_ticks_to_radians_normalized(esp_foc_axis_t *axis)
{
axis->rotor_shaft_ticks =
axis->rotor_sensor_driver->read_counts(axis->rotor_sensor_driver);
axis->rotor_position =
axis->rotor_shaft_ticks * axis->shaft_ticks_to_radians_ratio;
return esp_foc_normalize_angle(
esp_foc_mechanical_to_elec_angle(
axis->rotor_position, axis->motor_pole_pairs
)
);
}
esp_foc_err_t esp_foc_initialize_axis(esp_foc_axis_t *axis,
esp_foc_inverter_t *inverter,
esp_foc_rotor_sensor_t *rotor,
float motor_pole_pairs)
{
if(axis == NULL) {
return ESP_FOC_ERR_INVALID_ARG;
}
if(inverter == NULL) {
return ESP_FOC_ERR_INVALID_ARG;
}
if(rotor == NULL) {
return ESP_FOC_ERR_INVALID_ARG;
}
axis->dt = 0.0;
axis->last_timestamp = 0.0;
axis->v_qd[0] = 0.0;
axis->v_qd[1] = 0.0;
axis->target_speed = 0.0;
axis->inverter_driver = inverter;
axis->rotor_sensor_driver = rotor;
#if CONFIG_ESP_FOC_USE_VELOCITY_CONTROLLER
axis->velocity_controller.kp = CONFIG_ESP_FOC_VELOCITY_CONTROLLER_KP;
axis->velocity_controller.ki = CONFIG_ESP_FOC_VELOCITY_CONTROLLER_KI;
axis->velocity_controller.kd = CONFIG_ESP_FOC_VELOCITY_CONTROLLER_KD;
axis->velocity_controller.integrator_limit = CONFIG_ESP_FOC_VELOCITY_CONTROL_LIMIT;
esp_foc_pid_reset(&axis->velocity_controller);
esp_foc_low_pass_filter_init(&axis->velocity_filter, 0.9);
axis->inner_control_runs = CONFIG_ESP_FOC_VELOCITY_CONTROLLER_RATE;
#endif
axis->motor_pole_pairs = motor_pole_pairs;
axis->shaft_ticks_to_radians_ratio = ((2.0 * M_PI)) /
axis->rotor_sensor_driver->get_counts_per_revolution(axis->rotor_sensor_driver);
axis->dc_link_voltage =
axis->inverter_driver->get_dc_link_voltage(axis->inverter_driver);
axis->biased_dc_link_voltage = axis->dc_link_voltage * 0.5;
axis->inverter_driver->set_voltages(axis->inverter_driver, 0.0, 0.0, 0.0);
axis->rotor_sensor_driver->delay_ms(axis->rotor_sensor_driver, 250);
axis->rotor_aligned = ESP_FOC_ERR_NOT_ALIGNED;
return ESP_FOC_OK;
}
esp_foc_err_t esp_foc_align_axis(esp_foc_axis_t *axis)
{
if(axis == NULL) {
return ESP_FOC_ERR_INVALID_ARG;
}
if(axis->rotor_aligned != ESP_FOC_ERR_NOT_ALIGNED) {
return ESP_FOC_ERR_AXIS_INVALID_STATE;
}
axis->rotor_aligned = ESP_FOC_ERR_ALIGNMENT_IN_PROGRESS;
float theta = 0.0;
axis->v_qd[0] = 0.1f * axis->biased_dc_link_voltage;
axis->v_qd[1] = 0.0f;
for (float i = 0.0f; i < 500.0f; i += 1.0f) {
theta = esp_foc_normalize_angle(
esp_foc_mechanical_to_elec_angle(
(ALIGN_ANGLE_CONSTANT * i) / 500.0f, axis->motor_pole_pairs
)
);
esp_foc_modulate_dq_voltage(axis->biased_dc_link_voltage,
theta,
axis->v_qd[1],
axis->v_qd[0],
&axis->v_uvw[0],
&axis->v_uvw[1],
&axis->v_uvw[2]);
axis->inverter_driver->set_voltages(axis->inverter_driver,
axis->v_uvw[0],
axis->v_uvw[1],
axis->v_uvw[2]);
axis->rotor_sensor_driver->delay_ms(axis->rotor_sensor_driver,
CONFIG_ESP_FOC_ALIGN_STEP_DELAY_MS);
}
for (float i = 500.0f; i != 0.0f; i -= 1.0f) {
theta = esp_foc_normalize_angle(
esp_foc_mechanical_to_elec_angle(
(ALIGN_ANGLE_CONSTANT * i) / 500.0f, axis->motor_pole_pairs
)
);
esp_foc_modulate_dq_voltage(axis->biased_dc_link_voltage,
theta,
axis->v_qd[1],
axis->v_qd[0],
&axis->v_uvw[0],
&axis->v_uvw[1],
&axis->v_uvw[2]);
axis->inverter_driver->set_voltages(axis->inverter_driver,
axis->v_uvw[0],
axis->v_uvw[1],
axis->v_uvw[2]);
axis->rotor_sensor_driver->delay_ms(axis->rotor_sensor_driver,
CONFIG_ESP_FOC_ALIGN_STEP_DELAY_MS);
}
axis->rotor_sensor_driver->delay_ms(axis->rotor_sensor_driver, 250);
axis->inverter_driver->set_voltages(axis->inverter_driver, 0.0, 0.0, 0.0);
axis->rotor_sensor_driver->delay_ms(axis->rotor_sensor_driver, 250);
axis->rotor_sensor_driver->set_to_zero(axis->rotor_sensor_driver);
axis->rotor_aligned = ESP_FOC_OK;
return ESP_FOC_OK;
}
IRAM_ATTR esp_foc_err_t esp_foc_set_target_voltage(esp_foc_axis_t *axis,
esp_foc_q_voltage *vq,
esp_foc_d_voltage *vd)
{
if(axis == NULL) {
return ESP_FOC_ERR_INVALID_ARG;
}
if(vq == NULL) {
return ESP_FOC_ERR_INVALID_ARG;
}
if(vd == NULL) {
return ESP_FOC_ERR_INVALID_ARG;
}
if(axis->rotor_aligned != ESP_FOC_OK) {
return ESP_FOC_ERR_AXIS_INVALID_STATE;
}
axis->v_qd[0] = esp_foc_saturate(vq->raw, axis->biased_dc_link_voltage);
axis->v_qd[1] = esp_foc_saturate(vd->raw, axis->biased_dc_link_voltage);
return ESP_FOC_OK;
}
IRAM_ATTR esp_foc_err_t esp_foc_set_target_velocity(esp_foc_axis_t *axis, float radians)
{
if(axis == NULL) {
return ESP_FOC_ERR_INVALID_ARG;
}
if(axis->rotor_aligned != ESP_FOC_OK) {
return ESP_FOC_ERR_AXIS_INVALID_STATE;
}
#if CONFIG_ESP_FOC_USE_VELOCITY_CONTROLLER
axis->target_speed = esp_foc_saturate(radians, CONFIG_ESP_FOC_MAX_VELOCITY);
#endif
return ESP_FOC_OK;
}
IRAM_ATTR esp_foc_err_t esp_foc_run(esp_foc_axis_t *axis, float now)
{
if(axis == NULL) {
return ESP_FOC_ERR_INVALID_ARG;
}
if(axis->rotor_aligned != ESP_FOC_OK) {
return ESP_FOC_ERR_AXIS_INVALID_STATE;
}
float dt = now - axis->last_timestamp;
axis->velocity_dt += dt;
if(dt == 0.0 || dt < 0.0f) {
return ESP_FOC_ERR_TIMESTEP_TOO_SMALL;
}
axis->last_timestamp = now;
float theta = esp_foc_ticks_to_radians_normalized(axis);
#if CONFIG_ESP_FOC_USE_VELOCITY_CONTROLLER
axis->inner_control_runs--;
if(axis->inner_control_runs == 0) {
float measured_velocity =
(axis->rotor_position - axis->rotor_position_prev) / dt;
axis->rotor_position_prev = axis->rotor_position;
axis->v_qd[0] = esp_foc_saturate(
esp_foc_pid_update(
&axis->velocity_controller,
esp_foc_low_pass_filter_update(
&axis->velocity_filter, axis->target_speed
),
measured_velocity
),
axis->biased_dc_link_voltage;
)
axis->v_qd[1] = 0.0f;
axis->inner_control_runs = CONFIG_CONFIG_ESP_FOC_VELOCITY_CONTROLLER_RATE;
axis->velocity_dt = 0.0f;
}
#endif
esp_foc_modulate_dq_voltage(axis->biased_dc_link_voltage,
theta,
axis->v_qd[1],
axis->v_qd[0],
&axis->v_uvw[0],
&axis->v_uvw[1],
&axis->v_uvw[2]);
axis->inverter_driver->set_voltages(axis->inverter_driver,
axis->v_uvw[0],
axis->v_uvw[1],
axis->v_uvw[2]);
#if CONFIG_ESP_FOC_SIMULATE_ROTOR_SENSOR
axis->rotor_sensor_driver->set_simulation_count(axis->rotor_sensor_driver, (axis->v_qd[0] * 0.1f) / axis->dc_link_voltage);
#endif
return ESP_FOC_OK;
}