728x90

이제 관성 센서로 받는

 

가속도, 자이로계 값들을 실시간 플로팅 예제

 

 

 

 

from zumi.zumi import Zumi
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation




"""
common functions
"""
def get_mpu_val(zumi):
    mpus = zumi.get_all_mpu_data()
    acc_x = mpus[0]
    acc_y = mpus[1]
    acc_z = mpus[2]
    gyro_x = mpus[3]
    gyro_y = mpus[4]
    gyro_z = mpus[5]
    mpu_val = np.array([[acc_x, acc_y, acc_z, gyro_x, gyro_y, gyro_z]])
    return mpu_val


def new_data_insert(mpu, mpu_val):
    #push
    mpu = np.append(mpu, mpu_val, axis=0)
    #pop
    mpu = np.delete(mpu, 0, 0)


    return mpu




"""
initialize mpu data
"""
def init():
    idx = 0
    mpu = np.zeros((100, 6))
    t = np.linspace(0, 1, 100)

    while idx < 100:
        mpu_val = get_mpu_val(zumi)
        mpu = new_data_insert(mpu, mpu_val)
        idx = idx + 1
    return t, mpu





"""
functions for animation
"""
def ln_set_data(mpu):
    ln0.set_data(t, mpu[:,0])
    ln1.set_data(t, mpu[:,1])
    ln2.set_data(t, mpu[:,2])
    ln3.set_data(t, mpu[:,3])
    ln4.set_data(t, mpu[:,4])
    ln5.set_data(t, mpu[:,5])


# change y limit dynamically
def change_ylim(ax, mpu):
    ax[0, 0].set_ylim(min(mpu[:,0]), max(mpu[:,0]))
    ax[1, 0].set_ylim(min(mpu[:,1]), max(mpu[:,1]))
    ax[2, 0].set_ylim(min(mpu[:,2]), max(mpu[:,2]))
    ax[0, 1].set_ylim(min(mpu[:,3]), max(mpu[:,3]))
    ax[1, 1].set_ylim(min(mpu[:,4]), max(mpu[:,4]))
    ax[2, 1].set_ylim(min(mpu[:,5]), max(mpu[:,5]))


def mpu_init_plot(ax, mpu):
    ln0, = ax[0, 0].plot(t,mpu[:,0], 'r')
    ax[0, 0].grid(True)
    ax[0, 0].set_title("acc x")
    ln1, = ax[1, 0].plot(t,mpu[:,1], 'g')
    ax[1, 0].grid(True)
    ax[1, 0].set_title("acc y")
    ln2, = ax[2, 0].plot(t,mpu[:,2], 'b')
    ax[2, 0].grid(True)
    ax[2, 0].set_title("acc z")
    ln3, = ax[0, 1].plot(t,mpu[:,3], 'r')
    ax[0, 1].grid(True)
    ax[0, 1].set_title("gyro x")
    ln4, = ax[1, 1].plot(t,mpu[:,4], 'g')
    ax[1, 1].grid(True)
    ax[1, 1].set_title("gyro y")
    ln5, = ax[2, 1].plot(t,mpu[:,5], 'b')
    ax[2, 1].grid(True)
    ax[2, 1].set_title("gyro z")
    return ln0, ln1, ln2, ln3, ln4, ln5



def update(i):
    global mpu
    mpu_val = get_mpu_val(zumi)
    mpu = new_data_insert(mpu, mpu_val)

    ln_set_data(mpu)
    change_ylim(ax, mpu)
    return ln0, ln1, ln2, ln3, ln4, ln5




#initialize
zumi = Zumi()
t, mpu = init()

#plot
fig, ax = plt.subplots(3,2)
ln0, ln1, ln2, ln3, ln4, ln5 = mpu_init_plot(ax, mpu)

#animation
ani = FuncAnimation(fig, update, frames=t, blit=True)
plt.show()

 

 

다음엔 필터링

 

평균, 평균이동, 저주파 통과, 칼만 필터 순으로

 

 

 

 

 

 

 

 

300x250

'로봇 > 로봇' 카테고리의 다른 글

zumi - 22. 저주파 통과 필터  (0) 2020.08.25
zumi - 21. 평균 필터  (0) 2020.08.25
zumi - 19. 주미 에지 영상  (0) 2020.08.25
zumi - 18. 주미 흑백 영상 스트리밍  (0) 2020.08.25
zumi - 17. mpu 데이터 플로팅  (0) 2020.08.25
728x90

ref : https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_gradients/py_gradients.html

 

라플라시안 영상, 소벨 x/ 소벨 y 영상

 

 

 

 

 

import numpy as np
import cv2

