Skip to content

Commit 6abb265

Browse files
committed
Fix issue with angle wrap-around
The 0.8 factor was sub-optimal. Let's assume an angular velocity of 0.3*2PI per calls of update. Then a wrap-around will produce a change of 2PI - 0.3*2PI, that's 0.7*2PI So it won't be detected as a wrap-around of the angle at all and thus the number of full rotations will be off, as will be the velocity. The perfect number is 0.5.
1 parent f5ac652 commit 6abb265

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/common/base_classes/Sensor.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,22 @@
66

77
void Sensor::update() {
88
float val = getSensorAngle();
9-
if (val<0) // sensor angles are strictly non-negative. Negative values are used to signal errors.
9+
10+
// sensor angles are strictly non-negative. Negative values are used to signal errors.
11+
if (val < 0) {
1012
return; // TODO signal error, e.g. via a flag and counter
13+
}
14+
1115
angle_prev_ts = _micros();
1216
float d_angle = val - angle_prev;
13-
// if overflow happened track it as full rotation
14-
if(abs(d_angle) > (0.8f*_2PI) ) full_rotations += ( d_angle > 0 ) ? -1 : 1;
17+
18+
// if wrap-around happened. track full rotation
19+
if (d_angle > (0.5f *_2PI)) {
20+
full_rotations++;
21+
} else if (d_angle < -(0.5f *_2PI)) {
22+
full_rotations--;
23+
}
24+
1525
angle_prev = val;
1626
}
1727

0 commit comments

Comments
 (0)