skysparrow avatar

파이썬 소켓통신 <=> L3G4200D 자이로센서 값 받기

skysparrow

Published: 20 Jan 2018 › Updated: 20 Jan 2018파이썬 소켓통신 <=> L3G4200D 자이로센서 값 받기

파이썬 소켓통신 <=> L3G4200D 자이로센서 값 받기

파이썬 소켓통신 <=> L3G4200D 자이로센서 값 받기

  1. 자이로센서를 라즈베리 파이에 달아서 로컬에서 값을 받으려 할경우 쓰레드에서 제한적일수있다.

  2. 별로 소켓통신을 통해 자이로센서의 주기적 값을 받아오도록 한다.

  3. 서버+클라이언트 모두 아이피,포트를 마추어 실행하면되며
    서버쪽 출력은 접근정보 만 나타남.

    클라이언트 접근후 라즈베리 파이에 연결된 자이로센서의 움직임 값이 출력됨.

    L3G4200D 자이로센서 강좌 :

       http://icarusx.com/icarusx/index.php?BOARD=icarusx_raspberry#stMode=VIEW&stPage=1&stIdx=2937
    

#######################################

서버 + 자이로센서

파일명 : p_gryo_socket_s.py

실 행 : p_gryo_socket_s.py 아이피 포트

#######################################

#!/usr/bin/python

from socket import *
from time import ctime
from time import sleep
#import smbus to access i2c port
import smbus
import string
import sys

#converts 16 bit two"s compliment reading to signed int
def getSignedNumber(number):
if number & (1 << 15):
return number | ~65535
else:
return number & 65535

i2c_bus=smbus.SMBus(1) #open /dev/i2c-1
i2c_address=0x69 #i2c slave address of the L3G4200D
i2c_bus.write_byte_data(i2c_address,0x20,0x0F) #normal mode and all axes on to control reg1
i2c_bus.write_byte_data(i2c_address,0x23,0x20) #full 2000dps to control reg4

HOST = sys.argv[1]
PORT = int(sys.argv[2])
BUFSIZ = 1024
ADDR = (HOST, PORT)

tcpSerSock = socket(AF_INET, SOCK_STREAM)
tcpSerSock.bind(ADDR)
tcpSerSock.listen(5)

while True:
print "waiting for connection..."
tcpCliSock, addr = tcpSerSock.accept()
print "...connected from:", addr

while True:
i2c_bus.write_byte(i2c_address,0x28)
X_L = i2c_bus.read_byte(i2c_address)
i2c_bus.write_byte(i2c_address,0x29)
X_H = i2c_bus.read_byte(i2c_address)
X = X_H << 8 | X_L

  i2c_bus.write_byte(i2c_address,0x2A)
  Y_L = i2c_bus.read_byte(i2c_address)
  i2c_bus.write_byte(i2c_address,0x2B)
  Y_H = i2c_bus.read_byte(i2c_address)
  Y = Y_H << 8 | Y_L

  i2c_bus.write_byte(i2c_address,0x2C)
  Z_L = i2c_bus.read_byte(i2c_address)
  i2c_bus.write_byte(i2c_address,0x2D)
  Z_H = i2c_bus.read_byte(i2c_address)
  Z = Z_H << 8 | Z_L

  X = getSignedNumber(X)
  Y = getSignedNumber(Y)
  Z = getSignedNumber(Z)

tcpCliSock.send("%s,%s,%s" % (string.rjust(X, 10),string.rjust(Y, 10),string.rjust(Z, 10)))

  tcpCliSock.send("%s,%s,%s" % (X,Y,Z))
  sleep(0.02)

tcpCliSock.close()
tcpSerSock.close()

#######################################

클라이언트

파일명 : p_gryo_socket_c.py

실 행 : p_gryo_socket_c.py 아이피 포트

#######################################

#!/usr/bin/env python

from socket import *
import sys

HOST = sys.argv[1]
PORT = int(sys.argv[2])
BUFSIZ = 1024
ADDR = (HOST, PORT)

tcpCliSock = socket(AF_INET, SOCK_STREAM)
tcpCliSock.connect(ADDR)

while True:
data = tcpCliSock.recv(BUFSIZ)
print data," "

tcpCliSock.close()

Leave 파이썬 소켓통신 <=> L3G4200D 자이로센서 값 받기 to:

Written by

Read more #lg34200d posts


Best Posts From skysparrow

We have not curated any of skysparrow's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From skysparrow