Tuesday, March 1, 2011

Communications Protocol

I've got a few of the basic components working. I renamed InputPin/OutputPin to DigitalInput/DigitalOutput. I hooked up a couple of buttons and LEDs to the Arduino and I've got ButtonManager working nicely. I'd like to start on StepperDriver. That's going going to be super rewarding. I cleaned up my Java "simulation" of StepperDriver and I think the Arduino version will be easy enough. But eventually I need to communicate effectively with the Arduino from the laptop to set properties and issue commands. I'm building the manual controls first, but I think it will be easier to write StepperDriver if I can modify the state of the Arduino without having to constantly recompile and upload code to the Arduino.

I'm thinking there will be three things you can do:
Read a property
Write a property
Issue a command

Properties will certainly be all numbers. I can't see the need for any human-readable stuff, the host software can translate error codes, for example. I might need "boolean" values which could be represented with zero and one.

The Arduino supports byte, int, and long numbers. I plan to avoid any floating point numbers for performance reasons. Byte is one byte long, int is 2 bytes and long is 4 bytes. So a message will have an identifier (what property to set, what command to call) and 0, 1, 2, or 4 bytes of payload. Eventually I'll want a checksum or some other error correction.

The computer and the Arduino communicate with a Serial connection. I'd like to find a Serial communications protocol that is simple and robust and I don't want to get hung up building it myself. I've looked at Firmata, SNAP, basic MIDI, Modbus, and non-standard stuff. I can't seem to find anything I'm really happy with. I suspect I will do something based on MIDI, but for now I'm going with something very simple to get things going.

A favorite Object-Oriented Programming guideline of mine is "encapsulate the concept that varies". I expect to change the implementation of the serial communication protocol so I want to "wrap" it, or hide it from the main code. This means writing a Java class and a C++ class with an interface or "API" that is designed around the requirements of the "high-level" message format I described above and handles all the back and forth to the lower level serial protocol.

No comments:

Post a Comment