Lesson 19 - Continuous Directional Motion
Learning Objective
- Command directional movements using
move_forward(),move_backward(),turn_left(), andturn_right(). - Understand blocking duration execution versus non-blocking continuous motion.
- Halt all motor motion immediately using
bot.stop()and query motion state viabot.is_moving().
Introduction
Basic robot navigation relies on sending velocity commands to drive motors. The BonicBot class provides high-level directional helper methods that simplify driving forward, reversing, and turning.
This lesson details continuous velocity control, speed limits, and blocking duration parameters.
Code
import time
from bonicbot_bridge import BonicBot
with BonicBot(host="192.168.29.52") as bot:
print("1. Driving forward at 1 m/s for 4 seconds (Blocking)...")
# Passing duration makes the method wait 2 seconds before continuing
bot.move_forward(speed=1, duration=4.0)
time.sleep(1)
print("2. Turning left for 2 seconds...")
bot.turn_left(speed=45, duration=2)
time.sleep(1)
print("3. Turning Right for 2 seconds...")
bot.turn_right(speed=45, duration=2)
time.sleep(1)
print("4. Starting non-blocking backward motion...")
# Omitting duration starts motion without pausing execution
bot.move_backward(speed=1,duration=2)
print("Robot is currently moving:", bot.is_moving())
time.sleep(1.0)
print("5. Stopping robot immediately...")
bot.stop()
print("Robot is currently moving:", bot.is_moving())[!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
1. Driving forward at 1 m/s for 4 seconds (Blocking)...
2. Turning left for 2 seconds...
3. Turning Right for 2 seconds...
4. Starting non-blocking backward motion...
Robot is currently moving: True
5. Stopping robot immediately...
Robot is currently moving: False
🔌 Disconnected from BonicBot🔧 Under the Hood
How directional methods delegate to motion.py
motion.pyHigh-level methods in core.py delegate directly to MotionController in motion.py:
def move_forward(self, speed=DEFAULT_LINEAR_SPEED, duration=None):
return self.motion.move_forward(speed, duration)If duration is provided, motion.py calls time.sleep(duration) before invoking self.stop().
Student Challenge
Challenge 1 — Python Only
Write a function calculate_turn_duration(angle_degrees, turn_speed_rad_sec) that calculates how many seconds the robot must turn at turn_speed_rad_sec to rotate through angle_degrees.
Click to see solution
import math
def calculate_turn_duration(angle_degrees, turn_speed_rad_sec):
angle_radians = math.radians(angle_degrees)
return angle_radians / turn_speed_rad_sec
duration = calculate_turn_duration(90, 0.5)
print(f"Time required to turn 90° at 0.5 rad/s: {duration:.2f}s")Challenge 2 — Robot
Program a 4-step square maneuver using move_forward (0.3 m/s for 2s) and turn_left (0.5 rad/s for 3.14s) in a loop, ending with bot.stop().
Click to see solution
from bonicbot_bridge import BonicBot
with BonicBot(host="192.168.0.188") as bot:
for side in range(4):
print(f"Driving side {side + 1}...")
bot.move_forward(speed=0.3, duration=2.0)
bot.turn_left(speed=0.5, duration=3.14)
bot.stop()Quick Reference
Directional Methods
| Method | Description |
|---|---|
bot.move_forward(speed, duration) | Drive forward (speed in m/s, optional duration in sec) |
bot.move_backward(speed, duration) | Drive backward (speed in m/s, optional duration in sec) |
bot.turn_left(speed, duration) | Turn counter-clockwise (speed in rad/s) |
bot.turn_right(speed, duration) | Turn clockwise (speed in rad/s) |
bot.stop() | Stop motor output immediately |
bot.is_moving() | Check if motor commands are active |
Reflection Questions
What is the functional difference between passing duration=2.0 vs omitting duration when calling bot.move_forward()?
Why are rotational speeds measured in radians per second (rad/s) rather than degrees per second in robotics?