Skip to content

Commit 340ef15

Browse files
committed
FEATURE new library version with AS5x4x support and a lot of refactoring
1 parent 2be6e4b commit 340ef15

File tree

28 files changed

+1556
-348
lines changed

28 files changed

+1556
-348
lines changed

README.md

Lines changed: 181 additions & 81 deletions
Large diffs are not rendered by default.

examples/HMBGC_example/HMBGC_example.ino renamed to examples/encoder examples/HMBGC_example/HMBGC_example.ino

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <SimpleFOC.h>
2+
// software interrupt library
23
#include <PciManager.h>
34
#include <PciListenerImp.h>
45

@@ -67,7 +68,7 @@ void setup() {
6768
motor.useDebugging(Serial);
6869

6970
// link the motor to the sensor
70-
motor.linkEncoder(&encoder);
71+
motor.linkSensor(&encoder);
7172

7273
// intialise motor
7374
motor.init();

examples/angle_control/angle_control.ino renamed to examples/encoder examples/angle_control/angle_control.ino

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#include <SimpleFOC.h>
2+
// software interrupt library
3+
#include <PciManager.h>
4+
#include <PciListenerImp.h>
25

36
// Only pins 2 and 3 are supported
47
#define arduinoInt1 2 // Arduino UNO interrupt 0
@@ -19,12 +22,10 @@ Encoder encoder = Encoder(arduinoInt1, arduinoInt2, 8192, A0);
1922
// channel A and B callbacks
2023
void doA(){encoder.handleA();}
2124
void doB(){encoder.handleB();}
22-
// index calback interrupt code
23-
// please set the right PCINT(0,1,2)_vect parameter
24-
// PCINT0_vect - index pin in between D8 and D13
25-
// PCINT1_vect - index pin in between A0 and A5 (recommended)
26-
// PCINT2_vect - index pin in between D0 and D7
27-
ISR (PCINT1_vect) { encoder.handleIndex(); }
25+
void doIndex(){encoder.handleIndex();}
26+
27+
// If no available hadware interrupt pins use the software interrupt
28+
PciListenerImp listenerIndex(encoder.index_pin, doIndex);
2829

2930
void setup() {
3031
// debugging port
@@ -41,7 +42,11 @@ void setup() {
4142
encoder.pullup = Pullup::EXTERN;
4243

4344
// initialise encoder hardware
44-
encoder.init(doA, doB);
45+
encoder.init();
46+
// hardware interrupt enable
47+
encoder.enableInterrupts(doA, doB);
48+
// software interrupts
49+
PciManager.registerListener(&listenerIndex);
4550

4651
// power supply voltage
4752
// default 12V
@@ -58,14 +63,12 @@ void setup() {
5863
// default value is 100
5964
motor.PI_velocity_index_search.voltage_ramp = 100;
6065

61-
6266
// set FOC loop to be used
6367
// ControlType::voltage
6468
// ControlType::velocity
6569
// ControlType::velocity_ultra_slow
6670
// ControlType::angle
6771
motor.controller = ControlType::angle;
68-
6972

7073
// contoller configuration based on the controll type
7174
// velocity PI controller parameters
@@ -90,7 +93,7 @@ void setup() {
9093
motor.useDebugging(Serial);
9194

9295
// link the motor to the sensor
93-
motor.linkEncoder(&encoder);
96+
motor.linkSensor(&encoder);
9497

9598
// intialise motor
9699
motor.init();

examples/change_direction/change_direction.ino renamed to examples/encoder examples/change_direction/change_direction.ino

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,12 @@ BLDCMotor motor = BLDCMotor(9, 5, 6, 11, 8);
1313
// - encA, encB - encoder A and B pins
1414
// - ppr - impulses per rotation (cpr=ppr*4)
1515
// - index pin - (optional input)
16-
Encoder encoder = Encoder(arduinoInt1, arduinoInt2, 8192, A0);
16+
Encoder encoder = Encoder(arduinoInt1, arduinoInt2, 8192);
1717

1818
// Interrupt rutine intialisation
1919
// channel A and B callbacks
2020
void doA(){encoder.handleA();}
2121
void doB(){encoder.handleB();}
22-
// index calback interrupt code
23-
// please set the right PCINT(0,1,2)_vect parameter
24-
// PCINT0_vect - index pin in between D8 and D13
25-
// PCINT1_vect - index pin in between A0 and A5 (recommended)
26-
// PCINT2_vect - index pin in between D0 and D7
27-
ISR (PCINT1_vect) { encoder.handleIndex(); }
2822

2923
void setup() {
3024
// debugging port
@@ -41,7 +35,9 @@ void setup() {
4135
encoder.pullup = Pullup::EXTERN;
4236

4337
// initialise encoder hardware
44-
encoder.init(doA, doB);
38+
encoder.init();
39+
// hardware interrupt enable
40+
encoder.enableInterrupts(doA, doB);
4541

4642
// power supply voltage
4743
// default 12V
@@ -82,7 +78,7 @@ void setup() {
8278
motor.useDebugging(Serial);
8379

8480
// link the motor to the sensor
85-
motor.linkEncoder(&encoder);
81+
motor.linkSensor(&encoder);
8682
// intialise motor
8783
motor.init();
8884
// align encoder and start FOC

examples/encoder_example/encoder_example.ino renamed to examples/encoder examples/encoder_example/encoder_example.ino

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ void setup() {
2424
encoder.pullup = Pullup::EXTERN;
2525

2626
// initialise encoder hardware
27-
encoder.init(doA, doB);
27+
encoder.init();
28+
// hardware interrupt enable
29+
encoder.enableInterrupts(doA, doB);
2830

2931
Serial.println("Encoder ready");
3032
_delay(1000);

examples/find_pole_pairs_number/find_pole_pairs_number.ino renamed to examples/encoder examples/find_pole_pairs_number/find_pole_pairs_number.ino

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ void setup() {
2828
encoder.pullup = Pullup::EXTERN;
2929

3030
// initialise encoder hardware
31-
encoder.init(doA, doB);
31+
encoder.init();
32+
// hardware interrupt enable
33+
encoder.enableInterrupts(doA, doB);
3234

3335
// power supply voltage
3436
// default 12V
@@ -38,7 +40,7 @@ void setup() {
3840
motor.controller = ControlType::voltage;
3941

4042
// link the motor to the sensor
41-
motor.linkEncoder(&encoder);
43+
motor.linkSensor(&encoder);
4244
// intialise motor
4345
motor.init();
4446

examples/velocity_control/velocity_control.ino renamed to examples/encoder examples/velocity_control/velocity_control.ino

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#include <SimpleFOC.h>
2+
// software interrupt library
3+
#include <PciManager.h>
4+
#include <PciListenerImp.h>
25

36
// Only pins 2 and 3 are supported
47
#define arduinoInt1 2 // Arduino UNO interrupt 0
@@ -19,12 +22,9 @@ Encoder encoder = Encoder(arduinoInt1, arduinoInt2, 8192, A0);
1922
// channel A and B callbacks
2023
void doA(){encoder.handleA();}
2124
void doB(){encoder.handleB();}
22-
// index calback interrupt code
23-
// please set the right PCINT(0,1,2)_vect parameter
24-
// PCINT0_vect - index pin in between D8 and D13
25-
// PCINT1_vect - index pin in between A0 and A5 (recommended)
26-
// PCINT2_vect - index pin in between D0 and D7
27-
ISR (PCINT1_vect) { encoder.handleIndex(); }
25+
// If no available hadware interrupt pins use the software interrupt
26+
PciListenerImp listenerIndex(encoder.index_pin, doIndex);
27+
2828

2929
void setup() {
3030
// debugging port
@@ -41,8 +41,12 @@ void setup() {
4141
encoder.pullup = Pullup::EXTERN;
4242

4343
// initialise encoder hardware
44-
encoder.init(doA, doB);
45-
44+
encoder.init();
45+
// hardware interrupt enable
46+
encoder.enableInterrupts(doA, doB);
47+
// software interrupts
48+
PciManager.registerListener(&listenerIndex);
49+
4650
// power supply voltage
4751
// default 12V
4852
motor.voltage_power_supply = 12;
@@ -81,7 +85,7 @@ void setup() {
8185
motor.useDebugging(Serial);
8286

8387
// link the motor to the sensor
84-
motor.linkEncoder(&encoder);
88+
motor.linkSensor(&encoder);
8589

8690
// intialise motor
8791
motor.init();

examples/velocity_ultraslow_control/velocity_ultraslow_control.ino renamed to examples/encoder examples/velocity_ultraslow_control/velocity_ultraslow_control.ino

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#include <SimpleFOC.h>
2+
// software interrupt library
3+
#include <PciManager.h>
4+
#include <PciListenerImp.h>
25

36
// Only pins 2 and 3 are supported
47
#define arduinoInt1 2 // Arduino UNO interrupt 0
@@ -14,16 +17,13 @@ BLDCMotor motor = BLDCMotor(9, 10, 11, 11, 8);
1417
// - ppr - impulses per rotation (cpr=ppr*4)
1518
// - index pin - (optional input)
1619
Encoder encoder = Encoder(arduinoInt1, arduinoInt2, 8192, A0);
20+
1721
// Interrupt rutine intialisation
1822
// channel A and B callbacks
1923
void doA(){encoder.handleA();}
2024
void doB(){encoder.handleB();}
21-
// index calback interrupt code
22-
// please set the right PCINT(0,1,2)_vect parameter
23-
// PCINT0_vect - index pin in between D8 and D13
24-
// PCINT1_vect - index pin in between A0 and A5 (recommended)
25-
// PCINT2_vect - index pin in between D0 and D7
26-
ISR (PCINT1_vect) { encoder.handleIndex(); }
25+
// If no available hadware interrupt pins use the software interrupt
26+
PciListenerImp listenerIndex(encoder.index_pin, doIndex);
2727

2828
void setup() {
2929
// debugging port
@@ -40,7 +40,11 @@ void setup() {
4040
encoder.pullup = Pullup::EXTERN;
4141

4242
// initialise encoder hardware
43-
encoder.init(doA, doB);
43+
encoder.init();
44+
// hardware interrupt enable
45+
encoder.enableInterrupts(doA, doB);
46+
// software interrupts
47+
PciManager.registerListener(&listenerIndex);
4448

4549
// power supply voltage
4650
// default 12V
@@ -76,7 +80,7 @@ void setup() {
7680
motor.useDebugging(Serial);
7781

7882
// link the motor to the sensor
79-
motor.linkEncoder(&encoder);
83+
motor.linkSensor(&encoder);
8084

8185
// intialise motor
8286
motor.init();

examples/voltage_control/voltage_control.ino renamed to examples/encoder examples/voltage_control/voltage_control.ino

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,13 @@ BLDCMotor motor = BLDCMotor(9, 10, 11, 11, 8);
1414
// - ppr - impulses per rotation (cpr=ppr*4)
1515
// - index pin - (optional input)
1616
Encoder encoder = Encoder(arduinoInt1, arduinoInt2, 8192, A0);
17+
1718
// Interrupt rutine intialisation
1819
// channel A and B callbacks
1920
void doA(){encoder.handleA();}
2021
void doB(){encoder.handleB();}
21-
// index calback interrupt code
22-
// please set the right PCINT(0,1,2)_vect parameter
23-
// PCINT0_vect - index pin in between D8 and D13
24-
// PCINT1_vect - index pin in between A0 and A5 (recommended)
25-
// PCINT2_vect - index pin in between D0 and D7
26-
ISR (PCINT1_vect) { encoder.handleIndex(); }
22+
// If no available hadware interrupt pins use the software interrupt
23+
PciListenerImp listenerIndex(encoder.index_pin, doIndex);
2724

2825
void setup() {
2926
// debugging port
@@ -40,7 +37,11 @@ void setup() {
4037
encoder.pullup = Pullup::EXTERN;
4138

4239
// initialise encoder hardware
43-
encoder.init(doA, doB);
40+
encoder.init();
41+
// hardware interrupt enable
42+
encoder.enableInterrupts(doA, doB);
43+
// software interrupts
44+
PciManager.registerListener(&listenerIndex);
4445

4546
// power supply voltage
4647
// default 12V
@@ -69,7 +70,7 @@ void setup() {
6970
motor.useDebugging(Serial);
7071

7172
// link the motor to the sensor
72-
motor.linkEncoder(&encoder);
73+
motor.linkSensor(&encoder);
7374
// intialise motor
7475
motor.init();
7576
// align encoder and start FOC

0 commit comments

Comments
 (0)