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
7
void
Sensor::update
() {
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
11
angle_prev_ts
=
_micros
();
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) */
20
float
Sensor::getVelocity
() {
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
25
vel_angle_prev
=
angle_prev
;
26
vel_full_rotations
=
full_rotations
;
27
vel_angle_prev_ts
=
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
36
if
(
full_rotations
==
vel_full_rotations
) {
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
49
vel_angle_prev
=
angle_prev
;
50
vel_full_rotations
=
full_rotations
;
51
vel_angle_prev_ts
=
angle_prev_ts
;
52
}
53
54
return
velocity
;
55
}
56
57
58
59
void
Sensor::init
() {
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
64
vel_angle_prev_ts
=
_micros
();
65
delay(1);
66
getSensorAngle
();
// call once
67
delayMicroseconds(1);
68
angle_prev
=
getSensorAngle
();
// call again
69
angle_prev_ts
=
_micros
();
70
}
71
72
73
float
Sensor::getMechanicalAngle
() {
74
return
angle_prev
;
75
}
76
77
78
79
float
Sensor::getAngle
(){
80
return
(
float
)
full_rotations
*
_2PI
+
angle_prev
;
81
}
82
83
84
85
double
Sensor::getPreciseAngle
() {
86
return
(
double
)
full_rotations
* (double)
_2PI
+ (
double
)
angle_prev
;
87
}
88
89
90
91
int32_t
Sensor::getFullRotations
() {
92
return
full_rotations
;
93
}
94
95
96
97
int
Sensor::needsSearch
() {
98
return
0;
// default false
99
}
Sensor.h
Sensor::needsSearch
virtual int needsSearch()
Definition
Sensor.cpp:97
Sensor::getMechanicalAngle
virtual float getMechanicalAngle()
Definition
Sensor.cpp:73
Sensor::vel_angle_prev_ts
long vel_angle_prev_ts
Definition
Sensor.h:134
Sensor::min_elapsed_time
float min_elapsed_time
Definition
Sensor.h:109
Sensor::getAngle
virtual float getAngle()
Definition
Sensor.cpp:79
Sensor::velocity
float velocity
Definition
Sensor.h:130
Sensor::getVelocity
virtual float getVelocity()
Definition
Sensor.cpp:20
Sensor::vel_angle_prev
float vel_angle_prev
Definition
Sensor.h:133
Sensor::angle_prev_ts
long angle_prev_ts
Definition
Sensor.h:132
Sensor::update
virtual void update()
Definition
Sensor.cpp:7
Sensor::full_rotations
int32_t full_rotations
Definition
Sensor.h:135
Sensor::getSensorAngle
virtual float getSensorAngle()=0
Sensor::vel_full_rotations
int32_t vel_full_rotations
Definition
Sensor.h:136
Sensor::getFullRotations
virtual int32_t getFullRotations()
Definition
Sensor.cpp:91
Sensor::init
virtual void init()
Definition
Sensor.cpp:59
Sensor::angle_prev
float angle_prev
Definition
Sensor.h:131
Sensor::getPreciseAngle
virtual double getPreciseAngle()
Definition
Sensor.cpp:85
_2PI
#define _2PI
Definition
foc_utils.h:29
_micros
unsigned long _micros()
Definition
time_utils.cpp:21
src
common
base_classes
Sensor.cpp
Generated by
1.9.8