Thursday, January 10, 2013

Pi-A-Sketch

Python RaspberryPi Steppers Etch-A-Sketch


Last month, at PYPTUG PyHack workshop, we did a lot of stuff with motors. DC, servos, H bridges, PWM and steppers.

It was a very dense 3 hours. We covered a lot, and it was a lot of fun. I've already posted about the 2 bit H bridge, and I'll post some more articles covering some of the other aspects (steampunk DJ gear, anyone?)

But let us get back to steppers for a moment. Steppers are interesting, but just looking at them step one way, then the other, is not exactly exciting. Well, for most folks, anyway.

Silver screen

I've been keeping quiet for about a month on the last piece of the workshop. The reason is that I had a talk at the local IEEE chapter, and I was going to let that project go around the room for a live demo (that's right, battery powered), so I didn't want to kill the surprise too much (and it turned out to be a full house). Well, I gave the talk, so I can finally detail this project.

LCD? Not quite...

Meet Pi-A-Sketch

 


Pi-A-Sketch is a friendly Raspberry Pi in a raspberry colored case. The case is also housing an 8 channel darlington array.



It is powered by a Kodak Lithium battery pack. I was able to pick one up for a meager $6. It is small and low capacity, but it is perfect to power this setup.

It runs Raspbian. It auto logins at boot and start a Python script (roughly 150 lines of Python - I will do a step by step code review in a follow up post). The code is both a program and a module. It can be imported in python or ipython and thus we have an interactive session with the monitor.

Ah yes. the Pi-A-Sketch is connected to its "monitor" in a very mechanical way...

Steampunk Monitor


The pictures tell the bulk of the story, it is deceptively simple:

The Pi drives a pair of stepper motors
These stepper motors control the knobs of an Etch-A-Sketch
Each stepper is tied to each other to prevent rotation

The shaft of the Etch-A-Sketch, once we remove the knob, is the same diameter as the shaft of the stepper motors I chose. They are extremely cheap, but have enough torque due to a 64:1 built-in gearbox. They have no trouble doing their job.

With both side keyed, I coupled them with a hollow plastic tube, and filled the key with some hot glue. Good enough.


Walking the line

Horizontal is easy, just turn one knob. Vertical line? same thing. What about diagonals? Anything else?




On the above screenshot, not only are we doing some straight lines, but we are drawing circles. C.I.R.C.L.E.S. Yes, that.

Have you ever tried drawing circles on an Etch-A-Sketch???? Impossible. But, hey, a little Raspberry Pi, and what do you know!

It's tricky

There were a few challenges. For one thing, there is hysteresis to deal with. This is particularly an issue when drawing a circle due to slop when changing direction on an axis. Also, most algorithms are designed to work with a bitmap display. The Etch-A-Sketch is quite definitely not a framebuffer...

In our next episode we'll see how the Python code resolves some of these problems. And we will see how we can use the module to draw lines and circles, and you'll get a refresher on some of Bresenham's work (thank you Jack)... the line algorithm and the midpoint circle algorithm.

What's funny is that he initially started with plotters, but these algorithms are using a bitmap, yet we will reconvert these to be used with an X-Y plotter like interface (delta vs coordinates). Talk about going full circle.

In the interim, I have to get a few minutes to upload the video of the Pi-A-Sketch in operation...


@f_dion

7 comments:

  1. Can the RPi clear the screen, or are you disconnecting it and shaking in the traditional manner?

    Also, any plans to expose a serial port from the RPi and hook it up to some system expecting to drive a plotter?

    ReplyDelete
    Replies
    1. No need to disconnect anything, you just flip it and put if back down and try again.

      There is a serial connection on the GPIO, but it needs level conversion. It is however an interesting tought. Perhaps I'll investigate this.

      Delete
  2. Thats my board mounted in that Pi and my stepper motors ! You can see them at MyPiShop.com (Underconstruction) or buy them on ebay (Search "raspberry stepper gpio").

    NICE application.

    ReplyDelete
    Replies
    1. Hi John,

      The steppers I've had since last summer. But the daughterboard is yours. There is an interesting story behind how/why I ended up using it:

      http://raspberry-python.blogspot.com/2013/01/pyhacking-step-by-step.html

      Delete
  3. I have written Python routines (using the PyGame library) to draw spirographs and harmonographs on the Pi and PC/Mac/Android:

    https://github.com/Digital-Larry/curves-python/blob/master/curve.py

    The harmonograph routine already uses a continuous line drawing approach so would probably work quite directly on this project with some minor tweaks to the code.

    This application also has a "main.py" file at the same location in GitHub.

    To draw a Harmonograph I would:

    Instanciate a Harmonograph:

    class HarmonoGraph(Curve):
    def __init__(self, width, height, screen):
    print "==> HarmonoGraph init!"
    super(HarmonoGraph, self).__init__(width, height, screen)


    width and height are pixels, and screen is the PyGame screen structure handle which we use for drawing into. This would have to change to adapt to the drawing API on the sketch.

    ReplyDelete
    Replies
    1. The per-clock-tick loop is the App.run() method in main.py.

      This calls curveset.drawpoints() currently because I sequence through a list of pattern combinations and except for Harmonographs, I usually draw two different ones simultaneously. You can simplify this for the case of a single harmonograph by calling the Harmonograph instance's drawFunc method:

      def drawFunc(self, screen, color, start, end, dummy1, dummy2):
      # print (start,end)
      # print color
      pygame.draw.aaline(screen, color, start, end)

      replace the "pygame.draw.aaline" with whatever the EtchaSketch's line draw API is. Ignore the color obviously!

      Delete
  4. Hi friend, really this blog is very informative and the process of exposing about Pi-A-Sketch is very interesting. The images are also looking so beautiful.

    Thanks,
    Lithium polymer battery

    ReplyDelete