File tree Expand file tree Collapse file tree 4 files changed +156
-0
lines changed Expand file tree Collapse file tree 4 files changed +156
-0
lines changed Original file line number Diff line number Diff line change 1+
2+ #include " ./A1334.h"
3+
4+
5+
6+ A1334::A1334 (SPISettings settings, int nCS) : settings(settings), nCS(nCS) {
7+ // nix
8+ };
9+
10+
11+
12+ A1334::~A1334 () {
13+ };
14+
15+
16+
17+
18+ void A1334::init (SPIClass* _spi) {
19+ spi = _spi;
20+ if (nCS>=0 )
21+ pinMode (nCS, OUTPUT);
22+ digitalWrite (nCS, HIGH);
23+ // SPI has an internal SPI-device counter, it is possible to call "begin()" from different devices
24+ spi->begin ();
25+ readRawAngle (); // read an angle
26+ };
27+
28+
29+
30+
31+ A1334Angle A1334::readRawAngle () {
32+ uint16_t command = A1334_REG_ANG;
33+ uint16_t cmdResult = spi_transfer16 (command); // TODO fast mode
34+ cmdResult = spi_transfer16 (command);
35+ A1334Angle result = { .reg = cmdResult };
36+ // TODO check parity
37+ // errorflag = result.ef;
38+ return result;
39+ };
40+
41+
42+
43+
44+ uint16_t A1334::spi_transfer16 (uint16_t outdata) {
45+ if (nCS>=0 )
46+ digitalWrite (nCS, 0 );
47+ spi->beginTransaction (settings);
48+ uint16_t result = spi->transfer16 (outdata);
49+ spi->endTransaction ();
50+ if (nCS>=0 )
51+ digitalWrite (nCS, 1 );
52+ return result;
53+ };
54+
Original file line number Diff line number Diff line change 1+
2+
3+ #ifndef __A1334_H__
4+ #define __A1334_H__
5+
6+ #include < Arduino.h>
7+ #include < SPI.h>
8+
9+
10+ #define A1334_CPR 4096
11+ #define A1334_BITORDER MSBFIRST
12+
13+ static SPISettings A1334SPISettings (8000000 , A1334_BITORDER, SPI_MODE3); // @suppress("Invalid arguments")
14+
15+
16+ typedef union {
17+ struct {
18+ uint16_t angle:12 ;
19+ uint16_t p:1 ;
20+ uint16_t nf:1 ;
21+ uint16_t ef:1 ;
22+ uint16_t ridc:1 ;
23+ };
24+ uint16_t reg;
25+ } A1334Angle;
26+
27+
28+ #define A1334_REG_ANG 0x2000
29+
30+
31+
32+
33+ class A1334 {
34+ public:
35+ A1334 (SPISettings settings = A1334SPISettings, int nCS = -1 );
36+ virtual ~A1334 ();
37+
38+ virtual void init (SPIClass* _spi = &SPI);
39+ A1334Angle readRawAngle (); // 10 or 12 bit angle value
40+ protected:
41+ uint16_t spi_transfer16 (uint16_t outdata);
42+ SPIClass* spi;
43+ SPISettings settings;
44+ int nCS = -1 ;
45+ };
46+
47+
48+
49+
50+
51+ #endif
Original file line number Diff line number Diff line change 1+
2+ #include " ./MagneticSensorA1334.h"
3+ #include " common/foc_utils.h"
4+ #include " common/time_utils.h"
5+
6+ MagneticSensorA1334::MagneticSensorA1334 (int nCS, SPISettings settings) : A1334(settings, nCS) {
7+
8+ }
9+
10+
11+ MagneticSensorA1334::~MagneticSensorA1334 (){
12+
13+ }
14+
15+
16+ void MagneticSensorA1334::init (SPIClass* _spi) {
17+ this ->A1334 ::init (_spi);
18+ this ->Sensor ::init ();
19+ }
20+
21+
22+ float MagneticSensorA1334::getSensorAngle () {
23+ A1334Angle angle_data = readRawAngle ();
24+ float result = ( angle_data.angle / (float )A1334_CPR) * _2PI;
25+ // return the shaft angle
26+ return result;
27+ }
Original file line number Diff line number Diff line change 1+
2+ #ifndef __MAGNETIC_SENSOR_A1334_H__
3+ #define __MAGNETIC_SENSOR_A1334_H__
4+
5+ #include " common/base_classes/Sensor.h"
6+ #include " ./A1334.h"
7+
8+
9+
10+ class MagneticSensorA1334 : public Sensor , public A1334 {
11+ public:
12+ MagneticSensorA1334 (int nCS = -1 , SPISettings settings = A1334SPISettings);
13+ virtual ~MagneticSensorA1334 ();
14+
15+ virtual float getSensorAngle () override ;
16+
17+ virtual void init (SPIClass* _spi = &SPI);
18+ private:
19+ };
20+
21+
22+
23+
24+ #endif
You can’t perform that action at this time.
0 commit comments