제어#
소개#
파이썬을 사용한 제어를 통해 로봇에게 언제 기다려야 하는지, 언제 동작을 반복해야 하는지, 어떻게 결정을 내려야 하는지, 그리고 언제 프로젝트를 종료해야 하는지를 알려줄 수 있습니다.
다음은 메서드 및 핵심 파이썬 키워드를 포함하여 사용 가능한 컨트롤 목록입니다.
wait– 지정된 시간 동안 프로젝트를 일시 중지합니다.for– 시퀀스의 각 항목에 대해 코드를 반복합니다.if– 조건이 참이면 코드를 실행합니다.if/else– 조건에 따라 다른 코드를 실행합니다.if/elif/else– 여러 조건을 순서대로 확인합니다.while– 조건이 참인 동안 코드를 반복합니다.break– 루프를 즉시 종료합니다.quit– 실행 중인 프로젝트를 종료합니다.pass– 작업이 필요하지 않을 때 사용되는 플레이스홀더.
wait#
wait 합니다.
Usage:
wait(time, units)
매개변수 |
설명 |
|---|---|
|
기다려야 하는 시간을 양의 정수 또는 소수로 나타내십시오. |
|
대기 시간을 나타내는 단위: |
# Fly for 1 second.
drone.take_off(climb_to=500)
wait(1, SECONDS)
drone.land()
for#
for 특정 횟수만큼 일련의 작업을 반복합니다. for 목록, 튜플, 사전, 세트, 문자열의 항목을 반복하거나 range를 사용하여 지정된 루프 횟수만큼 반복하는 데 사용할 수 있습니다.
사용 횟수:
for value in expression_list:
pass
구성 요소 |
설명 |
|---|---|
|
반복문 내 현재 요소를 저장하는 임시 변수입니다. |
|
반복 대상이 되는 요소들의 모음(예: 리스트, 문자열, 범위). |
# Move back and forth four times then land.
drone.take_off(climb_to=500)
for i in range(4):
drone.move_for(direction=0, distance=500, velocity=50, units=MM)
wait(1, SECONDS)
drone.move_for(direction=180, distance=500, velocity=50, units=MM)
wait(1, SECONDS)
drone.land()
# Print each item in a list.
colors = ["Red", "Green", "Blue"]
for color in colors:
controller.screen.print(color)
controller.screen.next_row()
if#
if 조건이 참이면 들여쓰기된 코드 블록을 실행합니다. True.
용법:
if condition:
pass
구성 요소 |
설명 |
|---|---|
|
문장이 실행될 때 검사되는 표현식 또는 변수입니다. |
# Take a picture when button 5 is pressed.
drone.take_off(climb_to=500)
while True:
# Move with controller
drone.move_with_vectors(
forward=controller.axis4.position(),
rightward=controller.axis3.position(),
upward=controller.axis1.position(),
rotation=controller.axis2.position()
)
if controller.button5.pressing():
drone.camera.capture_image(FORWARD_CAMERA)
wait(5, MSEC)
if/else#
if 이고 else . 조건이 True False 에 따라 들여쓰기된 코드 블록 중 어느 것이 실행될지 결정합니다.
용법:
if condition:
pass
else:
pass
구성 요소 |
설명 |
|---|---|
|
An expression or variable that is checked when the statement runs. If it is |
# Climb when the left joystick is moved up.
drone.take_off(climb_to=500)
while True:
if controller.axis1.position() > 0:
drone.climb(direction=UP, velocity=50)
else:
drone.hover()
wait(5, MSEC)
if/elif/else#
if/elif/else 구조는 조건에 따라 어떤 들여쓰기된 코드 블록이 실행될지 선택합니다.
if이고, 조건이True.elif이전 조건이 모두 거짓인 경우에만 추가 조건을 확인합니다.False여러 개의elif문을 사용할 수 있습니다.else이전 조건 중 어느 것도 참이 아닌 경우에만 해당 블록을 실행합니다.True.
용법:
if condition:
pass
elif condition:
pass
else:
pass
구성 요소 |
설명 |
|---|---|
|
문장이 실행될 때 검사되는 표현식 또는 변수입니다. 첫 번째 조건이 참 |
# Move the drone up or down based on the position of the joystick.
drone.take_off(climb_to=500)
while True:
position = controller.axis1.position()
if position > 0:
drone.climb(direction=UP, velocity=50)
elif position < 0:
drone.climb(direction=DOWN, velocity=50)
else:
drone.hover()
wait(5, MSEC)
while#
while 조건이 True코드를 반복적으로 실행합니다. True 을 조건으로 사용하면 “무한 실행”처럼, not 조건에 추가하면 아래 예시와 같이 “조건이 참이 될 때까지 대기”처럼 사용할 수 있습니다.
용법:
while condition:
pass
구성 요소 |
설명 |
|---|---|
|
각 반복 전에 확인하는 표현식 또는 변수입니다. 값이 |
# Hover at 200 mm.
drone.take_off(climb_to=500)
while drone.range.get_distance(DOWNWARD_RANGE, MM) > 200:
drone.climb(direction=DOWN, velocity=10)
drone.hover()
wait(3, SECONDS)
drone.land()
break#
break 루프를 즉시 종료합니다. break 무한 루프에 빠질 수 있는 루프를 종료하는 데 사용할 수 있습니다.
# Land when button 5 is pressed.
drone.take_off(climb_to=500)
while True:
# Move with controller
drone.move_with_vectors(
forward=controller.axis4.position(),
rightward=controller.axis3.position(),
upward=controller.axis1.position(),
rotation=controller.axis2.position()
)
if controller.button5.pressing():
break
wait(5, MSEC)
drone.land()
quit#
quit 실행 중인 프로젝트를 종료합니다.
Usage:
quit()
매개변수 |
설명 |
|---|---|
이 메서드에는 매개변수가 없습니다. |
# Land when button 5 is pressed.
drone.take_off(climb_to=500)
while True:
# Move with controller
drone.move_with_vectors(
forward=controller.axis4.position(),
rightward=controller.axis3.position(),
upward=controller.axis1.position(),
rotation=controller.axis2.position()
)
if controller.button5.pressing():
quit()
wait(5, MSEC)
drone.land()
pass#
pass 은 향후 코드를 위한 자리 표시자이며, 빈 반복문, 조건문 및 함수에서 발생하는 오류를 방지하는 데 사용할 수 있습니다.
if condition:
pass
def function():
pass