Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
sojinj1
ttaracar
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
ttaracar
/
ultrasonic_wave_back.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
45 lines (36 loc) · 1.85 KB
master
Breadcrumbs
ttaracar
/
ultrasonic_wave_back.py
Copy path
Top
File metadata and controls
Code
Blame
45 lines (36 loc) · 1.85 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
# vcc : 5v, trig : 16, echo : 13, gnd : gnd
# echo = 19, trig = 26
# 카트 뒤 초음파 센서로 앞의 장애을 판단해서 모터를 조정
import
paho
.
mqtt
.
client
as
mqtt
import
RPi
.
GPIO
as
GPIO
# RPi.GPIO에 정의된 기능을 GPIO라는 명칭으로 사용
import
time
# time 모듈
GPIO
.
setmode
(
GPIO
.
BCM
)
# GPIO 이름은 BCM 명칭 사용
GPIO
.
setup
(
26
,
GPIO
.
OUT
)
# Trig, 초음파 신호 전송핀 번호 지정 및 출력지정
GPIO
.
setup
(
19
,
GPIO
.
IN
)
# Echo, 초음파 수신하는 수신 핀 번호 지정 및 입력지정
mqttc
=
mqtt
.
Client
()
# MQTT Client 오브젝트 생성
mqttc
.
connect
(
'localhost'
,
1883
)
# MQTT 서버에 연결
print
(
'Press SW or input Ctrl+C to quit'
)
# 메세지 화면 출력
try
:
while
True
:
GPIO
.
output
(
26
,
False
)
time
.
sleep
(
0.5
)
GPIO
.
output
(
26
,
True
)
# 10us 펄스를 내보냄
time
.
sleep
(
0.00001
)
# Python에서 이 펄스는 실제 100us 근처가 됨
GPIO
.
output
(
26
,
False
)
# 하지만 HC-SR04 센서는 이 오차를 받아줌
while
GPIO
.
input
(
19
)
==
0
:
# echo 핀이 OFF 되는 시점을 시작 시간으로 잡음
start
=
time
.
time
()
while
GPIO
.
input
(
19
)
==
1
:
# echo 핀이 다시 ON 되는 시점을 반사파 수신시간으로 잡음
stop
=
time
.
time
()
time_interval
=
stop
-
start
# 초음파가 수신되는 시간으로 거리를 계산
distance
=
time_interval
*
17000
distance
=
round
(
distance
,
2
)
time
.
sleep
(
1
)
print
(
'Back Distance => '
,
distance
,
'cm'
)
if
distance
>
30
:
# 거리가 15cm보다 크면 모터에게 ok, 그렇지 않으면 모터 stop
msg
=
"ok"
else
:
msg
=
"stop"
mqttc
.
publish
(
"motor_back"
,
msg
)
except
KeyboardInterrupt
:
# Ctrl-C 입력 시
GPIO
.
cleanup
()
# GPIO 관련설정 Clear
print
(
'bye~'
)
You can’t perform that action at this time.