Extension: CNC Plotter

3.15. Extension: CNC Plotter#

In this extension project, you will program your Maqueen robot to create a simple drawing.

The Maqueen can hold a marker or large pen in its gripper. When the gripper holds the pen against a sheet of paper, the robot can draw by driving around on the page.

This program does not use sensors to follow a printed line. Instead, the robot follows movement instructions that you write in the code.

3.15.1. Instructions#

  1. Open the MicroPython editor:

    https://python.microbit.org/v/3

    If you already have the editor open, use the existing window or tab.

  2. Connect your micro:bit using the instructions from the previous page.

  3. Enter the starter code below into the editor.

from microbit import *


def motor_left(speed, direction):
    buf = bytearray(3)
    buf[0] = 0x00
    buf[1] = direction
    buf[2] = speed
    i2c.write(0x10, buf)


def motor_right(speed, direction):
    buf = bytearray(3)
    buf[0] = 0x02
    buf[1] = direction
    buf[2] = speed
    i2c.write(0x10, buf)


def servo_one(angle=0):
    buf = bytearray(2)
    buf[0] = 0x14
    buf[1] = angle
    i2c.write(0x10, buf)


i2c.init()

# Startup sequence
while True:
    # Press button A to open the gripper and insert the pen
    if button_a.is_pressed():
        servo_one(100)
        sleep(500)

    # Press button B to close the gripper and begin drawing
    if button_b.is_pressed():
        servo_one(140)
        sleep(500)
        break

# Main loop - add your drawing code here
while True:
    sleep(1000)
  1. Flash the code to your micro:bit.

  2. Place the Maqueen on a sheet of paper with the pen touching the page.

  3. Press button A to open the gripper and position the pen.

  4. Press button B to close the gripper and start the drawing.

If you get stuck, ask a peer or your teacher for help.

Question 1

Extend the program so that the Maqueen creates a drawing.

Your program should move the robot while the gripper holds a marker or pen against the paper. As the robot moves, the pen will draw on the page.

The example below draws a square. The drawing is represented by a simple command string.

The command string uses these symbols:

S = start and end point
F = drive forward one side
R = turn right

The square drawing is:

S---+
|   |
|   |
+---+

The movement commands for this drawing are:

SFRFRFRF

This means:

Start at S
Forward
Turn right
Forward
Turn right
Forward
Turn right
Forward back to S
Solution

Solution is locked