ควบคุม#
การแนะนำ#
การควบคุมใน Python ช่วยให้คุณบอกหุ่นยนต์ได้ว่าเมื่อใดควรรอ เมื่อใดควรทำซ้ำการกระทำ วิธีการตัดสินใจ และเมื่อใดควรยุติโครงการ
ด้านล่างนี้คือรายการของตัวควบคุมที่มีให้ใช้งาน รวมถึงเมธอดและคำหลักหลักของ Python:
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
ส่วนประกอบ |
คำอธิบาย |
|---|---|
|
นิพจน์หรือตัวแปรที่จะถูกตรวจสอบเมื่อคำสั่งทำงาน ถ้าเป็น |
# 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รันบล็อก ถ้าเงื่อนไขเป็นTrueelifตรวจสอบเงื่อนไขเพิ่มเติมเฉพาะเมื่อเงื่อนไขก่อนหน้าทั้งหมดเป็น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