유선 센서인 EBIMU-9DOF용 파이썬 예제입니다.
윈도우즈 또는 리눅스 운영체제에서 사용할 수 있습니다.
연결된 시리얼 포트를 통해 ebimu 센서의 데이터를 읽고 항목 단위로 저장한 뒤 출력합니다.
pyserial 모듈을 설치 후 코드를 실행해야 합니다.
pip install pyserial
회전 데이터 외에 센서의 추가적인 데이터(각속도, 가속도..)를 얻으려면 터미널 프로그램(EBTerminal등)에서 추가 데이터 출력이 나오도록 먼저 설정하시기 바랍니다.
[python 예제 1]
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
|
import sys
import serial
import string
#comport_num = "/dev/ttyUSB0"
#comport_num = "COM3"
#comport_baudrate = 115200
comport_num = input("EBIMU Port(e.g., COM3 or /dev/ttyUSB0):")
comport_baudrate = input("Baudrate(e.g., 115200):")
ser = serial.Serial(port=comport_num,baudrate=comport_baudrate)
while 1:
line = ser.readline()
line = line.decode('utf-8')
words = line.split(",") # Fields split
if(-1 < words[0].find('*')) :
words[0]=words[0].replace('*','')
else:
continue
for word in words:
print(word + ' ', end='')
ser.close
|
cs |
[python 예제 2]
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
|
import sys
import serial
import string
import threading
import time
#comport_num = "/dev/ttyUSB0"
#comport_num = "COM3"
#comport_baudrate = 115200
comport_num = input("EBIMU Port(e.g., COM3 or /dev/ttyUSB0):")
comport_baudrate = input("Baudrate(e.g., 115200):")
ser = serial.Serial(port=comport_num,baudrate=comport_baudrate)
sdata = []
def data_parser():
global sdata
while 1:
line = ser.readline()
line = line.decode('utf-8')
words = line.split(",") # Fields split
if(-1 < words[0].find('*')) :
words[0]=words[0].replace('*','')
sdata = list(map(float, words)) # float type
th = threading.Thread(target=data_parser)
th.start()
time.sleep(0.5)
while 1:
#print(len(sdata),end="")
print(sdata)
time.sleep(0.01)
ser.close
|
cs |