Skip to content

Commit c6f91cb

Browse files
committed
v2.0.1 verison added
1 parent c1509d0 commit c6f91cb

File tree

133 files changed

+3687
-3017
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

133 files changed

+3687
-3017
lines changed

README.md

Lines changed: 150 additions & 62 deletions
Large diffs are not rendered by default.

library_source/BLDCMotor.cpp

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ void BLDCMotor::linkDriver(BLDCDriver* _driver) {
2929
driver = _driver;
3030
}
3131

32-
// init hardware pins
32+
// init hardware pins
3333
void BLDCMotor::init() {
3434
if(monitor_port) monitor_port->println("MOT: Initialise variables.");
3535
// sanity check for the voltage limit configuration
@@ -53,13 +53,13 @@ void BLDCMotor::disable()
5353
{
5454
// set zero to PWM
5555
driver->setPwm(0, 0, 0);
56-
// disable the driver
56+
// disable the driver
5757
driver->disable();
5858
}
5959
// enable motor driver
6060
void BLDCMotor::enable()
6161
{
62-
// enable the driver
62+
// enable the driver
6363
driver->enable();
6464
// set zero to PWM
6565
driver->setPwm(0, 0, 0);
@@ -93,7 +93,7 @@ int BLDCMotor::initFOC( float zero_electric_offset, Direction sensor_direction
9393
int BLDCMotor::alignSensor() {
9494
if(monitor_port) monitor_port->println("MOT: Align sensor.");
9595
// align the electrical phases of the motor and sensor
96-
// set angle -90 degrees
96+
// set angle -90 degrees
9797

9898
float start_angle = shaftAngle();
9999
for (int i = 0; i <=5; i++ ) {
@@ -112,6 +112,8 @@ int BLDCMotor::alignSensor() {
112112
sensor->natural_direction = Direction::CCW;
113113
} else if (mid_angle == start_angle) {
114114
if(monitor_port) monitor_port->println("MOT: Sensor failed to notice movement");
115+
} else{
116+
if(monitor_port) monitor_port->println("MOT: natural_direction==CW");
115117
}
116118

117119
// let the motor stabilize for 2 sec
@@ -134,19 +136,19 @@ int BLDCMotor::alignSensor() {
134136
}
135137

136138

137-
// Encoder alignment the absolute zero angle
139+
// Encoder alignment the absolute zero angle
138140
// - to the index
139141
int BLDCMotor::absoluteZeroAlign() {
140142

141143
if(monitor_port) monitor_port->println("MOT: Absolute zero align.");
142144
// if no absolute zero return
143145
if(!sensor->hasAbsoluteZero()) return 0;
144-
146+
145147

146148
if(monitor_port && sensor->needsAbsoluteZeroSearch()) monitor_port->println("MOT: Searching...");
147149
// search the absolute zero with small velocity
148150
while(sensor->needsAbsoluteZeroSearch() && shaft_angle < _2PI){
149-
loopFOC();
151+
loopFOC();
150152
voltage_q = PID_velocity(velocity_index_search - shaftVelocity());
151153
}
152154
voltage_q = 0;
@@ -167,9 +169,9 @@ int BLDCMotor::absoluteZeroAlign() {
167169
// Iterative function looping FOC algorithm, setting Uq on the Motor
168170
// The faster it can be run the better
169171
void BLDCMotor::loopFOC() {
170-
// shaft angle
172+
// shaft angle
171173
shaft_angle = shaftAngle();
172-
// set the phase voltage - FOC heart function :)
174+
// set the phase voltage - FOC heart function :)
173175
setPhaseVoltage(voltage_q, voltage_d, _electricalAngle(shaft_angle,pole_pairs));
174176
}
175177

@@ -219,7 +221,7 @@ void BLDCMotor::move(float new_target) {
219221

220222
// Method using FOC to set Uq and Ud to the motor at the optimal angle
221223
// Function implementing Space Vector PWM and Sine PWM algorithms
222-
//
224+
//
223225
// Function using sine approximation
224226
// regular sin + cos ~300us (no memory usaage)
225227
// approx _sin + _cos ~110us (400Byte ~ 20% of memory)
@@ -257,7 +259,7 @@ void BLDCMotor::setPhaseVoltage(float Uq, float Ud, float angle_el) {
257259
};
258260
// static int trap_150_state = 0;
259261
sector = 12 * (_normalizeAngle(angle_el + PI/6.0 + zero_electric_angle) / _2PI); // adding PI/6 to align with other modes
260-
262+
261263
Ua = Uq + trap_150_map[sector][0] * Uq;
262264
Ub = Uq + trap_150_map[sector][1] * Uq;
263265
Uc = Uq + trap_150_map[sector][2] * Uq;
@@ -272,7 +274,7 @@ void BLDCMotor::setPhaseVoltage(float Uq, float Ud, float angle_el) {
272274
break;
273275

274276
case FOCModulationType::SinePWM :
275-
// Sinusoidal PWM modulation
277+
// Sinusoidal PWM modulation
276278
// Inverse Park + Clarke transformation
277279

278280
// angle normalization in between 0 and 2pi
@@ -299,10 +301,10 @@ void BLDCMotor::setPhaseVoltage(float Uq, float Ud, float angle_el) {
299301
break;
300302

301303
case FOCModulationType::SpaceVectorPWM :
302-
// Nice video explaining the SpaceVectorModulation (SVPWM) algorithm
304+
// Nice video explaining the SpaceVectorModulation (SVPWM) algorithm
303305
// https://www.youtube.com/watch?v=QMSWUMEAejg
304306

305-
// if negative voltages change inverse the phase
307+
// if negative voltages change inverse the phase
306308
// angle + 180degrees
307309
if(Uq < 0) angle_el += _PI;
308310
Uq = abs(Uq);
@@ -316,14 +318,14 @@ void BLDCMotor::setPhaseVoltage(float Uq, float Ud, float angle_el) {
316318
// calculate the duty cycles
317319
float T1 = _SQRT3*_sin(sector*_PI_3 - angle_el) * Uq/driver->voltage_limit;
318320
float T2 = _SQRT3*_sin(angle_el - (sector-1.0)*_PI_3) * Uq/driver->voltage_limit;
319-
// two versions possible
321+
// two versions possible
320322
float T0 = 0; // pulled to 0 - better for low power supply voltage
321-
if (centered) {
323+
if (centered) {
322324
T0 = 1 - T1 - T2; //centered around driver->voltage_limit/2
323-
}
325+
}
324326

325327
// calculate the duty cycles(times)
326-
float Ta,Tb,Tc;
328+
float Ta,Tb,Tc;
327329
switch(sector){
328330
case 1:
329331
Ta = T1 + T2 + T0/2;
@@ -369,7 +371,7 @@ void BLDCMotor::setPhaseVoltage(float Uq, float Ud, float angle_el) {
369371
break;
370372

371373
}
372-
374+
373375
// set the voltages in driver
374376
driver->setPwm(Ua, Ub, Uc);
375377
}
@@ -386,7 +388,7 @@ void BLDCMotor::velocityOpenloop(float target_velocity){
386388
float Ts = (now_us - open_loop_timestamp) * 1e-6;
387389

388390
// calculate the necessary angle to achieve target velocity
389-
shaft_angle += target_velocity*Ts;
391+
shaft_angle += target_velocity*Ts;
390392

391393
// set the maximal allowed voltage (voltage_limit) with the necessary angle
392394
setPhaseVoltage(voltage_limit, 0, _electricalAngle(shaft_angle, pole_pairs));
@@ -403,17 +405,17 @@ void BLDCMotor::angleOpenloop(float target_angle){
403405
unsigned long now_us = _micros();
404406
// calculate the sample time from last call
405407
float Ts = (now_us - open_loop_timestamp) * 1e-6;
406-
408+
407409
// calculate the necessary angle to move from current position towards target angle
408410
// with maximal velocity (velocity_limit)
409411
if(abs( target_angle - shaft_angle ) > abs(velocity_limit*Ts))
410-
shaft_angle += _sign(target_angle - shaft_angle) * abs( velocity_limit )*Ts;
412+
shaft_angle += _sign(target_angle - shaft_angle) * abs( velocity_limit )*Ts;
411413
else
412414
shaft_angle = target_angle;
413-
415+
414416
// set the maximal allowed voltage (voltage_limit) with the necessary angle
415417
setPhaseVoltage(voltage_limit, 0, _electricalAngle(shaft_angle, pole_pairs));
416418

417419
// save timestamp for next call
418420
open_loop_timestamp = now_us;
419-
}
421+
}

library_source/SimpleFOC.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,6 @@ void loop() {
106106
#include "drivers/BLDCDriver3PWM.h"
107107
#include "drivers/BLDCDriver6PWM.h"
108108
#include "drivers/StepperDriver4PWM.h"
109+
#include "drivers/StepperDriver2PWM.h"
109110

110111
#endif

library_source/StepperMotor.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ int StepperMotor::alignSensor() {
105105
sensor->natural_direction = Direction::CCW;
106106
} else if (mid_angle == start_angle) {
107107
if(monitor_port) monitor_port->println("MOT: Sensor failed to notice movement");
108+
} else{
109+
if(monitor_port) monitor_port->println("MOT: natural_direction==CW");
108110
}
109111

110112
// let the motor stabilize for 2 sec
@@ -246,7 +248,7 @@ void StepperMotor::velocityOpenloop(float target_velocity){
246248

247249
// calculate the necessary angle to achieve target velocity
248250
shaft_angle += target_velocity*Ts;
249-
251+
250252
// set the maximal allowed voltage (voltage_limit) with the necessary angle
251253
setPhaseVoltage(voltage_limit, 0, _electricalAngle(shaft_angle,pole_pairs));
252254

library_source/common/time_utils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// function buffering delay()
44
// arduino uno function doesn't work well with interrupts
55
void _delay(unsigned long ms){
6-
#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)
6+
#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__) || defined(__AVR_ATmega2560__)
77
// if arduino uno and other atmega328p chips
88
// use while instad of delay,
99
// due to wrong measurement based on changed timer0
@@ -19,7 +19,7 @@ void _delay(unsigned long ms){
1919
// function buffering _micros()
2020
// arduino function doesn't work well with interrupts
2121
unsigned long _micros(){
22-
#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)
22+
#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__) || defined(__AVR_ATmega2560__)
2323
// if arduino uno and other atmega328p chips
2424
//return the value based on the prescaler
2525
if((TCCR0B & 0b00000111) == 0x01) return (micros()/32);

library_source/drivers/BLDCDriver3PWM.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ void BLDCDriver3PWM::disable()
3535

3636
// init hardware pins
3737
int BLDCDriver3PWM::init() {
38+
// a bit of separation
39+
_delay(1000);
3840

3941
// PWM pins
4042
pinMode(pwmA, OUTPUT);
@@ -56,14 +58,14 @@ int BLDCDriver3PWM::init() {
5658
// Set voltage to the pwm pin
5759
void BLDCDriver3PWM::setPwm(float Ua, float Ub, float Uc) {
5860
// limit the voltage in driver
59-
Ua = _constrain(Ua, -voltage_limit, voltage_limit);
60-
Ub = _constrain(Ub, -voltage_limit, voltage_limit);
61-
Uc = _constrain(Uc, -voltage_limit, voltage_limit);
61+
Ua = _constrain(Ua, 0.0, voltage_limit);
62+
Ub = _constrain(Ub, 0.0, voltage_limit);
63+
Uc = _constrain(Uc, 0.0, voltage_limit);
6264
// calculate duty cycle
6365
// limited in [0,1]
64-
float dc_a = _constrain(Ua / voltage_power_supply, 0 , 1 );
65-
float dc_b = _constrain(Ub / voltage_power_supply, 0 , 1 );
66-
float dc_c = _constrain(Uc / voltage_power_supply, 0 , 1 );
66+
float dc_a = _constrain(Ua / voltage_power_supply, 0.0 , 1.0 );
67+
float dc_b = _constrain(Ub / voltage_power_supply, 0.0 , 1.0 );
68+
float dc_c = _constrain(Uc / voltage_power_supply, 0.0 , 1.0 );
6769
// hardware specific writing
6870
// hardware specific function - depending on driver and mcu
6971
_writeDutyCycle3PWM(dc_a, dc_b, dc_c, pwmA, pwmB, pwmC);

library_source/drivers/BLDCDriver6PWM.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ void BLDCDriver6PWM::disable()
4141

4242
// init hardware pins
4343
int BLDCDriver6PWM::init() {
44+
// a bit of separation
45+
_delay(1000);
4446

4547
// PWM pins
4648
pinMode(pwmA_l, OUTPUT);
@@ -64,9 +66,9 @@ int BLDCDriver6PWM::init() {
6466
// Set voltage to the pwm pin
6567
void BLDCDriver6PWM::setPwm(float Ua, float Ub, float Uc) {
6668
// limit the voltage in driver
67-
Ua = _constrain(Ua, -voltage_limit, voltage_limit);
68-
Ub = _constrain(Ub, -voltage_limit, voltage_limit);
69-
Uc = _constrain(Uc, -voltage_limit, voltage_limit);
69+
Ua = _constrain(Ua, 0, voltage_limit);
70+
Ub = _constrain(Ub, 0, voltage_limit);
71+
Uc = _constrain(Uc, 0, voltage_limit);
7072
// calculate duty cycle
7173
// limited in [0,1]
7274
float dc_a = _constrain(Ua / voltage_power_supply, 0.0 , 1.0 );
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#include "StepperDriver2PWM.h"
2+
3+
StepperDriver2PWM::StepperDriver2PWM(int ph1PWM, int ph1INA, int ph1INB, int ph2PWM, int ph2INA, int ph2INB, int en1, int en2){
4+
// Pin initialization
5+
pwm1 = ph1PWM; //!< phase 1 pwm pin number
6+
ina1 = ph1INA; //!< phase 1 INA pin number
7+
inb1 = ph1INB; //!< phase 1 INB pin number
8+
pwm2 = ph2PWM; //!< phase 2 pwm pin number
9+
ina2 = ph2INA; //!< phase 2 INA pin number
10+
inb2 = ph2INB; //!< phase 2 INB pin number
11+
12+
// enable_pin pin
13+
enable_pin1 = en1;
14+
enable_pin2 = en2;
15+
16+
// default power-supply value
17+
voltage_power_supply = DEF_POWER_SUPPLY;
18+
voltage_limit = NOT_SET;
19+
20+
}
21+
22+
// enable motor driver
23+
void StepperDriver2PWM::enable(){
24+
// enable_pin the driver - if enable_pin pin available
25+
if ( enable_pin1 != NOT_SET ) digitalWrite(enable_pin1, HIGH);
26+
if ( enable_pin2 != NOT_SET ) digitalWrite(enable_pin2, HIGH);
27+
// set zero to PWM
28+
setPwm(0,0);
29+
}
30+
31+
// disable motor driver
32+
void StepperDriver2PWM::disable()
33+
{
34+
// set zero to PWM
35+
setPwm(0, 0);
36+
// disable the driver - if enable_pin pin available
37+
if ( enable_pin1 != NOT_SET ) digitalWrite(enable_pin1, LOW);
38+
if ( enable_pin2 != NOT_SET ) digitalWrite(enable_pin2, LOW);
39+
40+
}
41+
42+
// init hardware pins
43+
int StepperDriver2PWM::init() {
44+
// a bit of separation
45+
_delay(1000);
46+
47+
// PWM pins
48+
pinMode(pwm1, OUTPUT);
49+
pinMode(pwm2, OUTPUT);
50+
pinMode(ina1, OUTPUT);
51+
pinMode(ina2, OUTPUT);
52+
pinMode(inb1, OUTPUT);
53+
pinMode(inb2, OUTPUT);
54+
55+
if(enable_pin1 != NOT_SET) pinMode(enable_pin1, OUTPUT);
56+
if(enable_pin2 != NOT_SET) pinMode(enable_pin2, OUTPUT);
57+
58+
// sanity check for the voltage limit configuration
59+
if(voltage_limit == NOT_SET || voltage_limit > voltage_power_supply) voltage_limit = voltage_power_supply;
60+
61+
// Set the pwm frequency to the pins
62+
// hardware specific function - depending on driver and mcu
63+
_configure2PWM(pwm_frequency, pwm1, pwm2);
64+
return 0;
65+
}
66+
67+
68+
// Set voltage to the pwm pin
69+
void StepperDriver2PWM::setPwm(float Ualpha, float Ubeta) {
70+
float duty_cycle1(0.0),duty_cycle2(0.0);
71+
// limit the voltage in driver
72+
Ualpha = _constrain(Ualpha, -voltage_limit, voltage_limit);
73+
Ubeta = _constrain(Ubeta, -voltage_limit, voltage_limit);
74+
// hardware specific writing
75+
duty_cycle1 = _constrain(abs(Ualpha)/voltage_power_supply,0.0,1.0);
76+
duty_cycle2 = _constrain(abs(Ubeta)/voltage_power_supply,0.0,1.0);
77+
// set direction
78+
if( Ualpha > 0 ){
79+
digitalWrite(inb1, LOW);
80+
digitalWrite(ina1, HIGH);
81+
}
82+
else{
83+
digitalWrite(ina1, LOW);
84+
digitalWrite(inb1, HIGH);
85+
}
86+
87+
if( Ubeta > 0 ){
88+
digitalWrite(ina2, LOW);
89+
digitalWrite(inb2, HIGH);
90+
}
91+
else{
92+
digitalWrite(inb2, LOW);
93+
digitalWrite(ina2, HIGH);
94+
}
95+
96+
// write to hardware
97+
_writeDutyCycle2PWM(duty_cycle1, duty_cycle2, pwm1, pwm2);
98+
}

0 commit comments

Comments
 (0)