Skip to content
Parallax Learn

Parallax Learn

  • Welcome
  • Tutorials
        • Tutorial Series head tag

          Tutorial Series
        • Tutorial Series

          The special, classroom-ready series pages are organized collections of tutorials for our most popular hardware and/or languages. The tutorials for each topic are conveniently accessible from a single page, shown in the order it is recommended that they be completed.
        • Robotics Series Head tag

          Robotics Series
        • Robotics Series

          • Artificial Intelligence
          • Cybersecurity: Radio Data tutorialCybersecurity
          • cyber:bot + Python
          • cyber:bot + MakeCode
          • Boe-Bot Tutorial SeriesBoe-Bot
          • Arduino Shield-Bot
          • ActivityBot with C TutorialsActivityBot + C
          • ActivityBot with BlocklyProp Tutorial SeriesActivityBot + BlocklyProp
          • Scribbler 3 Tutorial SeriesScribbler 3
        • Electronics & Programming Series Head tag

          Electronics & Programming Series
          • BS2 Board of Education Tutorial SeriesBS2 Board of Education
          • Propeller C-Language BasicsPropeller C Basics
          • FLiP Try-It Kit C Tutorial SeriesFLiP Try-It Kit + C
          • FLiP Try-It Kit BlocklyProp TutorialsFLiP Try-It Kit + BlocklyProp
          • Badge WX Tutorial SeriesBadge WX
          • Propeller BlocklyProp Basics and ProjectsPropeller BlocklyProp Basics
          • View All Tutorial Series »
        • Browse Tutorials
        • Browse Tutorials

          Individual tutorials sorted by robot or kit, and language.
        • By Robot or Kit
          • ActivityBot
          • SumoBot WX
          • Boe-Bot
          • Shield-Bot
          • cyber:bot
          • Badge WX
          • ELEV-8
          • ARLO
        • By Language
        • By Language

          • Propeller C
          • Arduino
          • BlocklyProp
          • PBASIC
          • Python
          • MakeCode
          • View All Tutorials »
  • Educators
  • Reference
  • Downloads
  • Home
  • All Courses

Respond to Object Coordinates (Python)

Curriculum

  • 1 Section
  • 2 Lessons
  • Lifetime
Expand all sectionsCollapse all sections
  • Respond to Object Coordinates (Python)
    2
    • 1.0
      Get and Display Coordinates with a Program (Python)
    • 1.1
      Navigate to Center on Object Coordinates (Python)

Navigate to Center on Object Coordinates (Python)

In earlier lessons, you plotted an object’s (X, Y) coordinates on the micro:bit’s LED display.
Now you’ll use those same coordinates to steer the cyber:bot so the trained color target stays centered in the HUSKYLENS camera’s view.
This is your first feedback-control program—your robot will automatically adjust its direction and speed to keep what it “sees” centered in its field of vision.

Parts List

Use the same setup as before:

  • cyber:bot robot (assembled and tested)

  • HUSKYLENS with microSD card installed

  • 5 × AA batteries (fresh or fully charged NiMH)

  • I²C wiring connection (from previous activity)

Script: navigate_to_center.py

This program reads the color target’s position from the HUSKYLENS and moves the cyber:bot to keep that target centered in the camera view.
The code uses simple proportional control to calculate how much to turn and drive forward, and it smooths out movement for steady tracking.

✓ Open the micro:bit Python Editor.
✓ Load your previous project (show_object_xy).
✓ Update the project name to navigate_to_center.
✓ Replace the script contents with the version below.
✓ Click Save, then Send to micro:bit to load it onto your board.


Tests

This test verifies that the cyber:bot turns and drives in response to the object’s position in the HUSKYLENS view.

Safety First:
✓ For initial tests, lift the wheels off the table or place the robot on a test stand.
✓ Keep fingers clear of wheels and servo horns.

Set Up:
✓ Confirm that a color has been trained and saved as Color:ID1 on the microSD card.
✓ Ensure the HUSKYLENS is in Color Recognition mode (the script sets it automatically).
✓ Power the robot with five fresh AA batteries and set the switch to position 1 (center).

Wheel Direction Test (off the ground):
✓ Move the trained color to the left of center — the right wheel should move forward while the left slows or stops (turn left).
✓ Move the color to the right — the left wheel should move forward (turn right).
✓ Move the color above center — both wheels should move forward.
✓ Move the color below center — both should slow or reverse slightly (back up).
✓ If turning looks inverted, verify that the right motor is flipped in the maneuver() function.

Drive Test (on the floor):
✓ Place the robot on the floor with the trained color target ahead at a safe distance.
✓ The robot should steer toward the target and attempt to center it.
✓ If the robot oscillates or drives too quickly, reduce kpy.
✓ If it turns too sharply, reduce kpx.
✓ Adjust inc for smoother motion: smaller for steadier motion, larger for quicker response.

Did You Know

This script demonstrates proportional control — the foundation of robotics motion feedback.
When an error (difference between where you want to be and where you are) is multiplied by a proportional constant (the gain), it produces a control signal that adjusts movement.
If the object is far from center, the correction is large; if it’s close, the correction is small.

