ProgrammingPractical guide

From Visual Programming to Real Robot Control

Translate a block-based stop rule into Python, test it with sample readings, and see how a control interface handles motion requests.

Broxme JournalRobotics & embodied AI
Reviewed
Reading time4 min read

A block and a few lines of Python can express the same decision. Request a stop when the distance is at or below a threshold or when a reading is missing. For other readings, request forward motion. Test the rule with sample data before a separate controller translates its output into motion commands.

Visual command blocks connected to a separate motor-control interface
The decision logic requests an action. A separate control interface translates the request for the robot or simulator.

Start with the blocks

Imagine a distance block connected to an if/else block. The condition compares the reading with a threshold. Each branch selects one action, and the enclosing control loop repeats the check with the latest reading. An editor can make those relationships visible by showing which blocks belong inside the condition and which run in sequence.

Blockly is an example of an independent library for building block editors. Its developers describe how applications define blocks and the code those blocks generate. The surrounding application decides how to use the output. See Blockly's explanation of block editors and generated code.

Express the rule as code

Run this example in ordinary Python with sample data. The input is a valid distance in meters or None when no valid reading is available. The function returns the name of an action. The threshold is specific to this exercise.

def choose_action(distance_m):
    if distance_m is None or distance_m <= 0.30:
        return "stop"
    return "forward"

assert choose_action(0.20) == "stop"
assert choose_action(0.30) == "stop"
assert choose_action(0.45) == "forward"
assert choose_action(None) == "stop"

The case at exactly 0.30 meters checks that the visual and text versions agree at the boundary. The surrounding program is responsible for receiving fresh readings, handling timing, and forwarding the requested action.

Give each action a control interface

Decide what a sensor reading means before connecting a driver. Agree on units, where the measurement originates, and how long it stays valid. When the sensor fails to report, a status field or timestamp lets the receiving program distinguish a fresh measurement from an old one. Convert invalid and stale samples before passing them to the decision function.

Then define how each action maps to a motion request. “Forward” can mean a bounded forward speed with no requested turn. A motor controller or software adapter converts that request into commands for the specific drive. For example, the independent ros2_control differential drive controller accepts body velocity commands, maps them to wheel velocities, and can stop after a command timeout. See the official controller interfaces and parameters.

Different block editors and runtimes use different execution models. Some generate text in a target language, while an application can also interpret a block arrangement through its own runtime. When inspecting generated code, identify where sensor reads occur and where motion requests are sent. Blockly's code-generation guide shows how applications define those mappings.

Check when a decision expires

A learned controller can take different amounts of time to produce an action as sensor and compute load change. In a simulator, record when an observation was captured, when a policy used it, and when the controller received the action. Those timestamps show the age of the observation when the action would take effect.

Define when an action proposal expires and check it at the control interface. For a mobile robot example, the controller can discard a proposal whose observation is too old and use its configured stop behavior. Keep speed limits and the independent stop path in effect even when the policy proposes movement. ROS 2 distinguishes a topic’s deadline, which concerns the time between publications, from its lifespan, which concerns how long a published message remains valid. See the ROS 2 Jazzy documentation on quality of service.

Replay a normal trace, then repeat it with a delayed decision and with a missing observation. Check which motion requests the interface accepts and which it discards. For hardware, use the platform’s controlled test procedures and verify how long motion takes to end after the controller requests a stop.

Verify the transition in stages

First, compare the two versions with the same set of inputs. Then test the behavior with a simulated controller and delayed or missing readings. When working with a supported physical platform, follow the hardware's setup and test procedures. Validate direction, speed limits, the command timeout, and the independent stop method under controlled conditions before trying the full obstacle scenario.

Measure what happens after a stop request, including the time before motion actually ends. Motor response and braking depend on the drive and the platform. Use those observations to determine what clearance is appropriate for the conditions under test. The example threshold is a way to test a software branch.

Read the robot simulation guide for a repeatable test setup. The Broxme robotics development guide describes how the Studio engineering build represents programs and records a simulated run. The Python example above is a standalone learning exercise.

Sources and further reading

Use the original documentation and author resources for technical details and current instructions.

  1. Blockly documentation — What is Blockly?
  2. Blockly documentation — Code generation
  3. ros2_control documentation — Differential Drive Controller
  4. ROS 2 Jazzy documentation — Quality of Service settings
Broxme

About this guide

Published by the Broxme Journal. Examples use simplified robot models for learning. The projects and technical resources cited here are maintained by their respective authors.

Found an error or an outdated instruction? Tell us the page and the detail that needs attention. We review corrections against the relevant source.

How the Journal is put together

Keep building your robotics skills.

Put the concepts in this guide to work with another exercise or technical resource.

Read the robotics development guide