cap = cv2.VideoCapture(0)
while(True):
    ret, frame = cap.read()
    frame = cv2.flip(frame, 0)
    rsz = cv2.resize(frame, dsize=(320,240))
    gray = cv2.cvtColor(rsz, cv2.COLOR_BGR2GRAY)

    laplacian = cv2.Laplacian(gray,cv2.CV_64F)
    sobelx = cv2.Sobel(gray,cv2.CV_64F,1,0,ksize=5)
    sobely = cv2.Sobel(gray,cv2.CV_64F,0,1,ksize=5)

    res = np.concatenate((laplacian, sobelx, sobely), axis=1)
    cv2.imshow('res',res)
    if cv2.waitKey(20) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()
300x250

'로봇 > 로봇' 카테고리의 다른 글

zumi - 21. 평균 필터  (0) 2020.08.25
zumi - 20. 실시간 mpu 플로팅  (0) 2020.08.25
zumi - 18. 주미 흑백 영상 스트리밍  (0) 2020.08.25
zumi - 17. mpu 데이터 플로팅  (0) 2020.08.25
zumi - 16. raw acc/gyro 받기  (0) 2020.08.25
728x90

주미 충전시키는 동안 잠시 영상을 진행

 

 

리사이즈 후 흑백 채널로 변경

import numpy as np
import cv2

cap = cv2.VideoCapture(0)
while(True):
    ret, frame = cap.read()
    frame = cv2.flip(frame, 0)
    rsz = cv2.resize(frame, dsize=(320,240))

    res = cv2.cvtColor(rsz, cv2.COLOR_BGR2GRAY)
    cv2.imshow('res',res)
    if cv2.waitKey(20) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

 

 

 

 

 

 

300x250

'로봇 > 로봇' 카테고리의 다른 글

zumi - 20. 실시간 mpu 플로팅  (0) 2020.08.25
zumi - 19. 주미 에지 영상  (0) 2020.08.25
zumi - 17. mpu 데이터 플로팅  (0) 2020.08.25
zumi - 16. raw acc/gyro 받기  (0) 2020.08.25
zumi - 15. XYZ 축 가속도 플로팅  (0) 2020.08.25
728x90

지금까지 플로팅, mpu 전체 데이터 받기 등 마쳤습니다.

 

 

이번에는 전체 MPU raw 데이터를 플로팅 하는 예제를 정리하였습니다.

 

그 동안 뒤죽박죽 이던 코드를 함수로 나눠서 조금 정리했습니다.

 

 

 

from zumi.zumi import Zumi
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation




"""
common functions
"""
def get_mpu_val(zumi):
    mpus = zumi.get_all_mpu_data()
    acc_x = mpus[0]
    acc_y = mpus[1]
    acc_z = mpus[2]
    gyro_x = mpus[3]
    gyro_y = mpus[4]
    gyro_z = mpus[5]
    mpu_val = np.array([[acc_x, acc_y, acc_z, gyro_x, gyro_y, gyro_z]])
    return mpu_val


def new_data_insert(mpu, mpu_val):
    #push
    mpu = np.append(mpu, mpu_val, axis=0)
    #pop
    mpu = np.delete(mpu, 0, 0)

    return mpu




"""
initialize mpu data
"""
def init():
    idx = 0
    mpu = np.zeros((100, 6))
    t = np.linspace(0, 1, 100)

    while idx < 100:
        mpu_val = get_mpu_val(zumi)
        mpu = new_data_insert(mpu, mpu_val)
        idx = idx + 1
    return t, mpu





"""
functions for animation
"""
def mpu_init_plot(ax, mpu):
    ln0, = ax[0].plot(t,mpu[:,0], 'r')
    ax[0].grid(True)
    ax[0].set_title("acc x")
    ln1, = ax[1].plot(t,mpu[:,1], 'g')
    ax[1].grid(True)
    ax[1].set_title("acc y")
    ln2, = ax[2].plot(t,mpu[:,2], 'b')
    ax[2].grid(True)
    ax[2].set_title("acc z")
    ln3, = ax[3].plot(t,mpu[:,3], 'r')
    ax[3].grid(True)
    ax[3].set_title("gyro x")
    ln4, = ax[4].plot(t,mpu[:,4], 'g')
    ax[4].grid(True)
    ax[4].set_title("gyro y")
    ln5, = ax[5].plot(t,mpu[:,5], 'b')
    ax[5].grid(True)
    ax[5].set_title("gyro z")
    return ln0, ln1, ln2, ln3, ln4, ln5



#initialize
zumi = Zumi()
t, mpu = init()

