Skip to content

Commit cc59aaa

Browse files
committed
FEAT Added space vector modulation
1 parent 040df53 commit cc59aaa

File tree

27 files changed

+1758
-40
lines changed

27 files changed

+1758
-40
lines changed

arduino_foc_minimal_encoder/BLDCMotor.cpp

Lines changed: 466 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
#ifndef BLDCMotor_h
2+
#define BLDCMotor_h
3+
4+
#include "Arduino.h"
5+
#include "FOCutils.h"
6+
#include "Sensor.h"
7+
8+
// default configuration values
9+
// power supply voltage
10+
#define DEF_POWER_SUPPLY 12.0
11+
// velocity PI controller params
12+
#define DEF_PI_VEL_P 0.5
13+
#define DEF_PI_VEL_I 10
14+
#define DEF_PI_VEL_U_RAMP 300
15+
// angle P params
16+
#define DEF_P_ANGLE_P 20
17+
// angle velocity limit default
18+
#define DEF_P_ANGLE_VEL_LIM 20
19+
// index search velocity
20+
#define DEF_INDEX_SEARCH_TARGET_VELOCITY 1
21+
// velocity PI controller params for index search
22+
#define DEF_PI_VEL_INDEX_P 1
23+
#define DEF_PI_VEL_INDEX_I 10
24+
#define DEF_PI_VEL_INDEX_U_RAMP 100
25+
// velocity filter time constant
26+
#define DEF_VEL_FILTER_Tf 0.005
27+
28+
// controller type configuration enum
29+
enum ControlType{
30+
voltage,
31+
velocity,
32+
angle
33+
};
34+
35+
// FOC Type
36+
enum FOCModulationType{
37+
SinePWM,
38+
SpaceVectorPWM
39+
};
40+
41+
// PI controller structure
42+
struct PI_s{
43+
float P;
44+
float I;
45+
long timestamp;
46+
float voltage_prev, tracking_error_prev;
47+
float voltage_limit;
48+
float voltage_ramp;
49+
};
50+
51+
// P controller structure
52+
struct P_s{
53+
float P;
54+
long timestamp;
55+
float voltage_prev, tracking_error_prev;
56+
float velocity_limit;
57+
};
58+
59+
// flow pass filter structure
60+
struct LPF_s{
61+
float Tf;
62+
long timestamp;
63+
float prev;
64+
};
65+
66+
/**
67+
BLDC motor class
68+
*/
69+
class BLDCMotor
70+
{
71+
public:
72+
/*
73+
BLDCMotor( int phA, int phB, int phC, int pp , int cpr, int en)
74+
- phA, phB, phC - motor A,B,C phase pwm pins
75+
- pp - pole pair number
76+
- cpr - counts per rotation number (cpm=ppm*4)
77+
- enable pin - (optional input)
78+
*/
79+
BLDCMotor(int phA,int phB,int phC,int pp, int en = 0);
80+
// change driver state
81+
void init();
82+
void disable();
83+
void enable();
84+
// connect sensor
85+
void linkSensor(Sensor* _sensor);
86+
87+
// initilise FOC
88+
int initFOC();
89+
// iterative method updating motor angles and velocity measurement
90+
void loopFOC();
91+
// iterative control loop defined by controller
92+
void move(float target);
93+
94+
95+
// hardware variables
96+
int pwmA;
97+
int pwmB;
98+
int pwmC;
99+
int enable_pin;
100+
int pole_pairs;
101+
102+
103+
/** State calculation methods */
104+
//Shaft angle calculation
105+
float shaftAngle();
106+
//Shaft velocity calculation
107+
float shaftVelocity();
108+
109+
// state variables
110+
// current motor angle
111+
float shaft_angle;
112+
// current motor velocity
113+
float shaft_velocity;
114+
// current target velocity
115+
float shaft_velocity_sp;
116+
// current target angle
117+
float shaft_angle_sp;
118+
// current voltage u_q set
119+
float voltage_q;
120+
121+
// Power supply voltage
122+
float voltage_power_supply;
123+
124+
// configuration structures
125+
ControlType controller;
126+
FOCModulationType foc_modulation;
127+
PI_s PI_velocity;
128+
PI_s PI_velocity_index_search;
129+
P_s P_angle;
130+
LPF_s LPF_velocity;
131+
132+
// sensor link:
133+
// - Encoder
134+
// - MagneticSensor
135+
Sensor* sensor;
136+
// absolute zero electric angle - if available
137+
float zero_electric_angle;
138+
// index search velocity
139+
float index_search_velocity;
140+
141+
/** FOC methods */
142+
//Method using FOC to set Uq to the motor at the optimal angle
143+
void setPhaseVoltage(float Uq, float angle_el);
144+
145+
// debugging
146+
void useDebugging(Print &print);
147+
void monitor();
148+
Print* debugger;
149+
150+
float Ua,Ub,Uc;
151+
152+
private:
153+
//Sensor alignment to electrical 0 angle
154+
int alignSensor();
155+
//Motor and sensor alignment to the sensors absolute 0 angle
156+
int absoluteZeroAlign();
157+
158+
//Electrical angle calculation
159+
float electricAngle(float shaftAngle);
160+
//Set phase voltaget to pwm output
161+
void setPwm(int pinPwm, float U);
162+
163+
/** Utility functions */
164+
//normalizing radian angle to [0,2PI]
165+
float normalizeAngle(float angle);
166+
// determining if the enable pin has been provided
167+
int hasEnable();
168+
169+
/** Motor control functions */
170+
float controllerPI(float tracking_error, PI_s &controller);
171+
float velocityPI(float tracking_error);
172+
float velocityIndexSearchPI(float tracking_error);
173+
float positionP(float ek);
174+
175+
// phase voltages
176+
float Ualpha,Ubeta;
177+
178+
};
179+
180+
181+
#endif

0 commit comments

Comments
 (0)