A Year Later: OpenCPN on a Raspberry Pi 3

It’s been over a year since I’ve tried to use OpenCPN on a Raspberry Pi, and I thought I’d see what the community has done since then.

Wow.

Simply Wow.

A Raspberry Pi 3, with the latest versions of Raspbian/PIXEL and OpenCPN matches up as good as commercial entry-level chartplotters on the market today. This platform has really come a long way in the past year, and I’m truly impressed.

Recipe:

Hardware

  • Raspberry Pi 3, Power and HDMI cables, SD card, & case
  • HDMI Monitor
  • USB Keyboard with integral trackpad
  • USB Wifi Adapter
  • GlobalSat BU-353-S4 USB GPS Receiver
  • dAISy Hat AIS Receiver

Set Up Rasbian

  • I burned the most recent version of Raspbian/PIXEL (March 2017) onto a 32GB SD card, inserted it into a RPi3, and booted. In seconds I had a desktop.
  • Connect to WiFi with a USB dongle
  • I removed the products I don’t need:
    sudo apt-get purge wolfram-engine mathematica-fonts sonic-pi nodered libreoffice
    sudo apt-get autoremove
  • Update Kernel to latest for OpenGL
    sudo branch=next rpi-update
  • Reboot
  • sudo apt-get update
    sudo apt-get upgrade
    sudo apt-get install mesa-utils
  • sudo raspi-config
    • Set Timezone
    • Set Locale and Keyboart to US English
    • Change graphics memory split to 128MB
    • Enable OpenGL Driver
      • With Full KMS, I had problems with the display not working after a reboot, so I needed to use Fake KMS, Option G2. To recover, edit config.txt and comment out the dtoverlay=vc4-kms-v3d (or edit to be vc4-fkms-v3d)
  • Turn off screen blanking
    • sudo nano /etc/lightdm/lightdm.conf
      • [SeatDefault]
      • xserver-command=X -s 0 dpms
  • Reboot
  • Run glxgears
    • I was running glxgears at 60fps and 2% CPU
    • At full screen I saw 30fps and 11% CPU
  • Run glxinfo
    • Direct Rendering: yes
    • Renderer string is the Gallium renderer, and not Software Rasterizer
    • OpenGL Version string is 2.1 Mesa 13.0.0

Install OpenCPN

  • Install required supporting tools
    • sudo apt-get install gpsd gpsd-clients screen
      • This required a reboot to work properly
  • Install the OpenCPN software from the Ubuntu PPA
    • In previous recipes I have built from source. To save time, and because building from source truly isn’t necessary, this time I just used the pre-built packages
    • sudo nano /etc/apt/sources.list
    • sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys C865EB40
      sudo apt-get update
      sudo apt-get install opencpn
  • Download charts from NOAA
    • I simply create a directory under the user account to hold all OpenCPN files, and a Charts subdirectory
    • After extracting, to pre-render all the charts, from a terminal window run:
      • opencpn -unit_test_1 0
  • Set up GPS
    • I actually had a bit of difficulty for the first time in a while getting GPS set up. If I ran cgps or xgps I got no data, but running gpsmon showed a good solid fix
    • This is the sequence of commands I followed to get it working (and works on subsequent reboots):
      • sudo dpkg-reconfigure gpsd
      • sudo nano /lib/systemd/system/gpsd.socket
        • Change ListenStream=127.0.0.1:2947
          to
          ListenStream=0.0.0.0:2947
      • sudo nano /etc/default/gpsd
        • START_DAEMON="true"
          USBAUTO="true"
          DEVICES="/dev/ttyUSB0"
          GPSD_OPTIONS="-n"
          GPSD_SOCKET="/var/run/gpsd.sock"
      • sudo killall gpsd
        sudo gpsd /dev/ttyUSB0 -F /var/run/gpsd.sock
    • Connect GPS to Opencpn
      • Open Settings, Connections Tab. Click Add Connection
        • Type: Network
        • Protocol: GPSD
        • Address: localhost
        • DataPort: 2947
        • Priority: 1
        • Control Checksum: Checked

