Range sensor

Finally got my HC-SR04 ultrasonic range sensor the other day, and tried it out today. Hooking it up to the nano was just too easy. The user’s manual includes schematics for testing, and links to a github repo where there is library code. The library has centimeter resolution by default, but if you change the division value in the cpp file you could get a little bit better (but not much). For my purposes cm is fine, so I won’t be doing that. The schematic wasn’t very clear about how to attach the LCD display, and I didn’t really care that much so I used a dump to serial instead, and watched the output on the serial monitor. Here is my sketch code:

#include "Ultrasonic.h"

Ultrasonic ultrasonic(12,13);

void setup() {
  Serial.println("HC-SR4 testing..");

void loop() {
  Serial.println(" cm");

Holding my cell phone in front of the sensor produced very acceptably accurate results. Ultrasonic FTW!


This blog is beginning to sound like a commercial for pretty much everything. Seems like each entry is just to describe some new thing that I bought. Well, this one is no different. I got the Pi Nano the other day, but haven’t plugged it in yet. Too much real work to do.

And I totally chickened out. I was too worried about crashing the plane and having to buy all kinds of new parts, so I caved and bought the simulator software. It wasn’t listed on Amazon, so no Prime this time. I have to wait 2 weeks for some place that sells on EBay in New Jersey to ship it up here. It wasn’t terribly cheap, but I figure it could save me hundreds in replacement parts. Plus, I can fly it for as long as my family lets me sit at the computer, so I should be able to train much faster. (Nigel at the hobby shop said that if the throttle is on continuously – as opposed to gliding on updrafts with the engine off – I could expect the battery to last about 10 minutes, less if the electronics are using the same power supply. And it takes about an hour to charge. So I might reasonably get about 30 minutes of flying in a day if I’m really lucky – weekend only – as opposed to possibly hours a day on the simulator.) Sure, it’s not going to be exactly the same experience, but if I can get to even 70% competence it’s worth it. Besides, all I need is to be able to fly the thing long enough to store up enough data to make a decent flight model. I don’t need to be able to dogfight. I’ll get the computer to do that.


Trying to figure out servos today. I got one working with the Arduino – sorry, Gemuino – which was really easy. Then, I used the same Gemuino to read the signal wire to the servo using the pulseIn function. Again, really easy.

Then I went to try and do the same thing with the RaspPi. I was a bit perturbed at first, not being able to find any help online, especially after finding lots of stuff for Arduino. Eventually I hit on my answer. Duh: RaspPi is a digital computer, and doesn’t have any analog inputs (which I’m still not really sure would work for reading a servo signal anyway). And the digital inputs are no help because the O/S doesn’t time slice nearly fine enough to parse the PWM servo signals, which are on the order of ~500-2500 microseconds.

I briefly looked into analog to digital converters (ADCs), but eventually just settled on buying an Arduino Nano instead. So, the Nano will read the four input values and somehow, probably serial via USB, send the values to the Pi.Hopefully the throughput will be decent, or I’ll have to revisit ADCs.


So I got my airplane. It’s the Apprentice S 15e. Before I was worried about whether the Pi would fit inside, but I needn’t have been: it will fit the Pi in its case, the GPS, the the accelerometer, the power supply (the plane’s power supply is 11V, everything else uses 5V), the range sensor, and all of the wiring between them all. In fact, turned sideways the Pi fits in nice and snuggly-like against the Styrofoam sides. Seriously, the payload in this thing is huge. I’ll just need to be careful with the overall balance: there’s a center of gravity to the plane about which the manual is very specific.

The receiver connects to all of the servos via standard headers, meaning I won’t have to cut any wires or otherwise do anything to the kit that can’t be easily undone. And there are only four outputs: ailerons, elevator, rudder, and throttle. I’m thinking that this means that there are plenty of pins on the Pi for both input and output, but I’ll need to check. The plan is to collect data by reading the three sensors, and also directing the servo signal wires to Pi inputs so that every control command can be recorded. (I’ve never done this before, so I’ll have to test whether this actually works.) Putting the two together, I should be able to build a decent flight model, if I can collect enough data before crashing the plane to destruction.

Oh, I also was able to change some defaults in the GPS receiver. I used NMEA PMTK commands to set the fix and update rates. (E.g. the rate at which it gets a position fix, and the rate at which it reports the fix via UART respectively.) The details for doing this were in the header file of the Adafruit GPS library. Don’t forget to add “\r\n” to the end of the strings that you send. The idea here was to set these values for as fast updating as possible. (Note that the documentation in the same file says that the minimum settings are 5Hz, or every 200ms, but I found that the minimum it would accept was 3Hz, or 330ms.) Then, I can boxcar the readings over, say, 1 second, and hopefully end up with greater accuracy overall. I have not yet tested this theory.

So, next step is to get the plane powered up, and check that everything works. Then I start hacking. I want to pull a wire off of each output (servos and throttle) and attach to the Pi to see if I can collect data. Oh, and I’m using Pi4J for GPIO access via Java. I don’t know who wrote this, but it looks like a really sweet piece of coding so far. Kudos gentlemen.

GPS RaspPi pins

I’m probably going to do some GPS testing directly against the workstation, and so i just wanted to note the connections between it and the RaspPi, using the pin numbers, not the GPIO identifiers.

TX -> 10

RX -> 8

GND -> 6

VIN -> 4


AGI again?

I was thinking about the GPS stuff, in particular about how the accuracy doesn’t seem that great. Of course I’ve only tested so far a walking speed – it could be much better at more typical flying speeds. But even if it isn’t, I’m thinking that the accelerometer (which I still haven’t received – I’m really starting to like Amazon Prime) could help here. When you put the two together, GPS and acceleromoter, you might be able to determine location and velocity much more accurately than with only one or the other.

This got me thinking about mental modalities, i.e. vision, hearing, proprioception, and the vestibular system, which work together to provide similar, otherwise hidden, information. Which is kind of neat since this is the sort of thing I previously wanted to research, but for which I never had any data. It seems, whether I meant it or not, I’m back in the AGI world.

Oh, BTW, I got some code hooked up to use Google maps to visualize a GPS recording. It’s based off of code from PubNub.

GPS testing

So I took my GPS for a walk around the block. I wrote a little thingy to read the serial stream. I will get a github repo created soon, but in the meantime here is the code. It depends on jSSC, which is available from maven here. (I just added it to the build.gradle file, which I will put in the repo.)

package lohbihler.manfred.nmea;

import java.io.PrintWriter;
import jssc.SerialPort;

public class SimpleLogger {
    public static void main(String[] args) throws Exception {
        SerialPort port = new SerialPort("/dev/ttyAMA0");
        boolean success = port.openPort();
        if (!success)
            throw new RuntimeException("Failed to open serial port");
        success = port.setParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        if (!success)
            throw new RuntimeException("Failed to set serial port params");
        try (PrintWriter out = new PrintWriter("nmea.out")) {
            while (true) {
                String s = port.readString();
                if (s != null)

Anyway, shortly after this outing my hard drive decided it was going to die. I knew about this for a while actually, but only recently did it become clear that I was going to have to figure out how to get everything on the new hard drive that had been sitting around since last November. Sadly it was too late to do a simple clone. Reflect would die with various error codes at various moments, and diagnostics declared unilaterally that there were too many bad sectors. Kind of annoying since the drive started up without any issues besides a bit of slowness for months, and there had only been one BSOD that whole time. Long story short, $170 CAD for a new Windows license and about a week of getting it installed (don’t bother cutting to a CD, go straight for the bootable USB) and getting everything else installed and copied over and configured et al later, and we’re back up and running.

I managed to get my NMEA parsing code all written up too. Or at least enough code for my purposes.

The bottom line so far is that the GPS is accurate within about a meter, which might be good enough. I will probably have to look into what I mentioned before: how to smooth the variability.

It also occurred to me that there is a landing/takeoff problem, i.e. how does the plane know where the ground is. For landing I’m thinking if it’s maybe 10cm or so off the ground it could just cut the engine and hope for the best, but 2 cm would be a lot better. Even if it knew the current elevation above sea level (I could perhaps always take off and land at the same place(s)), the GPS isn’t nearly accurate enough to guess lift off and touch down. So with only this speculation I threw fiscal caution to the wind and blew $4.42 for this. Can’t wait to test out that baby.

Weather is getting warmer, so it might be time to pick up the actual aircraft. I’m still worried about fitting everything inside. I’ll have to become good friends with the nice fellows at the hobby shop so that they maybe let me take a look inside some of the boxes before I buy. Or maybe I should just expect to spend a few hundreds. Chances I’ll destroy the first plane anyway just trying to learn to fly it.