They Can Talk?

The next big hurdle in this DIY chartplotter project (which I need to name, because that’s too long to type), is to get the Raspberry Pi microprocessor to communicate with the Arduino motor control unit in the stern.

After talking to a few people, including Al Thomason (who has much more experience in Arduino and microprocessors than I), I have decided that the communication architecture will be NMEA 0183 over the I2C bus. I chose NMEA 0183 is a very easy, standardized messaging protocol, with many devices supporting it. It makes sense that this project, in the end, should be able to communicate with other devices. The new NMEA 2000 format has all sorts of political and copyright issues that I don’t want to deal with until it gets figured out by others. I chose I2C because it’s only 3 wires, and you can have over 100 devices on the bus (ignoring details like capacitance, interference, and device support). With a repeater, or something similar, the distances between devices shouldn’t be a problem either.

So, with that in mind, I decided to perform a test to see if I could get the Arduino device and the Raspberry Pi device to communicate with each other. For this test, the communication is going to be “backwards” – that is, the Arduino is going to send a message to the Raspberry Pi, rather than the other way around.

Borrowing heavily from pages on the web , I wrote a simple Arduino sketch that counts from 0 to 360 and back to 0 again, every second, infinitely, to generate a fake compass heading. I then generate an NMEA $HCHDG sentence with that heading inserted – once consumed by OpenCPN, the heading should slowly rotate around the compass rose back and forth. The code for the sketch is here:


#include <Wire.h>

#define SLAVE_ADDRESS 0x04
int heading = 0;
bool increasing;
String buffer;

/*
    Setup Method
*/
void setup()
{
    //Setup Arduino for I2C Slave
    pinMode(13, OUTPUT);
    Serial.begin(9600);
    Wire.begin(SLAVE_ADDRESS);
    Wire.onReceive(receiveData);
    Wire.onRequest(sendData);

    Serial.println("I2C Configured");

    //Setup communications
    buffer = "";
    increasing = true;
    heading = 0;

}

/*
    Loop Method
*/
void loop()
{
    //Get compass heading
    getHeading();

    //Send Compass heading message across wire
    sendData();

    delay(200);
}

/*
    Method to receive data across I2C Bus
*/
void receiveData(int byteCount)
{
    while (Wire.available())
    {
        char c = (char) Wire.read();
        buffer += c;
        if (buffer.endsWith("\r\n"))
        {
            Serial.println(buffer);
            buffer = "";
        }
    }
}

/*
    Method to send data across I2C bus
*/
void sendData()
{
    Wire.beginTransmission(SLAVE_ADDRESS);
    Wire.write(getNMEA());
    Wire.endTransmission();
}

/*
    Method to return current heading as an NMEA0183 HCHDG message
*/
char *getNMEA()
{
    char nmeastr[32] = "$HCHDG";
    char intStr[8];

    strcat(nmeastr, itoa(heading, intStr, 10));
    strcat(nmeastr, ",,,,");

    //Generate checksum
    int checksum = 0;
    for(int i=0; i&lt;strlen(nmeastr); i++)
    {
        checksum ^= nmeastr[i];
    }

    strcat(nmeastr, itoa(checksum, intStr, 10));
    strcat(nmeastr, "\r\n");

    Serial.println(nmeastr);
    return nmeastr;
}

/*
    Method to get the current compass heading
*/
void getHeading()
{
    //TODO: Pull from digital magnetometer device. Currently just makes up a heading
    if (increasing)
    {
        if (heading < 360)
        {
            heading++;
        }
        else
        {
            increasing = false;
        }
    }
    else
    {
        if (heading > 0)
        {
            heading--;
        }
        else
        {
            increasing = true;
        }

    }
    Serial.print("Current Heading : ");
    Serial.println(heading);
}

As for the physical connection between the devices, I decided to go with standard CAT-5 cable. I had a couple extra breakout boards for Ethernet jacks, so I wired those up directly to the SDA and SCL lines on both devices, using only three of the 8 wires available:

IMG_20130829_104530

Now, to see if they can talk.

References:

Advertisements

A DIY Chartplotter

It only took two evenings to get a Raspberry Pi chart plotter built out.

Yesterday I got OpenCPN up and running following instructions found on the web, and today I finished the process to the point that the hardware is a fully functional chart plotter.

The first improvement that had to be made was getting charts installed in OpenCPN. Thankfully NOAA provides free versions of nautical charts for US Waters and most frequented coasts regionally. It was simply a matter a downloading the appropriate charts (Washington State, and Region 15, Puget Sound) and unzipping them to a folder. I then added that folder to OpenCPN using the built-in options screen, and after a few minutes of compiling, the charts displayed.

IMG_20130821_174054

Then I needed to add GPS capability. First, I hacked together a few wires to an existing Venus GPS Module that I had from a previous Arduino project. This was a simple matter of soldering 3.3v power, ground, and TX/RX digital inputs between the items:

IMG_20130822_181042