Setup dAISy Hat

  • Update config.txt to enable UART
    • sudo nano /boot/config.txt
      • add the following lines
        • # Enable UART
        • enable_uart=1
        • dtoverlay=p3-disable-bt
  • Disable Console Serial
    • sudo nano /boot/cmdline.txt
      • remove the following:
        • console=serial0,115200
  • From the Menu, select Preferences->Raspberry Pi Configuration
    • Interfaces Tab
      • Enable Serial
  • Reboot
  • Test Hat Serial communication
    • screen /dev/serial0 38400
      • [ESC] brings up a menu
      • ‘T’ sends a test message every 5 seconds
      • [CTRL-A], ‘K’, ‘Y’ to exit
  • Add AIS stream to OpenCPN
    • Open Settings, Connections Tab. Click Add Connection
      • Type: Serial
      • DataPort: /dev/serial0
      • Baudrate: 38400
      • Priority: 1
      • Control Checksum: Checked

Results

OpenCPN running ENC charts, with location set by GPS, and AIS contacts listed.

This also shows 24 AIS Targets in a list, as well as xgps running in the background. 6% CPU.

AIS Target Query. Deep zoom into the Hylebos waterway to find three vessels currently underway, including the cargo ship Indigo Lake. These vessels were approximately three miles away from my current location

References

Advertisements

RPi2 with Hardware OpenGL & OpenCPN Recipe

On and off for the past few months I’ve been trying to figure out a problem with OpenGL and the Raspberry Pi 2. Quite simply I want to be able to build, from bare metal, a chartplotter system on a RPi2 that has AIS, GPS, Vector charts, and 10fps. Based on the hardware specs, it should be very possible. However, in practice, I have seen only 1-2fps.

The very good folks on cruisersforum have been hard at work trying to solve this problem, and over the past few days I resurrected this project took a stab at using the latest versions.

On a new board and SD card, the recipe below was used to create a working version of the RPi2 that meets these performance goals. With this setup, I get very usable performance with OpenGL enabled, less than 5% CPU with a satisfactorily responsive UI, USB AIS and GPS, and Vector Charts.

Hardware

  1. Raspberry Pi 2
  2. 16G micro SD card
  3. Tontec 7″ HDMI screen
  4. USB keyboard and touchpad
  5. USB WiFi Dongle
  6. USB GPS Puck
  7. dAISy USB AIS receiver

Build the Pi with a Jessie Distribution and the open source OpenGL drivers

Image the most recent version of Raspian onto a class-10 sd card. I used Raspbian Jessie, with a build date of 3-18-2016.

Once booted, connect to the internet, and then in Raspi-config:

  • Expand file system to full card
  • Change user password
  • Automatically boot to desktop
  • Set overclock to high
  • Set language, keyboard, and locale appropriately.
  • Enable the experimental GL driver in Advanced Options

Reboot, and then:
sudo apt-get upgrade
sudo apt-get update
sudo rpi-update
sudo ldconfig
sudo reboot

Install required libraries

apt-get install cmake gettext gpsd gpsd-clients libgps-dev wx-common libwxgtk3.0-dev libgtk2.0-dev wx3.0-headers libbz2-dev libtinyxml-dev portaudio19-dev libcurl4-openssl-dev libcairo2-dev mesa-utils

Build OpenCPN

git clone git://github.com/OpenCPN/OpenCPN.git
cd OpenCPN

Optionally, get the tides & currents, and high-resolution shoreline data.

  • From  http://sourceforge.net/projects/opencpnplugins/files/opencpn_packaging_data/ download the latest opencpn-gshhs and opencpn-tcdata files
  • Extract their contents into data/gshhs

mkdir build
cd build
cmake -DBUNDLE_DOCS=ON -DBUNDLE_TCDATA=ON -DBUNDLE_GSHHS=FULL ../
make
sudo make install

Download the appropriate charts:

  • For all US waters, it’s very easy to find appropriate charts at NOAA
  • I decided to try ENC vector charts this time.
  • Expand into an appropriate directory. I used /usr/local/include/Charts
  • run opencpn with the -unit_test_1 flag to ingest and process all the charts that were downloaded

Run OpenCPN and set the chart directory to wherever you’ve downloaded the NOAA charts. Exit OpenCPN and then edit .opencpn/opencpn.conf and add the following lines:

MEMCacheLimit=20
NCacheLimit=5

Setup USB

  • Start the gpsd service
    • sudo gpsd /dev/ttyUSB0 -F /var/run/gpsd.sock
  • Launch OpenCPN
  • Add a connection for the gpsd service
    • Settings -> Connections -> Add Connection
      • Network
      • GPSD protocol
      • localhost address
      • 2947 port
  • To get GPSD to run on startup automatically, modify the /etc/default/gpsd file like so:
    START_DAEMON="true"
    GPSD_OPTIONS="-n"
    DEVICES="/dev/gps0"
    USBAUTO="false"
    GPSD_SOCKET="/var/run/gpsd.sock"

