SimpleFOClibrary 2.4.0
Loading...
Searching...
No Matches
Sensor.h
Go to the documentation of this file.
1#ifndef SENSOR_H
2#define SENSOR_H
3
4#include <inttypes.h>
5
6/**
7 * Direction structure
8 */
9enum Direction : int8_t {
10 CW = 1, // clockwise
11 CCW = -1, // counter clockwise
12 UNKNOWN = 0 // not yet known or invalid state
13};
14
15
16/**
17 * Pullup configuration structure
18 */
19enum Pullup : uint8_t {
20 USE_INTERN = 0x00, //!< Use internal pullups
21 USE_EXTERN = 0x01 //!< Use external pullups
22};
23
24/**
25 * Sensor abstract class defintion
26 *
27 * This class is purposefully kept simple, as a base for all kinds of sensors. Currently we have
28 * Encoders, Magnetic Encoders and Hall Sensor implementations. This base class extracts the
29 * most basic common features so that a FOC driver can obtain the data it needs for operation.
30 *
31 * To implement your own sensors, create a sub-class of this class, and implement the getSensorAngle()
32 * method. getSensorAngle() returns a float value, in radians, representing the current shaft angle in the
33 * range 0 to 2*PI (one full turn).
34 *
35 * To function correctly, the sensor class update() method has to be called sufficiently quickly. Normally,
36 * the BLDCMotor's loopFOC() function calls it once per iteration, so you must ensure to call loopFOC() quickly
37 * enough, both for correct motor and sensor operation.
38 *
39 * The Sensor base class provides an implementation of getVelocity(), and takes care of counting full
40 * revolutions in a precise way, but if you wish you can additionally override these methods to provide more
41 * optimal implementations for your hardware.
42 *
43 */
44class Sensor{
45 friend class SmoothingSensor;
46 public:
47 /**
48 * Get mechanical shaft angle in the range 0 to 2PI. This value will be as precise as possible with
49 * the hardware. Base implementation uses the values returned by update() so that
50 * the same values are returned until update() is called again.
51 */
52 virtual float getMechanicalAngle();
53
54 /**
55 * Get current position (in rad) including full rotations and shaft angle.
56 * Base implementation uses the values returned by update() so that the same
57 * values are returned until update() is called again.
58 * Note that this value has limited precision as the number of rotations increases,
59 * because the limited precision of float can't capture the large angle of the full
60 * rotations and the small angle of the shaft angle at the same time.
61 */
62 virtual float getAngle();
63
64 /**
65 * On architectures supporting it, this will return a double precision position value,
66 * which should have improved precision for large position values.
67 * Base implementation uses the values returned by update() so that the same
68 * values are returned until update() is called again.
69 */
70 virtual double getPreciseAngle();
71
72 /**
73 * Get current angular velocity (rad/s)
74 * Can be overridden in subclasses. Base implementation uses the values
75 * returned by update() so that it only makes sense to call this if update()
76 * has been called in the meantime.
77 */
78 virtual float getVelocity();
79
80 /**
81 * Get the number of full rotations
82 * Base implementation uses the values returned by update() so that the same
83 * values are returned until update() is called again.
84 */
85 virtual int32_t getFullRotations();
86
87 /**
88 * Updates the sensor values by reading the hardware sensor.
89 * Some implementations may work with interrupts, and not need this.
90 * The base implementation calls getSensorAngle(), and updates internal
91 * fields for angle, timestamp and full rotations.
92 * This method must be called frequently enough to guarantee that full
93 * rotations are not "missed" due to infrequent polling.
94 * Override in subclasses if alternative behaviours are required for your
95 * sensor hardware.
96 */
97 virtual void update();
98
99 /**
100 * returns 0 if it does need search for absolute zero
101 * 0 - magnetic sensor (& encoder with index which is found)
102 * 1 - ecoder with index (with index not found yet)
103 */
104 virtual int needsSearch();
105
106 /**
107 * Minimum time between updates to velocity. If time elapsed is lower than this, the velocity is not updated.
108 */
109 float min_elapsed_time = 0.000100; // default is 100 microseconds, or 10kHz
110
111 protected:
112 /**
113 * Get current shaft angle from the sensor hardware, and
114 * return it as a float in radians, in the range 0 to 2PI.
115 *
116 * This method is pure virtual and must be implemented in subclasses.
117 * Calling this method directly does not update the base-class internal fields.
118 * Use update() when calling from outside code.
119 */
120 virtual float getSensorAngle()=0;
121 /**
122 * Call Sensor::init() from your sensor subclass's init method if you want smoother startup
123 * The base class init() method calls getSensorAngle() several times to initialize the internal fields
124 * to current values, ensuring there is no discontinuity ("jump from zero") during the first calls
125 * to sensor.getAngle() and sensor.getVelocity()
126 */
127 virtual void init();
128
129 // velocity calculation variables
130 float velocity=0.0f;
131 float angle_prev=0.0f; // result of last call to getSensorAngle(), used for full rotations and velocity
132 long angle_prev_ts=0; // timestamp of last call to getAngle, used for velocity
133 float vel_angle_prev=0.0f; // angle at last call to getVelocity, used for velocity
134 long vel_angle_prev_ts=0; // last velocity calculation timestamp
135 int32_t full_rotations=0; // full rotation tracking
136 int32_t vel_full_rotations=0; // previous full rotation value for velocity calculation
137};
138
139#endif
Pullup
Definition Sensor.h:19
@ USE_EXTERN
Use external pullups.
Definition Sensor.h:21
@ USE_INTERN
Use internal pullups.
Definition Sensor.h:20
Direction
Definition Sensor.h:9
@ UNKNOWN
Definition Sensor.h:12
@ CCW
Definition Sensor.h:11
@ CW
Definition Sensor.h:10
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
friend class SmoothingSensor
Definition Sensor.h:45
float angle_prev
Definition Sensor.h:131
virtual double getPreciseAngle()
Definition Sensor.cpp:85