Lego Matrix of Dots printer – snappy title, eh?

Here’s the Egg Drawing robot converted into a simple one-dot dot matrix printer. I’ve borrowed a routine that I used in the Minecraft Relief Maps project and used that to interpret a JPEG file. There are methods which use Pygame, but I’ve stuck with things I know – and can achieve quickly.

The program reads in the image and converts it into a pattern of “#” symbols and spaces using JP2A. For this reason, a decent monochrome image makes a good starting point. Once converted, it’s split into lines and the servos are then made to scan backwards and forwards, putting in a dot wherever JP2A has left a “#”.

I needed to convert between coordinates on the bitmap and the actual positions needed by the servo. This is relatively easy to do once the correct range given by the servos has been found experimentally.

I had a few problems with the pen bouncing, and also the route that the pen takes is really a curve. This would be more suited to drawing bitmaps on eggs, so that’s the next step. Maybe “Egg Minions?”.

import commands
import os
import time

DELAY = 0.002

pen=2
leftright=1
updown=0

def pwm(pin, angle):
#    print("servo[" + str(pin) + "][" + str(angle) + "]")
    cmd = "echo " + str(pin) + "=" + str(angle) + " > /dev/servoblaster"
    os.system(cmd)
    time.sleep(DELAY)

def slow(servo,start,stop):
	difference = start-stop
	servopos = start
	if start<stop:
		step = 1
	else:
		step = -1
	servopos = start
	while servopos != stop:
		pwm(servo,servopos)
		servopos += step

def penup():
    pwm(2,160)

def pendown():
    pwm(2,140)

def dot():
    slow(2,160,140)
    slow(2,140,160)
		
def servo_off():
	pwm(0,0)

def servoY(posY):
    pwm(0,(posY*1.1)+100)

def servoX(posX):
    pwm(1,(posX*1.2)+80)

penup()
time.sleep(0.5)
pwm(1,60)
pwm(0,100)

wr_str = "MonoLogo.jpg"

cmd = 'jp2a --width=20 --height=20  --background=light --chars=" #" '+wr_str # create an operating system command
line = commands.getoutput( cmd ) # send cmd as a command to the operating system and receive the result.
list_line = line.rstrip().split("\n") # split the result from "figlet" into separate lines (right strip new line feeds)

print "Your picture has been converted to"
print line

yplot=0
xplot=0

for row in list_line: # one row at a time from list_line (the result from figlet)
    slow(1,(xplot*1.2)+60,60)

    print row
    servoY(yplot)
    yplot=yplot+5
    xplot=0
    for letter in row: # work along each row - check each character.
        blockshade=letter
#        print blockshade
        if blockshade=="#":
            servoX(xplot)
            time.sleep(0.5)
            dot()
        xplot = xplot+5
servoY(0)
time.sleep(1)

servo_off()

print("All done!")

Once that’s all running, assuming you have a MonoLogo.jpg file available, the machine will convert and start automatically.

Lego printer image

Simple print out of the Raspberry Pi logo using a Lego dot matrix printer.

Minion bitmap drawn on the egg

Drawing a design on an Egg

The finished egg design

The finished egg – just in time for breakfast.

Advertisement

Programming the Egg drawing robot

Scratch program for drawing squares on the egg.

The program to draw a grid of squares on the egg.

In order to program the egg drawing robot, a simple pair of loops are required. One draws the horizontal lines and the other is responsible for the vertical ones.

In this case, I’ve simply laid out a loop that:

  • Move to the left
  • Drop the pen
  • Move to the right
  • Lift the pen
  • Rotate the egg upwards slightly
  • Repeat ten times

Followed by:

  • Rotate the egg upwards to the start position
  • Move to the left
  • Drop the pen
  • Rotate the egg downwards
  • Lift the pen
  • Move the pen to the right slightly
  • Repeat ten times

In order to make the servos move over their required range, it was necessary to find out what the extremities of movement were – for example, how far can the pen be moved from left to right. This then gave a range of values. I subtracted the smaller value from the larger value. Then I needed to find the offset – the starting value. I then used a scale value to make my servos now respond to a range of 0 to 100. A bit difficult to describe, but a quick visit from the formula for a straight line: y = mx+c

Overall, the grid is fairly simple but it’s ideal for demonstrating control over movements, the resolution of the servos and the pen and just showing what can be done with some Lego and some servos.

Next up… hopefully bitmap conversion!

Miniature Servo-controlled plotter

Amazing what you find when trawling the internet…

This little plotter, built from 6 3d-printed parts, looks like an interesting challenge. I built a stepper-motor controlled plotter years ago and even wrote a simple CAD package in BBC-Basic, but this is a great approach. I’m imagining this combined with ServoBlaster and some simple Python programming. The main  website is at: Miniature Servo-controlled plotter