Setup AIS

  • sudo apt-get install screen
    sudo usermod -a -G dialout $USER
    screen /dev/ttyACM0
  • CTRL-A, K exists screen
  • Start OpenCPN and go to Options -> Connections | Data Connections, and Add Connection. Select a Serial message, and set the DataPort to the dAISy USB message port (generally /dev/ttyACM0). Set the Baudrate to 9600, and uncheck Control checksum. Apply and return to OpenCPN.

Finalize the Setup

  • Install VNC Server so you can easily access the device from other computers on the network
    • apt-get install tightvncserver
    • tightvncserver :1
      • enter a password twice, and optionally a view-only password
      • Then you can VNC into the device from a computer running on the attached network
  • Remove extraneous packages
    • sudo apt-get  remove --purge wolfram-engine minecraft-pi libreoffice xscreensaver
  • Disable screen blanking
    • sudo nano /etc/lightdm/lightdm.conf
      [SeatDefault]
      xserver-command=X -s 0 dpms

References

OpenCPN on the Raspberry Pi 2 with AIS

This past weekend I gave a presentation to a group of local sailors and boaters touting the benefits of running OpenCPN on a System-on-a-chip board such as the Raspberry Pi.

In preparation for the talk, I rebuilt OpenCPN on the newest generation Raspberry Pi 2, running the most current source code available on git at the time (4.1.412). I didn’t have the time to do any performance tuning of the system, and ran it out of the box all weekend long. OpenGL was not running appropriately, so performance was less than previous installations, but it did run out of the box, following the Raspberry Pi installation steps, with the additional step of compiling OpenCPN from source to get the most recent version.

However, I did add a new feature to the installation, and one that I am extraordinarily happy with — the ability to receive AIS data on OpenCPN running on a Raspberry Pi 2!

First AIS Contact

The first AIS contact!

In order to read AIS messages, I purchased a demo board of a product called dAISy designed by Adrian Studer. I’ve had the board in my hands for a few months, but as always work and life gets in the way of projects such as this. This past weekend I sailed my boat the 35nm or so from Tacoma to Poulsbo and had a few hours to play around with the chartplotter AND dAISy in an environment where the messages could be received. In short, it worked exceptionally well, and you can see the very first AIS contact that was picked up by the system just as we exited the north end of Colvos Passage in the photo above.

dAISy is a very well designed board that is easy to use. Just connect an antennae to the BNC connector, and a USB cable to the mini-USB port. In the current Raspbian image, all the required kernel support is set up, so simply plugging in the board is enough.

To test that it is working, perform the following test:

sudo apt-get install screen
screen /dev/ttyACM0

To exit screen, type CTRL-A, followed by K. At this point you should have a terminal window set up, and if you are within range of AIS targets you should see a list of AIVDM NMEA messages, that look something like this:

!AIVDM,1,1,,A,85Mwp`1Kf3aCnsNvBWLi=wQuNhA5t43N`5nCuI=p<IBfVqnMgPGs,0*47 !AIVDM,2,1,6,A,8>qc9wiKf>d=Cq5r0mdew:?DLq>1LmhHrsqmBCKnJ50,0*30
!AIVDM,2,1,1,A,55?MbV02;H;s<HtKR20EHE:0@T4@Dn2222222216L961O5Gf0NSQEp6ClRp8,0*1C

If you are not within range of AIS targets, you can still confirm that dAISy is responding by accessing it’s menu. Simply press [esc] and the menu will display such as the screencapture from Windows shows below:

AIS

If you turn on debug messages, and you are out of range (or have a very low signal) then you will see a series of messages like the following:
sync A RSSI=-085dBm
error: invalid stuff bit
sync A RSSI=-073dBm
error: CRC error
sync A RSSI=-086dBm
error: invalid stuff bit
sync B RSSI=-071dBm
error: CRC error
sync B RSSI=-087dBm
error: CRC error
sync B RSSI=-086dBm
error: invalid stuff bit

Then the last step is to render the targets in OpenCPN. It’s a fairly simple process of creating a new data connection. Go to Options -> Connections | Data Connections, and Add Connection. Select a Serial message, and set the DataPort to the dAISy USB message port (generally /dev/ttyACM0). Set the Baudrate to 9600, and uncheck Control checksum. Apply and return to OpenCPN.

