SimpleFOClibrary 2.4.0
Loading...
Searching...
No Matches
Sensor.cpp
Go to the documentation of this file.
1#include "Sensor.h"
2#include "../foc_utils.h"
3#include "../time_utils.h"
4
5
6
8 float val = getSensorAngle();
9 if (val<0) // sensor angles are strictly non-negative. Negative values are used to signal errors.
10 return; // TODO signal error, e.g. via a flag and counter
12 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;
15 angle_prev = val;
16}
17
18
19 /** get current angular velocity (rad/s) */
21 // calculate sample time
22 // if timestamps were unsigned, we could get rid of this section, unsigned overflow handles it correctly
23 float Ts = (angle_prev_ts - vel_angle_prev_ts)*1e-6f;
24 if (Ts < 0.0f) { // handle micros() overflow - we need to reset vel_angle_prev_ts
28 return velocity;
29 }
30 if (Ts < min_elapsed_time) return velocity; // don't update velocity if deltaT is too small
31
32 float current_angle = 0.0f;
33 float prev_angle = 0.0f;
34 // Avoid floating point precision loss for large full_rotations
35 // this is likely optional
37 current_angle = angle_prev;
38 prev_angle = vel_angle_prev;
39 } else {
40 current_angle = (float) full_rotations * _2PI + angle_prev;
41 prev_angle = (float) vel_full_rotations * _2PI + vel_angle_prev;
42 }
43 const float delta_angle = current_angle - prev_angle;
44
45 // floating point equality checks are bad, so instead we check that the angle change is very small
46 if (fabsf(delta_angle) > 1e-8f) {
47 velocity = delta_angle / Ts;
48
52 }
53
54 return velocity;
55}
56
57
58
60 // initialize all the internal variables of Sensor to ensure a "smooth" startup (without a 'jump' from zero)
61 getSensorAngle(); // call once
62 delayMicroseconds(1);
63 vel_angle_prev = getSensorAngle(); // call again
65 delay(1);
66 getSensorAngle(); // call once
67 delayMicroseconds(1);
68 angle_prev = getSensorAngle(); // call again
70}
71
72
74 return angle_prev;
75}
76
77
78
80 return (float)full_rotations * _2PI + angle_prev;
81}
82
83
84
86 return (double)full_rotations * (double)_2PI + (double)angle_prev;
87}
88
89
90
92 return full_rotations;
93}
94
95
96
98 return 0; // default false
99}
virtual int needsSearch()
Definition Sensor.cpp:97
virtual float getMechanicalAngle()
Definition Sensor.cpp:73
long vel_angle_prev_ts
Definition Sensor.h:134
float min_elapsed_time
Definition Sensor.h:109
virtual float getAngle()
Definition Sensor.cpp:79
float velocity
Definition Sensor.h:130
virtual float getVelocity()
Definition Sensor.cpp:20
float vel_angle_prev
Definition Sensor.h:133
long angle_prev_ts
Definition Sensor.h:132
virtual void update()
Definition Sensor.cpp:7
int32_t full_rotations
Definition Sensor.h:135
virtual float getSensorAngle()=0
int32_t vel_full_rotations
Definition Sensor.h:136
virtual int32_t getFullRotations()
Definition Sensor.cpp:91
virtual void init()
Definition Sensor.cpp:59
float angle_prev
Definition Sensor.h:131
virtual double getPreciseAngle()
Definition Sensor.cpp:85
#define _2PI
Definition foc_utils.h:29
unsigned long _micros()