Posts

DemocracyBot – Raspberry Pi, LINX Toolkit, WebSockets & NXG WebVIs

DemocracyBot – Why vote every 2-4 years when you can vote every 10 seconds?

For NI Days 2019 I was asked to give a demo of something fun/interesting using a Raspberry Pi and the LINX Toolkit. I was originally thinking of demoing some simple sensors but then I remembered an idea I had a few years ago for a 3D printed two-wheeled robot (I nicknamed it the PuckBot) for STEM/educational use.

At the time I was developing the demo, we were in the throws of figuring out Brexit and the 2019 General Election so I decided to take my PuckBot concept and turn it into a piece of policitical humour instead – DemocracyBot was born!

With the United Kingdom enjoying democracy so much with 4 general elections and the Brexit referendum that I would embrace that spirit of democracy and develop a robot where participants could vote every 10 seconds and instantly see their vote in action. “Why vote every 2-4 years when you can vote every 10 seconds?”

DemocracyBot has a web-based voting interface where participants could cast their vote for which direction they wanted the robot to move in and at the end of each 10 seconds the votes would be added up and the robot would perform the most popular option. There was also score tracking to see who voted with the majority the most times.

DemocracyBot Voting User Interface

DemocracyBot voting interface using LabVIEW NXG.

DemocracyBot Software

All of the code for the project is Open Source and available on the DemocracyBot GitHub repository.

The software consists of a LabVIEW 2019 (or LabVIEW 2020 Community Edition) project for the code running on the Raspberry Pi – the Raspberry Pi runs the LabVIEW code as a deployed RT Executable and listens for incoming player connections (using WebSockets), handles the voting aspect and controls the servos to enact the winning vote.

The player interface is written using a couple of LabVIEW NXG WebVIs – the player interface allows players to connect using their phones/tablets and cast their votes in real-time. There is also an admin interface which is used to enable/disable the servo drive and display the leaderboard.

Raspberry Pi Code (LV2019 / LV 2020 Community Edition)

The LabVIEW current-gen (2019+) code uses the LINX Toolkit to develop code that can run on the Raspberry Pi. The architecture is a Queued-Message-Handler with multiple loops. The code uses our internal QMH library – this is based on the NI Template but with some additional functions and templates.

Vote/Main Message Handler

The main message handler is responsible for handling incoming votes, keeping track of players/votes/scores and sending commands to the servo loop. The tracking of voters is done using the Sets & Maps API added in LabVIEW 2019 – I hadn’t yet had chance to try these out but as someone that quite often uses Variant Attributes for lookup tables/dictionaries they seem like an excellent replacement.

The small timer loop is used to trigger the message handler loop to check the remaining time.

Main Message Loop Code

WebSockets Listener

The WebSockets Listener is responsible for listening for incoming WebSockets connections (TCP/IP) and launching instances of the WebSockets Connection Handler VI when there’s a valid connection.

WebSockets Listener LabVIEW Code

Connection Handler

An instance of the Connection Handler VI is asynchronously launched for each client connection – it listens for incoming WebSockets messages (e.g. a vote) and sends status information back to the clients (e.g. time remaining, vote results). It also converts from LabVIEW data types to JSON and vice-versa.

Connection Handler Code

Servo Control Loop

The Servo Control Loop is responsible for controlling the servos – it receives movement commands from the main message handler and performs. The interface to the PCA9865 Servo Board uses LVOOP to wrap the low-level LINX I2C functions into a more beginner/developer friendly API.

Servo Control Loop

Servo Control Loop

NXG WebVI

The NXG WebVI is based on the QMH template included in NXG 3.1 – it opens a WebSockets connection to the Raspberry Pi and then listens for incoming messages to update the display and front-panel events to trigger sending commands/data to the Raspberry Pi.

User Interface LabVIEW NXG Code

WebVI Hosting on the Raspberry Pi