If you think you have done everything correctly, but you are still not seeing AIS contacts or messages, and especially if ‘screen’ is showing messages, make sure that your user is added to the ‘dialout’ group. To add yourself to the group:
sudo usermod -a -G dialout $USER

Otherwise, enjoy the benefits of knowing where other vessels are, where they are heading, and who they are!

20150426_112418

20150426_112546

Lots of traffic to this little blog

Originally, I built this blog for myself. I wanted to use it to track maintenance and projects on our boat and to share trip reports and stories with friends and family; basically a captain’s log online.

But in the past few months, the traffic to this blog has dramatically increased, due to my OpenCPN Chartplotter project. It has been linked to in other forums, by other people that have found it in searches. It has been interesting to see.

I’ve also answered a few emails and forum posts from people that would like me to build one for them — either because they don’t have the time, or that have no interest in the electronics/software DIY aspect of the project.

Since I have a bit of an independent/entrepreneurial streak in me, this has had me thinking for some time that it might be interesting to create a device that takes all the leg-work out of creating a chart-plotter using standard components and OpenCPN. Everything, both hardware and software, that I’ve used so far has been Open Source, which allows commercial application and use. I’m not looking to quit my day job and be a chart-plotter vendor — Garmin, Lowrance, and Raymarine are already well established.

The idea is simply that the baseline system could be provided in an out-of-the-box solution. A low-powered device that can display GPS-located charts on a 7 or 10″ sunlight readable display by simply providing 12v power. Is this something that more people would be interested in?

CubieTruck Chartplotter Setup

As I did with the Raspberry Pi, below is a complete set of instructions to set up the CubieTruck as a dedicated OpenCPN chartplotter:

20141004_102259

Base Operating System

  • Download the latest Cubian OS image from Cubian.org
  • Write the image to a micro SD card (I used a 32 gig card) I used Win32DiskImager on my Widows machine
  • Insert SD card into the CubieTruck, connect HDMI monitor, Keyboard, and mouse, and boot.
  • Login as cubie/cubie
  • Setup Wifi
    • sudo modprobe bcmdhd
    • edit /etc/modules and add bcmdhd module so that WiFi will be available on reboot
  • apt-get update
  • apt-get upgrade
  • Install cubian-update
    • The deb command was already in /etc/apt/sources.list so that step could be skipped
  • Turn off the screensaver
    • apt-get remove xscreensaver
  • Auto-Login to the system
    • create a new user and grant them sudo access
      • I can’t understand vi (Hey, I’m a Windows guy), so I used nano to update visudo:
        • su root
        • export VISUAL=nano; visudo
    • edit /etc/lightdm/lightdm.conf and add:
      • autologin-user={new user}
      • autologin-user-timeout=0
    • In a terminal run:
      • sudo groupadd autologin
      • sudo gpasswd -a {user} autologin
  • Install Hardware acceleration
    • apt-get install mesa-utils build-essential git cmake libx11-dev
    • install a user version of libGL from source
    • install libGLU from source
    • set the LD_LIBRARY_PATH environment variable to use usr/local/lib
      • edit ~/.bashrc file
        • add export LD_LIBRARY_PATH=/usr/local/lib
    • Verify with glxgears
      • over 100 FPS and a minimum of CPU usage

Setup OpenCPN

  • Build and install OpenCPN
    • $ git clone git://github.com/OpenCPN/OpenCPN.git
    • $ cd OpenCPN/
    • $ mkdir build
    • $ cd build
    • $ cmake ../
    • $ make
    • $ sudo make install
  • Download the appropriate charts
    • For all US waters, it’s very easy to find appropriate charts at NOAA
    • I decided to try ENC vector charts this time. On the Raspberry Pi the rendering was just simply too slow and I used Raster charts.
    • Expand into an appropriate directory. I used /usr/local/include/Charts
    • run opencpn with the -unit_test_1 flag to ingest and process all the charts that were downloaded
  • Restart the gpsd service
    • sudo killall gpsd
    • sudo gpsd -n -D 2 /dev/ttyUSB0
    • Note: cubian auto-starts the gpsd service on a restart
  • Launch OpenCPN
  • Set the charts directory to the directory you expanded your charts into
  • Add a connection for the gpsd service
    • Settings -> Connections -> Add Connection
      • Network
      • GPSD protocol
      • localhost address
      • 2947 port

At this point, in one weekend day, I had a complete chartplotter solution running on a CubieTruck:
20141005_175023

Resources

