Skip to content

Commit 134474a

Browse files
committed
adding support for two i2c buses
1 parent f97f890 commit 134474a

File tree

9 files changed

+89
-62
lines changed

9 files changed

+89
-62
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
#include <SimpleFOC.h>
1111

1212
// magnetic sensor instance - SPI
13-
MagneticSensorSPI sensor = MagneticSensorSPI(10, 14, 0x3FFF);
13+
MagneticSensorSPI sensor = MagneticSensorSPI(AS5147_SPI, 10);
1414
// magnetic sensor instance - MagneticSensorI2C
15-
//MagneticSensorI2C sensor = MagneticSensorI2C(0x36, 12, 0x0E, 4);
15+
//MagneticSensorI2C sensor = MagneticSensorI2C(AS5600_I2C);
1616
// magnetic sensor instance - analog output
1717
// MagneticSensorAnalog sensor = MagneticSensorAnalog(A1, 14, 1020);
1818

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
#include <SimpleFOC.h>
1010

1111
// magnetic sensor instance - SPI
12-
MagneticSensorSPI sensor = MagneticSensorSPI(10, 14, 0x3FFF);
13-
// magnetic sensor instance - I2C
14-
//MagneticSensorI2C sensor = MagneticSensorI2C(0x36, 12, 0x0E, 4);
12+
MagneticSensorSPI sensor = MagneticSensorSPI(AS5147_SPI, 10);
13+
// magnetic sensor instance - MagneticSensorI2C
14+
//MagneticSensorI2C sensor = MagneticSensorI2C(AS5600_I2C);
1515
// magnetic sensor instance - analog output
1616
// MagneticSensorAnalog sensor = MagneticSensorAnalog(A1, 14, 1020);
1717

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

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

15-
// magnetic sensor instance
16-
MagneticSensorSPI sensor = MagneticSensorSPI(10, 14, 0x3FFF);
17-
// magnetic sensor instance
18-
//MagneticSensorI2C sensor = MagneticSensorI2C(0x36, 12, 0x0E, 4);
19-
// magnetic sensor instance - analog output
15+
// magnetic sensor instance - SPI
16+
MagneticSensorSPI sensor = MagneticSensorSPI(AS5147_SPI, 10);
17+
// magnetic sensor instance - MagneticSensorI2C
18+
//MagneticSensorI2C sensor = MagneticSensorI2C(AS5600_I2C);
2019
// MagneticSensorAnalog sensor = MagneticSensorAnalog(A1, 14, 1020);
2120

2221
// BLDC motor instance

examples/motor_commands_serial_examples/magnetic_sensor/full_control_serial/full_control_serial.ino

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@
3838
*/
3939
#include <SimpleFOC.h>
4040

41-
// SPI magnetic sensor instance
42-
// MagneticSensorSPI sensor = MagneticSensorSPI(10, 14, 0x3FFF);
43-
// I2C magnetic sensor instance
44-
MagneticSensorI2C sensor = MagneticSensorI2C(0x36, 12, 0x0E, 4);
41+
// magnetic sensor instance - SPI
42+
MagneticSensorSPI sensor = MagneticSensorSPI(AS5147_SPI, 10);
43+
// magnetic sensor instance - MagneticSensorI2C
44+
//MagneticSensorI2C sensor = MagneticSensorI2C(AS5600_I2C);
4545
// magnetic sensor instance - analog output
4646
// MagneticSensorAnalog sensor = MagneticSensorAnalog(A1, 14, 1020);
4747

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <SimpleFOC.h>
2+
3+
/** Annoyingly some i2c sensors (e.g. AS5600) have a fixed chip address. This means only one of these devices can be addressed on a single bus
4+
* This example shows how a second i2c bus can be used to communicate with a second sensor.
5+
*/
6+
7+
MagneticSensorI2C sensor0 = MagneticSensorI2C(AS5600_I2C);
8+
MagneticSensorI2C sensor1 = MagneticSensorI2C(AS5600_I2C);
9+
10+
#if defined(_STM32_DEF_) // if stm chips
11+
// example of stm32 defining 2nd bus
12+
TwoWire Wire1(PB11, PB10);
13+
14+
#elif defined(ESP_H) // if esp32
15+
// esp32 defines a Wire1 but doesn't define pins!
16+
// nothing to do here for esp32! (See below)
17+
#else
18+
// Wire constructors vary - you'll have to check what works for your chip
19+
TwoWire Wire1(SDA1, SCL1);
20+
#endif
21+
22+
void setup() {
23+
24+
Serial.begin(115200);
25+
_delay(750);
26+
27+
Wire.setClock(400000);
28+
Wire1.setClock(400000);
29+
30+
#if defined(ESP_H) // if esp32
31+
// Normally SimpeFOC will call begin for i2c but with esp32 begin() is the only way to set pins!
32+
// It seems safe to call begin multiple times
33+
Wire1.begin(19,23,400000);
34+
#endif
35+
36+
sensor0.init();
37+
sensor1.init(&Wire1);
38+
}
39+
40+
void loop() {
41+
_delay(200);
42+
Serial.print(sensor0.getAngle());
43+
Serial.print(" - ");
44+
Serial.print(sensor1.getAngle());
45+
Serial.println();
46+
}

