Tuesday, April 28, 2015

Hacking Typewriters Volume 1: My Smith Corona Daisywheel

I want a CNC typewriter. I want it to have full control of the paper feed and carriage placement, and do it with really high resolution. I want to be able to type any character I want,  anywhere I want. I want it to use ink ribbon and have adjustable strike strength.

Why do I want a typewriter with all these features? Because I want to be able to generate artwork that a typewriter can then print out. Much in the same way that vector graphics can be 'printed' by a pen-plotter, I want to print out images based on digital images. You may be thinking I am describing a simple terminal printer to create printer ASCII art, but I want better resolution and control. Basically I want to create the hardware and software needed to take a photograph and spit out something like these:

'Looking Forward' by Leslie Nichols (2010) Check out her website.
by Keira Rathbone (2015) Check out her website

I doubt a purely coded approach could ever make something as good as these images, which are typed 'freehand' by skilled artists. But software could inform much of the output, and artistic additions and editing of the process could yield interesting and compelling results (or so I hope).

With this in mind, I went looking for a typewriter I could start playing with. Manual typewriters were too much work to automate (would likely require either a large amount of powerful actuators or addition of a complex mechanical switching mechanism). I started looking at the IBM Memory, a electronic controlled version of a Selectric typewriter, but after ordering a broken one off eBay I found it was going to be too difficult and too destructive to the rare machine to add stepper motor control to get the fine control of the two axes I was looking for.

I found a fair compromise in the more modern daisy wheel printers that have stepper motors in the two axes already, and lucked out in finding one for $10 at a local Goodwill. It's a  Smith Corona Spellright 100 from the early 1980s. I was excited to see a DB9 connector in the back and I was optimistic I could hack a simple interface to command the typing element, but I had no such luck as it turns out this port is an undocumented proprietary communication port. I tried sniffing around for some sort of serial connection inside the typewriter, looking up the data sheets for the DIP components, and connecting to open headers on the board, but was not able to establish communication. I did find that the keyboard was a somewhat simple switch matrix that I could simulate with about 8 output pins and some logic decoders, so that is the route I took.

I used a EiBotBoard as my control center for this project. With it I could have my PC spit out somewhat simple commands to do the output pin switching (faking keyboard inputs) and to control the two stepper motors. I made a protoboard with all the logic (consisting of two 3-to-8 decoders and a bunch of small transistors) for keyboard faking and had it plug-in to the same spot the keyboard ribbon connected. I wrote some Processing code to take my PC keyboard inputs and translate them to the needed outputs to fake the same keyboard press. I had successfully  turned the typewriter in a terminal printer. I even wrote a little more code to have it print out a text file.

Emboldened by this success, I went about wrestling control of the stepper motors away from the typewriter, so I could add the fine control I wanted. This was not easy. I found quickly that the typewriter had unipolar motors, and these unipolar motors were none too happy about being run in any of the methods I found online when I googled "run unipolar motors with bipolar drivers".  I eventually got them to run pretty happy with a four-diode-setup per motor, effectively allowing the bipolar driver to simulate a unipolar current in the motors. The motors were working with a fair bit of torque and fairly smoothly, but there were more troubling issues at hand, namely that the motors were not capable of micro-stepping and the resulting resolution was not as good as I had hoped (about 1.5mm). Also the left/right motor would occasionally skip a step or two, which was fine for typing, but when doing long open-loop control 'prints' it caused drifts that would create errors. I also ran into some other issues with power on the EiBotBoard. Even with an external 3A 9V power supply hooked up, I found that when I would run the motors, some of my output pin voltages and grounds would have noise on them, resulting in somewhat unpredictable faked-keyboard outputs.

Despite the issues with the motors and controls, I pressed on to try and get my typewriter to do something worthwhile. I found that the pins I needed to type a period were not effected by the noise issue, so I wrote an instruction to print out a stipple imaged of my dog, Milo, using nothing periods. I created this image with a simple dithering algorithm, and printed it out. It took a surprisingly long time to print (about 5 hours), but considering it has over 16,000 periods, that works out to about 1 character per second. 


'Milo' by zeke.

I had some other issues with this typewriter not mentioned above. For one, there was some considerable dead-band on the two axes, so for every typed character I had to have the motors approach from the same direction, which complicates the code and takes time out to perform extra movement. Secondly, in order to advance the ribbon I had to move the carriage forward immediately after a character was typed, but the time between when a key press was 'faked' and when a character was typed on the page had some variables associated with it. A big part of this variable time was how long it took the daisy wheel to move to the desired letter. I came up with some code to estimate this time, but it wasn't pretty. Even with this correction in place, there was still some variation, which made it tough to advance the ribbon and not start moving the carriage before the letter was struck.

All these issues resulted in what is possibly the worlds worst, slowest dot matrix printer. A far cry from the typewriter I really want.

