Skip to content

Commit 6e21605

Browse files
committed
Merge branch 'dev'
2 parents 59a66f4 + 3a4268e commit 6e21605

File tree

14 files changed

+147
-21
lines changed

14 files changed

+147
-21
lines changed

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Arduino Field Oriented Control (FOC) library
1+
# Arduino Simple Field Oriented Control (FOC) library
22

33

44
![Library Compile](https://github.com/askuric/Arduino-FOC/workflows/Library%20Compile/badge.svg)
@@ -15,6 +15,8 @@ Proper low cost FOC supporting boards are very hard to find these days and even
1515

1616
For minimal version of the code more suitable for experimenting please visit the [minimal branch](https://github.com/askuric/Arduino-FOC/tree/minimal).
1717

18+
For minimal version of the code more suitable for experimenting please visit the [minimal branch](https://github.com/askuric/Arduino-FOC/tree/minimal).
19+
1820
#### The closest you can get to FOC support and low cost (I was able to find) is:
1921

2022
<a href="https://odriverobotics.com/" >Odroid</a> | <a href="https://www.youtube.com/watch?v=g2BHEdvW9bU">Trinamic</a>
@@ -46,13 +48,13 @@ Alternatively the library supports the arduino based gimbal controllers such as:
4648
- HMBGC V2.2 ([Ebay](https://www.ebay.com/itm/HMBGC-V2-0-3-Axle-Gimbal-Controller-Control-Plate-Board-Module-with-Sensor/351497840990?hash=item51d6e7695e:g:BAsAAOSw0QFXBxrZ:rk:1:pf:1))
4749

4850

49-
## Arduino FOC Shield V1.2
51+
## Arduino Simple FOC Shield V1.2
5052

5153
At this moment we are developing an open source version of Arduin shiled specifically for FOC motor control.
5254
We already have prototypes of the board and we are in the testing phase. We will be coming out with the details very soon!
5355

5456
### Features
55-
- Plug and play capability with the Arduino FOC library
57+
- Plug and play capability with the Arduino Simple FOC library
5658
- Price in the range of \$20-\$40
5759
- Gerber files and BOM available Open Source
5860

@@ -72,7 +74,7 @@ The code is simple enough to be run on Arudino Uno board.
7274
### Encoder
7375
- Encoder channels `A` and `B` are connected to the Arduino's external intrrupt pins `2` and `3`.
7476
- Optionally if your encoder has `index` signal you can connect it to any available pin, figure shows pin `4`.
75-
- The library doesnt support the Index pin for now (version v1.1.0)
77+
- The library doesnt support the Index pin for now (version v1.0)
7678
### L6234 breakout board
7779
- Connected to the arduino pins `9`,`10` and `11`.
7880
- Additionally you can connect the `enable` pin to the any digital pin of the arduino the picture shows pin `8` but this is optional. You can connect the driver enable directly to 5v.
@@ -97,7 +99,7 @@ Since HMBGC doesn't have acces to the arduinos external interrupt pins `2` and `
9799

98100
- Encoder channels `A` and `B` are connected to the pins `A0` and `A1`.
99101
- Optionally if your encoder has `index` signal you can connect it to any available pin, figure shows pin `A2`.
100-
- The library doesnt support the Index pin for now (version v1.1.0)
102+
- The library doesnt support the Index pin for now (version v1.0)
101103
### Motor
102104
- Motor phases `a`,`b` and `c` are connected directly to the driver outputs
103105

@@ -106,7 +108,7 @@ Motor phases `a`,`b`,`c` and encoder channels `A` and `B` have to be oriented ri
106108

107109

108110

109-
# Arduino FOC library code
111+
# Arduino Simple FOC library code
110112
The code is organised into a librarie. The library contains two classes `BLDCmotor` and `Endcoder`. `BLDCmotor` contains all the necessary FOC algorithm funcitons as well as PI controllers for the velocity and angle control. `Encoder` deals with the encoder interupt funcitons, calcualtes motor angle and velocity( using the [Mixed Time Frequency Method](https://github.com/askuric/Arduino-Mixed-Time-Frequency-Method)).
111113

112114
## Encoder setup
@@ -317,7 +319,7 @@ This function aligns encoder and motor zero positions and intialises FOC variabl
317319

318320
### Real-time execution `loop()`
319321

320-
The real time execution of the Arduino FOC library is govenred by two funcitons `motor.loopFOC()` and `motor.move(float target)`.
322+
The real time execution of the Arduino Simple FOC library is govenred by two funcitons `motor.loopFOC()` and `motor.move(float target)`.
321323
```cpp
322324
// iterative state calculation calculating angle
323325
// and setting FOC pahse voltage

examples/HMBGC_example/HMBGC_example.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <ArduinoFOC.h>
1+
#include <SimpleFOC.h>
22
#include <PciManager.h>
33
#include <PciListenerImp.h>
44

examples/angle_control/angle_control.ino

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

33
// Only pins 2 and 3 are supported
44
#define arduinoInt1 2 // Arduino UNO interrupt 0

examples/angle_control_serial/angle_control_serial.ino

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

33
// Only pins 2 and 3 are supported
44
#define arduinoInt1 2 // Arduino UNO interrupt 0
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
#include <SimpleFOC.h>
2+
3+
// Only pins 2 and 3 are supported
4+
#define arduinoInt1 2 // Arduino UNO interrupt 0
5+
#define arduinoInt2 3 // Arduino UNO interrupt 1
6+
7+
// BLDCMotor( int phA, int phB, int phC, int pp, int en)
8+
// - phA, phB, phC - motor A,B,C phase pwm pins
9+
// - pp - pole pair number
10+
// - enable pin - (optional input)
11+
BLDCMotor motor = BLDCMotor(9, 10, 11, 11, 8);
12+
// Encoder(int encA, int encB , int cpr, int index)
13+
// - encA, encB - encoder A and B pins
14+
// - ppr - impulses per rotation (cpr=ppr*4)
15+
// - index pin - (optional input)
16+
Encoder encoder = Encoder(arduinoInt1, arduinoInt2, 8192, 4);
17+
// interrupt ruotine intialisation
18+
void doA(){encoder.handleA();}
19+
void doB(){encoder.handleB();}
20+
21+
void setup() {
22+
// debugging port
23+
Serial.begin(115200);
24+
25+
// check if you need internal pullups
26+
// Pullup::EXTERN - external pullup added - dafault
27+
// Pullup::INTERN - needs internal arduino pullup
28+
encoder.pullup = Pullup::EXTERN;
29+
30+
// initialise encoder hardware
31+
encoder.init(doA, doB);
32+
33+
// set driver type
34+
// DriverType::unipolar
35+
// DriverType::bipolar - default
36+
motor.driver = DriverType::bipolar;
37+
38+
// power supply voltage
39+
// default 12V
40+
motor.power_supply_voltage = 12;
41+
42+
// set FOC loop to be used
43+
// ControlType::voltage
44+
// ControlType::velocity
45+
// ControlType::velocity_ultra_slow
46+
// ControlType::angle
47+
motor.controller = ControlType::velocity;
48+
49+
// velocity PI controller parameters
50+
// default K=1.0 Ti = 0.003
51+
motor.PI_velocity.K = 1;
52+
motor.PI_velocity.Ti = 0.003;
53+
54+
// link the motor to the sensor
55+
motor.linkEncoder(&encoder);
56+
// intialise motor
57+
motor.init();
58+
// align encoder and start FOC
59+
motor.initFOC();
60+
61+
62+
Serial.println("Motor ready.");
63+
delay(1000);
64+
}
65+
66+
// target velocity variable
67+
float target_velocity = 3;
68+
int t = 0;
69+
70+
void loop() {
71+
// iterative state calculation calculating angle
72+
// and setting FOC pahse voltage
73+
// the faster you run this funciton the better
74+
// in arduino loop it should have ~1kHz
75+
// the best would be to be in ~10kHz range
76+
motor.loopFOC();
77+
78+
// direction chnaging logic
79+
// change direction each 1000 loop passes
80+
target_velocity *= (t >= 1000) ? -1 : 1;
81+
// loop passes counter
82+
t = (t >= 1000) ? 0 : t+1;
83+
84+
85+
// iterative function setting the outter loop target
86+
// velocity, position or voltage
87+
// this funciton can be run at much lower frequency than loopFOC funciton
88+
// it can go as low as ~50Hz
89+
motor.move(target_velocity);
90+
91+
92+
// function intended to be used with serial plotter to monitor motor variables
93+
// significantly slowing the execution down!!!!
94+
//motor_monitor();
95+
}
96+
97+
// utility function intended to be used with serial plotter to monitor motor variables
98+
// significantly slowing the execution down!!!!
99+
void motor_monitor() {
100+
switch (motor.controller) {
101+
case ControlType::velocity_ultra_slow:
102+
case ControlType::velocity:
103+
Serial.print(motor.voltage_q);
104+
Serial.print("\t");
105+
Serial.print(motor.shaft_velocity_sp);
106+
Serial.print("\t");
107+
Serial.println(motor.shaft_velocity);
108+
break;
109+
case ControlType::angle:
110+
Serial.print(motor.voltage_q);
111+
Serial.print("\t");
112+
Serial.print(motor.shaft_angle_sp);
113+
Serial.print("\t");
114+
Serial.println(motor.shaft_angle);
115+
break;
116+
case ControlType::voltage:
117+
Serial.print(motor.voltage_q);
118+
Serial.print("\t");
119+
Serial.println(motor.shaft_velocity);
120+
break;
121+
}
122+
}
123+

examples/encoder_example/encoder_example.ino

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

33
// Only pins 2 and 3 are supported
44
#define arduinoInt1 2 // Arduino UNO interrupt 0

examples/minimal_example/minimal_example.ino

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

33
// This example gives you a minimal code needed to run the FOC algorithm
44
// All the configuration is set to defualt values

examples/velocity_control/velocity_control.ino

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

33
// Only pins 2 and 3 are supported
44
#define arduinoInt1 2 // Arduino UNO interrupt 0

examples/velocity_control_serial/velocity_control_serial.ino

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

33
// Only pins 2 and 3 are supported
44
#define arduinoInt1 2 // Arduino UNO interrupt 0

examples/velocity_ultraslow_control_serial/velocity_ultraslow_control_serial.ino

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

33
// Only pins 2 and 3 are supported
44
#define arduinoInt1 2 // Arduino UNO interrupt 0

0 commit comments

Comments
 (0)