-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwave.h
303 lines (276 loc) · 7.34 KB
/
wave.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#ifndef WAVE_H
#define WAVE_H
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
/**
* The horizontal sensor resolution.
*
* For users of the C bindings:
* Change the value of this definition to change the x-axis resolution of the sensor.
*
* For VL53L5CX: Resolution either 4x4 or 8x8.
*/
#define RES_X 8
/**
* The vertical sensor resolution.
*
* For users of the C bindings:
* Change the value of this definition to change the y-axis resolution of the sensor.
*
* For VL53L5CX: Resolution either 4x4 or 8x8.
*/
#define RES_Y 8
/**
* The history size.
*
* Must be large enough to hold at least ca 2 seconds of data to be able to reliably recognize gestures.
*/
#define HISTORY_SIZE 60
/**
* A hand gesture.
*/
typedef enum Gesture {
/**
* No recognized gesture.
*/
GestureNone = 0,
/**
* A static hold.
*/
GestureStaticHold,
/**
* A right swipe.
*/
GestureSwipeRight,
/**
* A left swipe.
*/
GestureSwipeLeft,
/**
* A up swipe.
*/
GestureSwipeUp,
/**
* A down swipe.
*/
GestureSwipeDown,
} Gesture;
/**
* The status of the gesture recognizer.
*/
typedef enum RecognizerStatus {
/**
* Ok.
*/
RecognizerStatusOk = 0,
/**
* Indicates a failure while the recognizer was initialized.
*/
RecognizerStatusInitFailure,
/**
* Indicates invalid input to the recognizer.
*/
RecognizerStatusInvalidInput,
} RecognizerStatus;
/**
* Represents a sensor measurement coming from the TOF sensor.
*
* Expects that the zones are already rotated and mirrored,
* so that the zone with index \[0\]\[0\] is the top left corner when looking at the sensor.
*/
typedef struct SensorMeasurement_RES_X__RES_Y {
/**
* The measured distances of each zone.
*
* Invalid distance measurements are represented by value -1.0.
*/
float zone_dist[RES_Y][RES_X];
/**
* The time of the measurement in milliseconds. Must be monotonically increasing.
*/
uint32_t time_ms;
} SensorMeasurement_RES_X__RES_Y;
/**
* Configurable sensor parameters. Different for every sensor.
*/
typedef struct SensorParams {
/**
* The horizontal FOV of the sensor.
*/
float fov_horizontal;
/**
* The vertical FOV of the sensor.
*/
float fov_vertical;
} SensorParams;
/**
* Parameters for gesture recognition.
*/
typedef struct RecognizerParams {
/**
* The furthest hand distance for gesture recognition.
*/
float gesture_threshold_dist;
/**
* The time the hand has to be still to recognize a static hold.
*/
uint32_t static_hold_time_ms;
/**
* How much the hand can move towards / away from the sensor while doing a static hold.
*/
float static_hold_tolerance_dist;
/**
* How much the hand can move towards / away from the sensor while doing a swipe.
*/
float swipe_tolerance_dist;
/**
* How much distance the hand has to travel to detect a horizontal swipe.
*/
float swipe_horizontal_travel_dist;
/**
* How much distance the hand has to travel to detect a vertical swipe.
*/
float swipe_vertical_travel_dist;
} RecognizerParams;
/**
* Represents spherical coordinates in mathematical naming convention.
* ([Reference](https://mathworld.wolfram.com/SphericalCoordinates.html))
*/
typedef struct CoordsSpherical {
/**
* Distance to the origin.
*/
float r;
/**
* Angle with respect to x-axis (azimuth) (rad).
*/
float theta;
/**
* Angle with respect to polar / z-axis (zenith) (rad).
*/
float phi;
} CoordsSpherical;
/**
* The recognized hand state.
*/
typedef enum HandState_Tag {
/**
* No hand was found.
*/
HandNotFound,
/**
* Hand was found with this position.
*/
HandFound,
} HandState_Tag;
typedef struct HandFound_Body {
/**
* The hand position in spherical coordinates.
*/
struct CoordsSpherical hand_pos;
} HandFound_Body;
typedef struct HandState {
HandState_Tag tag;
union {
HandFound_Body hand_found;
};
} HandState;
/**
* A gesture prediction result.
*/
typedef struct RecognizerResult {
/**
* The current hand state.
*/
struct HandState hand_state;
/**
* The recognized gesture, GestureNone if no gesture was recognized.
*/
enum Gesture gesture;
} RecognizerResult;
typedef struct HistoryEntry_RES_X__RES_Y {
struct SensorMeasurement_RES_X__RES_Y measurement;
struct HandState hand_state;
} HistoryEntry_RES_X__RES_Y;
/**
* The gesture recognizer.
*
* Is initially configured through parameters and gets fed measurements and time and predicts gestures.
*/
typedef struct GestureRecognizer_RES_X__RES_Y__HISTORY_SIZE {
struct RecognizerParams params;
struct SensorParams sensor_params;
uint32_t start_time;
struct HistoryEntry_RES_X__RES_Y history[HISTORY_SIZE];
uintptr_t received_measurements;
} GestureRecognizer_RES_X__RES_Y__HISTORY_SIZE;
/**
* Cartesian coordinates.
*/
typedef struct CoordsCartesian {
/**
* The distance to the origin on the x-axis.
*/
float x;
/**
* The distance to the origin on the y-axis.
*/
float y;
/**
* The distance to the origin on the z-axis.
*/
float z;
} CoordsCartesian;
/**
* Creates an invalid measurement (distances are set to `-1.0`).
*/
struct SensorMeasurement_RES_X__RES_Y sensor_measurement_invalid(void);
/**
* Default sensor parameters for the ST VL53L5CX TOF-Sensor.
*/
struct SensorParams sensor_params_default_vl53l5cx(void);
/**
* Default recognizer parameters, providing a good starting point for gesture recognition.
*/
struct RecognizerParams recognizer_params_default(void);
/**
* Default result, meaning no hand is found and no gestures are recognized.
*
* Used to initialize the result before passing it to the recognizer.
*/
struct RecognizerResult recognizer_result_default(void);
/**
* A new gesture recognizer with the given parameters.
*
* The sensor parameters have preconfigured defaults for common TOF-Sensors such as the ST VL53L5CX.
*/
struct GestureRecognizer_RES_X__RES_Y__HISTORY_SIZE gesture_recognizer_new(struct RecognizerParams params,
struct SensorParams sensor_params);
/**
* Resets the gesture recognizer with the given parameters.
*
* Clears the history, ongoing predictions and resets the internal state.
*/
enum RecognizerStatus gesture_recognizer_reset(struct GestureRecognizer_RES_X__RES_Y__HISTORY_SIZE *gesture_recognizer,
struct RecognizerParams params,
struct SensorParams sensor_params,
uint32_t now);
/**
* Updates the gesture recognizer with a new measurement and time.
*
* The time in the measurement must be monotonically increasing (usually coming from a systick timer).
*/
enum RecognizerStatus gesture_recognizer_update(struct GestureRecognizer_RES_X__RES_Y__HISTORY_SIZE *gesture_recognizer,
struct SensorMeasurement_RES_X__RES_Y measurement,
struct RecognizerResult *gesture_result);
/**
* Converts cartesian to spherical coordinates.
*/
struct CoordsSpherical coords_spherical_from_cartesian(struct CoordsCartesian coords_cart);
/**
* Converts spherical to cartesian coordinates.
*/
struct CoordsCartesian coords_cartesian_from_spherical(struct CoordsSpherical coords_spher);
#endif /* WAVE_H */