Hello everyone, I’m working on a torque control system using DC current and an STM32, based on the DC Current Mode | Arduino-FOC for stepper motors. However, the motor isn’t running as expected; it’s only making random steps. I would appreciate any help in improving the code. Below is the code and some output data.
Components utilized include the A1333 magnetic sensor and the A4954 driver.
Code
uint16_t current_a, current_b, current_resultant;
int16_t torque_component, magnitude_current, phase_voltage_q, phase_voltage_d;
int16_t phase_current_q, phase_current_d;
double sin_theta, cos_theta, theta;
char gv_log_buffer32[50];
uint32_t pwm1, pwm2;
extern PIDController gs_pid_foc;
extern motorParams s_motor_params;
int8_t getSign(int16_t value)
{
if(value>=0) {
return 1;
} else {
return -1;
}
}
void updateSensor()
{
theta = (double)200*getRawValueA1333()*((2*PI)/65535) - 3; // -3 is used to set the zero electrical angle
//sensorUpdate();
theta = fmod(theta, 2*PI);
if (theta < 0) {
theta += 2 * PI;
}
sin_theta = sin(theta);
cos_theta = cos(theta);
}
void currentVectors()
{
dmaInit();
current_a = getCurrent(getADC(1)); //CurrentA
current_b = getCurrent(getADC(2)); //CurrentB
current_resultant = (int16_t)sqrt((double)(current_a * current_a + current_b * current_b));
}
void parkTransform()
{
torque_component = cos_theta*current_b-sin_theta*current_a;
magnitude_current = getSign(torque_component)*current_resultant; // I_dc
}
void phaseCurrents()
{
phase_current_q = (int16_t)PIDUpdate(&gs_pid_foc, s_motor_params.current_setpoint, magnitude_current);
phase_current_d = 0;
}
void inverseParkTransform()
{
phase_voltage_q = (int16_t)(-cos_theta * phase_current_d + sin_theta * phase_current_q);
phase_voltage_d = (int16_t)(sin_theta * phase_current_d + cos_theta * phase_current_q);
}
void setPWM()
{
// 1500 mA is the max current, so the max magnitude current is 1060 for each vector q and d
if(phase_voltage_d<0) {
pwm1 = (uint32_t)(phase_voltage_d*-1*10);
} else {
pwm1 = (uint32_t)(phase_voltage_d*10);
}
if(phase_voltage_q<0) {
pwm2 = (uint32_t)(phase_voltage_q*-1*10);
} else {
pwm2 = (uint32_t)(phase_voltage_q*10);
}
htim2.Instance->CCR4 = pwm1; //IN1 and IN2
htim3.Instance->CCR4 = pwm2; //IN3 and IN4
}
void focLoop()
{
updateSensor();
currentVectors();
parkTransform();
phaseCurrents();
inverseParkTransform();
setPWM();
// Energizing coils
if(phase_voltage_d>0){
bridge(1); //IN1
} else {
bridge(0); //IN2
}
if(phase_voltage_q>0) {
bridge(2); //IN3
} else {
bridge(3); //IN4
}
sprintf(gv_log_buffer32, "%d, %d, %ld, %ld, %.2lf, %lu\r\n", phase_voltage_d, phase_voltage_q, pwm1, pwm2, theta, getAngle_A1333());
HAL_UART_Transmit(&huart4, (uint8_t *)gv_log_buffer32, strlen(gv_log_buffer32), HAL_MAX_DELAY);
//HAL_Delay(1000);
}
Phase Voltages
The wave I planned to use for the phase voltages was this one:
PID return a value between -9 and 9.
Output Values
To get this values I predefined a fixed current a and b and simulated the raw angle to understand the movement.
Phase Voltage D | Phase Voltage Q | PWM1 | PWM2 | Theta | Simulated Sampled Angle |
---|---|---|---|---|---|
9 | 0 | 720 | 0 | 0.03 | 63243 |
9 | 0 | 720 | 0 | 0.07 | 63245 |
9 | 1 | 720 | 80 | 0.11 | 63247 |
9 | 1 | 720 | 80 | 0.15 | 63249 |
9 | 1 | 720 | 80 | 0.19 | 63251 |
9 | 2 | 720 | 160 | 0.22 | 63253 |
9 | 2 | 720 | 160 | 0.26 | 63255 |
9 | 2 | 720 | 160 | 0.3 | 63257 |
9 | 3 | 720 | 240 | 0.34 | 63259 |
9 | 3 | 720 | 240 | 0.38 | 63261 |
9 | 4 | 720 | 320 | 0.42 | 63263 |
8 | 4 | 640 | 320 | 0.45 | 63265 |
8 | 4 | 640 | 320 | 0.49 | 63267 |
8 | 5 | 640 | 400 | 0.53 | 63269 |
8 | 5 | 640 | 400 | 0.57 | 63271 |
8 | 5 | 640 | 400 | 0.61 | 63273 |
7 | 6 | 560 | 480 | 0.65 | 63275 |
7 | 6 | 560 | 480 | 0.69 | 63277 |
7 | 6 | 560 | 480 | 0.72 | 63279 |
7 | 6 | 560 | 480 | 0.76 | 63281 |
6 | 7 | 480 | 560 | 0.8 | 63283 |
6 | 7 | 480 | 560 | 0.84 | 63285 |
6 | 7 | 480 | 560 | 0.88 | 63287 |
6 | 7 | 480 | 560 | 0.92 | 63289 |
5 | 8 | 400 | 640 | 0.95 | 63291 |
5 | 8 | 400 | 640 | 0.99 | 63293 |
5 | 8 | 400 | 640 | 1.03 | 63295 |
4 | 8 | 320 | 640 | 1.07 | 63297 |
4 | 8 | 320 | 640 | 1.11 | 63299 |
4 | 9 | 320 | 720 | 1.15 | 63301 |
3 | 9 | 240 | 720 | 1.18 | 63303 |
3 | 9 | 240 | 720 | 1.22 | 63305 |
3 | 9 | 240 | 720 | 1.26 | 63307 |
2 | 9 | 160 | 720 | 1.3 | 63309 |
2 | 9 | 160 | 720 | 1.34 | 63311 |
1 | 9 | 80 | 720 | 1.38 | 63313 |
1 | 9 | 80 | 720 | 1.41 | 63315 |
1 | 9 | 80 | 720 | 1.45 | 63317 |
0 | 9 | 0 | 720 | 1.49 | 63319 |
0 | 9 | 0 | 720 | 1.53 | 63321 |
0 | 9 | 0 | 720 | 1.57 | 63323 |
0 | 9 | 0 | 720 | 1.61 | 63325 |
0 | 9 | 0 | 720 | 1.64 | 63327 |
-1 | 9 | 80 | 720 | 1.68 | 63329 |
-1 | 9 | 80 | 720 | 1.72 | 63331 |
-1 | 9 | 80 | 720 | 1.76 | 63333 |
-2 | 9 | 160 | 720 | 1.8 | 63335 |
-2 | 9 | 160 | 720 | 1.84 | 63337 |
-2 | 9 | 160 | 720 | 1.87 | 63339 |
-3 | 9 | 240 | 720 | 1.91 | 63341 |
-3 | 9 | 240 | 720 | 1.95 | 63343 |
-4 | 9 | 320 | 720 | 1.99 | 63345 |
-4 | 8 | 320 | 640 | 2.03 | 63347 |
-4 | 8 | 320 | 640 | 2.07 | 63349 |
-5 | 8 | 400 | 640 | 2.1 | 63351 |
-5 | 8 | 400 | 640 | 2.14 | 63353 |
-5 | 8 | 400 | 640 | 2.18 | 63355 |
-6 | 7 | 480 | 560 | 2.22 | 63357 |
-6 | 7 | 480 | 560 | 2.26 | 63359 |
-6 | 7 | 480 | 560 | 2.3 | 63361 |
-6 | 7 | 480 | 560 | 2.33 | 63363 |
-7 | 6 | 560 | 480 | 2.37 | 63365 |
-7 | 6 | 560 | 480 | 2.41 | 63367 |
-7 | 6 | 560 | 480 | 2.45 | 63369 |
-7 | 6 | 560 | 480 | 2.49 | 63371 |
-8 | 5 | 640 | 400 | 2.53 | 63373 |
-8 | 5 | 640 | 400 | 2.56 | 63375 |
-8 | 5 | 640 | 400 | 2.6 | 63377 |
-8 | 4 | 640 | 320 | 2.64 | 63379 |
-8 | 4 | 640 | 320 | 2.68 | 63381 |
-9 | 4 | 720 | 320 | 2.72 | 63383 |
-9 | 3 | 720 | 240 | 2.76 | 63385 |
-9 | 3 | 720 | 240 | 2.79 | 63387 |
-9 | 3 | 720 | 240 | 2.83 | 63389 |
-9 | 2 | 720 | 160 | 2.87 | 63391 |
-9 | 2 | 720 | 160 | 2.91 | 63393 |
-9 | 1 | 720 | 80 | 2.95 | 63395 |
-9 | 1 | 720 | 80 | 2.99 | 63397 |
-9 | 1 | 720 | 80 | 3.02 | 63399 |
-9 | 0 | 720 | 0 | 3.06 | 63401 |
-9 | 0 | 720 | 0 | 3.1 | 63403 |
-9 | 0 | 720 | 0 | 3.14 | 63405 |
-9 | 0 | 720 | 0 | 3.18 | 63407 |
-9 | 0 | 720 | 0 | 3.22 | 63409 |
-9 | -1 | 720 | 80 | 3.25 | 63411 |
-9 | -1 | 720 | 80 | 3.29 | 63413 |
-9 | -1 | 720 | 80 | 3.33 | 63415 |
-9 | -2 | 720 | 160 | 3.37 | 63417 |
-9 | -2 | 720 | 160 | 3.41 | 63419 |
-9 | -2 | 720 | 160 | 3.45 | 63421 |
-9 | -3 | 720 | 240 | 3.48 | 63423 |
-9 | -3 | 720 | 240 | 3.52 | 63425 |
-9 | -4 | 720 | 320 | 3.56 | 63427 |
-8 | -4 | 640 | 320 | 3.6 | 63429 |
-8 | -4 | 640 | 320 | 3.64 | 63431 |
-8 | -5 | 640 | 400 | 3.68 | 63433 |
-8 | -5 | 640 | 400 | 3.71 | 63435 |
-8 | -5 | 640 | 400 | 3.75 | 63437 |
-7 | -6 | 560 | 480 | 3.79 | 63439 |
-7 | -6 | 560 | 480 | 3.83 | 63441 |
-7 | -6 | 560 | 480 | 3.87 | 63443 |
-7 | -6 | 560 | 480 | 3.91 | 63445 |
-6 | -7 | 480 | 560 | 3.94 | 63447 |
-6 | -7 | 480 | 560 | 3.98 | 63449 |
-6 | -7 | 480 | 560 | 4.02 | 63451 |
-6 | -7 | 480 | 560 | 4.06 | 63453 |
-5 | -8 | 400 | 640 | 4.1 | 63455 |
-5 | -8 | 400 | 640 | 4.14 | 63457 |
-5 | -8 | 400 | 640 | 4.17 | 63459 |
-4 | -8 | 320 | 640 | 4.21 | 63461 |
-4 | -8 | 320 | 640 | 4.25 | 63463 |
-4 | -9 | 320 | 720 | 4.29 | 63465 |
-3 | -9 | 240 | 720 | 4.33 | 63467 |
-3 | -9 | 240 | 720 | 4.37 | 63469 |
-3 | -9 | 240 | 720 | 4.4 | 63471 |
-2 | -9 | 160 | 720 | 4.44 | 63473 |
-2 | -9 | 160 | 720 | 4.48 | 63475 |
-1 | -9 | 80 | 720 | 4.52 | 63477 |
-1 | -9 | 80 | 720 | 4.56 | 63479 |
-1 | -9 | 80 | 720 | 4.6 | 63481 |
0 | -9 | 0 | 720 | 4.64 | 63483 |
0 | -9 | 0 | 720 | 4.67 | 63485 |
0 | -9 | 0 | 720 | 4.71 | 63487 |
0 | -9 | 0 | 720 | 4.75 | 63489 |
0 | -9 | 0 | 720 | 4.79 | 63491 |
1 | -9 | 80 | 720 | 4.83 | 63493 |
1 | -9 | 80 | 720 | 4.87 | 63495 |
1 | -9 | 80 | 720 | 4.9 | 63497 |
2 | -9 | 160 | 720 | 4.94 | 63499 |
2 | -9 | 160 | 720 | 4.98 | 63501 |
3 | -9 | 240 | 720 | 5.02 | 63503 |
3 | -9 | 240 | 720 | 5.06 | 63505 |
3 | -9 | 240 | 720 | 5.1 | 63507 |
4 | -9 | 320 | 720 | 5.13 | 63509 |
4 | -8 | 320 | 640 | 5.17 | 63511 |
4 | -8 | 320 | 640 | 5.21 | 63513 |
5 | -8 | 400 | 640 | 5.25 | 63515 |
5 | -8 | 400 | 640 | 5.29 | 63517 |
5 | -8 | 400 | 640 | 5.33 | 63519 |
6 | -7 | 480 | 560 | 5.36 | 63521 |
6 | -7 | 480 | 560 | 5.4 | 63523 |
6 | -7 | 480 | 560 | 5.44 | 63525 |
6 | -7 | 480 | 560 | 5.48 | 63527 |
7 | -6 | 560 | 480 | 5.52 | 63529 |
7 | -6 | 560 | 480 | 5.56 | 63531 |
7 | -6 | 560 | 480 | 5.59 | 63533 |
7 | -6 | 560 | 480 | 5.63 | 63535 |
8 | -5 | 640 | 400 | 5.67 | 63537 |
8 | -5 | 640 | 400 | 5.71 | 63539 |
8 | -5 | 640 | 400 | 5.75 | 63541 |
8 | -4 | 640 | 320 | 5.79 | 63543 |
8 | -4 | 640 | 320 | 5.82 | 63545 |
9 | -4 | 720 | 320 | 5.86 | 63547 |
9 | -3 | 720 | 240 | 5.9 | 63549 |
9 | -3 | 720 | 240 | 5.94 | 63551 |
9 | -3 | 720 | 240 | 5.98 | 63553 |
9 | -2 | 720 | 160 | 6.02 | 63555 |
9 | -2 | 720 | 160 | 6.05 | 63557 |
9 | -1 | 720 | 80 | 6.09 | 63559 |
9 | -1 | 720 | 80 | 6.13 | 63561 |
9 | -1 | 720 | 80 | 6.17 | 63563 |
9 | 0 | 720 | 0 | 6.21 | 63565 |
9 | 0 | 720 | 0 | 6.25 | 63567 |