forked from Babak-SSH/lcm-loggers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_imu.py
47 lines (38 loc) · 1.3 KB
/
read_imu.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
import serial
import time
import sys
import lcm
from data_types import IMU_t
# initializing lcm and IMU_t data type
lc = lcm.LCM()
resp = IMU_t()
# initializing serial communication between rpi and arduino (ttyACM0 is serial device name for Arduino)
ser = serial.Serial('/dev/ttyACM0', 2000000)
# flushing input buffer
ser.reset_input_buffer()
print("IMU ready!")
while True :
try:
# data rate is about 5khz, with this sleep we reduce it to 1khz so we data every one milisecond
time.sleep(0.0008)
if ser.in_waiting > 0:
data = ser.readline()
try:
# removing whitespaces and spliting the numbers
acc_x, acc_y, acc_z = map(float, str(data)[2:-4].lstrip().replace(" ", " ").split(" "))
resp.acc_x = acc_x
resp.acc_y = acc_y
resp.acc_z = acc_z
# publish response data to lcm
lc.publish("IMU_ACC", resp.encode())
except Exception as e:
print("IMU Error!")
if len(data) != 0:
print(str(data[-1]))
else:
print("Empty data recieved!")
print(e)
pass
except KeyboardInterrupt:
print("IMU closed manually!")
sys.exit()