Skip to content

Commit ed089a9

Browse files
committed
includes fixed
1 parent 37c5ee3 commit ed089a9

File tree

4 files changed

+152
-146
lines changed

4 files changed

+152
-146
lines changed

ArduinoFOC.h

Lines changed: 4 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -1,146 +1,7 @@
1-
#ifndef BLDCMotor_h
2-
#define BLDCMotor_h
1+
#ifndef ArduinoFOC_h
2+
#define ArduinoFOC_h
33

4-
#include "Arduino.h"
4+
#include "BLDCMotor.h"
55
#include "Encoder.h"
66

7-
// default configuration values
8-
// power supply voltage
9-
#define DEF_POWER_SUPPLY 12.0
10-
// velocity PI controller params
11-
#define DEF_PI_VEL_K 1.0
12-
#define DEF_PI_VEL_TI 0.003
13-
// ultra slow velocity PI params
14-
#define DEF_PI_VEL_US_K 120.0
15-
#define DEF_PI_VEL_US_TI 100.0
16-
// angle P params
17-
#define DEF_P_ANGLE_K 20
18-
// angle velocity limit default
19-
#define DEF_P_ANGLE_VEL_LIM 20
20-
21-
// sign funciton
22-
#define sign(a) ( ( (a) < 0 ) ? -1 : ( (a) > 0 ) )
23-
// utility defines
24-
#define _2_SQRT3 1.15470053838
25-
#define _1_SQRT3 0.57735026919
26-
#define _SQRT3_2 0.86602540378
27-
#define _SQRT2 1.41421356237
28-
#define _120_D2R 2.09439510239
29-
30-
// controller type configuration enum
31-
enum ControlType{
32-
voltage,
33-
velocity,
34-
velocity_ultra_slow,
35-
angle
36-
};
37-
38-
// driver type configuration enum
39-
enum DriverType{
40-
bipolar, // L6234
41-
unipolar // HMBGC
42-
};
43-
44-
// P/PI controller strucutre
45-
struct PI_s{
46-
float K;
47-
float Ti;
48-
long timestamp;
49-
float uk_1, ek_1;
50-
float u_limit;
51-
float velocity_limit;
52-
};
53-
54-
/**
55-
BLDC motor class
56-
*/
57-
class BLDCMotor
58-
{
59-
public:
60-
BLDCMotor(int phA,int phB,int phC,int pp, int en = 0);
61-
// change driver state
62-
void init();
63-
void disable();
64-
void enable();
65-
// connect encoder
66-
void linkEncoder(Encoder* enc);
67-
68-
// initilise FOC
69-
void initFOC();
70-
// iterative method updating motor angles and velocity measurement
71-
void loopFOC();
72-
// iterative control loop defined by controller
73-
void move(float target);
74-
75-
76-
// hardware variables
77-
int pwmA;
78-
int pwmB;
79-
int pwmC;
80-
int enable_pin;
81-
int pole_pairs;
82-
83-
// state variables
84-
float elctric_angle;
85-
float shaft_velocity;
86-
float shaft_angle;
87-
float shaft_velocity_sp;
88-
float shaft_angle_sp;
89-
float voltage_q;
90-
91-
// Power supply woltage
92-
float power_supply_voltage;
93-
94-
// configuraion structures
95-
DriverType driver;
96-
ControlType controller;
97-
PI_s PI_velocity;
98-
PI_s PI_velocity_ultra_slow;
99-
PI_s P_angle;
100-
101-
// encoder link
102-
Encoder* encoder;
103-
104-
105-
private:
106-
//Encoder alignment to electrical 0 angle
107-
void alignEncoder();
108-
/** State calculation methods */
109-
//Shaft angle calculation
110-
float shaftAngle();
111-
//Shaft velocity calculation
112-
float shaftVelocity();
113-
114-
//Electrical angle calculation
115-
float electricAngle(float shaftAngle);
116-
//Set phase voltaget to pwm output
117-
void setPwm(int pinPwm, float U);
118-
119-
/** FOC methods */
120-
//Method using FOC to set Uq to the motor at the optimal angle
121-
void setPhaseVoltage(double Uq, double angle_el);
122-
void setPhaseVoltageUnipolar(double Uq, double angle_el);
123-
void setPhaseVoltageBipolar(double Uq, double angle_el);
124-
125-
/** Utility funcitons */
126-
//normalizing radian angle to [0,2PI]
127-
double normalizeAngle(double angle);
128-
//Reference low pass filter
129-
float filterLP(float u);
130-
131-
/** Motor control functions */
132-
float velocityPI(float ek);
133-
float velocityUltraSlowPI(float ek);
134-
float positionP(float ek);
135-
136-
float Ua,Ub,Uc;
137-
float Ualpha,Ubeta;
138-
};
139-
140-
141-
/*
142-
High PWM frequency
143-
*/
144-
void setPwmFrequency(int pin);
145-
146-
#endif
7+
#endif