For this project, I decided to host the WebVIs using an Apache Web Server running on the Raspberry Pi. The NI Web Server for deploying LabVIEW Web Services (as demonstrated in my last post) has been a bit buggy and doesn’t allow you to serve your files from the ‘root’ of the Raspberry Pi (e.g. http://<Pi IP address>).

The build specification includes the NXG WebVI build files and installs them to /home/lvuser/natinst/bin/webroot (where the rtexe is deployed to) on the Raspberry Pi. For the Apache Web Server to show them when we visit our Raspberry Pi’s IP address, we need to install Apache and then configure it to use that folder as the document root.

To install/configure the Apache Web Server on the Pi:

  1. SSH into the Raspberry Pi (e.g. using PuTTy)
  2. Run ‘sudo apt update’ and ‘sudo apt install apache2 -y’ to install the Apache Web Server
  3. Modify (e.g. ‘sudo nano’) the ‘DocumentRoot’ parameter in ‘/etc/apache2/sites-available/000-default.conf’ to ‘/home/lvuser/natinst/bin/webroot’
  4. Modify the ‘/etc/apache2/apache2.conf’ file and change the Directory section to point to your application webroot i.e. ‘/home/lvuser/natinst/bin/webroot’
  5. Restart apache using ‘sudo service apache2 restart’

Deployment

The first step in deployment is to build the NXG WebVIs – this generates a folder of HTML/CSS/JavaScript that we can host using a web-browser on the Raspberry Pi.

The LabVIEW 2019 project has two auto-populating folders (one for the player & admin interface) that points to the build output folder of the NXG WebVIs – this is then included in the LabVIEW 2019 build specification for the Raspberry Pi real-time executable so that the files are automatically copied over to the Pi when we deploy our code to the Raspberry Pi.

You can then right-click on the RT Executable Build Specification to ‘Build’, ‘Deploy’ and ‘Run as Startup’ on the Raspberry Pi.

DemocacyBot Hardware

Parts List

DemocracyBot is made up of a 3D printed chassis (STL file available in the GitHub Repo) and the rest are off-the-shelf parts that are easily available online (e.g. Amazon).

Here is the full parts list:

  • 3D Printed Chassis: The STL file is in the GitHub repo for you to print yourself or order from a 3D printing service. We can also print and send you one – please get in touch if you’d like to order one.
  • Raspberry Pi 3/4: The brains of the operation – runs the LabVIEW code and provides connectivity over Wi-Fi. Raspberry Pi 3 board only – £36 from Amazon UK. Raspberry Pi 4 Starter Kit – £89.99.
  • GeeekPi Mini UPS Power Supply Hat: Allows DemocracyBot to run without a wire on batteries – takes two 18650 rechargeable cells and provides charging capability. £19.99 from Amazon UK.
  • 18650 Rechargeable 3.7V Batteries: Due to shipping regulations, it can be difficult to order these online but you can get 4 batteries plus a torch for £16.99 from Amazon UK.
  • PCA9865 16 Channel 12-bit PWM Servo Driver: The Raspberry Pi doesn’t have onboard hardware-timed PWM so I’m using an external PWM chip for driving the servo wheels – this board provides 16 channels but we only really need two of them. £9.99 from Amazon UK.
  • FT90R Microservos + Wheels: The FT90R are a continuous rotation digital micro servo and you can get a pack of 4 + rubber wheels. £17.99 from Amazon UK.
  • Breadboard Jumper Cables + PCB Standoffs: You might already have some jumper cables & PCB standoffs but you can easily order them – used to mount the Raspberry Pi to the chassis and connect the Pi’s I2C pins and power to the Servo Driver board. £5.95 for 120pc breadboard jumper cable set and £14.99 for 80pc standoff + nuts + screws kit from Amazon UK.
  • (Optional!) Union Jack Flags: These fit nicely over the PCB standoffs or over the GPIO pins of the Raspberry Pi. £2.99 for 10 from eBay.

Prices correct as of 12/01/2020.

This makes the total cost of the robot excluding the chassis under around £100/€100 – even cheaper if you already have the Raspberry Pi.

Assembly Instructions

  1. 3D Print the Chassis
    I won’t go into the details of the print settings used as this will vary from printer to printer – just be aware that it will stick pretty well to the bed due to the large flat bottom surface.
    3D printing DemocracyBot
  2. Attach the Servos and Servo Driver Board
    Using the screws provided with the board/servos, mount the servo driver board and the servos so that the wires pass through the cutout and connect to the first two channels of the servo board PCB. The orange wire of the servo is the PWM signal wire.
    DemocracyBot construction
  3. Mount the UPS/Battery Board
    Screw 4 long PCB standoffs into the mounting holes on the top of the chassis. You may need to drill the holes slightly larger if they don’t screw in easily. Avoid using too much force as the standoffs are fairly fragile – I had the thread break off on a couple of them. The standoffs need to be long enough so that the batteries will fit underneath.
    Take two of the 18650 batteries and insert them into the UPS board – ensuring you insert them the right way round.
    Position the UPS Board onto the chassis and secure using the smaller standoffs provided.
  4. Mount the Raspberry Pi
    You can now mount the Raspberry Pi onto the UPS board using PCB standoff screws – the two springed power pins on the UPS board should line up with and push against the underside of the Raspberry Pi power pins.
  5. Connect the Servo Board
    Connect the power (5V & GND) and I2C from the Raspberry Pi GPIO to the PWM Servo Board. You will need to connect power for both the Servos and the PWM chip, as well as the I2C SDA/SCL lines.
  6. That’s it!
    You should now be able to charge up the batteries by connecting a MicroUSB cable to the UPS/Battery board and see the charging status on the LEDs. You can turn on the Raspberry Pi by pressing the push button and a long-press to turn it off.
Completed DemocracyBot

Completed DemocracyBot

Summary

I really enjoyed working on DemocracyBot – it’s a nice way to demonstrate LabVIEW or the free LabVIEW Community Edition running on the Raspberry Pi. The demo itself went pretty well (some WiFi connection issues aside) and I gave a walkthrough of the code at CSLUG in December 2019 as well.

LabVIEW Community Edition Beta available now!

A few months ago I posted about the announcement from National Instruments at GDevCon#2 about the release of a free LabVIEW Community Edition for non-commercial use.

I’m pleased to announce that as of last week, the beta of the Community Edition is now available!

If you want to get started straight away – click the button and follow the instructions below to get access to the Beta:

Instructions for signing up for the LabVIEW Community Edition Beta

Click the button above or visit ni.com/beta to access the NI Beta signup page – simply select the ‘LabVIEW Community Beta’ and click ‘Apply Now’:

Sign up for the LabVIEW Community Edition Beta

Fill out the form with your details and a little background information about how you’ll be using the Community Edition:

Community Edition Beta Registration Form

You can then press Save to submit your application. It may take some time for your application to be approved – so in the meantime why not check out my previous post about the LabVIEW Community Edition or my series of posts about using the LINX Toolkit with the Raspberry Pi.

Timing – NI Days Europe

The beta release of the Community Edition has come just in time for NI Days Europe taking place next week – I’ll be there demoing a fun little project using a Raspberry Pi and WebSockets on the Thursday – come and say hello if you see me around!

I’ll also be spending some time on the LabVIEW Community booth so you might also be able to find me there!

MAKE-ing with LabVIEW & Raspberry Pi: Part 1 – Introduction

This is the first part in a series about using the Raspberry Pi with LabVIEW and the LINX Toolkit. It follows the announcement of the release of the free LabVIEW Community Edition from National Instruments.

Introduction

I recently posted about the big news from National Instruments that they would be releasing a free LabVIEW Community Edition in 2020 with a beta/preview version available later this year.

Of course, as an NI Alliance partner I use LabVIEW professionally on a daily basis so LabVIEW being free for non-commercial use doesn’t really affect me but the updates to the LINX Toolkit are very interesting – namely that it can now be used in commercial applications. Of course, making LabVIEW free will help to grow its adoption as a language which is definitely a good thing.

To celebrate this announcement, over the coming weeks and months (as I find the time) I am going to be posting a series of articles about using the LINX Toolkit with the Raspberry Pi in LabVIEW.

Over the years I have built up a pretty extensive collection of Raspberry Pis and various bits of useful hardware to that allow a Raspberry Pi to interact with the outside world as you might using LabVIEW and DAQmx/cRIO etc. The goal of these articles is to help you get started with the Raspberry Pi and LabVIEW and to show you some of the things you can do with it.

Raspberry Pi 4 Promo
The Raspberry Pi 4 was launched in June 2019.

An extra bonus to this is the recent launch of the Raspberry Pi 4 – the latest Raspberry Pi board with a more powerful processor (1.5GHz quad core, 1/2/4GB of RAM, USB 3, onboard Wifi/Bluetooth and Gigabit Ethernet. Although not officially supported by the LINX Toolkit yet, I’ll be having a go at getting it up and running with LabVIEW.

MAKE-ing with LabVIEW & Raspberry Pi – Series Topics

I haven’t fully planned out each article but over the course of the series I’d like to cover the following:

  • Getting Started with the LINX Toolkit, Raspberry Pi and LabVIEW
    • Installation & Configuring the Raspberry Pi
    • Building and deploying your first application
    • Raspberry Pi 4
  • On-board communications
    • Networking (TCP/IP over Ethernet/WiFi)
    • Serial / RS-232
  • Data Acquisition / Interacting with the real world
    • Analog I/O
    • Digital I/O
    • Other Sensors
  • User Interface / Display Options
    • Nextion Touch Screen
    • LabVIEW NXG Web Module (which will also be available for free with the Community Edition)
    • Configuring the Raspberry Pi to run a web-page in Kiosk mode
Raspberry Pi hardware and sensors.
Some of the Raspberry Pi hardware we’ll be using over this series of articles

That covers the basics (and there’s a lot there already!) but some other topics I could cover include GSM/GPS. the Pi Zero, using an Arduino as an additional I/O device, Bluetooth communications etc. – please leave a comment if there’s something you’d like me to cover and I’ll try to include it in a future article.

Prerequisite LabVIEW Knowledge?

For this series I’m going to be assuming that you are already fairly familiar with LabVIEW but that are maybe new to the LINX Toolkit and using it with a Raspberry Pi. If you’re new to LabVIEW (maybe you’re checking out the free Community Edition – great, welcome!) then I would suggest starting here or here to learn some of the basics first.

Why use a Raspberry Pi?

The main reason for using a Raspberry Pi is that it is a low-cost and small form-factor computer with plenty of GPIO pins increasing its hardware capability. As an open-source platform, there are 100s of sensors, hats (addon boards), cases and display options available.

Some commercial applications that might be suited to a Raspberry Pi could be:

  • Low-cost low-channel count data acquisition system (e.g. monitoring a handful of analog inputs where timing/accuracy aren’t critical)
  • Headless monitoring system (to monitor a system and report data to the cloud)
  • Low-cost HMI / display module (instead of using a PC alongside a CompactRIO, for example)

Of course, while a Raspberry Pi can be a neat low-cost solution for a headless data acquisition system, it definitely doesn’t replace NI’s other platforms like the sbRIO/CompactRIO or PXI. These systems still have their place as they offer things like:

  • Deterministic control applications with LabVIEW Real-Time and high-speed control/processing with LabVIEW FPGA
  • CompactRIO C-Series modules and PXI cards offer a much wider range of input/output options and much higher measurement specifications (e.g. ranges, resolution, accuracy, channel counts etc.)
  • CompactRIO is a robust/rugged platform that can operate in industrial environments

For this series of articles, I’m going to be exploring the Raspberry Pi’s capabilities through my own hobby project – a storm/weather monitoring station.

Application: Raspberry Pi Storm Station

Most of the time when I’m learning new technologies, I like to have an application in mind as it gives me something to work towards. For this Series, I’m going to be looking at building a Storm Watcher / Weather Station. This is very much a hobby project but it will demonstrate a number of capabilities

Here’s a rough idea of what I want it to be able to do:

  • Temperature, Pressure & Humidity Measurements
  • Lightning Detection (using one of these)
  • On-board display of measurements
  • The ability to push measurement data to the cloud
  • Retrieve current/forecast weather information
An exploded view of a Raspberry Pi with a touchscreen display case.
Raspberry Pi case with a 3.5″ Touchscreen

Why? My mum is a keen weather watcher and often uses websites like this one to see where there are storms etc. – I thought it would be a nice little project so she can start detecting her own storms. The Raspberry Pi is ideal for this as it’s a low-cost piece of hardware with built-in WiFi and there are lots of options for small displays etc.

Up Next: Part 2

Stay tuned for Part 2 where we’ll get started with installing and configuring the necessary software on our PCs. For this we’ll be using LabVIEW 2019 and the latest beta version of the LINX Toolkit.

Free LabVIEW™ for everyone – LabVIEW Community Edition coming soon!

One of the most newsworthy aspects of GDevCon#2 was a very special announcement from National Instruments that they would be releasing the LabVIEW Community Edition – a free version of LabVIEW™ Professional for non-commercial use.

Yes, that’s right…

You will be able to download and use LabVIEW™ for FREE!

Read more