Skip to content

Commit eae25dd

Browse files
committed
Alternative struct based approach
1 parent 7f71f01 commit eae25dd

File tree

6 files changed

+110
-27
lines changed

6 files changed

+110
-27
lines changed

src/Hardware.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#ifndef HARDWARE_H
2+
#define HARDWARE_H
3+
4+
5+
#include "MagneticSensorSPI.h"
6+
7+
/** Typical configuration for the 14bit AMS AS5147 magnetic sensor over SPI interface */
8+
MagneticSensorSPIConfig_s AS5147_SPI = {
9+
.spi_mode = SPI_MODE1,
10+
.clock_speed = 1000000,
11+
.bit_resolution = 14,
12+
.angle_register = 0xCFFF,
13+
.data_start_bit = 13,
14+
.command_rw_bit = 14,
15+
.command_parity_bit = 15
16+
};
17+
18+
/** Typical configuration for the 14bit MonolithicPower MA730 magnetic sensor over SPI interface */
19+
MagneticSensorSPIConfig_s MA730_SPI = {
20+
.spi_mode = SPI_MODE0,
21+
.clock_speed = 1000000,
22+
.bit_resolution = 14,
23+
.angle_register = 0x0000,
24+
.data_start_bit = 15,
25+
.command_rw_bit = 0, // not required
26+
.command_parity_bit = 0 // parity not implemented
27+
};
28+
29+
/** Typical configuration for the 12bit AMS AS5600 magnetic sensor over I2C interface */
30+
MagneticSensorI2CConfig_s AS5600_I2C = {
31+
.chip_address = 0x36,
32+
.clock_speed = 1000000,
33+
.bit_resolution = 12,
34+
.angle_register = 0x0E,
35+
.data_start_bit = 11
36+
};
37+
38+
/** Typical configuration for the 12bit AMS AS5048 magnetic sensor over I2C interface */
39+
MagneticSensorI2CConfig_s AS5048_I2C = {
40+
.chip_address = 0x40, // highly configurable. if A1 and A2 are held low, this is probable value
41+
.clock_speed = 1000000,
42+
.bit_resolution = 14,
43+
.angle_register = 0xFE,
44+
.data_start_bit = 15
45+
};
46+
47+
#endif

src/MagneticSensorI2C.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,24 @@ MagneticSensorI2C::MagneticSensorI2C(uint8_t _chip_address, int _bit_resolution,
2828

2929
}
3030

31-
MagneticSensorI2C MagneticSensorI2C::AS5600() {
32-
MagneticSensorI2C* sensor = new MagneticSensorI2C(0x36, 12, 0x0E, 4);
33-
return *sensor;
34-
}
31+
MagneticSensorI2C::MagneticSensorI2C(MagneticSensorI2CConfig_s config){
32+
chip_address = config.chip_address;
3533

34+
// angle read register of the magnetic sensor
35+
angle_register_msb = config.angle_register;
36+
// register maximum value (counts per revolution)
37+
cpr = pow(2, config.bit_resolution);
38+
39+
int bits_used_msb = config.data_start_bit - 7;
40+
lsb_used = config.bit_resolution - bits_used_msb;
41+
// extraction masks
42+
lsb_mask = (uint8_t)( (2 << lsb_used) - 1 );
43+
msb_mask = (uint8_t)( (2 << bits_used_msb) - 1 );
44+
clock_speed = 400000;
45+
sda_pin = SDA;
46+
scl_pin = SCL;
47+
48+
}
3649

