Tuesday 8 March 2016

Near-miss-o-meter, Arduino Code



A short post to describe the functionality implemented in the initial release of the Arduino software on github. This is the first Arduino/C code I have written, so feedback is more than welcome. It is has been an interesting challenge to get all the things I'd wanted into just 28kB of space. Avoiding the use of floating point numbers in TinyGPS meant I had to take on sprintf (and I won, albeit at 2am!).

The code is structured to allow for easy adaptation to other boards and sonars, so tends to use 'global' variables more than usual. The initial version, built for the Adafruit Feather 32u4 Adalogger, will support 'pulse output' sonars via NewPing and serial output sonars via SerialPing (my first Arduino library, be kind).

The choice of sonars is slightly limited by the Feather running at 3.3 volts, so the ultra-cheap ones such as the HC-SR04 will require either a voltage booster or a 5 volt board. The cheaper serial sonars will run at 3.3 volts.

User interface

The nearmiss-o-meter has an extremely simple user interface, consisting of  three LEDs:
  • Red LED: GPS and battery status.
  • Green LED: Record and tagging status
  • Blue LED: Nearmiss and 'test mode' status
and one push button (momentary) switch:
  • Medium press: Start/stop recording
  • Short press: Start stop tagging (if recording)
  • Long press: Start/stop test mode (will ping sonar without a GPS fix)

What does it do?


Power Up

When the Arduino is powered on, the green LED will flash twice if the SD card is present. The red LED will remain fully lit until a GPS fix is obtained, at which point it will show one short flash per second.

Start/Stop Recording

A medium press on the switch (greater than half a second) will start recording if there is a GPS fix. The green LED will light fully when you have pressed for long enough and when released will show one short flash per second to indicate recording.

Recording is stopped in the same way (even without a GPS fix).

Files are recorded in the root directory of the SD card named in the format YYMMDDHH.csv. Note: the standard Arduino SD library does not support file timestamps so all files will appear to be created on 01/01/1970.

The CSV record consists of:
  1. Date/time in ISO ISO 8601 format (UTC)
  2. Longitude, to 4 decimal places
  3. Latitude, to 4 decimal places
  4. Speed, in kph
  5. Course or heading, in whole degrees
  6. Passing distance, minus offset (if specified)
  7. Tag state, either 0 or 1

Tagging

When recording, the nearmiss-on-meter will log a continuous trace, so the design also allows for continuous tagging (perhaps to denote sections of cycletrack). The switch implements a 'press to toggle tagging' rather than 'press and hold to tag'.

A short press (less than half a second) will start or stop tagging.

The green LED will show two short flashes per second while tagging.

Test Mode

A long press (greater than 5 seconds) will enter/exit test mode, irrespective of whether a GPS fix has been obtained. The CSV data will be sent to the serial monitor (via USB) of the Arduino IDE.

Source Code

The initial release of the source code is here. It is fairly well documented, with links to all the required libraries. Instructions for setting up and using the Arduino IDE are here

Have fun!

Part 5: The Difficult Second Album