-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04_read_every_sensor.cpp
More file actions
316 lines (292 loc) · 12.4 KB
/
Copy path04_read_every_sensor.cpp
File metadata and controls
316 lines (292 loc) · 12.4 KB
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
//============================================================================
// Example 04 — 为每种传感器各写一个数据读取例子
// --------------------------------------------------------------------------
// 本文件为设备上**每一种**受支持的传感器类型分别实现了一个读取函数
// (read*Sensor()),并演示如何按类型正确解读 values[]:
//
// * 三维矢量类(1/2/4/9/10):values[0..2] = X/Y/Z
// * 旋转矢量类(11/15/20) :values[0..2] = X/Y/Z,values[3] = w
// * 单值类(5/6/8/12/13) :values[0] = 唯一数值
// * 事件类(17/18) :仅在事件发生时推一条(values[0] 一般为 0)
// * 计步器(19) :values[0] = 累计步数(float),用 stepCountU32()
// * 心率(21) :values[0] = bpm,-1 表示无接触
//
// 每个 read*Sensor() 都是自包含的“例子”:打开 → 注册回调 → 采样 2 秒
// → 停止。main() 依次调用它们。
//
// 用法:Example04ReadEverySensor [主机IP] [端口]
//============================================================================
#include <RemoteSensor/RemoteSensor.hpp>
#include <RemoteSensor/Sensor.hpp>
#include <chrono>
#include <cstdio>
#include <memory>
#include <string>
#include <thread>
// 通用小助手:对已打开的传感器 start → 运行 seconds 秒 → stop → 关闭。
// 注意:回调在接收线程中触发,这里只需睡眠等待即可。
// closeSensor() 会解除注册并释放,避免传感器一直登记在 Device 里。
static void runFor(rs::Device& dev, const std::shared_ptr<rs::Sensor>& s,
int64_t periodUs, int seconds) {
if (!s) return;
s->start(periodUs);
std::this_thread::sleep_for(std::chrono::seconds(seconds));
s->stop();
dev.closeSensor(s);
}
//============================================================================
// 以下每个函数对应一种传感器类型的数据读取
//============================================================================
// 1) 加速度计 Accelerometer —— 三维矢量,单位 m/s²
static void readAccelerometer(rs::Device& dev) {
auto acc = dev.openSensor(rs::SensorType::Accelerometer);
if (!acc) { printf(" [ACCELEROMETER] 设备无此传感器,跳过\n"); return; }
printf("\n=== 加速度计 (m/s²) ===\n");
int n = 0;
acc->setEventCallback([&](const rs::SensorEvent& e) {
if (n++ >= 8) return;
printf(" x=%.3f y=%.3f z=%.3f\n", e.value(0), e.value(1), e.value(2));
});
runFor(dev, acc, 20000, 2);
}
// 2) 磁场 MagneticField —— 三维矢量,单位 µT
static void readMagneticField(rs::Device& dev) {
auto mag = dev.openSensor(rs::SensorType::MagneticField);
if (!mag) { printf(" [MAGNETIC_FIELD] 设备无此传感器,跳过\n"); return; }
printf("\n=== 磁场 (µT) ===\n");
int n = 0;
mag->setEventCallback([&](const rs::SensorEvent& e) {
if (n++ >= 8) return;
printf(" x=%.2f y=%.2f z=%.2f\n", e.value(0), e.value(1), e.value(2));
});
runFor(dev, mag, 20000, 2);
}
// 4) 陀螺仪 Gyroscope —— 三维矢量,单位 rad/s
static void readGyroscope(rs::Device& dev) {
auto gyro = dev.openSensor(rs::SensorType::Gyroscope);
if (!gyro) { printf(" [GYROSCOPE] 设备无此传感器,跳过\n"); return; }
printf("\n=== 陀螺仪 (rad/s) ===\n");
int n = 0;
gyro->setEventCallback([&](const rs::SensorEvent& e) {
if (n++ >= 8) return;
printf(" x=%.4f y=%.4f z=%.4f\n", e.value(0), e.value(1), e.value(2));
});
runFor(dev, gyro, 20000, 2);
}
// 5) 光照 Light —— 单值,单位 lux
static void readLight(rs::Device& dev) {
auto light = dev.openSensor(rs::SensorType::Light);
if (!light) { printf(" [LIGHT] 设备无此传感器,跳过\n"); return; }
printf("\n=== 光照 (lux) ===\n");
int n = 0;
light->setEventCallback([&](const rs::SensorEvent& e) {
if (n++ >= 8) return;
printf(" %.1f\n", e.value(0));
});
runFor(dev, light, 100000, 2);
}
// 6) 气压 Pressure —— 单值,单位 hPa
static void readPressure(rs::Device& dev) {
auto p = dev.openSensor(rs::SensorType::Pressure);
if (!p) { printf(" [PRESSURE] 设备无此传感器,跳过\n"); return; }
printf("\n=== 气压 (hPa) ===\n");
int n = 0;
p->setEventCallback([&](const rs::SensorEvent& e) {
if (n++ >= 8) return;
printf(" %.2f\n", e.value(0));
});
runFor(dev, p, 100000, 2);
}
// 8) 距离 Proximity —— 单值,单位 cm(接近 0 表示有物体靠近)
static void readProximity(rs::Device& dev) {
auto prox = dev.openSensor(rs::SensorType::Proximity);
if (!prox) { printf(" [PROXIMITY] 设备无此传感器,跳过\n"); return; }
printf("\n=== 距离 (cm) ===\n");
int n = 0;
prox->setEventCallback([&](const rs::SensorEvent& e) {
if (n++ >= 8) return;
printf(" %.1f\n", e.value(0));
});
runFor(dev, prox, 100000, 2);
}
// 9) 重力 Gravity —— 三维矢量,单位 m/s²(静止时约为 (0,0,9.81))
static void readGravity(rs::Device& dev) {
auto g = dev.openSensor(rs::SensorType::Gravity);
if (!g) { printf(" [GRAVITY] 设备无此传感器,跳过\n"); return; }
printf("\n=== 重力 (m/s²) ===\n");
int n = 0;
g->setEventCallback([&](const rs::SensorEvent& e) {
if (n++ >= 8) return;
printf(" x=%.3f y=%.3f z=%.3f\n", e.value(0), e.value(1), e.value(2));
});
runFor(dev, g, 20000, 2);
}
// 10) 线性加速度 LinearAcceleration —— 三维矢量,单位 m/s²
static void readLinearAcceleration(rs::Device& dev) {
auto la = dev.openSensor(rs::SensorType::LinearAcceleration);
if (!la) { printf(" [LINEAR_ACCELERATION] 设备无此传感器,跳过\n"); return; }
printf("\n=== 线性加速度 (m/s²) ===\n");
int n = 0;
la->setEventCallback([&](const rs::SensorEvent& e) {
if (n++ >= 8) return;
printf(" x=%.3f y=%.3f z=%.3f\n", e.value(0), e.value(1), e.value(2));
});
runFor(dev, la, 20000, 2);
}
// 11) 旋转矢量 RotationVector —— X/Y/Z + 第4项(四元数 w 或精度)
static void readRotationVector(rs::Device& dev) {
auto rv = dev.openSensor(rs::SensorType::RotationVector);
if (!rv) { printf(" [ROTATION_VECTOR] 设备无此传感器,跳过\n"); return; }
printf("\n=== 旋转矢量 ===\n");
int n = 0;
rv->setEventCallback([&](const rs::SensorEvent& e) {
if (n++ >= 8) return;
printf(" x=%.4f y=%.4f z=%.4f w=%.4f\n",
e.value(0), e.value(1), e.value(2),
e.valueCount > 3 ? e.value(3) : 0.0f);
});
runFor(dev, rv, 20000, 2);
}
// 12) 相对湿度 RelativeHumidity —— 单值,单位 %
static void readHumidity(rs::Device& dev) {
auto h = dev.openSensor(rs::SensorType::RelativeHumidity);
if (!h) { printf(" [RELATIVE_HUMIDITY] 设备无此传感器,跳过\n"); return; }
printf("\n=== 相对湿度 (%%) ===\n");
int n = 0;
h->setEventCallback([&](const rs::SensorEvent& e) {
if (n++ >= 8) return;
printf(" %.1f\n", e.value(0));
});
runFor(dev, h, 100000, 2);
}
// 13) 环境温度 AmbientTemperature —— 单值,单位 °C
static void readTemperature(rs::Device& dev) {
auto t = dev.openSensor(rs::SensorType::AmbientTemperature);
if (!t) { printf(" [AMBIENT_TEMPERATURE] 设备无此传感器,跳过\n"); return; }
printf("\n=== 环境温度 (°C) ===\n");
int n = 0;
t->setEventCallback([&](const rs::SensorEvent& e) {
if (n++ >= 8) return;
printf(" %.1f\n", e.value(0));
});
runFor(dev, t, 100000, 2);
}
// 15) 游戏旋转矢量 GameRotationVector —— X/Y/Z + w(不受地磁影响)
static void readGameRotationVector(rs::Device& dev) {
auto rv = dev.openSensor(rs::SensorType::GameRotationVector);
if (!rv) { printf(" [GAME_ROTATION_VECTOR] 设备无此传感器,跳过\n"); return; }
printf("\n=== 游戏旋转矢量 ===\n");
int n = 0;
rv->setEventCallback([&](const rs::SensorEvent& e) {
if (n++ >= 8) return;
printf(" x=%.4f y=%.4f z=%.4f w=%.4f\n",
e.value(0), e.value(1), e.value(2),
e.valueCount > 3 ? e.value(3) : 0.0f);
});
runFor(dev, rv, 20000, 2);
}
// 17) 显著运动 SignificantMotion —— 事件类:检测到显著运动才发一条
static void readSignificantMotion(rs::Device& dev) {
auto sm = dev.openSensor(rs::SensorType::SignificantMotion);
if (!sm) { printf(" [SIGNIFICANT_MOTION] 设备无此传感器,跳过\n"); return; }
printf("\n=== 显著运动(检测到才会输出) ===\n");
sm->setEventCallback([](const rs::SensorEvent& e) {
printf(" 检测到显著运动! ts=%lld\n",
static_cast<long long>(e.timestamp));
});
runFor(dev, sm, 0, 4);
}
// 18) 计步检测 StepDetector —— 事件类:每走一步发一条
static void readStepDetector(rs::Device& dev) {
auto sd = dev.openSensor(rs::SensorType::StepDetector);
if (!sd) { printf(" [STEP_DETECTOR] 设备无此传感器,跳过\n"); return; }
printf("\n=== 计步检测(每走一步输出一次) ===\n");
sd->setEventCallback([](const rs::SensorEvent& e) {
printf(" 一步! ts=%lld\n", static_cast<long long>(e.timestamp));
});
runFor(dev, sd, 200000, 4);
}
// 19) 计步器 StepCounter —— 累计步数,用 stepCountU32() 取整型
static void readStepCounter(rs::Device& dev) {
auto sc = dev.openSensor(rs::SensorType::StepCounter);
if (!sc) { printf(" [STEP_COUNTER] 设备无此传感器,跳过\n"); return; }
printf("\n=== 计步器(累计步数) ===\n");
int n = 0;
sc->setEventCallback([&](const rs::SensorEvent& e) {
if (n++ >= 8) return;
printf(" 累计步数: %u\n", e.stepCountU32());
});
runFor(dev, sc, 200000, 2);
}
// 20) 地磁旋转矢量 GeomagneticRotationVector —— X/Y/Z + w
static void readGeomagneticRotationVector(rs::Device& dev) {
auto rv = dev.openSensor(rs::SensorType::GeomagneticRotationVector);
if (!rv) { printf(" [GEOMAGNETIC_ROTATION_VECTOR] 设备无此传感器,跳过\n"); return; }
printf("\n=== 地磁旋转矢量 ===\n");
int n = 0;
rv->setEventCallback([&](const rs::SensorEvent& e) {
if (n++ >= 8) return;
printf(" x=%.4f y=%.4f z=%.4f w=%.4f\n",
e.value(0), e.value(1), e.value(2),
e.valueCount > 3 ? e.value(3) : 0.0f);
});
runFor(dev, rv, 20000, 2);
}
// 21) 心率 HeartRate —— 单值 bpm;-1 表示无皮肤接触
static void readHeartRate(rs::Device& dev) {
auto hr = dev.openSensor(rs::SensorType::HeartRate);
if (!hr) { printf(" [HEART_RATE] 设备无此传感器,跳过\n"); return; }
printf("\n=== 心率 (bpm) ===\n");
int n = 0;
hr->setEventCallback([&](const rs::SensorEvent& e) {
if (n++ >= 8) return;
float bpm = e.value(0);
printf(" %.1f bpm (%s)\n", bpm, bpm >= 0.0f ? "有接触" : "未接触");
});
runFor(dev, hr, 500000, 2);
}
//============================================================================
// main:依次调用每个传感器类型的读取示例
//============================================================================
int main(int argc, char** argv) {
std::string host;
int port = 45999;
if (argc >= 2) host = argv[1];
if (argc >= 3) port = std::atoi(argv[2]);
rs::Device dev;
if (host.empty()) {
rs::DiscoveredServer s;
if (!rs::discoverFirstServer(s, 2500)) {
printf("未发现服务端,请先启动手机端 APP\n");
return 1;
}
host = s.ip; port = s.port;
}
try {
dev.connect(host, static_cast<uint16_t>(port), 5000);
} catch (const std::exception& ex) {
printf("连接失败: %s\n", ex.what());
return 1;
}
printf("已连接: %s:%d,开始逐传感器读取...\n", host.c_str(), port);
readAccelerometer(dev);
readMagneticField(dev);
readGyroscope(dev);
readLight(dev);
readPressure(dev);
readProximity(dev);
readGravity(dev);
readLinearAcceleration(dev);
readRotationVector(dev);
readHumidity(dev);
readTemperature(dev);
readGameRotationVector(dev);
readSignificantMotion(dev);
readStepDetector(dev);
readStepCounter(dev);
readGeomagneticRotationVector(dev);
readHeartRate(dev);
dev.disconnect();
printf("\n完成\n");
return 0;
}