#plot
fig, ax = plt.subplots(6,1)
ln0, ln1, ln2, ln3, ln4, ln5 = mpu_init_plot(ax, mpu)
plt.show()

 

 

 

다음에는 전체 데이터 animation을 하겠습니다.

300x250

'로봇 > 로봇' 카테고리의 다른 글

zumi - 19. 주미 에지 영상  (0) 2020.08.25
zumi - 18. 주미 흑백 영상 스트리밍  (0) 2020.08.25
zumi - 16. raw acc/gyro 받기  (0) 2020.08.25
zumi - 15. XYZ 축 가속도 플로팅  (0) 2020.08.25
zumi - 14. x축 가속도 플로팅  (0) 2020.08.24
728x90

 

 

주미 lib에서는

 

각도를 반환해주는 update_angles() 함수가 있지만

 

raw mpu데이터가 아니라 필터 처리된

 

조금 다른 값들을 반환해주고 있습니다.

 

그래서 지난번과 마찬가지로

 

주미 스트립트를 조금 고치겠습니다.

 

 

 

 

    def get_all_mpu_data(self):
        self.mpu_list = self.mpu.read_all_MPU_data()
        x_acc = self.mpu_list[0]
        y_acc = self.mpu_list[1]
        z_acc = self.mpu_list[2]
        x_gyro = self.mpu_list[3]
        y_gyro = self.mpu_list[4]
        z_gyro = self.mpu_list[5]
        return [x_acc, y_acc, z_acc, x_gyro, y_gyro, z_gyro]

 

 

지난번에 추가한 get_acc 함수 밑에 넣어주면 이제 가속도, 자이로 전체 값을 사용할수 있게 됩니다.

 

300x250
728x90

목표

- 3개의 서브 플롯으로 xyz축 출력 -> plt.subplots()

- 각 축의 데이터는 실시간 -> FuncAnimation()

- 센서 값의 크기. y축 데이터는 값에 따라 범위가 동적으로 변화해야함 -> set_ylim(min(), max())

 

 

subplots

-figure 내부에 하부 플롯을 그려주는 함수

ref : https://wikidocs.net/14604

 

 

y_limit 동적 변화

- 애니메이션을 통해 y data가 실시간으로 변함

-> 값이 튀거나 안정될때 범위를 고려하여 y축 범위 변화

 matplotlib doc

ref : https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.axes.Axes.set_ylim.html

 동적으로 limt를 바꾸는 방법

ref : https://stackoverflow.com/questions/53423868/matplotlib-animation-how-to-dynamically-extend-x-limits

 

 

 

 

 

구현 코드

from zumi.zumi import Zumi
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

def init():
    idx = 0
    acc = np.zeros((100, 3))
    t = np.linspace(0, 1, 100)

    while idx < 100:
        accs = zumi.get_acc()
        acc_x = accs[0]
        acc_y = accs[1]
        acc_z = accs[2]
        acc_val = np.array([[acc_x, acc_y, acc_z]])

        #push
        acc = np.append(acc, acc_val, axis=0)
        #pop
        acc = np.delete(acc, 0, 0)
        idx = idx + 1

    return t, acc

def update(i):
    global acc
    accs = zumi.get_acc()
    acc_x = accs[0]
    acc_y = accs[1]
    acc_z = accs[2]
    acc_val = np.array([[acc_x, acc_y, acc_z]])

    #push
    acc = np.append(acc, acc_val, axis=0)
    #pop
    acc = np.delete(acc, 0, 0)

    #change y axis data
    ln0.set_data(t, acc[:,0])
    ln1.set_data(t, acc[:,1])
    ln2.set_data(t, acc[:,2])

    # change y limit dynamically
    ax[0].set_ylim(min(acc[:,0]), max(acc[:,0]))
    ax[1].set_ylim(min(acc[:,1]), max(acc[:,1]))
    ax[2].set_ylim(min(acc[:,2]), max(acc[:,2]))
    return ln0, ln1, ln2


#initialize
zumi = Zumi()
t, acc = init()

#plot
fig, ax = plt.subplots(3,1)
ln0, = ax[0].plot(t,acc[:,0], 'r')
ax[0].grid(True)
ax[0].set_title("acc x")
ln1, = ax[1].plot(t,acc[:,1], 'g')
ax[1].grid(True)
ax[1].set_title("acc y")
ln2, = ax[2].plot(t,acc[:,2], 'b')
ax[2].grid(True)
ax[2].set_title("acc z")

#animation
ani = FuncAnimation(fig, update, frames=t, blit=True)
plt.show()

 

 

결과 캡처

 

결과 gif

 

 

 

 

 

 

 

 

300x250

