2#include "../../communication/SimpleFOCDebug.h"
21 if(motor_electrical_angle) {
24 _sincos(motor_electrical_angle, &st, &ct);
25 sign = (ABcurrent.
beta*ct - ABcurrent.
alpha*st) > 0 ? 1 : -1;
45 return return_current;
57 return_ABcurrent.
alpha = current.
a;
58 return_ABcurrent.
beta = current.
b;
59 return return_ABcurrent;
66 return_ABcurrent.
alpha = -current.
c - current.
b;
67 return_ABcurrent.
beta = current.
b;
68 }
else if(current.
b == 0){
69 return_ABcurrent.
alpha = current.
a;
70 return_ABcurrent.
beta = -current.
c - current.
a;
72 return_ABcurrent.
alpha = current.
a;
73 return_ABcurrent.
beta = current.
b;
75 return return_ABcurrent;
80 float i_alpha, i_beta;
87 float a = -current.
c - current.
b;
92 float b = -current.
a - current.
c;
97 float mid = (1.f/3) * (current.
a + current.
b + current.
c);
98 float a = current.
a - mid;
99 float b = current.
b - mid;
105 return_ABcurrent.
alpha = i_alpha;
106 return_ABcurrent.
beta = i_beta;
107 return return_ABcurrent;
119 return_current.
d = current.
alpha * ct + current.
beta * st;
120 return_current.
q = current.
beta * ct - current.
alpha * st;
121 return return_current;
179 for (
int i = 0; i < N; i++) {
181 c.a =
c.a * 0.6f + 0.4f * c1.
a;
182 c.b =
c.b * 0.6f + 0.4f * c1.
b;
183 c.c =
c.c * 0.6f + 0.4f * c1.
c;
201 bool phases_switched = 0;
202 bool phases_inverted = 0;
209 for(
int i=0; i < 100; i++){
210 bldc_driver->
setPwm(
voltage/100.0*((
float)i)+zero , zero, zero);
215 bldc_driver->
setPwm(zero, zero, zero);
221 if((fabs(c_a.
a) < 0.1f) && (fabs(c_a.
b) < 0.1f) && (fabs(c_a.
c) < 0.1f)){
236 float ca[3] = {fabs(c_a.
a), fabs(c_a.
b), fabs(c_a.
c)};
239 float max_c_ratio = 0;
240 for(
int i = 0; i < 3; i++){
245 for(
int j = 0; j < 3; j++){
248 float ratio = max_c / ca[j];
249 if(ratio > max_c_ratio) max_c_ratio = ratio;
262 if(max_c_ratio >=1.5f){
271 phases_switched =
true;
280 phases_switched =
true;
287 phases_inverted =
true;
301 phases_switched =
true;
308 phases_switched =
true;
320 for(
int i=0; i < 100; i++){
321 bldc_driver->
setPwm(zero,
voltage/100.0*((
float)i)+zero, zero);
326 bldc_driver->
setPwm(zero, zero, zero);
331 float cb[3] = {fabs(c_b.
a), fabs(c_b.
b), fabs(c_b.
c)};
335 for(
int i = 0; i < 3; i++){
340 for(
int j = 0; j < 3; j++){
343 float ratio = max_c / cb[j];
344 if(ratio > max_c_ratio) max_c_ratio = ratio;
348 if(max_c_ratio >= 1.5f){
360 phases_switched =
true;
367 phases_inverted =
true;
381 phases_switched =
true;
395 phases_inverted =
true;
403 uint8_t exit_flag = 1;
404 if(phases_switched) exit_flag += 1;
405 if(phases_inverted) exit_flag += 2;
422 bool phases_switched = 0;
423 bool phases_inverted = 0;
432 for(
int i=0; i < 100; i++){
439 stepper_driver->
setPwm(0, 0);
440 if (fabs(
c.a) < 0.1f && fabs(
c.b) < 0.1f ){
447 if (fabs(
c.a) < fabs(
c.b)){
453 phases_switched =
true;
459 phases_inverted =
true;
467 for(
int i=0; i < 100; i++){
473 stepper_driver->
setPwm(0, 0);
477 if (fabs(
c.b) < 0.1f ){
485 phases_inverted =
true;
493 uint8_t exit_flag = 1;
494 if(phases_switched) exit_flag += 1;
495 if(phases_inverted) exit_flag += 2;
507 bool phases_switched = 0;
508 bool phases_inverted = 0;
513 for(
int i=0; i < 100; i++){
520 bldc_driver->
setPwm(0, 0, 0);
521 if (fabs(
c.a) < 0.1f && fabs(
c.b) < 0.1f && fabs(
c.c) < 0.1f ){
527 float cc[3] = {fabs(
c.a), fabs(
c.b), fabs(
c.c)};
530 float max_c_ratio = 0;
531 for(
int i = 0; i < 3; i++){
536 for(
int j = 0; j < 3; j++){
539 float ratio = max_c / cc[j];
540 if(ratio > max_c_ratio) max_c_ratio = ratio;
544 if(max_c_ratio >=1.5f){
553 phases_switched =
true;
562 phases_switched =
true;
570 phases_inverted =
true;
583 phases_switched =
true;
590 phases_switched =
true;
603 for(
int i=0; i < 100; i++){
610 bldc_driver->
setPwm(0, 0, 0);
613 if((fabs(
c.a) < 0.1f) && (fabs(
c.b) < 0.1f) && (fabs(
c.c) < 0.1f)){
628 float ca[3] = {fabs(
c.a), fabs(
c.b), fabs(
c.c)};
632 if((fabs(
c.a) - fabs(
c.c)) > 0.1f){
636 }
else if(
c.b &&
c.c){
639 if((fabs(
c.a) - fabs(
c.c)) > 0.1f){
650 phases_switched =
true;
652 }
else if(
c.a &&
c.b){
654 if(fabs(fabs(
c.a) - fabs(
c.b)) < 0.1f){
659 if (fabs(
c.a) < fabs(
c.b)){
666 phases_switched =
true;
676 phases_inverted =
true;
684 for(
int i=0; i < 100; i++){
690 bldc_driver->
setPwm(0, 0, 0);
693 if((fabs(
c.a) < 0.1f) && (fabs(
c.b) < 0.1f) && (fabs(
c.c) < 0.1f)){
701 float cb[3] = {fabs(
c.a), fabs(
c.b), fabs(
c.c)};
705 if((fabs(
c.b) - fabs(
c.c)) > 0.1f){
709 }
else if(
c.a &&
c.b){
711 if(fabs(fabs(
c.a) - fabs(
c.b)) < 0.1f){
721 phases_inverted =
true;
729 uint8_t exit_flag = 1;
730 if(phases_switched) exit_flag += 1;
731 if(phases_inverted) exit_flag += 2;
@ voltage
Torque control using voltage.
#define SIMPLEFOC_DEBUG(msg,...)
virtual void setPwm(float Ua, float Ub, float Uc)=0
float offset_ic
zero current C voltage value (center of the adc reading)
virtual int driverAlign(float align_voltage, bool modulation_centered=false)
PhaseCurrent_s readAverageCurrents(int N=100)
DriverType driver_type
driver type (BLDC or Stepper)
int pinC
pin C analog pin for current measurement
int alignBLDCDriver(float align_voltage, BLDCDriver *driver, bool modulation_centered)
FOCDriver * driver
driver link
int alignStepperDriver(float align_voltage, StepperDriver *driver, bool modulation_centered)
int alignHybridDriver(float align_voltage, BLDCDriver *driver, bool modulation_centered)
bool skip_align
variable signaling that the phase current direction should be verified during initFOC()
DQCurrent_s getDQCurrents(ABCurrent_s current, float angle_el)
virtual PhaseCurrent_s getPhaseCurrents()=0
int pinB
pin B analog pin for current measurement
int pinA
pin A analog pin for current measurement
virtual float getDCCurrent(float angle_el=0)
float offset_ib
zero current B voltage value (center of the adc reading)
ABCurrent_s getABCurrents(PhaseCurrent_s current)
float offset_ia
zero current A voltage value (center of the adc reading)
DQCurrent_s getFOCCurrents(float angle_el)
void linkDriver(FOCDriver *driver)
virtual DriverType type()=0
float voltage_limit
limiting voltage set to the motor
virtual void setPwm(float Ua, float Ub)=0
void _sincos(float a, float *s, float *c)
void _delay(unsigned long ms)