3750
void MagneticSensorI2C::init(){
3851

src/MagneticSensorI2C.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
#include "FOCutils.h"
77
#include "Sensor.h"
88

9+
struct MagneticSensorI2CConfig_s {
10+
int chip_address;
11+
long clock_speed;
12+
int bit_resolution;
13+
int angle_register;
14+
int data_start_bit;
15+
};
916

1017
class MagneticSensorI2C: public Sensor{
1118
public:
@@ -18,6 +25,12 @@ class MagneticSensorI2C: public Sensor{
1825
*/
1926
MagneticSensorI2C(uint8_t _chip_address, int _bit_resolution, uint8_t _angle_register_msb, int _msb_bits_used);
2027

28+
/**
29+
* MagneticSensorI2C class constructor
30+
* @param config I2C config
31+
*/
32+
MagneticSensorI2C(MagneticSensorI2CConfig_s config);
33+
2134
static MagneticSensorI2C AS5600();
2235

2336
/** sensor initialise pins */

src/MagneticSensorSPI.cpp

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// _bit_resolution sensor resolution bit number
66
// _angle_register - (optional) angle read register - default 0x3FFF
77
MagneticSensorSPI::MagneticSensorSPI(int cs, float _bit_resolution, int _angle_register){
8-
// chip select pin
8+
99
chip_select_pin = cs;
1010
// angle read register of the magnetic sensor
1111
angle_register = _angle_register ? _angle_register : DEF_ANGLE_REGISTAR;
@@ -21,25 +21,21 @@ MagneticSensorSPI::MagneticSensorSPI(int cs, float _bit_resolution, int _angle_r
2121

2222
}
2323

24-
MagneticSensorSPI MagneticSensorSPI::MA730(int cs) {
25-
MagneticSensorSPI* sensor = new MagneticSensorSPI(cs, 14, 0x0000);
26-
sensor->spi_mode = SPI_MODE0;
27-
sensor->command_parity_bit = -1; // parity bit insertion not implemented
28-
sensor->command_rw_bit = -1; // rw bit not required for angle
29-
sensor->data_start_bit = 15;
30-
return *sensor;
31-
}
32-
33-
MagneticSensorSPI MagneticSensorSPI::AS5147(int cs) {
34-
MagneticSensorSPI* sensor = new MagneticSensorSPI(cs, 14, 0xCFFF);
35-
sensor->spi_mode = SPI_MODE1;
36-
sensor->command_parity_bit = 15;
37-
sensor->command_rw_bit = 14;
38-
sensor->data_start_bit = 13;
39-
return *sensor;
24+
MagneticSensorSPI::MagneticSensorSPI(MagneticSensorSPIConfig_s config, int cs){
25+
chip_select_pin = cs;
26+
// angle read register of the magnetic sensor
27+
angle_register = config.angle_register ? config.angle_register : DEF_ANGLE_REGISTAR;
28+
// register maximum value (counts per revolution)
29+
cpr = pow(2, config.bit_resolution);
30+
spi_mode = config.spi_mode;
31+
clock_speed = config.clock_speed;
32+
bit_resolution = config.bit_resolution;
33+
34+
command_parity_bit = config.command_parity_bit; // for backwards compatibilty
35+
command_rw_bit = config.command_rw_bit; // for backwards compatibilty
36+
data_start_bit = config.data_start_bit; // for backwards compatibilty
4037
}
4138

42-
4339
void MagneticSensorSPI::init(){
4440
// 1MHz clock (AMS should be able to accept up to 10MHz)
4541
settings = SPISettings(clock_speed, MSBFIRST, spi_mode);
@@ -174,10 +170,10 @@ word MagneticSensorSPI::read(word angle_register){
174170

175171
word command = angle_register;
176172

177-
if (command_rw_bit > -1) {
173+
if (command_rw_bit > 0) {
178174
command = angle_register | (1 << command_rw_bit);
179175
}
180-
if (command_parity_bit > -1) {
176+
if (command_parity_bit > 0) {
181177
//Add a parity bit on the the MSB
182178
command |= ((word)spiCalcEvenParity(command) << command_parity_bit);
183179
}

src/MagneticSensorSPI.h

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@
88

99
#define DEF_ANGLE_REGISTAR 0x3FFF
1010

11+
struct MagneticSensorSPIConfig_s {
12+
int spi_mode;
13+
long clock_speed;
14+
int bit_resolution;
15+
int angle_register;
16+
int data_start_bit;
17+
int command_rw_bit;
18+
int command_parity_bit;
19+
};
20+
1121
class MagneticSensorSPI: public Sensor{
1222
public:
1323
/**
@@ -17,9 +27,12 @@ class MagneticSensorSPI: public Sensor{
1727
* @param angle_register (optional) angle read register - default 0x3FFF
1828
*/
1929
MagneticSensorSPI(int cs, float bit_resolution, int angle_register = 0);
20-
21-
static MagneticSensorSPI MA730(int cs);
22-
static MagneticSensorSPI AS5147(int cs);
30+
/**
31+
* MagneticSensorSPI class constructor
32+
* @param config SPI config
33+
* @param cs SPI chip select pin
34+
*/
35+
MagneticSensorSPI(MagneticSensorSPIConfig_s config, int cs);
2336

2437
/** sensor initialise pins */
2538
void init();

src/SimpleFOC.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,6 @@ void loop() {
9494
#include "MagneticSensorAnalog.h"
9595
#include "HallSensor.h"
9696
#include "BLDCMotor.h"
97+
#include "Hardware.h"
9798

9899
#endif

0 commit comments

Comments
 (0)