Then I installed the GPSD service from the web, run the service, create a socket for it, and verify that the hardware is receiving the signal. Currently I need to hand-type this sequence each time I reboot the microcomputer: 
$ sudo service gpsd restart
$ sudo killall gpsd
$ sudo gpsd /dev/ttyAMA0 -F /var/run/gpsd.sock

Eventually this will be added to a script that will run on boot.

And then to test: 
$ sudo cgps
or
$ sudo xgps

And behold, signals from outer space:

IMG_20130821_191632

Now that all the pieces are in place, the final step is to add this GPS device to OpenCPN so that it can read the information:

IMG_20130822_180807

In this picture you should be able to see a rendered nautical chart of Puget sound, Heading, GPS satellite status, GPS location, and  Compass gauges on the left, and my “boat”, sitting off in Issaquah, since that’s where my GPS device is located.

It’s running at about 80% of the Raspberry Pi’s capability, at 1280×1024 with no overclocking.

And boom goes the dynamite.

It’s ALIVE!

My Raspberry Pi board showed up in the mail today. After a few minutes downloading and expanding the operating system, I got the machine up and running:

IMG_20130821_132454

Thanks to the very helpful walkthrough/tutorial at http://www.agurney.com/raspberry-pi/pi-chart , I now have version 3.2.0 of OpenCPN running on a brand new Raspberry Pi Model B. At this point I just have it up and running… No charts have been installed, GPS is not hooked up, there are no plugins installed, etc. But OpenCPN is clearly running:

IMG_20130821_143807

Meanwhile, on the Arduino side of the system, I was playing around with a 128×64 Graphic LCD screen that I had laying around. I plugged it in and found a library written for it that worked with very few changes. I doubt that the Arduino will actually drive an LCD, as it’s not in a convenient location to do so (down near the rudder post), but it was good to see that it worked regardless.

IMG_20130821_145943

An Easy Tile Upgrade

While wandering through a home-improvement store, my wife came across some peel-and-stick tiles. We looked at a few different options, but decided to go with the product that seemed to be the easiest to install – a “stainless” pattern with no need for any grout at all. We simply cut the tiles to size, using a razor blade to cut between the tiles, peeled of the adhesive backing:

IMG_20130818_161855

… and pressed down:

IMG_20130818_161822

There’s not a straight line on a boat, and the “rectangle” countertop was no exception. So of course some tiles needed to be cut — a Dremel made quick work of these tiles, and it was surprisingly easy to get straight cuts with it.

After a few hours, the project was complete. Here are some before-and-after shots:

IMG_20130818_150524IMG_20130818_171733

IMG_20130818_150015IMG_20130818_171744

Lovin LEDs

First up was the V-berth stateroom. There was a mish-mash of fixtures in the master, including one ugly fluorescent. I pulled apart that fixture, and hijacked the switch. Then we installed the strip lighting under the overhang to the switch:

IMG_20130818_195928

20 minutes later the entire master stateroom was lit:

IMG_20130818_200033

Then we moved into the main cabin

I cut the strips along the pre-marked lines, and installed them along the inboard of the curtain track, and tied it into the same switch as the undercabinet lights we did for the countertops project. Then I soldered wires to the next strip, and created some indirect lighting effects:

IMG_20130818_200422

Only a couple hours’ work, and we have much more light, at much less energy usage throughout the interior.

IMG_20130818_200551

Homebuilt Integrated Chartplotter and Autopilot

I’m going to try something bigger next.

My plan is to build an integrated chart plotter with a microprocessor controlled autopilot using open sourced hardware and software. That means, in the end, I should spend less than $500, total, for a fully functional autopilot and chart plotter.

Chart plotter Parts List

  1. Raspberry Pi
  2. OpenCPN
  3. GPS Module
  4. 10″ Display (by far the most expensive item)
  5. LCD display
  6. Controls (buttons, etc)
  7. Case/mounting

Autopilot Parts List

  1. Arduino ATMega 328
  2. Relay board
  3. Ethernet board
  4. Digital Compass
  5. Ethernet crossover cable

As far as implementation is concerned, at this point I have only done some basic research and tests.

I have already hooked up the existing hydraulic pump directly to a 12v source, as you can see below:

The next steps include:

  1. Installing OpenCPN on a Raspberry Pi device and getting that to run. Once installed on the boat, just that will be a nice addition, since I currently use a small PC hooked up to the inverter to power OpenCPN on board.
  2. Build an install a relay shield on my Arduino, and code it to keep a compass heading (I already have a 3-axis compass breakout).
  3. Build and add an Ethernet shield to my Arduino, and get it to communicate with Raspberry Pi
  4. Add a rudder angle sensor to the Arduino module, and display the information on the LCD screen in the cockpit
  5. Extract the NMEA messages (crosstrack, etc) from OpenCPN, and set that as the heading to hold on the Arduino module
  6. Have a drink and let the boat drive itself

There are of course additional features I am planning:

  • Configurable alarm to remind the “helmsman” to do a safety sweep
  • Connect GPS output to VHF radio for DSC service
  • Engine sensors (RPM, temperature, etc)
  • Cameras
  • TBD