Skip to content

Commit 6a5edac

Browse files
committed
VER 1.4.0 - added magnetic sensor I2C interface
2 parents 07753ce + eb0f790 commit 6a5edac

File tree

21 files changed

+379
-93
lines changed

21 files changed

+379
-93
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@ Therefore this is an attempt to:
1010
- Demystify FOC algorithm and make a robust but simple Arduino library: [Arduino *SimpleFOClibrary*](https://docs.simplefoc.com/arduino_simplefoc_library_showcase)
1111
- Develop a modular BLDC driver board: [Arduino *SimpleFOCShield*](https://docs.simplefoc.com/arduino_simplefoc_shield_showcase).
1212

13-
> ***UPDATE:*** 📢 Arduino *SimpleFOClibrary* now supports high performance DRV8302 driver boards (60V, 15A, 30$). [YouTube link](https://www.youtube.com/watch?v=RI4nNMF608I).
13+
> ***NEWS:*** 📢 Arduino *SimpleFOClibrary* now supports magnetic sensors using I2C communication! Check out the library [docs](https://docs.simplefoc.com/sensors) to see how to use them with your project. You can also find full configuration in the library examples. <br>
14+
> ***BEWARE:*** 📢 The `MagneticSensor` class has been renamed and divided to `MagneticSensorSPI` and `MagneticSensorI2C` classes.
15+
16+
> ***NEWS:*** 📢 Arduino *SimpleFOClibrary* now supports high performance DRV8302 driver boards (60V, 15A, 30$). [YouTube link](https://www.youtube.com/watch?v=RI4nNMF608I).
1417
> You can find the board by clicking on [Aliexpress link](https://bit.ly/2BZZ5fG) and the Arduino code example in the examples folder!
1518
19+
1620
## Arduino *SimpleFOCShield*
1721

1822
<p align="">
@@ -27,7 +31,7 @@ Therefore this is an attempt to:
2731
- **Max power 120W** - max current 5A, power-supply 12-24V
2832
- Designed for Gimbal motors with the internal resistance >10 Ωs.
2933
- **Stackable**: running 2 motors in the same time
30-
- **Encoder interface**: Integrated 3kΩ pullups (configurable)
34+
- **Encoder interface**: Integrated 3.3kΩ pullups (configurable)
3135
- **Configurable pinout**: Hardware configuration - soldering connections
3236
- **Arduino headers**: Arduino UNO, Arduino MEGA, STM32 Nucleo boards...
3337
- **Open Source**: Fully available fabrication files - [how to make it yourself](https://docs.simplefoc.com/arduino_simplefoc_shield_fabrication)

examples/hardware_specific_examples/Bluepill_examples/magnetic_sensor/bluepill_position_control/bluepill_position_control.ino

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,27 @@
77
*/
88
#include <SimpleFOC.h>
99

10-
// Magnetic sensor instance
10+
// SPI Magnetic sensor instance (AS5047U example)
1111
// MISO PA7
1212
// MOSI PA6
1313
// SCK PA5
14-
MagneticSensor AS5x4x = MagneticSensor(PA4, 16384, 0x3FFF);
14+
MagneticSensorSPI sensor = MagneticSensorSPI(PA4, 14, 0x3FFF);
15+
16+
// I2C Magnetic sensor instance (AS5600 example)
17+
// make sure to use the pull-ups!!
18+
// SDA PB7
19+
// SCL PB6
20+
//MagneticSensorI2C sensor = MagneticSensorI2C(0x36, 12, 0x0E, 4);
1521

1622
// Motor instance
17-
BLDCMotor motor = BLDCMotor(PB6, PB7, PB8, 11, PA5);
23+
BLDCMotor motor = BLDCMotor(PA3, PA2, PA1, 11, PA0);
1824

1925
void setup() {
2026

2127
// initialise magnetic sensor hardware
22-
AS5x4x.init();
28+
sensor.init();
2329
// link the motor to the sensor
24-
motor.linkSensor(&AS5x4x);
30+
motor.linkSensor(&sensor);
2531

2632
// power supply voltage
2733
// default 12V

examples/motion_control/position_motion_control/magnetic_sensor/angle_control/angle_control.ino

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,20 @@
99
*/
1010
#include <SimpleFOC.h>
1111

12-
// Magnetic sensor instance
13-
MagneticSensor AS5x4x = MagneticSensor(10, 16384, 0x3FFF);
12+
// magnetic sensor instance
13+
MagneticSensorSPI sensor = MagneticSensorSPI(10, 16384, 0x3FFF);
14+
// magnetic sensor instance
15+
//MagneticSensorI2C sensor = MagneticSensorI2C(0x36, 12, 0x0E, 4);
1416

1517
// Motor instance
1618
BLDCMotor motor = BLDCMotor(9, 5, 6, 11, 8);
1719

1820
void setup() {
1921

2022
// initialise magnetic sensor hardware
21-
AS5x4x.init();
23+
sensor.init();
2224
// link the motor to the sensor
23-
motor.linkSensor(&AS5x4x);
25+
motor.linkSensor(&sensor);
2426

2527
// power supply voltage
2628
// default 12V

examples/motion_control/torque_voltage_control/magnetic_sensor/voltage_control/voltage_control.ino

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,20 @@
88
*/
99
#include <SimpleFOC.h>
1010

11-
// Magnetic sensor instance
12-
MagneticSensor AS5x4x = MagneticSensor(10, 16384, 0x3FFF);
11+
// magnetic sensor instance
12+
MagneticSensorSPI sensor = MagneticSensorSPI(10, 16384, 0x3FFF);
13+
// magnetic sensor instance
14+
//MagneticSensorI2C sensor = MagneticSensorI2C(0x36, 12, 0x0E, 4);
1315

1416
// Motor instance
1517
BLDCMotor motor = BLDCMotor(9, 5, 6, 11, 8);
1618

1719
void setup() {
1820

1921
// initialise magnetic sensor hardware
20-
AS5x4x.init();
22+
sensor.init();
2123
// link the motor to the sensor
22-
motor.linkSensor(&AS5x4x);
24+
motor.linkSensor(&sensor);
2325

2426
// power supply voltage
2527
// default 12V

examples/motion_control/velocity_motion_control/magnetic_sensor/velocity_control/velocity_control.ino

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,20 @@
1212
*/
1313
#include <SimpleFOC.h>
1414

15-
// Magnetic sensor instance
16-
MagneticSensor AS5x4x = MagneticSensor(10, 16384, 0x3FFF);
15+
// magnetic sensor instance
16+
MagneticSensorSPI sensor = MagneticSensorSPI(10, 16384, 0x3FFF);
17+
// magnetic sensor instance
18+
//MagneticSensorI2C sensor = MagneticSensorI2C(0x36, 12, 0x0E, 4);
1719

1820
// Motor instance
1921
BLDCMotor motor = BLDCMotor(9, 5, 6, 11, 8);
2022

2123
void setup() {
2224

2325
// initialise magnetic sensor hardware
24-
AS5x4x.init();
26+
sensor.init();
2527
// link the motor to the sensor
26-
motor.linkSensor(&AS5x4x);
28+
motor.linkSensor(&sensor);
2729

2830
// power supply voltage
2931
// default 12V

examples/motor_full_control_serial_examples/encoder/full_control_serial/full_control_serial.ino

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,8 @@ void setup() {
9494
motor.target = 2;
9595

9696

97-
Serial.println("Full control example: ");
98-
Serial.println("Run user commands to configure and the motor (find the full command list in docs.simplefoc.com) \n ");
99-
Serial.println("Initial motion control loop is voltage loop.");
100-
Serial.println("Initial target voltage 2V.");
97+
// Run user commands to configure and the motor (find the full command list in docs.simplefoc.com)
98+
Serial.println("Motor commands sketch | Initial motion control > torque/voltage : target 2V.");
10199

102100
_delay(1000);
103101
}

examples/motor_full_control_serial_examples/magnetic_sensor/full_control_serial/full_control_serial.ino

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,20 @@
3737
*/
3838
#include <SimpleFOC.h>
3939

40-
// magnetic sensor instance
41-
MagneticSensor AS5x4x = MagneticSensor(10, 16384, 0x3FFF);
40+
// SPI magnetic sensor instance
41+
MagneticSensorSPI sensor = MagneticSensorSPI(10, 16384, 0x3FFF);
42+
// I2C magnetic sensor instance
43+
//MagneticSensorI2C sensor = MagneticSensorI2C(0x36, 12, 0x0E, 4);
4244

4345
// motor instance
4446
BLDCMotor motor = BLDCMotor(9, 5, 6, 11, 8);
4547

4648
void setup() {
4749

4850
// initialise magnetic sensor hardware
49-
AS5x4x.init();
51+
sensor.init();
5052
// link the motor to the sensor
51-
motor.linkSensor(&AS5x4x);
53+
motor.linkSensor(&sensor);
5254

5355
// choose FOC modulation
5456
motor.foc_modulation = FOCModulationType::SpaceVectorPWM;
@@ -88,10 +90,8 @@ void setup() {
8890
motor.target = 2;
8991

9092

91-
Serial.println("Full control example: ");
92-
Serial.println("Run user commands to configure and the motor (find the full command list in docs.simplefoc.com) \n ");
93-
Serial.println("Initial motion control loop is voltage loop.");
94-
Serial.println("Initial target voltage 2V.");
93+
// Run user commands to configure and the motor (find the full command list in docs.simplefoc.com)
94+
Serial.println("Motor commands sketch | Initial motion control > torque/voltage : target 2V.");
9595

9696
_delay(1000);
9797
}

examples/utils/find_pole_pair_number/magnetic_sensor/find_pole_pairs_number/find_pole_pairs_number.ino

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@
2020
BLDCMotor motor = BLDCMotor(9, 5, 6, 0, 8);
2121

2222
// magnetic sensor instance
23-
MagneticSensor AS5x4x = MagneticSensor(10, 16384, 0x3FFF);
23+
MagneticSensorSPI sensor = MagneticSensorSPI(10, 16384, 0x3FFF);
24+
// magnetic sensor instance
25+
//MagneticSensorI2C sensor = MagneticSensorI2C(0x36, 12, 0x0E, 4);
2426

2527
void setup() {
2628

2729
// initialise magnetic sensor hardware
28-
AS5x4x.init();
30+
sensor.init();
2931
// link the motor to the sensor
30-
motor.linkSensor(&AS5x4x);
32+
motor.linkSensor(&sensor);
3133

3234
// power supply voltage
3335
motor.voltage_power_supply = 12;
@@ -51,7 +53,7 @@ void setup() {
5153
motor.setPhaseVoltage(pp_search_voltage, 0);
5254
_delay(1000);
5355
// read the sensor angle
54-
float angle_begin = AS5x4x.getAngle();
56+
float angle_begin = sensor.getAngle();
5557
_delay(50);
5658

5759
// move the motor slowly to the electrical angle pp_search_angle
@@ -62,7 +64,7 @@ void setup() {
6264
}
6365
_delay(1000);
6466
// read the sensor value for 180
65-
float angle_end = AS5x4x.getAngle();
67+
float angle_end = sensor.getAngle();
6668
_delay(50);
6769
// turn off the motor
6870
motor.setPhaseVoltage(0,0);

examples/utils/sensor_test/magnetic_sensor_example/magnetic_sensor_example.ino

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <SimpleFOC.h>
2+
3+
// MagneticSensorI2C(uint8_t _chip_address, float _cpr, uint8_t _angle_register_msb)
4+
// chip_address I2C chip address
5+
// bit_resolution resolution of the sensor
6+
// angle_register_msb angle read register msb
7+
// bits_used_msb number of used bits in msb register
8+
//
9+
// make sure to read the chip address and the chip angle register msb value from the datasheet
10+
// also in most cases you will need external pull-ups on SDA and SCL lines!!!!!
11+
//
12+
// For AS5058B use MagneticSensorI2C(0x40, 14, 0xFE, 8)
13+
// Example of AS5600 configuration
14+
MagneticSensorI2C sensor = MagneticSensorI2C(0x36, 12, 0x0E, 4);
15+
16+
17+
void setup() {
18+
// monitoring port
19+
Serial.begin(115200);
20+
21+
// initialise magnetic sensor hardware
22+
sensor.init();
23+
24+
Serial.println("Sensor ready");
25+
_delay(1000);
26+
}
27+
28+
void loop() {
29+
// display the angle and the angular velocity to the terminal
30+
Serial.print(sensor.getAngle());
31+
Serial.print("\t");
32+
Serial.println(sensor.getVelocity());
33+
}

0 commit comments

Comments
 (0)