Tuesday, October 22, 2013

Maxwell's Daemons

Busy, busy. I have 2,300 lines of C++ Arduino code now. It just kind of happened. I think I've physically had the platform for about two weeks. (second one arrived too! backups are good.)


I have (running on the Leonardo beside me) a "nano-operating system", with:

  • Paged virtual memory
  • Real-time associative arrays.
  • Compressed prefix trees
  • Micro-parsers for syntax decoding
  • Serial command line with various utilities:
    • time : shows system clocks (millisecond and microsecond timer values)
    • pin : set pin modes, output values (digital and pwm), read inputs 
    • serial : set serial modes
    • aref : set analog voltage reference mode
    • lcd : command LCD module on direct-parallel interface.
    • env : list, get and set "environment variables" in system associative array
    • peek : dump contents of sram, flash, or eeprom
    • compass : interface to HMC5883L magnetometer
    • scope : control a 6-LED bar to display various analog input values in real-time.

All of that currently fits in 24k, leaving me a whopping 4k of flash for further commands. Although there's a lot of underlying libraries and other cruft that get pulled in by the standard libs.

Sketch uses 24,044 bytes (83%) of program storage space. Maximum is 28,672 bytes.
Global variables use 1,115 bytes (43%) of dynamic memory, leaving 1,445 bytes for local variables. Maximum is 2,560 bytes.

That's what happens when a 'real' computer scientist like me gets ahold of actual devices like this. I make them dance.

And while some of it is ported code from previous microcontroller projects, that's not the bulk of it. In fact, the ported code actually slowed me down, because re-adapting code for a different environment (especially subtle algorithms like red-black trees) requires you to be thinking simultaneously about two systems and all their differences.

I've been adding code and drivers pretty much as the hardware comes in. That would be more impressive if "driver" in this case wasn't 20 lines of real code, and the "devices" weren't more than a single chip that talks one of the standard inter-chip serial protocols. Or things arrived faster from Hong Kong. That would be nice.

I've had one partial failure so far, which took me a little time to figure out. The compass module is apparently a 3.3v I2C interface, and can't quite muster a signal of wide enough range to drive the 5v expectant microcontroller pins. Here's what that looks like on the digital oscilloscope:


The Arduino knows it's there, kind of, but the signal isn't clean enough to properly decode. (all those "mid-way stops" instead of slamming straight from floor to ceiling in nice square-wave pulses.) although it does, rarely, get a successful exchange. So I know the chip works.

Speaking of 'scope traces, I've also been playing around with "sonic" technology, in the form of 40Khz ultrasonic transducers. (When you stick one on the end of your oscilloscope probe, it looks a lot like the Pertwee-era sonic screwdriver.)


Here's what it looks like when you apply a single 'impulse' to the transmitter crystal (just a 5v-0v level change, in this case, which 'rings' the transmit crystal like hitting a bell, or tapping a wine glass.) and the resulting waveform generated by the receiver crystal. (which was placed face-to-face in this experiment.)


The high-frequency part of that signal isn't coming from an electronic source. It's purely the natural resonance of the piezoelectric crystal pair, as the "bell peal" dies away. I expect the actual output from the transmitter starts with a huge pulse and then exponentially decays, but it takes a 8-10 cycles for the receive crystal to begin resonating in response, and even longer (40-60) for it to "reset".

Not actually what I expected. So I learned something. Science!

I'm waiting for a couple of these to arrive in the post:


Which are integrated range-finder modules, but I can tell (just by looking at the circuit board in photos) how to hack it to act in a slightly more sophisticated manner, so that a couple of Arduinos can communicate using the same original technology - ultrasonic remote TV controls - that possibly inspired some beloved fiction.

(essentially, the middle chip is an 8-bit microcontroller. Remove that, and put the arduino directly in charge!)

Well.. I might use phase-shift keying instead of direct amplitude modulation, because it's now so very easy to write a tiny daemon and dedicate it to the task.

We've got so used to 'general purpose' computers that we've neglected the advantages you get by putting a single dedicated, unhackable processor in charge. One that will continue doing exactly what you told it to, ten thousand times a second, until it's 9v battery runs out. Maxwell's Demons, in silicon form.