يتحكم#

مقدمة#

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

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

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

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

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

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

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

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

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

  • program_stop – إنهاء البرنامج قيد التشغيل.

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

انتظر#

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

Usage:
wait(time, units)

حدود

وصف

time

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

units

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

# Move the arm to the desired position and then blink the signal tower
arm.move_to(40, 140, 210, False)
wait(1, SECONDS)
signal_tower.set_color(SignalTower.BLUE, SignalTower.BLINK)

ل#

يكرر الأمر for مجموعة من الإجراءات عددًا محددًا من المرات. ويمكن استخدام الأمر for للتكرار عبر عناصر القوائم، والصفوف، والقواميس، والمجموعات، والسلاسل النصية، أو لعدد محدد من التكرارات باستخدام range.

الاستخدام:

for value in expression_list:
    pass

عناصر

وصف

value

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

expression_list

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

# Move the arm 4 times by 100 millimeters on the y axis
for index in range(4):
    arm.move_inc(0, 100, 0)

لو#

if يقوم بتشغيل كتلة التعليمات البرمجية ذات المسافة البادئة إذا كان الشرط True.

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

if condition:
    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 block is skipped.

# Display the time until the move is completed
arm.move_to(-100, 200, 100, False)
while True:
    print("\033[2J")
    print(brain.timer_time(SECONDS))
    wait(50, MSEC)
    if arm.is_done():
        brain.timer_reset()

إذا/وإلا#

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.

# Display the time until the move is completed
arm.move_to(-100, 200, 100, False)
while True:
    if arm.is_done():
        brain.timer_reset()
    else:
        print("\033[2J")
        print(brain.timer_time(SECONDS))
    wait(50, MSEC)

إذا/إلا/وإلا#

يحدد هيكل if/elif/else ) أي كتلة من التعليمات البرمجية ذات المسافة البادئة يتم تشغيلها بناءً على الشروط التالية:

  • if يتم تشغيل كتلته إذا كان الشرط True.

  • elif يتحقق من الشروط الإضافية فقط إذا كانت جميع الشروط السابقة خاطئة elifيمكن استخدام عبارات elif متعددة False.

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

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

if condition:
    pass
elif condition:
    pass
else:
    pass

عناصر

وصف

condition

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

# Change the signal tower light if arm can reach to the desired position
while True:
    if arm.can_arm_reach_to(200, 200, 200):
        signal_tower.set_color(SignalTower.BLUE, SignalTower.BLINK)
    elif arm.can_arm_reach_to(300, 300, 300):
        signal_tower.set_color(SignalTower.GREEN, SignalTower.BLINK)
    else:
        signal_tower.set_color(SignalTower.ALL, SignalTower.OFF)
    wait(50, MSEC)

بينما#

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خطأ)، تتوقف الحلقة.

# Display the time until the move is completed
arm.move_to(-100, 200, 100, False)
while True:
    if arm.is_done():
        break
    else:
        print("\033[2J")
        print(brain.timer_time(SECONDS))
    wait(50, MSEC)

# Blink blue light until the move is completed
signal_tower.set_color(SignalTower.BLUE, SignalTower.BLINK)
arm.move_to(-100, 200, 100, False)
while not arm.is_done():
    wait(0.2, SECONDS)
signal_tower.set_color(SignalTower.BLUE, SignalTower.ON)

استراحة#

break يخرج من الحلقة فورًا. break يمكن استخدامه للخروج من الحلقات التي ستستمر في التكرار إلى ما لا نهاية.

# Display the time until the move is completed
arm.move_to(-100, 200, 100, False)
while True:
    if arm.is_done():
        break
    else:
        print("\033[2J")
        print(brain.timer_time(SECONDS))
    wait(50, MSEC)

إيقاف البرنامج#

program_stop ينهي مشروعًا قيد التشغيل.

Usage:
brain.program_stop()

حدود

وصف

لا تحتوي هذه الطريقة على أي معلمات.

# Stop the project until the move is completed
arm.move_to(-100, 200, 100, False)
while True:
    if arm.is_done():
        break
    else:
        print("\033[2J")
        print(brain.timer_time(SECONDS))
    wait(50, MSEC)
brain.program_stop()

يمر#

pass هو عنصر نائب للتعليمات البرمجية المستقبلية ويمكن استخدامه لتجنب الأخطاء في الحلقات الفارغة والشروط والوظائف.

if condition:
    pass

def function():
    pass