നിയന്ത്രണം#
ആമുഖം#
പൈത്തണിലെ നിയന്ത്രണം ഉപയോഗിച്ച് റോബോട്ടിനോട് എപ്പോൾ കാത്തിരിക്കണം, എപ്പോൾ പ്രവർത്തനങ്ങൾ ആവർത്തിക്കണം, എങ്ങനെ തീരുമാനങ്ങൾ എടുക്കണം, എപ്പോൾ പ്രോജക്റ്റ് അവസാനിപ്പിക്കണം എന്നിവ പറയാൻ കഴിയും.
മെത്തേഡുകളും കോർ പൈത്തൺ കീവേഡുകളും ഉൾപ്പെടെ ലഭ്യമായ നിയന്ത്രണങ്ങളുടെ ഒരു ലിസ്റ്റ് ചുവടെയുണ്ട്:
wait– ഒരു നിശ്ചിത സമയത്തേക്ക് പ്രോജക്റ്റ് താൽക്കാലികമായി നിർത്തുന്നു.for– Repeats code for each item in a sequence.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, അവസ്ഥTrue.മുൻ വ്യവസ്ഥകളെല്ലാം
Falseആണെങ്കിൽ മാത്രമേelifഅധിക വ്യവസ്ഥകൾ പരിശോധിക്കൂ. ഒന്നിലധികംelifപ്രസ്താവനകൾ ഉപയോഗിക്കാം.മുമ്പത്തെ അവസ്ഥകളൊന്നും
Trueമാത്രമേelseഅതിന്റെ ബ്ലോക്ക് പ്രവർത്തിപ്പിക്കുന്നുള്ളൂ.
ഉപയോഗം:
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 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
ഘടകങ്ങൾ |
വിവരണം |
|---|---|
|
ഓരോ ആവർത്തനത്തിനും മുമ്പായി പരിശോധിക്കുന്ന ഒരു എക്സ്പ്രഷൻ അല്ലെങ്കിൽ വേരിയബിൾ. അത് |
# 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