메인
home
소프트웨어
home

[피코-미디어파이프-시리얼통신][강동고][이예준]

실시간 확인

1. 시리얼 통신으로 피코 제어

1 : 내부 LED 켜기 2: 내부 LED 끄기 3: 내부 LED 깜빡이기 4: 서보모터 1 움직이기 5: 서보모터 2 움직이기 6: 서보모터 1,2 동시에 움직이기 7 :서보모터 1,2 모두 동작 그만 8: 서보모터 1,2 초기 각도로 돌아가기 (90도)
실습 1. 내부 LED 제어 코드
from machine import Pin import sys # 내장 LED는 일반적으로 GP25에 연결되어 있음 led = Pin("LED", Pin.OUT) print("UART LED Controller Ready") print("Type 'on' to turn on the LED, 'off' to turn it off") while True: try: # 한 줄 입력 받기 (터미널에서) command = sys.stdin.readline().strip().lower() if command == "on": led.value(1) print("LED ON") elif command == "off": led.value(0) print("LED OFF") else: print("Unknown command. Type 'on' or 'off'.") except Exception as e: print("Error:", e)
Python
복사
from machine import Pin, PWM import sys from time import sleep # 내장 LED 설정 led = Pin("LED", Pin.OUT) # 서보모터 핀 및 PWM 설정 servo1 = PWM(Pin(15)) servo2 = PWM(Pin(17)) servo1.freq(50) servo2.freq(50) # 각도 상태 변수 초기화 angle1 = 90 angle2 = 90 # 각도 → 듀티사이클 변환 함수 def angle_to_duty(angle): return int(1638 + (angle / 180.0) * (8192 - 1638)) # 현재 각도에 맞게 서보모터 위치 갱신 def update_servos(): servo1.duty_u16(angle_to_duty(angle1)) servo2.duty_u16(angle_to_duty(angle2)) # 초기 위치로 이동 update_servos() print("시리얼 통신 메뉴") print("1: LED 켜기 | 2: LED 끄기 | 3: LED 깜빡이기") print("4: 서보1 움직이기 | 5: 서보2 움직이기") print("6: 서보1,2 동시 | 7: 서보 정지 | 8: 서보 초기화") while True: try: command = input("명령어 입력 (1~8): ").strip() if command == "1": led.value(1) print("LED ON") elif command == "2": led.value(0) print("LED OFF") elif command == "3": for _ in range(5): led.toggle() sleep(0.3) print("LED 깜빡이기 완료") elif command == "4": angle1 = min(180, angle1 + 5) update_servos() print("서보1 ↑ 각도:", angle1) elif command == "5": angle2 = min(180, angle2 + 5) update_servos() print("서보2 ↑ 각도:", angle2) elif command == "6": angle1 = min(180, angle1 + 5) angle2 = min(180, angle2 + 5) update_servos() print("서보1,2 ↑ 각도:", angle1, angle2) elif command == "7": servo1.deinit() servo2.deinit() print("서보모터 정지") elif command == "8": angle1 = 90 angle2 = 90 servo1 = PWM(Pin(16)) servo2 = PWM(Pin(17)) servo1.freq(50) servo2.freq(50) update_servos() print("서보모터 초기화 (90도)") else: print("잘못된 명령입니다. 1~8 중 입력해주세요.") except Exception as e: print("오류 발생:", e)
Python
복사

2. 미디어파이프로 피코 제어

웹캠 버튼 제어 코드
import cv2 import mediapipe as mp mp_hands = mp.solutions.hands mp_drawing = mp.solutions.drawing_utils hands = mp_hands.Hands( max_num_hands=1, min_detection_confidence=0.5, min_tracking_confidence=0.5 ) class Button: def __init__(self, x, y, w, h, text): self.x, self.y = x, y self.w, self.h = w, h self.text = text self.clicked = False def draw(self, img): color = (0, 255, 0) if self.clicked else (255, 0, 0) cv2.rectangle(img, (self.x, self.y), (self.x + self.w, self.y + self.h), color, 2) cv2.putText(img, self.text, (self.x + 10, self.y + self.h - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, color, 2) def is_hover(self, px, py): return self.x <= px <= self.x + self.w and self.y <= py <= self.y + self.h buttons = [] button_size = 100 for i in range(3): for j in range(3): num = str(i * 3 + j + 1) btn = Button(x=50 + j * (button_size + 20), y=50 + i * (button_size + 20), w=button_size, h=button_size, text=num) buttons.append(btn) cap = cv2.VideoCapture(0) while cap.isOpened(): success, frame = cap.read() if not success: break frame = cv2.flip(frame, 1) h, w, _ = frame.shape image_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) results = hands.process(image_rgb) index_finger = None if results.multi_hand_landmarks: hand_landmarks = results.multi_hand_landmarks[0] mp_drawing.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS) landmark = hand_landmarks.landmark[8] index_finger = (int(landmark.x * w), int(landmark.y * h)) cv2.circle(frame, index_finger, 8, (0, 255, 255), -1) for btn in buttons: if index_finger and btn.is_hover(*index_finger): if not btn.clicked: print(f"버튼 {btn.text} 클릭됨") btn.clicked = True else: btn.clicked = False btn.draw(frame) cv2.imshow("Hand Button UI", frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows() hands.close()
Python
복사
[과제명][학교][이름] 바꿔주세요, 과제태그를 선생님 설명 듣고 넣어주세요.
[과제명][학교][이름] 바꿔주세요, 과제태그를 선생님 설명 듣고 넣어주세요.