forked from MarkFzp/mobile-aloha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrealsense_test.py
69 lines (63 loc) · 1.88 KB
/
realsense_test.py
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
import pyrealsense2 as rs
import time
from pprint import pprint
from collections import namedtuple
from functools import partial
from matplotlib import pyplot as plt
attrs = [
'acceleration',
'angular_acceleration',
'angular_velocity',
'mapper_confidence',
'rotation',
'tracker_confidence',
'translation',
'velocity',
]
Pose = namedtuple('Pose', attrs)
def main():
pipeline = rs.pipeline()
cfg = rs.config()
# if only pose stream is enabled, fps is higher (202 vs 30)
cfg.enable_stream(rs.stream.pose)
pipeline.start(cfg)
poses = []
z_vels = []
x_vels = []
y_vels = []
try:
print('Start!')
while True:
frames = pipeline.wait_for_frames()
pose_frame = frames.get_pose_frame()
if pose_frame:
pose = pose_frame.get_pose_data()
n = pose_frame.get_frame_number()
timestamp = pose_frame.get_timestamp()
p = Pose(*map(partial(getattr, pose), attrs))
z_vel = pose.velocity.z
y_vel = pose.velocity.y
x_vel = pose.velocity.x
z_vels.append(z_vel)
x_vels.append(x_vel)
y_vels.append(y_vel)
poses.append((n, timestamp, p))
if len(poses) == 1000:
return
time.sleep(0.02)
finally:
print('End!')
pipeline.stop()
duration = (poses[-1][1]-poses[0][1])/1000
print(f'start: {poses[0][1]}')
print(f'end: {poses[-1][1]}')
print(f'duration: {duration}s')
print(f'fps: {len(poses)/duration}')
plt.plot(z_vels, label='z_vel')
plt.plot(x_vels, label='x_vel')
plt.plot(y_vels, label='y_vel')
plt.legend()
plt.savefig('rs_vel.png')
plt.show()
if __name__ == "__main__":
main()