root layout

패스트캠퍼스

  1. 강의 질문
  2. AI TECH

"05. 롤,피치,요 각도값 Test by Python 코드" 파트 부분 오류 (2)

2026.02.13 00:21 작성

강사님 아까 ""05. 롤,피치,요 각도값 Test by Python 코드" 파트 부분 오류"질문 했던 사람입니다. 다름이 아니라 제가 다시 해봐도 잘 안돼서 그런지 일단 제가 여기 있는 코드들하고 printf 상태을 올려드리겠습니다. 다시한번 도움 주시면 감사하겠습니다.


파이썬 코드:

(파이썬 코드 시작)

import serial

import matplotlib.pyplot as plt

import threading

import time



# 포트와 속도 설정

PORT_1 = "COM5"  # 첫 번째 아두이노

BAUD_RATE = 57600



# 시리얼 포트 열기

ser1 = serial.Serial(PORT_1, BAUD_RATE, timeout=1)



data1 = []  # 아두이노 1 데이터 저장



def parse_data(line):

    cleaned_line = line.replace(" ", "").lower()  # 공백 제거 및 소문자 변환

    print(f"수신된 원본 데이터: '{line}'")  # 디버깅 출력

    try:

        if "phi_est" in cleaned_line and "theta_est" in cleaned_line and "psi_est" in cleaned_line:

            parts = line.split(",")  # 쉼표로 분리

            phi = float(parts[0].split("=")[1].strip())

            theta = float(parts[1].split("=")[1].strip())

            psi = float(parts[2].split("=")[1].strip())

            return phi, theta, psi

        else:

            print("필수 키워드 누락")

            return None

    except (IndexError, ValueError) as e:

        print(f"데이터 파싱 에러: {e} - 수신된 데이터: {line}")

        return None



def read_arduino(ser, data_list, label):

    while True:

        try:

            # 안전한 디코딩

            line = ser.readline().decode('utf-8', errors='ignore').strip()

            if line:

                parsed_data = parse_data(line)

                if parsed_data:

                    phi, theta, psi = parsed_data

                    data_list.append((phi, theta, psi))

                    print(f"{label}: phi_est={phi:.2f}, theta_est={theta:.2f}, psi_est={psi:.2f}")

                else:

                    print(f"{label} - 유효하지 않은 데이터 수신: {line}")

        except Exception as e:

            print(f"{label} 에러: {e}")

            break



def plot_graph():

    plt.ion()  # 실시간 플롯 모드 활성화

    fig, ax = plt.subplots()

    while True:

        ax.clear()

        if data1:

            ax.plot([d[0] for d in data1], label="#1 - Phi (Roll)")

            ax.plot([d[1] for d in data1], label="#1 - Theta (Pitch)")

            ax.plot([d[2] for d in data1], label="#1 - Psi (Yaw)")

        ax.legend()

        ax.set_title("Real-Time Data Plot")

        ax.set_xlabel("Time")

        ax.set_ylabel("Value")

        plt.pause(0.1)




# 스레드 실행

thread1 = threading.Thread(target=read_arduino, args=(ser1, data1, "Arduino 1"))

thread1.start()



# 실시간 플롯 실행

try:

    plot_graph()

except KeyboardInterrupt:

    print("종료 중...")

    ser1.close()

    thread1.join()

    plt.close()

(파이선 코드 끝)

스크린샷 2026-02-13 001629.png

printf 구간은 이렇게 나와있습니다.


스크린샷 2026-02-13 001906.png

다시 해봤는데도 계속 이런식으로 나와있습니다.


답변 

연관 질문

커뮤니티 질문보기