CubieTruck Upgrade to CPN Pilot

I love the Raspberry Pi. I really do.

I have three of them, and will probably purchase more. As a movie player (XBMC), or low powered (battery) computer, it really is great. And the price cannot be beat. Also, if you simply want a cheap integrated chartplotter, the Raspberry Pi is by far the most cost-effective platform. It also has the best community support.

However, for the autopilot/chartplotter project, I actually am running into limitations with it’s processing power. For example, when testing AIS functionality, the CPU was pretty much maxed out when running the services as well as OpenCPN — so much so that the entire UI would hang for a few seconds.

After researching the different inexpensive, low-powered, single-board computers, I decided to go with CubieBoard. Specifically the CubieBoard3, aka CubieTruck. It has a DualCore 1Ghz A7 SOC processor, 2Gigs of RAM, 4Gigs of NAND flash memory for an OS as well as micro SD, built in WiFi and Bluetooth, and most importantly a graphical processor that complies with OpenGL ES 2.0/1.1 so the graphics can be offloaded from the CPU. It should be more than capable for my needs in this project, especially since I still consider the performance of the Raspberry Pi to be sufficient.

It does come at a cost — It’s about twice the price of a Raspberry Pi, it consumes about 1.5 times the energy (2 amps at 5v), and is about twice the size physically:

RPI and CT

AIS on the CPN Pi-lot project

First of all, what is AIS?

Automatic Identification System, or AIS for short, is a system for automatically displaying location, tracks and information about ships (and other large vessels) It works automatically, transmitting the data over standard VHF frequencies. The information provided is then generally rendered by a chartplotter or other software.

There are some very interesting websites on the web that display AIS information in near realtime. MarineTraffic.com is one of my favorites.

I wanted to add the capability to receive real-time AIS information and display it on the CPN Pi-lot project I’ve been building. Fortunately, once again, the hard work and research has already been performed by other people across the world. They have discovered that USB software defined radio dongles (SDR-RTL) have the capability to extract AIS information using cheap and readily-available hardware and software.

I recently purchased a usb dongle from Amazon that was listed to work with SDR-RTL, and wanted to see if I could get any usable data out of it.

AIS on the Raspberry Pi

I followed the instructions on a few different blogs and forums. The following is the steps I performed to get AIS around all the small gotchas:

sudo apt-get install git-core
sudo apt-get install cmake
sudo apt-get install libusb-1.0-0-dev
git clone git://git.osmocom.org/rtl-sdr.git
cd rtl-sdr
mkdir build
cd build
cmake ../ -DINSTALL_UDEV_RULES=ON
make
sudo make install
sudo ldconfig
cd ~
sudo cp ./rtl-sdr/rtl-sdr.rules /etc/udev/rules.d/
sudo reboot
rtl_test -t

At this point, the rtl_test program displayed the following error:

Kernel driver is active, or device is claimed by second instance of librtlsdr.
In the first case, please either detach or blacklist the kernel module
(dvb_usb_rtl28xxu), or enable automatic detaching at compile time.
usb_claim_interface error -6
Failed to open rtlsdr device #0.

To resolve it, I needed to edit the /etc/modprobe.d/raspi-blacklist.conf file, by adding the following lines:

blacklist dvb_usb_rtl28xxu
blacklist rtl2832
blacklist rtl2830

After a reboot, the rtl_test program confirmed that I was receiving samples from the USB dongle.

The next step was to install an AIS receiver:

sudo  apt-get  install  libasound-dev  libpulse-dev
wget  http://www.aishub.net/downloads/aisdecoder.tar.gz
tar  zxvf  aisdecoder.tar.gz
cd  aisdecoder
mkdir  build
cd  build
cmake  ../ -DCMAKE_BUILD_TYPE=Release
make

Then, open up two terminal windows. In the first terminal, type:

mkfifo /tmp/aisdata
rtl_fm -f 161975000 -g 40 -p 95 -s 48k -r 48k /tmp/aisdata

and in the second termnial, type:

./aisdecoder -h 127.0.0.1 -p 10110 -a file -c mono -d -l -f /tmp/aisdata

At this point, it appears I’m getting data flowing through, as I see the following:

Level on ch 0: 50 %
Level on ch 0: 50 %
Level on ch 0: 50 %
.
.
.

Since I am currently at home, and not within range of any ships that should be broadcasting AIS, I believe this is a success. However, I will need to bring all this gear down to the boat, test it there (where I know there are AIS signals) to confirm.

References