Why smoothing matters.
The maneuver() function uses incremental speed changes to keep motion stable. This prevents the robot from jittering due to tiny, rapid changes in camera readings — a common real-world robotics issue known as sensor noise.

How It Works

The script begins by importing the cyberbot, i2c_repeat, and huskylens modules.
The i2c_repeat(5, 4).connect() statement sets up shared I²C communication between the micro:bit, the cyber:bot board, and the HUSKYLENS.
After a short sleep(100), initialize_i2c() starts communication with the HUSKYLENS, and switch_algorithm("color") sets it to color recognition mode.

The variable current_id identifies which color model the robot will track.
The manage_model("load", current_id) command loads that color’s model from the microSD card.

Two control constants, kpx and kpy, define how strongly the robot turns or moves forward based on horizontal and vertical “errors” — differences between the object’s detected position (x, y) and the screen center (160, 120).
The inc variable defines how quickly motor speeds can change, providing smoothing for steady motion.

During each loop cycle, the program:

  • Reads the object’s coordinates using get_frame_value().

  • Calculates the horizontal error (ex) and vertical error (ey).

  • Computes new wheel speeds using differential drive math (vL = ey - ex, vR = ey + ex).

  • Calls maneuver(vL, vR) to apply smoothed speed adjustments to the servo motors.

The maneuver() function prevents abrupt direction changes by increasing or decreasing each wheel speed in small increments.
If the target leaves the field of view, the robot slows smoothly to a stop.
Together, these features create a proportional control system that reacts gently and keeps the target centered in the HUSKYLENS view.

Your Turn

Now that your cyber:bot can follow a color target, let’s make its motion more precise.
Try experimenting with the control constants and smoothing value to balance accuracy and smoothness.

✓ Increase kpy to make the robot move forward faster toward the target.
✓ Increase kpx to make it turn more aggressively toward center.
✓ Adjust inc to fine-tune how gradually the robot changes speed.

Can you find a balance where the robot centers the target quickly but doesn’t overshoot or oscillate?

Questions, Exercises, Projects

Questions

Answer the following questions to review the key concepts in this activity.

  1. What does the kpx variable control in this script?
    It controls how strongly the robot turns left or right based on the object’s horizontal position.

  2. What does the kpy variable control?
    It determines how strongly the robot moves forward or backward based on the object’s vertical position.

  3. What is the purpose of the inc variable?
    It limits how quickly motor speeds change, preventing jittery movement.

  4. Why is the maneuver() function needed instead of directly calling servo_speed()?
    It smooths the motion by gradually changing wheel speeds and ensures the right motor direction is corrected for the chassis.

  5. What is meant by the term “feedback loop” in this program?
    The robot continually reads camera data, computes errors, and adjusts motion based on those errors in real time.

  6. How does proportional control help the robot center the color target?
    It scales motor commands according to how far the target is from center — larger errors cause stronger corrections, smaller ones cause gentler adjustments.

Exercises

Try these exercises to explore how control parameters affect robot performance.

  1. Experiment with the kpx value.
    Increase or decrease it to see how turning responsiveness changes. Observe what happens if it’s too high or too low.

  2. Change kpy to a smaller number (for example, 0.2).
    Notice how reducing this gain slows the robot’s forward motion and makes its tracking steadier.

  3. Modify inc to a larger value (for example, 6).
    This allows faster acceleration but can make the robot less stable when following small movements.

  4. Add a debug print statement that also shows error values ex and ey.
    Use print("ex:", ex, "ey:", ey) to monitor the robot’s control response in real time.

Projects

Apply what you’ve learned to design and test more advanced control behaviors.

  1. Target Centering with Distance Zones
    Expand the program so the robot slows down when the color target is near the center of the HUSKYLENS view and speeds up when it’s farther away.
    Hint: Use the size of the detected block (width or height) as a rough measure of distance, and adjust kpy based on that value.

  2. Smooth Stop and Resume
    Modify the code so that when the color target disappears, the robot doesn’t stop immediately but gradually slows to a halt.
    Hint: Instead of calling maneuver(0, 0) right away, reduce target speeds step by step until both motors reach zero.*

Solutions

Questions

  1. What does the kpx variable control in this script?
    It controls how strongly the robot turns left or right based on the object’s horizontal position.

  2. What is the purpose of the inc variable?
    It limits how quickly motor speeds change, preventing jittery movement.

  3. What is meant by the term “feedback loop” in this program?
    The robot continually reads camera data, computes errors, and adjusts motion based on those errors in real time.

Exercises

  1. Experiment with the kpx value.
    Increasing kpx makes turns sharper but can cause oscillation; reducing it makes turns slower and smoother.

  2. Modify inc to a larger value (for example, 6).
    This lets the robot accelerate and decelerate faster, but motion may become twitchy on uneven surfaces.

Projects

  1. Target Centering with Distance Zones
    Measure the detected block’s width or height. When these values are small (target far away), use a higher kpy to move faster. As they increase (target closer), reduce kpy to slow down for precise centering.


Printer-friendly version
Get and Display Coordinates with a Program (Python)
Prev

DISCUSSION FORUMS | PARALLAX INC. STORE

About | Terms of Use | Feedback: learn@parallax.com | Copyright©Parallax Inc. 2024

© 2026 Parallax Learn • Built with GeneratePress