يتحكم#

مقدمة#

تتيح لك خاصية التحكم في بايثون إخبار الروبوت متى ينتظر، ومتى يكرر الإجراءات، وكيفية اتخاذ القرارات، ومتى ينهي المشروع.

فيما يلي قائمة بالعناصر المتاحة، بما في ذلك الطرق والكلمات الرئيسية الأساسية في لغة بايثون:

  • wait – يوقف المشروع مؤقتًا لفترة زمنية محددة.

  • for – يكرر الكود لكل عنصر في التسلسل.

  • if – يقوم بتشغيل التعليمات البرمجية إذا كان الشرط صحيحًا.

  • if/else – يقوم بتشغيل كود مختلف اعتمادًا على شرط معين.

  • if/elif/else – يتحقق من عدة شروط بالترتيب.

  • while – يكرر الكود طالما أن الشرط صحيح.

  • break – يخرج من الحلقة فورًا.

  • quit – ينهي المشروع قيد التشغيل.

  • pass – عنصر نائب يستخدم عندما لا تكون هناك حاجة إلى إجراء.

wait#

wait توقف مؤقتًا لفترة زمنية محددة قبل الانتقال إلى سطر التعليمات البرمجية التالي.

Usage:
wait(time, units)

حدود

وصف

time

مقدار الوقت اللازم للانتظار، كعدد صحيح موجب أو عدد عشري.

units

الوحدة التي تمثل وقت الانتظار: MSEC (افتراضي) - أو SECONDS

# 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

عناصر

وصف

value

متغير مؤقت يخزن العنصر الحالي في التكرار.

expression_list

مجموعة العناصر التي يتم المرور عليها (مثل القائمة، السلسلة، النطاق).

# 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

عناصر

وصف

condition

تعبير أو متغير يتم التحقق منه عند تنفيذ العبارة. إذا كانت قيمته True، يتم تنفيذ الكود الموجود داخل كتلة if ؛ وإذا كانت قيمته False، يتم تخطي الكتلة.

# 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

عناصر

وصف

condition

An expression or variable that is checked when the statement runs. If it is True, the code inside the if block executes; if it is False, the code inside the else block executes instead.

# 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 يتحقق من الشروط الإضافية فقط إذا كانت جميع الشروط السابقة خاطئة elifيمكن استخدام عبارات elif متعددة False.

  • else يتم تشغيل كتلتها فقط إذا لم يكن أي من الشروط السابقة صحيحًا True.

طريقة الاستخدام:

if condition:
    pass
elif condition:
    pass
else:
    pass

عناصر

وصف

condition

تعبير أو متغير يتم التحقق منه عند تنفيذ العبارة. الشرط الأول الذي تكون قيمته True يحدد الكتلة التي يتم تنفيذها؛ إذا لم تكن أي من القيم True، يتم تنفيذ الكتلة else.

# 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 repeatedly runs code as long as the condition is True. It can also be used like a “forever” by using True as the condition, or a “wait until” by adding not to the condition, as shown in the examples below.

طريقة الاستخدام:

while condition:
    pass

عناصر

وصف

condition

تعبير أو متغير يتم التحقق منه قبل كل تكرار. إذا كانت قيمته Trueخطأ)، تستمر الحلقة؛ وإذا كانت قيمته Falseخطأ)، تتوقف الحلقة.

# 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