All in all, I enjoyed playing with this typewriter, but I don't think I am going to continue much further with this daisywheel in particular. The idiosyncrasies listed above, the fact that I can't use ink ribbons without considerable extra effort, the slow type speed resulting from navigating around dead band and ribbon advance, and the poor resolution make me want to look for some different hardware to bring my typewriter ideas to life. It was fun while it lasted, but I think the Spellright and me are going different places. Luckily, all my hacking was non-destructive and all my jack-ins were done at the ribbon-header connections, so I can put this typewriter back together and donate her back to a thrift shop to find another home. (Update: I was planning on taking some pictures of the boards and motor connections I had added to the typewriter, but I took the boards out and converted the typewriter back to 'stock' before I remembered to take pictures, and the typewriter is already back at Goodwill.)

I think my next avenue may be modifying a broken Olivetti Praxis 48, which should provide a great starting point for building my ideal typewriter (typebar mechanism, electric, possibly electronic?, ink ribbons). I will likely acquire and start modding in the fall, when I might have some more free time. I plan on documenting the next typewriter with much more detail and hope to get even better 'prints' that will make complicated type planning algorithms worthwhile.   

Friday, March 13, 2015

How to Win an Oscar Contest

In February 2011, I became aware that my local paper, the Peoria Journal Star, organized a yearly Oscar contest where you could win tickets to the different theaters around Peoria, IL. At first I thought the prizes would be fairly small, a couple tickets here, a couple tickets there, but I went to the rules webpage and found that the total estimated value of all the tickets they were giving away totaled more than $2000. The biggest prize was 104 movie passes to Willow Knolls, my favorite theater in town. Nothing to scoff at considering back in 2011 I would frequent Willow Knolls pretty much every week (it was only a five minute walk from my apartment).

So there were a bunch of prizes, and in order to win I would need two things: lots of entries and some confidence that I could out-choose the general public.

How does one get multiple entries in this contests? Well, the rules limited the entries to 1 per household, and because I wanted to not break the rules, I would just need multiple households. How to get entries from multiple households? Use your friends! All I would need to do is make my friends aware of the contest, tell them how to fill out the ballot to fit our larger strategy, make everyone agree to share in the bounty, and immediately we have a leg up on the competition, while still staying within the letter of the law with regards to the rules.

After working out how to get multiple shots at winning, how does one out-pick the public on an Academy Awards ballot? I'd need data that strongly correlates with the real probability of nominees winning. What data is readily available and represents the best possible knowledge available on the odds of a future occurrence? Online bookie websites. 

Bookies require knowing probability within a tight tolerance, because if they are off, they stand to lose a lot of money in the long term. In early February 2011 you could go online and find out what the payouts are for correctly guessing if "127 Hours" will win best picture. By taking data from many online bookies you can harness their collective understanding and through a little manipulation find the nugget of truth within. This method of utilizing the knowledge of bookies is a useful tool for other applications as well. Bookies won't tell you the future, but they will tell you how much more likely one future is than another, and often do it with a high degree of confidence.

I made a quick and dirty spreadsheet that took data I copied and pasted from easyodds.com and converted that data into usable probabilities for each of the categories. Once we have the probabilities for each category we can come up with a theoretical ballot and come up with the odds that ballot will be correct. It is a trivial task to find the most likely ballot from here (just select the odds-on favorite in every category), but what is not trivial is picking the top 15 or so ballots and determining if any of these should entered by more than one of our 'team'.

Through an intuitive process of determining what deviations (or combination of deviations) are the most likely, I came up with the best 25 ballots and double checked to make sure it was right. The combined probability of the top 7 was about 32%  (see the graph below) and the top ten had a 40% of one of those ten being perfect. This is a huge improvement over a simple random selection that only had about a 0.008% chance of getting a correct ballot in 10 tries.

  

It should be noted that this strategy is only optimized to get the best likelihood of having a correct entry. What we really want is a strategy to maximize the amount of tickets we will win, which may (or may not) be different than what we are doing. To understand this, imagine if a million people all put the most likely ballot in, then if that ballot turned out to be correct than we would only have a one in a million chance of winning any of the prizes, whereas if we played the 8 top ballots, minus the top ballot, we would decrease the odds of having a correct ballot (by about 7%), but the conditional probability of winning in a drawing would go up more than enough to compensate. To find these optimums however, it would require knowing how many people would enter each possible ballot (or at least relative percentage of entries among ballot choices),  which is impossible to know. So we just played the strategy that would optimize our chances of picking correct ballots, and hoped for the best from there.

This analysis is a little bit of overkill, but it won us some tickets. The local paper published ther results here, and you can see my friend Allen Chu won 52 tickets to Willow Knolls movie theater the first year we did this. We also won a fair number of tickets in 2012, and 2013 (they did not publish the winners in these years). By 2014 the contest had been discontinued. We continued to enter the local Willow Knolls Movie Theater Oscar contest, but didn't win anything in 2014 and 2015, despite having 2-3 people having perfect ballots. The last couple years there were fewer prizes and a much simpler ballot (fewer categories), so many more people got perfect ballots, crowding us out from winning in the random drawings that resulted.


This shows the limit of the strategy in contests that are overly simplified and overcrowded. Look for contests that have fewer entrants, hard to get perfect entries,  and/or have lots of prizes to make it worth your time.

Always check the odds using online bookies when doing an Oscar contest, making sports bets with friends, or if you just want to know how likely something is to happen. They provide their research to you for free, and they back up their findings with cash. Learning how to read betting odds can be pretty useful.