Skip to content

Commit 709a558

Browse files
committed
add a (optional) init method to sensor base class
1 parent c642756 commit 709a558

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/common/base_classes/Sensor.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,19 @@ float Sensor::getVelocity() {
3030
return vel;
3131
}
3232

33+
void Sensor::init() {
34+
// initialize all the internal variables of Sensor to ensure a "smooth" startup (without a 'jump' from zero)
35+
getSensorAngle(); // call once
36+
delayMicroseconds(1);
37+
vel_angle_prev = getSensorAngle(); // call again
38+
vel_angle_prev_ts = _micros();
39+
delay(1);
40+
getSensorAngle(); // call once
41+
delayMicroseconds(1);
42+
angle_prev = getSensorAngle(); // call again
43+
angle_prev_ts = _micros();
44+
}
45+
3346

3447
float Sensor::getShaftAngle() {
3548
return angle_prev;
@@ -57,4 +70,4 @@ int32_t Sensor::getFullRotations() {
5770

5871
int Sensor::needsSearch() {
5972
return 0; // default false
60-
}
73+
}

src/common/base_classes/Sensor.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@ class Sensor{
113113
* Use updateSensor() when calling from outside code.
114114
*/
115115
virtual float getSensorAngle()=0;
116+
/**
117+
* Call Sensor::init() from your sensor subclass's init method if you want smoother startup
118+
* The base class init() method calls getSensorAngle() several times to initialize the internal fields
119+
* to current values, ensuring there is no discontinuity ("jump from zero") during the first calls
120+
* to sensor.getAngle() and sensor.getVelocity()
121+
*/
122+
virtual void init();
116123

117124
// velocity calculation variables
118125
float angle_prev=0; // result of last call to getSensorAngle(), used for full rotations and velocity

0 commit comments

Comments
 (0)