File tree Expand file tree Collapse file tree 3 files changed +105
-0
lines changed
Expand file tree Collapse file tree 3 files changed +105
-0
lines changed Original file line number Diff line number Diff line change 1+
2+ #include " ./HysteresisSensor.h"
3+
4+
5+ HysteresisSensor::HysteresisSensor (Sensor& wrapped, float amount) : _wrapped(wrapped), _amount(amount) {
6+ // empty
7+ };
8+
9+
10+ void HysteresisSensor::init () {
11+ _wrapped.update ();
12+ _window = _wrapped.getMechanicalAngle ();
13+ this ->Sensor ::init ();
14+ };
15+
16+ float HysteresisSensor::getSensorAngle () {
17+ _wrapped.update ();
18+ float raw = _wrapped.getMechanicalAngle ();
19+
20+ float d_angle = raw - _window;
21+ if (abs (d_angle) > (0 .8f *_2PI) ) {
22+ if (d_angle > 0 ) {
23+ if (raw < (_2PI - _amount + _window)) {
24+ _window = _normalizeAngle (raw + _amount);
25+ return raw;
26+ }
27+ } else {
28+ if (raw > (_amount - (_2PI - _window))) {
29+ _window = _normalizeAngle (raw - _amount);
30+ return raw;
31+ }
32+ }
33+ }
34+ else {
35+ if (raw > (_window + _amount)) {
36+ _window = _normalizeAngle (raw - _amount);
37+ return raw;
38+ }
39+ if (raw < (_window - _amount)) {
40+ _window = _normalizeAngle (raw + _amount);
41+ return raw;
42+ }
43+ }
44+ return angle_prev; // no change
45+ };
46+
Original file line number Diff line number Diff line change 1+
2+ #pragma once
3+
4+
5+ #include " Arduino.h"
6+ #include " common/base_classes/FOCMotor.h"
7+ #include " common/base_classes/Sensor.h"
8+
9+ class HysteresisSensor : public Sensor {
10+ public:
11+ HysteresisSensor (Sensor& wrapped, float amount = 0 .0125f );
12+
13+ float getSensorAngle () override ;
14+
15+ void init () override ;
16+
17+ float _amount;
18+ protected:
19+ Sensor& _wrapped;
20+ float _window;
21+
22+ };
23+
Original file line number Diff line number Diff line change 1+
2+ # HysteresisSensor
3+
4+ A simple wrapper sensor which adds a configurable amount of hysteresis to any other sensor.
5+
6+ Set the amount of hysteresis, in rads. The hysteresis window will have a width of twice the set
7+ amount.
8+
9+ ## Usage
10+
11+ ``` c++
12+ // for example, using a MA730 as the real sensor
13+ MagneticSensorMA730 sensor = MagneticSensorMA730(PIN_nCS);
14+ // wrapping sensor with hysteresis
15+ HysteresisSensor hysteresisSensor = HysteresisSensor(sensor, 0 .01f );
16+
17+ void setup () {
18+ ...
19+
20+ // init real sensor
21+ sensor.init();
22+ hysteresisSensor.init();
23+ motor.linkSensor(&hysteresisSensor);
24+
25+ ...
26+
27+ // it may be better to run the calibration without hysteresis...
28+ hysteresisSensor._amount = 0.0f;
29+ motor.init();
30+ motor.initFOC();
31+ // switch it back on afterwards
32+ hysteresisSensor._amount = 0.01f;
33+
34+ ...
35+ }
36+ ```
You can’t perform that action at this time.
0 commit comments