File tree Expand file tree Collapse file tree 2 files changed +16
-9
lines changed Expand file tree Collapse file tree 2 files changed +16
-9
lines changed Original file line number Diff line number Diff line change 2
2
#include " ../foc_utils.h"
3
3
#include " ../time_utils.h"
4
4
5
- // TODO add an init method to make the startup smoother by initializing internal variables to current values rather than 0
5
+
6
6
7
7
void Sensor::update () {
8
8
float val = getSensorAngle ();
@@ -18,17 +18,17 @@ void Sensor::update() {
18
18
float Sensor::getVelocity () {
19
19
// calculate sample time
20
20
float Ts = (angle_prev_ts - vel_angle_prev_ts)*1e-6 ;
21
- // quick fix for strange cases (micros overflow)
22
- if (Ts <= 0 ) Ts = 1e-3f ;
23
- // velocity calculation
24
- float vel = ( (float )(full_rotations - vel_full_rotations)*_2PI + (angle_prev - vel_angle_prev) ) / Ts;
25
- // save variables for future pass
21
+ if (Ts<minDeltaT) return velocity; // don't update velocity if deltaT is too small
22
+
23
+ velocity = ( (float )(full_rotations - vel_full_rotations)*_2PI + (angle_prev - vel_angle_prev) ) / Ts;
26
24
vel_angle_prev = angle_prev;
27
25
vel_full_rotations = full_rotations;
28
26
vel_angle_prev_ts = angle_prev_ts;
29
- return vel ;
27
+ return velocity ;
30
28
}
31
29
30
+
31
+
32
32
void Sensor::init () {
33
33
// initialize all the internal variables of Sensor to ensure a "smooth" startup (without a 'jump' from zero)
34
34
getSensorAngle (); // call once
Original file line number Diff line number Diff line change @@ -101,6 +101,12 @@ class Sensor{
101
101
* 1 - ecoder with index (with index not found yet)
102
102
*/
103
103
virtual int needsSearch ();
104
+
105
+ /* *
106
+ * Minimum time between updates to velocity. If time elapsed is lower than this, the velocity is not updated.
107
+ */
108
+ float minDeltaT = 0.000100 ; // default is 100 microseconds, or 10kHz
109
+
104
110
protected:
105
111
/* *
106
112
* Get current shaft angle from the sensor hardware, and
@@ -120,9 +126,10 @@ class Sensor{
120
126
virtual void init ();
121
127
122
128
// velocity calculation variables
123
- float angle_prev=0 ; // result of last call to getSensorAngle(), used for full rotations and velocity
129
+ float velocity=0 .0f ;
130
+ float angle_prev=0 .0f ; // result of last call to getSensorAngle(), used for full rotations and velocity
124
131
long angle_prev_ts=0 ; // timestamp of last call to getAngle, used for velocity
125
- float vel_angle_prev=0 ; // angle at last call to getVelocity, used for velocity
132
+ float vel_angle_prev=0 . 0f ; // angle at last call to getVelocity, used for velocity
126
133
long vel_angle_prev_ts=0 ; // last velocity calculation timestamp
127
134
int32_t full_rotations=0 ; // full rotation tracking
128
135
int32_t vel_full_rotations=0 ; // previous full rotation value for velocity calculation
You can’t perform that action at this time.
0 commit comments