forked from WEARTHaptics/WEART-SDK-Cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WeArtMessages.h
525 lines (402 loc) · 13.1 KB
/
WeArtMessages.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/// @private
#pragma once
#include "WeArtCommon.h"
#include <string>
#include <vector>
#include <cassert>
#include <cstdint>
#include <map>
#include "nlohmann/json.hpp"
// Utility Methods, called to serialize/deserialize types
HandSide StringToHandside(std::string& str);
std::string HandsideToString(HandSide hs);
std::string CalibrationHandSideToString(HandSide hs);
HandSide StringToCalibrationHandSide(std::string& str);
ActuationPoint StringToActuationPoint(std::string& str);
std::string ActuationPointToString(ActuationPoint ap);
TrackingType StringToTrackingType(const std::string& str);
std::string TrackingTypeToString(TrackingType trackType);
std::string MiddlewareStatusToString(MiddlewareStatus status);
//! @brief Generic Weart message
//! @private
class WeArtMessage {
public:
//! @brief Allows to get the message ID, used to deserialize the correct message type
//! @return the message ID
virtual std::string getID() = 0;
//! @brief Sets the hand to which the message is applied (if needed)
//! @param hs Hand to which the message is applied
virtual void setHandSide(HandSide hs) = 0;
//! @brief Sets the actuation point to which the message is applied (if needed)
//! @param ap Actuation point to which the message is applied
virtual void setActuationPoint(ActuationPoint ap) = 0;
virtual std::string serialize() = 0;
virtual void deserialize(std::string message) = 0;
};
//! @private
class WeArtCsvMessage : public WeArtMessage {
public:
const char field_separator = ':';
virtual std::vector<std::string> getValues() = 0;
virtual void setValues(std::vector<std::string>& values) = 0;
virtual std::string serialize() override;
virtual void deserialize(std::string message) override;
};
//! @private
class WeArtJsonMessage : public WeArtMessage {
public:
WeArtJsonMessage();
virtual std::string serialize() override;
virtual void deserialize(std::string message) override;
std::uint64_t timestamp() { return _timestamp; }
virtual void setHandSide(HandSide hs) override {};
virtual void setActuationPoint(ActuationPoint ap) override {};
protected:
virtual nlohmann::json serializePayload() { return nlohmann::json(); }
virtual void deserializePayload(nlohmann::json payload) {}
std::uint64_t _timestamp;
};
//! @brief Message without handside or actuation point parameters
//! @private
class WeArtMessageNoParams : public WeArtCsvMessage {
public:
virtual void setHandSide(HandSide hs) override {};
virtual void setActuationPoint(ActuationPoint ap) override {};
// This message carries no values.
virtual std::vector<std::string> getValues() override {
return std::vector<std::string>();
};
virtual void setValues(std::vector<std::string>& values) override {};
};
//! @brief Message related to a given handside
//! @private
class WeArtMessageHandSpecific : public WeArtCsvMessage {
public:
HandSide getHand() {
return handSide;
}
protected:
HandSide handSide;
public:
virtual void setHandSide(HandSide hs) override {
handSide = hs;
};
virtual void setActuationPoint(ActuationPoint ap) override {};
};
//! @brief Message related to a given hand and actuation point
//! @private
class WeArtMessageObjectSpecific : public WeArtCsvMessage {
public:
HandSide getHand() {
return handSide;
}
virtual void setHandSide(HandSide hs) override {
handSide = hs;
};
ActuationPoint getActuationPoint() {
return actuationPoint;
}
virtual void setActuationPoint(ActuationPoint ap) override {
actuationPoint = ap;
};
protected:
HandSide handSide;
ActuationPoint actuationPoint;
};
//! @private
class StartFromClientMessage : public WeArtMessageNoParams {
public:
static constexpr const char* ID = "StartFromClient";
StartFromClientMessage(TrackingType trackType = TrackingType::WEART_HAND) : _trackType(trackType) {}
virtual std::string getID() override {
return ID;
};
virtual std::vector<std::string> getValues() override;
virtual void setValues(std::vector<std::string>& values) override;
private:
TrackingType _trackType;
};
//! @private
class StopFromClientMessage : public WeArtMessageNoParams {
public:
static constexpr const char* ID = "StopFromClient";
virtual std::string getID() override {
return ID;
};
};
//! @private
class StartCalibrationMessage : public WeArtMessageNoParams {
public:
static constexpr const char* ID = "StartCalibration";
virtual std::string getID() override {
return ID;
}
};
//! @private
class StopCalibrationMessage : public WeArtMessageNoParams {
public:
static constexpr const char* ID = "StopCalibration";
virtual std::string getID() override {
return ID;
}
};
//! @private
class CalibrationStatusMessage : public WeArtMessageHandSpecific {
public:
static constexpr const char* ID = "CalibrationStatus";
virtual std::string getID() override {
return ID;
}
CalibrationStatus getStatus() {
return status;
}
virtual std::vector<std::string> getValues() override;
virtual void setValues(std::vector<std::string>& values) override;
protected:
CalibrationStatus status;
};
//! @private
class CalibrationResultMessage : public WeArtMessageHandSpecific {
public:
static constexpr const char* ID = "CalibrationResult";
virtual std::string getID() override {
return ID;
}
bool getSuccess() {
return success;
}
virtual std::vector<std::string> getValues() override;
virtual void setValues(std::vector<std::string>& values) override;
protected:
bool success;
};
//! @private
class ExitMessage : public WeArtMessageNoParams {
public:
static constexpr const char* ID = "exit";
virtual std::string getID() override {
return std::string("exit");
};
};
//! @private
class DisconnectMessage : public WeArtMessageNoParams {
public:
static constexpr const char* ID = "disconnect";
virtual std::string getID() override {
return ID;
};
};
//! @private
class SetTemperatureMessage : public WeArtMessageObjectSpecific {
public:
static constexpr const char* ID = "temperature";
SetTemperatureMessage(const float t) : temperature(t) {};
SetTemperatureMessage() {};
virtual std::string getID() override {
return ID;
};
virtual std::vector<std::string> getValues() override;
virtual void setValues(std::vector<std::string>& values) override;
protected:
float temperature;
};
//! @private
class StopTemperatureMessage : public WeArtMessageObjectSpecific {
public:
static constexpr const char* ID = "stopTemperature";
virtual std::string getID() override {
return ID;
};
virtual std::vector<std::string> getValues() override;
virtual void setValues(std::vector<std::string>& values) override;
};
//! @private
class SetForceMessage : public WeArtMessageObjectSpecific {
public:
static constexpr const char* ID = "force";
SetForceMessage(const float f[3]) : force{ f[0], f[1], f[2] } {};
SetForceMessage() {};
virtual std::string getID() override {
return ID;
};
virtual std::vector<std::string> getValues() override;
virtual void setValues(std::vector<std::string>& values) override;
protected:
float force[3];
};
//! @private
class StopForceMessage : public WeArtMessageObjectSpecific {
public:
static constexpr const char* ID = "stopForce";
virtual std::string getID() override {
return ID;
};
virtual std::vector<std::string> getValues() override;
virtual void setValues(std::vector<std::string>& values) override;
};
//! @private
class SetTextureMessage : public WeArtMessageObjectSpecific {
public:
static constexpr const char* ID = "texture";
SetTextureMessage(const int idx, const float vel, const float vol) : index(idx), velocity{ 0.5f, 0.0f, vel }, volume(vol) {};
SetTextureMessage() {};
virtual std::string getID() override {
return ID;
};
virtual std::vector<std::string> getValues() override;
virtual void setValues(std::vector<std::string>& values) override;
protected:
int index;
float velocity[3] = { 0.5f, 0.0f, 0.0f };
float volume;
};
//! @private
class StopTextureMessage : public WeArtMessageObjectSpecific {
public:
static constexpr const char* ID = "stopTexture";
virtual std::string getID() override {
return ID;
};
virtual std::vector<std::string> getValues() override;
virtual void setValues(std::vector<std::string>& values) override;
};
//! @brief Generic Tracking message, contains information on closure and abduction (based on tracking type)
//! @private
//!
//! Message containing the closure and abduction values of the different actuation points (thimbles and palm)
//! The abduction values available depends on the tracking type selected when the connection started.
//!
//! In particular:
//! * DEFAULT: no abduction values (deprecated)
//! * CLAP_HAND: 3 angles (X,Y,Z) for the thumb, and a single abduction value for index and middle
//! * WEART_HAND: single abduction value only for the thumb
//!
class TrackingMessage : public WeArtMessageNoParams {
public:
static constexpr const char* ID = "Tracking";
virtual std::string getID() override {
return std::string(ID);
};
struct Angles {
float X;
float Y;
float Z;
};
virtual std::vector<std::string> getValues() override;
virtual void setValues(std::vector<std::string>& values) override;
TrackingType GetType() {
return _trackingType;
}
//! @brief Getter for abduction value (based on tracking type and given point)
//! @param handSide Hand from which to get the value
//! @param actuationPoint Actuation Point from which to get the value
//! @return the requested abduction value
float GetAbduction(HandSide handSide, ActuationPoint actuationPoint);
//! @brief Getter for closure value
//! @param handSide Hand from which to get the value
//! @param actuationPoint Actuation Point from which to get the value
//! @return the requested closure value (from 0 to 1)
float GetClosure(HandSide handSide, ActuationPoint actuationPoint);
private:
TrackingType _trackingType;
// Closures
uint8_t RightThumbClosure;
uint8_t RightIndexClosure;
uint8_t RightMiddleClosure;
uint8_t RightPalmClosure;
uint8_t LeftThumbClosure;
uint8_t LeftIndexClosure;
uint8_t LeftMiddleClosure;
uint8_t LeftPalmClosure;
// Abductions
uint8_t RightThumbAbduction;
uint8_t LeftThumbAbduction;
};
//! @private
class RawDataOn : public WeArtJsonMessage {
public:
static constexpr const char* ID = "RAW_DATA_ON";
virtual std::string getID() override { return ID; }
virtual void setHandSide(HandSide hs) override {}
virtual void setActuationPoint(ActuationPoint ap) override {}
};
//! @private
class RawDataOff : public WeArtJsonMessage {
public:
static constexpr const char* ID = "RAW_DATA_OFF";
virtual std::string getID() override { return ID; }
virtual void setHandSide(HandSide hs) override {}
virtual void setActuationPoint(ActuationPoint ap) override {}
};
//! @private
class RawSensorsData : public WeArtJsonMessage {
public:
static constexpr const char* ID = "RAW_DATA";
virtual std::string getID() override { return ID; };
virtual void setHandSide(HandSide hs) override { hand = hs; }
virtual void setActuationPoint(ActuationPoint ap) override {}
HandSide getHand() { return hand; }
bool hasSensor(ActuationPoint ap);
SensorData getSensor(ActuationPoint ap);
protected:
virtual nlohmann::json serializePayload() override;
virtual void deserializePayload(nlohmann::json payload) override;
HandSide hand;
std::map<ActuationPoint, SensorData> sensors;
};
//! @private
class AnalogSensorsData : public WeArtJsonMessage {
public:
static constexpr const char* ID = "RAW_SENSOR_ON_MASK";
virtual std::string getID() override { return ID; };
virtual void setHandSide(HandSide hs) override { hand = hs; }
virtual void setActuationPoint(ActuationPoint ap) override {}
HandSide getHand() { return hand; }
bool hasSensor(ActuationPoint ap);
AnalogSensorRawData getSensor(ActuationPoint ap);
protected:
virtual nlohmann::json serializePayload() override;
virtual void deserializePayload(nlohmann::json payload) override;
HandSide hand;
std::map<ActuationPoint, AnalogSensorRawData> sensors;
};
//!@private
class GetMiddlewareStatus : public WeArtJsonMessage {
public:
GetMiddlewareStatus() : WeArtJsonMessage() {}
static constexpr const char* ID = "MW_GET_STATUS";
virtual std::string getID() override { return ID; };
};
//!@private
class MiddlewareStatusMessage : public WeArtJsonMessage {
public:
MiddlewareStatusMessage() : WeArtJsonMessage() {}
static constexpr const char* ID = "MW_STATUS";
virtual std::string getID() override { return ID; };
MiddlewareStatusData data() { return _data; }
protected:
virtual nlohmann::json serializePayload() override;
virtual void deserializePayload(nlohmann::json payload) override;
private:
MiddlewareStatusData _data;
};
//! @private
class GetDevicesStatusMessage : public WeArtJsonMessage {
public:
GetDevicesStatusMessage() : WeArtJsonMessage() {}
static constexpr const char* ID = "DEVICES_GET_STATUS";
virtual std::string getID() override { return ID; };
};
//! @private
class DevicesStatusMessage : public WeArtJsonMessage {
public:
DevicesStatusMessage() : WeArtJsonMessage() {}
static constexpr const char* ID = "DEVICES_STATUS";
virtual std::string getID() override { return ID; };
std::vector<ConnectedDeviceStatus> devices() { return _devices; }
protected:
virtual nlohmann::json serializePayload() override;
virtual void deserializePayload(nlohmann::json payload) override;
private:
std::vector<ConnectedDeviceStatus> _devices;
};