-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera_move_save.py
More file actions
194 lines (163 loc) · 5.91 KB
/
Copy pathcamera_move_save.py
File metadata and controls
194 lines (163 loc) · 5.91 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import os
import Jetson.GPIO as GPIO
import time
import cv2
import keyboard
import threading
from datetime import datetime
import subprocess
# GPIO 핀에서 PWM 기능을 활성화하는 함수
def enable_pwm_on_pins():
# Pin 32 (PWM0) 활성화
subprocess.run(["sudo", "busybox", "devmem", "0x700031fc", "32", "0x45"])
subprocess.run(["sudo", "busybox", "devmem", "0x6000d504", "32", "0x2"])
# Pin 33 (PWM2) 활성화
subprocess.run(["sudo", "busybox", "devmem", "0x70003248", "32", "0x46"])
subprocess.run(["sudo", "busybox", "devmem", "0x6000d100", "32", "0x00"])
# 폴더 생성 (출력 파일 저장 경로)
output_dir = "./jwj"
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# GPIO 핀 설정
servo_pin = 33
dc_motor_pwm_pin = 32 # DC 모터 속도 제어 핀
dc_motor_dir_pin1 = 29 # DC 모터 방향 제어 핀 1
dc_motor_dir_pin2 = 31 # DC 모터 방향 제어 핀 2
# PWM을 사용할 핀을 초기화하기 전에 활성화
enable_pwm_on_pins()
# GPIO 설정
GPIO.setmode(GPIO.BOARD)
GPIO.setup(servo_pin, GPIO.OUT)
GPIO.setup(dc_motor_pwm_pin, GPIO.OUT)
GPIO.setup(dc_motor_dir_pin1, GPIO.OUT)
GPIO.setup(dc_motor_dir_pin2, GPIO.OUT)
# PWM 설정
servo = GPIO.PWM(servo_pin, 50)
dc_motor_pwm = GPIO.PWM(dc_motor_pwm_pin, 1000)
servo.start(0)
dc_motor_pwm.start(0)
# 서보 모터 각도 설정 함수
def set_servo_angle(angle):
if angle < 0:
angle = 0
elif angle > 180:
angle = 180
duty_cycle = 2 + (angle / 18)
servo.ChangeDutyCycle(duty_cycle)
time.sleep(0.1)
servo.ChangeDutyCycle(0)
# DC 모터 방향과 속도 조절 함수
def set_dc_motor(speed, direction):
if direction == "forward":
GPIO.output(dc_motor_dir_pin1, GPIO.HIGH)
GPIO.output(dc_motor_dir_pin2, GPIO.LOW)
elif direction == "backward":
GPIO.output(dc_motor_dir_pin1, GPIO.LOW)
GPIO.output(dc_motor_dir_pin2, GPIO.HIGH)
dc_motor_pwm.ChangeDutyCycle(speed)
# 카메라 처리 쓰레드
class CameraHandler(threading.Thread):
def __init__(self):
super().__init__()
self.cap = cv2.VideoCapture(1)
self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
self.recording = False
self.out = None
self.running = True
def run(self):
while self.running:
ret, frame = self.cap.read()
if not ret:
print("프레임을 읽을 수 없습니다.")
break
cv2.imshow('Webcam Feed', frame)
if self.recording and self.out is not None:
self.out.write(frame)
# 'q' 키로 종료
if cv2.waitKey(1) & 0xFF == ord('q'):
self.running = False
self.cleanup()
def start_recording(self):
# datetime을 이용한 파일 이름 생성
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
output_file = os.path.join(output_dir, f"output_{timestamp}.avi")
self.out = cv2.VideoWriter(output_file, cv2.VideoWriter_fourcc(*'H264'), 30.0, (1280, 720))
self.recording = True
print(f"Recording started: {output_file}")
def stop_recording(self):
if self.recording and self.out is not None:
self.out.release()
self.recording = False
print("Recording stopped.")
def cleanup(self):
if self.recording and self.out is not None:
self.out.release()
self.cap.release()
cv2.destroyAllWindows()
# 모터 제어 쓰레드
class MotorHandler(threading.Thread):
def __init__(self):
super().__init__()
self.servo_angle = 90
set_servo_angle(self.servo_angle)
self.running = True
def run(self):
while self.running:
# 서보 모터 제어
if keyboard.is_pressed('0'):
self.servo_angle = 90
set_servo_angle(self.servo_angle)
print("Servo angle reset to 90 degrees.")
elif keyboard.is_pressed('left'):
self.servo_angle -= 5
if self.servo_angle < 0:
self.servo_angle = 0
set_servo_angle(self.servo_angle)
print(f"Left pressed. Servo angle: {self.servo_angle} degrees")
elif keyboard.is_pressed('right'):
self.servo_angle += 5
if self.servo_angle > 180:
self.servo_angle = 180
set_servo_angle(self.servo_angle)
print(f"Right pressed. Servo angle: {self.servo_angle} degrees")
# DC 모터 제어
if keyboard.is_pressed('up'):
set_dc_motor(50, "forward")
print("DC motor moving forward...")
elif keyboard.is_pressed('down'):
set_dc_motor(50, "backward")
print("DC motor moving backward...")
else:
set_dc_motor(0, "forward")
time.sleep(0.1) # CPU 점유율 방지
def stop(self):
self.running = False
# 메인 함수
def main():
camera_handler = CameraHandler()
motor_handler = MotorHandler()
try:
camera_handler.start()
motor_handler.start()
print("Press 'r' to start/stop recording. Press 'q' to exit.")
while camera_handler.running:
if keyboard.is_pressed('r'):
if camera_handler.recording:
camera_handler.stop_recording()
else:
camera_handler.start_recording()
time.sleep(0.2) # 키 입력 중복 방지
except KeyboardInterrupt:
print("프로그램 종료 중...")
finally:
camera_handler.running = False
motor_handler.stop()
camera_handler.join()
motor_handler.join()
servo.stop()
dc_motor_pwm.stop()
GPIO.cleanup()
print("정상적으로 종료되었습니다.")
if __name__ == "__main__":
main()