Skip to Content
BonicBot A2DevelopmentPython ProgrammingLesson 21 - Motion Command Queueing

Lesson 21 - Motion Command Queueing

Learning Objective

  • Build multi-step motion sequences using command dictionaries.
  • Queue and execute motion plans using bot.enqueue_move() and bot.run_queue().
  • Flush active command sequences using bot.clear_queue() and execute geometric macros like bot.draw_square().

Introduction

Complex robotic behaviors require executing sequence lists of motions (e.g. drive 0.5m -> rotate 90° -> drive 0.2m -> rotate -45°). Command queueing allows you to declare full motion plans and execute them sequentially.

This lesson covers enqueue_move(), run_queue(), clear_queue(), and built-in shape macros like draw_square().


Code

draw_square.py
import time from bonicbot_bridge import BonicBot with BonicBot(host="192.168.29.52") as bot: time.sleep(5) print("Using shape macro: draw_square(side_m=0.4)...") # Automatically queues and executes a 4-side square trajectory bot.draw_square(side_m=0.4, speed=0.2, turn_speed=45.0)

[!NOTE] Before running the code, make sure to replace "192.168.29.52" with your BonicBot’s actual IP address.


Expected Output

Click to see expected output

Visual Output

Terminal Output

🤖 Connected to BonicBot at 192.168.29.52:9090 Using shape macro: draw_square(side_m=0.4)... 🔌 Disconnected from BonicBot

🔧 Under the Hood

How run_queue() executes command items

enqueue_move() appends command items to MotionController._command_queue. Calling run_queue(block=True) launches a worker loop that pops items one by one, dispatching each to drive_distance() or rotate_angle(). bot.clear_queue() flushes remaining queue items.


Student Challenge

Challenge 1 — Python Only

Write a function generate_star_pattern(num_points, side_meters) that returns a list of command dictionaries for driving a star trajectory.

Click to see solution

star_pattern.py
def generate_star_pattern(num_points=5, side_meters=0.4): turn_angle = 180.0 - (180.0 / num_points) plan = [] for _ in range(num_points): plan.append({"type": "drive", "dist": side_meters, "speed": 0.2}) plan.append({"type": "rotate", "angle": turn_angle, "speed": 45.0}) return plan print("Generated star plan items:", len(generate_star_pattern()))

Challenge 2 — Robot

Queue a 3-step inspection routine (drive 0.3m, rotate 180°, drive 0.3m), but clear the queue if any command fails.

Click to see solution

inspection_queue.py
from bonicbot_bridge import BonicBot with BonicBot(host="192.168.0.188") as bot: routine = [ {"type": "drive", "dist": 0.3, "speed": 0.2}, {"type": "rotate", "angle": 180.0, "speed": 45.0}, {"type": "drive", "dist": 0.3, "speed": 0.2}, ] bot.enqueue_move(routine) ok = bot.run_queue(block=True) if not ok: print("Maneuver aborted! Clearing queue.") bot.clear_queue()

Quick Reference

MethodDescription
bot.enqueue_move(cmd_list)Append list of command dicts to queue
bot.run_queue(block)Execute queued commands sequentially
bot.clear_queue()Flush all pending items from queue
bot.draw_square(side_m, speed, turn_speed)Execute square driving macro

Reflection Questions

What advantage does declaring a full motion plan in a list offer compared to calling individual blocking functions one line at a time?

What happens if bot.clear_queue() is called while bot.run_queue(block=False) is actively executing?

Last updated on