Skip to content

Commit 3a4d671

Browse files
author
Richard Unger
committed
add HysteresisSensor
1 parent 4178759 commit 3a4d671

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+

src/encoders/hysteresis/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
```

0 commit comments

Comments
 (0)