ArduinoFOC.cpp renamed to BLDCMotor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "ArduinoFOC.h"
1+
#include "BLDCMotor.h"
22

33

44
/*

BLDCMotor.h

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
#ifndef BLDCMotor_h
2+
#define BLDCMotor_h
3+
4+
#include "Arduino.h"
5+
#include "Encoder.h"
6+
7+
// default configuration values
8+
// power supply voltage
9+
#define DEF_POWER_SUPPLY 12.0
10+
// velocity PI controller params
11+
#define DEF_PI_VEL_K 1.0
12+
#define DEF_PI_VEL_TI 0.003
13+
// ultra slow velocity PI params
14+
#define DEF_PI_VEL_US_K 120.0
15+
#define DEF_PI_VEL_US_TI 100.0
16+
// angle P params
17+
#define DEF_P_ANGLE_K 20
18+
// angle velocity limit default
19+
#define DEF_P_ANGLE_VEL_LIM 20
20+
21+
// sign funciton
22+
#define sign(a) ( ( (a) < 0 ) ? -1 : ( (a) > 0 ) )
23+
// utility defines
24+
#define _2_SQRT3 1.15470053838
25+
#define _1_SQRT3 0.57735026919
26+
#define _SQRT3_2 0.86602540378
27+
#define _SQRT2 1.41421356237
28+
#define _120_D2R 2.09439510239
29+
30+
// controller type configuration enum
31+
enum ControlType{
32+
voltage,
33+
velocity,
34+
velocity_ultra_slow,
35+
angle
36+
};
37+
38+
// driver type configuration enum
39+
enum DriverType{
40+
bipolar, // L6234
41+
unipolar // HMBGC
42+
};
43+
44+
// P/PI controller strucutre
45+
struct PI_s{
46+
float K;
47+
float Ti;
48+
long timestamp;
49+
float uk_1, ek_1;
50+
float u_limit;
51+
float velocity_limit;
52+
};
53+
54+
/**
55+
BLDC motor class
56+
*/
57+
class BLDCMotor
58+
{
59+
public:
60+
BLDCMotor(int phA,int phB,int phC,int pp, int en = 0);
61+
// change driver state
62+
void init();
63+
void disable();
64+
void enable();
65+
// connect encoder
66+
void linkEncoder(Encoder* enc);
67+
68+
// initilise FOC
69+
void initFOC();
70+
// iterative method updating motor angles and velocity measurement
71+
void loopFOC();
72+
// iterative control loop defined by controller
73+
void move(float target);
74+
75+
76+
// hardware variables
77+
int pwmA;
78+
int pwmB;
79+
int pwmC;
80+
int enable_pin;
81+
int pole_pairs;
82+
83+
// state variables
84+
float elctric_angle;
85+
float shaft_velocity;
86+
float shaft_angle;
87+
float shaft_velocity_sp;
88+
float shaft_angle_sp;
89+
float voltage_q;
90+
91+
// Power supply woltage
92+
float power_supply_voltage;
93+
94+
// configuraion structures
95+
DriverType driver;
96+
ControlType controller;
97+
PI_s PI_velocity;
98+
PI_s PI_velocity_ultra_slow;
99+
PI_s P_angle;
100+
101+
// encoder link
102+
Encoder* encoder;
103+
104+
105+
private:
106+
//Encoder alignment to electrical 0 angle
107+
void alignEncoder();
108+
/** State calculation methods */
109+
//Shaft angle calculation
110+
float shaftAngle();
111+
//Shaft velocity calculation
112+
float shaftVelocity();
113+
114+
//Electrical angle calculation
115+
float electricAngle(float shaftAngle);
116+
//Set phase voltaget to pwm output
117+
void setPwm(int pinPwm, float U);
118+
119+
/** FOC methods */
120+
//Method using FOC to set Uq to the motor at the optimal angle
121+
void setPhaseVoltage(double Uq, double angle_el);
122+
void setPhaseVoltageUnipolar(double Uq, double angle_el);
123+
void setPhaseVoltageBipolar(double Uq, double angle_el);
124+
125+
/** Utility funcitons */
126+
//normalizing radian angle to [0,2PI]
127+
double normalizeAngle(double angle);
128+
//Reference low pass filter
129+
float filterLP(float u);
130+
131+
/** Motor control functions */
132+
float velocityPI(float ek);
133+
float velocityUltraSlowPI(float ek);
134+
float positionP(float ek);
135+
136+
float Ua,Ub,Uc;
137+
float Ualpha,Ubeta;
138+
};
139+
140+
141+
/*
142+
High PWM frequency
143+
*/
144+
void setPwmFrequency(int pin);
145+
146+
#endif

library.properties

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@ sentence=A library demistifying FOC for BLDC motors
66
paragraph=Simple library intended for hobby comunity to run the gimbal motors using FOC algorithm
77
category=Device Control
88
url=http://github.com/askuric/Arduino-FOC/
9-
architectures=avr
10-
includes=ArduinoFOC.h
9+
architectures=*

0 commit comments

Comments
 (0)