'로봇 > 로봇' 카테고리의 다른 글

zumi - 17. mpu 데이터 플로팅  (0) 2020.08.25
zumi - 16. raw acc/gyro 받기  (0) 2020.08.25
zumi - 14. x축 가속도 플로팅  (0) 2020.08.24
zumi - 13. 영상 블러링 + 이미지 연결  (0) 2020.08.24
zumi - 12. 영상 스트리밍  (0) 2020.08.24
728x90

다음 링크를 참고하여

 

ref : https://matplotlib.org/3.3.0/api/animation_api.html

 

FuncAnimation 함수로 실시간 x축 가속도 값 플로팅

 

from zumi.zumi import Zumi
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

def init():
    idx = 0
    acc_x_lst = np.zeros(100)
    t = np.linspace(0, 1, 100)

    while idx < 100:
        acc = zumi.get_acc()
        acc_x = acc[0]
        acc_x_lst = np.append(acc_x_lst, acc_x)
        acc_x_lst = np.delete(acc_x_lst, 0)
        idx = idx + 1

    return t, acc_x_lst

def update(i):
    global acc_x_lst
    acc = zumi.get_acc()
    acc_x = acc[0]
    acc_x_lst = np.append(acc_x_lst, acc_x)
    acc_x_lst = np.delete(acc_x_lst, 0)
    ln.set_data(t, acc_x_lst)
    return ln,



zumi = Zumi()

t, acc_x_lst = init()


fig, ax = plt.subplots()
ln, = plt.plot(t,acc_x_lst, 'r')
#ax.set_xlim(0, 2*np.pi)
ax.set_ylim(-0.3, 0.3)

ani = FuncAnimation(fig, update, frames=t, blit=True)
plt.show()

 

 

 

중간에 잠시 주미를 흔들어 값이 조금씩 튑니다.

 

 

 

 

 

 

 

 

 

300x250

'로봇 > 로봇' 카테고리의 다른 글

zumi - 16. raw acc/gyro 받기  (0) 2020.08.25
zumi - 15. XYZ 축 가속도 플로팅  (0) 2020.08.25
zumi - 13. 영상 블러링 + 이미지 연결  (0) 2020.08.24
zumi - 12. 영상 스트리밍  (0) 2020.08.24
zumi - 11. opencv 버전 확인  (0) 2020.08.24
728x90

영상에서 잡음 제거를 위해 블러링을 자주 합니다

 

가우시안 필터를 이용한 가우시안 블러링을 사용하고

 

원본과 블러링 결과를 비교할수 있도록

 

영상을 이어보겠습니다.

 

가우시안 블러링

ref : https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_filtering/py_filtering.html#d-convolution-image-filtering

 

이미지 연결

ref : https://stackoverflow.com/questions/7589012/combining-two-images-with-opencv

 

 

import numpy as np
import cv2

cap = cv2.VideoCapture(0)
while(True):
    ret, frame = cap.read()
    frame = cv2.flip(frame, 0)
    rsz = cv2.resize(frame, dsize=(320,240))


    blur = cv2.GaussianBlur(rsz,(10,10),0)

    res = np.concatenate((rsz, blur), axis=1)
    cv2.imshow('res',res)
    if cv2.waitKey(20) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

 

 

 

 

 

 

 

 

 

 

 

 

300x250

'로봇 > 로봇' 카테고리의 다른 글

zumi - 15. XYZ 축 가속도 플로팅  (0) 2020.08.25
zumi - 14. x축 가속도 플로팅  (0) 2020.08.24
zumi - 12. 영상 스트리밍  (0) 2020.08.24
zumi - 11. opencv 버전 확인  (0) 2020.08.24
zumi - 10. xyz 축 가속도 플로팅  (0) 2020.08.24
728x90

opnecv-python tutorial 참고

 

영상 스트리밍

ref : https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_video_display/py_video_display.html

 

영상 자체가 뒤집혀 나오므로 flip 필요

flip

ref: https://crmn.tistory.com/54

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()


    # Display the resulting frame
    frame = cv2.flip(frame)
    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

 

 

 

 

 

 

 

 

 

주미가 너무 힘들어해서 사이즈를 줄여 사용하겠습니다.

 

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()


    # Display the resulting frame
    frame = cv2.flip(frame, 0)
    dst = cv2.resize(frame, dsize=(320,240))
    cv2.imshow('dst',dst)
    if cv2.waitKey(20) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

 

300x250
728x90

잠시 베터리 충전시키는 동안

 

cv2 자료를 정리하겠습니다.

 

설치된 버전 확인 코드

import cv2
print(cv2.__version__)

 

 

300x250

+ Recent posts