Bundle and save to celebrate summer! Shop Now

View All Materials

Lesson:

Robot Text Printer

Experience Level

N/A

Duration

N/A

Group Size

N/A

Compatible Robots

N/A

Details

Use Python to transform your robot into a text printer.

Downloads & Resources

1. Gather your supplies. You'll need: (1) Root Robot, (1) roll of butcher paper, (1) dry erase marker, and (1) coding device that can run the Python Web Playground.

2. Open a bluetooth supported web browser like Google Chrome. Visit python.irobot.com to open the iRobot Education Python Web Playground.

3. Power on your Root Coding Robot and click "Connect" in the upper-left-hand corner of the page. This should open your browser's bluetooth device manager. Inside the bluetooth device manager, find your robot's name. Click the name to highlight it, and then click the "Pair" button.

4. Delete the template project text on the page. Copy & paste the code below into your project.


#
# Licensed under 3-Clause BSD license available in the License file. Copyright (c) 2023 iRobot Corporation. All rights reserved.
#

# Write arbitrary text using the Hershey vector fonts. Set the "size" and "text" below.
text = "Hi!"
size = 16

from irobot_edu_sdk.backend.bluetooth import Bluetooth
from irobot_edu_sdk.robots import event, Root

robot = Root(Bluetooth())

import micropip
await micropip.install("Hershey-Fonts")
from HersheyFonts import HersheyFonts

font = HersheyFonts()
font.load_default_font()
font.normalize_rendering(size)

def swap_coords(x, y, x1, y1, x2, y2):
# Returns True if (x2, y2) is closer than (x1, y1) to (x, y)
    try:
        dist1 = ((x-x1)**2 + (y-y1)**2)**0.5
        dist2 = ((x-x2)**2 + (y-y2)**2)**0.5
        return dist1 > dist2
    except TypeError:
        return False

@event(robot.when_play)
async def write(robot):
    await robot.reset_navigation()
    prev_x = None
    prev_y = None

    for (x1, y1), (x2, y2) in font.lines_for_text(text):
        if swap_coords(prev_x, prev_y, x1, y1, x2, y2):
            x1, x2 = x2, x1
            y1, y2 = y2, y1
        if x1 != prev_x or y1 != prev_y:
            await robot.set_marker(Root.MARKER_UP)
            await robot.navigate_to(x1, y1)
            await robot.set_marker(Root.MARKER_DOWN)
        await robot.navigate_to(x2, y2)
        prev_x = x2
        prev_y = y2

robot.play()

6. Scroll to read line #6, which reads: text = "Hi!". You can replace this text with whatever you would like your robot to print. For this example, we chose "Robot".

7. Insert your dry-erase marker into your Root Robot and roll out the butcher paper onto a firm, flat surface.

8. Place your robot near the bottom-left of the paper. NOTE: You WILL need to watch your robot as it prints in order to protect the surface underneath the paper if your robot drives off of the page.

9. Press the Play button in the top-let corner of the Python Web Playground to start running your program. Watch as your robot writes out your text!



Extension

Interested in exploring more fonts? Visit https://pypi.org/project/Hersh... to learn more about the Hershey Fonts project. When you have selected a new font to try, follow the instructions in the 'Loading a font' section. Hint: you'll need to edit Line #9!