Skip to content

Commit a5df234

Browse files
committed
add full rotations to velocity calculations
1 parent 83798ea commit a5df234

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

src/common/base_classes/Sensor.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include "../foc_utils.h"
33
#include "../time_utils.h"
44

5-
5+
// TODO add an init method to make the startup smoother by initializing internal variables to current values rather than 0
66

77
float Sensor::updateSensor() {
88
float val = getSensorAngle();
@@ -22,9 +22,10 @@ float Sensor::getVelocity() {
2222
// quick fix for strange cases (micros overflow)
2323
if(Ts <= 0 || Ts > 0.5) Ts = 1e-3;
2424
// velocity calculation
25-
float vel = (angle_prev - vel_angle_prev)/Ts;
25+
float vel = ( (full_rotations - vel_full_rotations)*_2PI + (angle_prev - vel_angle_prev) ) / Ts;
2626
// save variables for future pass
2727
vel_angle_prev = angle_prev;
28+
vel_full_rotations = full_rotations;
2829
vel_angle_prev_ts = angle_prev_ts;
2930
return vel;
3031
}

src/common/base_classes/Sensor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ class Sensor{
112112
float vel_angle_prev=0; // angle at last call to getVelocity, used for velocity
113113
long vel_angle_prev_ts=0; //!< last velocity calculation timestamp
114114
int32_t full_rotations=0; // full rotation tracking
115+
int32_t vel_full_rotations=0;
115116
};
116117

117118
#endif

0 commit comments

Comments
 (0)