Skip to content
UAVProg
Languageen
Blocks

Blocks

Variables and Functions

A name for a number, and a name for a group of blocks — the two ways a program stops repeating itself.

These two categories start empty, and that is not a mistake. Everything in them is something you make, so until you have made something there is nothing to show. Each has a button at the top of its flyout instead: Create variable… and Create function….

Variables

A variable is a box with a name written on it. You put a number in, and after that you can say the name instead of the number.

Press Create variable…, type a name, and three blocks appear in the category:

BlockWhat it doesWhat it writes
set hopSeconds to ⟨value⟩Puts a value in the box. A stacking block.hopSeconds = 2
hopSecondsHands over what is in the box. A value block.hopSeconds
change hopSeconds by ⟨value⟩Adds to what is already in there.hopSeconds = (hopSeconds if isinstance(hopSeconds, Number) else 0) + 1
Setting hopSeconds to 2 and using that variable as a wait duration

A number is stored under a name and read through the wait block’s value socket.

Generated by Blocks
hopSeconds = None


hopSeconds = 2
drone.active_fly()
drone.takeoff()
for count in range(4):
    drone.fly('forward', 1)
    time.sleep(hopSeconds)
    drone.fly_stop()
    time.sleep(0.5)
drone.land()

One variable controls the duration of every repeated movement. Changing 2 changes each use of hopSeconds; it does not change direction or guarantee a particular distance.

Functions

A function is a name for a group of blocks. Make one, and it appears in the category as a single block you can use as many times as you like.

Press Create function…, and a definition block appears on the canvas:

BlockWhat it doesWhat it writes
to fly one side doThe definition. It stands on its own — it has no notch and no bump, because it is not part of the flight.def fly_one_side():
fly one sideUses it. This one goes in the stack.fly_one_side()

A function can package one forward movement and its stop. In this example it is named one_side; its name does not change the simulator’s coordinate frame.

A separate one_side definition and a block that calls it

The definition stands on its own. The call belongs in the connected program that should use it.

Generated by Blocks
def one_side():
    drone.fly('forward', 1)
    time.sleep(2)
    drone.fly_stop()
    time.sleep(0.5)

drone.active_fly()
drone.takeoff()
one_side()
drone.land()

Defining a function does not run it. A call runs its body and then continues at the next command. Repeating this call repeats the same forward movement; it does not automatically turn that movement into a square.

Giving a function something to work with

Click the gear on the definition block and a panel opens where you can add inputs — values the function is handed each time it is used. Add one called seconds, drop it into the wait inside, and fly one side becomes a block with a hole in it: fly one side for ⟨3⟩ seconds.

Calling the function with 1, 2, 3 and 4 can produce movements of increasing duration. The actual path depends on the directions and stops inside it; changing duration alone does not produce a spiral.

Functions that answer a question

The gear panel also offers a definition with a return — a function that hands a value back instead of doing something. Those appear in the category as value blocks, rounded, to be dropped into holes rather than stacked.

A function called safe speed that constrains a number to 1 or 2 is a value block you can use anywhere a speed is needed.