Skip to content

Commit f6430f3

Browse files
committed
FIX improving velocity calculation time consistency
1 parent 8998b1d commit f6430f3

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

src/MagneticSensorI2C.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ void MagneticSensorI2C::init(){
2929

3030
//I2C communication begin
3131
Wire.begin();
32-
32+
3333
// velocity calculation init
3434
angle_prev = 0;
3535
velocity_calc_timestamp = _micros();
@@ -66,7 +66,8 @@ float MagneticSensorI2C::getAngle(){
6666
// Shaft velocity calculation
6767
float MagneticSensorI2C::getVelocity(){
6868
// calculate sample time
69-
float Ts = (_micros() - velocity_calc_timestamp)*1e-6;
69+
unsigned long now_us = _micros();
70+
float Ts = (now_us - velocity_calc_timestamp)*1e-6;
7071
// quick fix for strange cases (micros overflow)
7172
if(Ts <= 0 || Ts > 0.5) Ts = 1e-3;
7273

@@ -77,7 +78,7 @@ float MagneticSensorI2C::getVelocity(){
7778

7879
// save variables for future pass
7980
angle_prev = angle_c;
80-
velocity_calc_timestamp = _micros();
81+
velocity_calc_timestamp = now_us;
8182
return vel;
8283
}
8384

src/MagneticSensorSPI.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ float MagneticSensorSPI::getAngle(){
6868
// Shaft velocity calculation
6969
float MagneticSensorSPI::getVelocity(){
7070
// calculate sample time
71-
float Ts = (_micros() - velocity_calc_timestamp)*1e-6;
71+
unsigned long now_us = _micros();
72+
float Ts = (now_us - velocity_calc_timestamp)*1e-6;
7273
// quick fix for strange cases (micros overflow)
7374
if(Ts <= 0 || Ts > 0.5) Ts = 1e-3;
7475

@@ -79,7 +80,7 @@ float MagneticSensorSPI::getVelocity(){
7980

8081
// save variables for future pass
8182
angle_prev = angle_c;
82-
velocity_calc_timestamp = _micros();
83+
velocity_calc_timestamp = now_us;
8384
return vel;
8485
}
8586

0 commit comments

Comments
 (0)