examples/utils/sensor_test/magnetic_sensors/magnetic_sensor_i2c_example/magnetic_sensor_i2c_example.ino

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,19 @@
99
// make sure to read the chip address and the chip angle register msb value from the datasheet
1010
// also in most cases you will need external pull-ups on SDA and SCL lines!!!!!
1111
//
12-
// For AS5058B use MagneticSensorI2C(0x40, 14, 0xFE, 8)
12+
// For AS5058B
13+
// MagneticSensorI2C sensor = MagneticSensorI2C(0x40, 14, 0xFE, 8);
14+
1315
// Example of AS5600 configuration
14-
MagneticSensorI2C sensor = MagneticSensorI2C(0x36, 12, 0x0E, 4);
16+
MagneticSensorI2C sensor = MagneticSensorI2C(AS5600_I2C);
1517

1618

1719
void setup() {
1820
// monitoring port
1921
Serial.begin(115200);
2022

23+
// configure i2C
24+
Wire.setClock(400000);
2125
// initialise magnetic sensor hardware
2226
sensor.init();
2327

examples/utils/sensor_test/magnetic_sensors/magnetic_sensor_spi_example/magnetic_sensor_spi_example.ino

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#include <SimpleFOC.h>
22

3-
// MagneticSensorSPI(int cs, float _cpr, int _angle_register)
4-
// cs - SPI chip select pin
5-
// bit_resolution - sensor resolution
6-
// angle_register - (optional) angle read register - default 0x3FFF
7-
MagneticSensorSPI sensor = MagneticSensorSPI(10, 14, 0x3FFF);
3+
// MagneticSensorSPI(MagneticSensorSPIConfig_s config, int cs)
4+
// config - SPI config
5+
// cs - SPI chip select pin
6+
// magnetic sensor instance - SPI
7+
MagneticSensorSPI sensor = MagneticSensorSPI(AS5147_SPI, 10);
8+
// alternative constructor (chipselsect, bit_resolution, angle_read_register, )
9+
// MagneticSensorSPI sensor = MagneticSensorSPI(10, 14, 0x3FFF);
810

911
void setup() {
1012
// monitoring port

src/MagneticSensorI2C.cpp

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
/** Typical configuration for the 12bit AMS AS5600 magnetic sensor over I2C interface */
44
MagneticSensorI2CConfig_s AS5600_I2C = {
55
.chip_address = 0x36,
6-
.clock_speed = 1000000,
76
.bit_resolution = 12,
87
.angle_register = 0x0E,
98
.data_start_bit = 11
@@ -12,7 +11,6 @@ MagneticSensorI2CConfig_s AS5600_I2C = {
1211
/** Typical configuration for the 12bit AMS AS5048 magnetic sensor over I2C interface */
1312
MagneticSensorI2CConfig_s AS5048_I2C = {
1413
.chip_address = 0x40, // highly configurable. if A1 and A2 are held low, this is probable value
15-
.clock_speed = 1000000,
1614
.bit_resolution = 14,
1715
.angle_register = 0xFE,
1816
.data_start_bit = 15
@@ -41,10 +39,7 @@ MagneticSensorI2C::MagneticSensorI2C(uint8_t _chip_address, int _bit_resolution,
4139
// extraction masks
4240
lsb_mask = (uint8_t)( (2 << lsb_used) - 1 );
4341
msb_mask = (uint8_t)( (2 << _bits_used_msb) - 1 );
44-
clock_speed = 400000;
45-
sda_pin = SDA;
46-
scl_pin = SCL;
47-
42+
wire = &Wire;
4843
}
4944

5045
MagneticSensorI2C::MagneticSensorI2C(MagneticSensorI2CConfig_s config){
@@ -60,29 +55,16 @@ MagneticSensorI2C::MagneticSensorI2C(MagneticSensorI2CConfig_s config){
6055
// extraction masks
6156
lsb_mask = (uint8_t)( (2 << lsb_used) - 1 );
6257
msb_mask = (uint8_t)( (2 << bits_used_msb) - 1 );
63-
clock_speed = 400000;
64-
sda_pin = SDA;
65-
scl_pin = SCL;
66-
58+
wire = &Wire;
6759
}
6860

69-
void MagneticSensorI2C::init(){
61+
void MagneticSensorI2C::init(TwoWire* _wire){
62+
63+
wire = _wire;
7064

71-
#if defined(_STM32_DEF_) // if stm chips
7265
// I2C communication begin
73-
Wire.begin();
74-
Wire.setClock(clock_speed);
75-
Wire.setSCL(scl_pin);
76-
Wire.setSDA(sda_pin);
77-
#elif defined(ESP_H) // if esp32
78-
//I2C communication begin
79-
Wire.begin(sda_pin, scl_pin, clock_speed);
80-
#else
81-
// I2C communication begin
82-
Wire.begin();
83-
Wire.setClock(clock_speed);
84-
#endif
85-
66+
wire->begin();
67+
8668
// velocity calculation init
8769
angle_prev = 0;
8870
velocity_calc_timestamp = _micros();
@@ -187,14 +169,14 @@ int MagneticSensorI2C::read(uint8_t angle_reg_msb) {
187169
byte readArray[2];
188170
uint16_t readValue = 0;
189171
// notify the device that is aboout to be read
190-
Wire.beginTransmission(chip_address);
191-
Wire.write(angle_reg_msb);
192-
Wire.endTransmission(false);
172+
wire->beginTransmission(chip_address);
173+
wire->write(angle_reg_msb);
174+
wire->endTransmission(false);
193175

194176
// read the data msb and lsb
195-
Wire.requestFrom(chip_address, (uint8_t)2);
177+
wire->requestFrom(chip_address, (uint8_t)2);
196178
for (byte i=0; i < 2; i++) {
197-
readArray[i] = Wire.read();
179+
readArray[i] = wire->read();
198180
}
199181

200182
// depending on the sensor architecture there are different combinations of

src/MagneticSensorI2C.h

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
struct MagneticSensorI2CConfig_s {
1111
int chip_address;
12-
long clock_speed;
1312
int bit_resolution;
1413
int angle_register;
1514
int data_start_bit;
@@ -37,7 +36,7 @@ class MagneticSensorI2C: public Sensor{
3736
static MagneticSensorI2C AS5600();
3837

3938
/** sensor initialise pins */
40-
void init();
39+
void init(TwoWire* _wire = &Wire);
4140

4241
// implementation of abstract functions of the Sensor class
4342
/** get current angle (rad) */
@@ -60,15 +59,6 @@ class MagneticSensorI2C: public Sensor{
6059

6160
int needsAbsoluteZeroSearch() override;
6261

63-
/* the speed of the i2c clock signal */
64-
long clock_speed;
65-
66-
/* the pin used for i2c data */
67-
int sda_pin;
68-
69-
/* the pin used for i2c clock */
70-
int scl_pin;
71-
7262
private:
7363
float cpr; //!< Maximum range of the magnetic sensor
7464
uint16_t lsb_used; //!< Number of bits used in LSB register
@@ -98,6 +88,10 @@ class MagneticSensorI2C: public Sensor{
9888
float angle_prev; //!< angle in previous velocity calculation step
9989
long velocity_calc_timestamp; //!< last velocity calculation timestamp
10090

91+
/* the two wire instance for this sensor */
92+
TwoWire* wire;
93+
94+
10195
};
10296

10397

0 commit comments

Comments
 (0)