File tree Expand file tree Collapse file tree 2 files changed +21
-1
lines changed Expand file tree Collapse file tree 2 files changed +21
-1
lines changed Original file line number Diff line number Diff line change @@ -30,6 +30,19 @@ float Sensor::getVelocity() {
30
30
return vel;
31
31
}
32
32
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
+
33
46
34
47
float Sensor::getShaftAngle () {
35
48
return angle_prev;
@@ -57,4 +70,4 @@ int32_t Sensor::getFullRotations() {
57
70
58
71
int Sensor::needsSearch () {
59
72
return 0 ; // default false
60
- }
73
+ }
Original file line number Diff line number Diff line change @@ -113,6 +113,13 @@ class Sensor{
113
113
* Use updateSensor() when calling from outside code.
114
114
*/
115
115
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 ();
116
123
117
124
// velocity calculation variables
118
125
float angle_prev=0 ; // result of last call to getSensorAngle(), used for full rotations and velocity
You can’t perform that action at this time.
0 commit comments