Controls Theory, Programming & Other Stuff
Python love, Matlab hate and random obsessions

A better cv::waitKey function

The deceptively name OpenCV cv::waitKey in OpenCV is the centre of any use of the highgui module. This module makes it easy to create simple user interfaces and the waitKey function, in addition to waiting for keyboard events, is responsible for updating the message pump for all the windows.

The prototype of the function is:

int cv::waitKey(int mills);

Where millis is the amount of time to wait. If millis is 0 the functions waits until a key is pressed. Otherwise, if millis is greater than 0 the function will return the keycode for the pressed key (a …

( Continue Reading )

Quick-Tip: Check that your documentation is up-to-date with clang

It is too easy to forget to update documentation when refactoring code. I often change the parameters of a function and forget to update the doc doc comment which lives on, wrong and misguiding. In c++-land doxygen is the standard documentation generator and it can be configured to warn when a parameter of a function is not documented. This helps but documenting every single parameter of every single function is hard, especially when developing new features which leads to a high false positive rate.

A finer grained approach that only warns if the documentation comment and the function disagree …

( Continue Reading )

Fixing Nodelet does not exist errors in ROS

This error is caused by the fact that ROS does not know about the nodelet. To be sure we can check what nodelets ROS knows about by running:

$ rosrun nodelet declared_nodelets

If the nodelet you are trying to run is not listed in the output then we have confirmed that ROS does not know about it. If you are trying to run a nodelet from another package this usually means that the package itself is not in your ROS_PACKAGE_PATH. This can happen if, for example, the package is not correctly installed.

If you are seeing this error for a nodelet …

( Continue Reading )

A CMake Include Rule for IDEs

When creating a CMakeLists.txt file we would like to only list the .cpp files in the add_executable and add_library commands. Whilst this is sufficient to compile the code (and should be enough for any IDE) most IDE's today will exhibit limited functionality unless the header files are also included in the add_executable and add_library commands.

A simple workaround that preserves the best of both worlds is to create a dummy rule that includes all the headers but doesn't actually do anything. better yet, we can make the rule locate all the headers with a simple GLOB expression:

# Dummy target …
( Continue Reading )

Enable locate on systems with and encrypted home directory

If you have an encrypted home directory on a linux system chances are that you are using ecryptfs. This is the simplest way of encrypting the sensitive parts of your computer. You may also have noticed that the locate command does not find files in your home directory any more.

This behaviour is intentional. locate does not search the disk directly, instead it queries a database file generally called mlocate.db generated by the updatedb tool. This file is updated by the updatedb tool which is usually run with cron and it contains information about every file and folder on …

( Continue Reading )

CMake Tips & Tricks

( Continue Reading )

The development of the telegraph - An Incomplete time-line

Last week I was in London and, true to habit, I went to visit the London Science Museum. I spent a lot of time in the Information Gallery, specifically the exhibits dedicated to the development of the telegraph.

The telegraph, for the first time, made it possible to communicate almost instantly over long distances. In addition to speeding up the distribution of information it created a host of other problems. Things that we take for granted today like time zones can be traced directly to the telegraph.

The rest of this post is (an incomplete) timeline of the development of …

( Continue Reading )

List included directories of a target with CMake

When debugging a CMake build file it is often useful to print out all the directories included by a target. The following snippet does just that. Simply replace ${target_name} and run cmake to see the list of included files.

get_property(includes
        TARGET ${target_name}
        PROPERTY INCLUDE_DIRECTORIES
        )

message(STATUS "Included directories for target ${target_name}")
for (dir in ${includes})
    message(STATUS "  - ${dir}")
endfor()
( Continue Reading )

A very non-comprehesive introduction to ROS package format 2

The ROS package format 2 is the new recommended package format for ROS projects. It supersedes the previous package format with a handful of improvements. Let's take a look at how to structure the new file.

To specify that you are using format 2 you must change the opening <package ...> tag in your package.xml to

<?xml version="1.0"?>
<package format="2">
    ...
</ package>

The changes from format 1 to 2 only affect the tags specifying dependencies. The old <build_depend> and <run_depend> tags are gone now. In there place we have a handful of more explicit tags.

<build_depend>
This tag …
( Continue Reading )

Lobster, a simple template engine for MATLAB

Lobster is a template engine for MATLAB with a syntax quite similar to Jinja, a template engine implemented in python. A template engine is useful if you need to create documents with a repetitive syntax. I use them for example to

  • generate C++ to convert enumeration fields to and from strings,
  • generate HTML content
  • generate latex contents from MATLAB variables.

Let's start with a simple example of the syntax. Create a file called template.tpl with the following contents

Welcome {{ FirstName }},

Your grocery list contains:

{% for item in groceries %}
- {{ item.name }} {% if item.quantity > 1 %}(x{{ item.quantity}}){% endif %}
{% endfor …
( Continue Reading )

Matlab, Linux & Serial Ports

MATLAB on linux follows Ubuntu LTS releases and as such can be quite behind on more recent changes in the linux ecosystem. One recent-ish change that MATLAB still struggles with is the new naming convention for USB-Serial ports. In previous versions of Ubuntu these emulated serial ports were named /dev/ttyUSBX where X is an integer. In newer Ubuntu versions however, these ports are named /dev/ttyACMX, a change that MATLAB has not caught on to yet.

This seemingly small change means that, out-of-the-box, USB-Serial ports will not work at all in MATLAB. The instrhwinfo('serial') command will never see …

( Continue Reading )

CLion & ROS

CLion is a great C++ IDE. It also has the advantage of being cross platform and working on Linux. It has become my IDE of choice on that platform. In this post I'll be sharing a couple of tips to use more productively with ROS.

Sourcing the ROS environment

Unfortunately CLion does not natively understand ROS. This can however be solved quite easily, we just need to change the .desktop file used to launch it so that it sources the ROS workspace. You can find it in ~/.local/share/applications/jetbrains-clion.desktop. If the file does not exist you can …

( Continue Reading )

Setting up CMake and Doxygen

Setting up CMake to build your documentation with Doxygen is really quite simple. All you really need is

find_package(Doxygen)
if(${DOXYGEN_FOUND})

        add_custom_target(doc ALL
            COMMAND ${DOXYGEN_EXECUTABLE} Doxyfile.h
            WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
            COMMENT "Generating API documentation with Doxygen"
            VERBATIM)

endif() # Doxygen Found

This adds a doc command that builds the documentation and includes it in the ALL rule so that it will be built by default. However, we can make this much smarter. For instance, we can easily make doxygen entirely optional by only enabling it with a CMake option.

Here is the snippet I use in all of my projects …

( Continue Reading )

Useful snippets for ROS

Set CMake options in catkin build

To pass CMake options to the catkin build tool simply use the --cmake-args command line switch. For example to set the CMAKE_BUILD_TYPE I would use:

catkin build --cmake-args -DCMAKE_BUILD_TYPE=Debug

To save the so that they are added for every call you can use the --save-config command line option :

catkin build --save-CONFIG --cmake-args -DCMAKE_BUILD_TYPE=Debug

Build a single package

To build a single package just specify its name at after catkin build, e.g.

catkin build my_package

There is also an even more convenient form. If you are in the package directory then you …

( Continue Reading )

Common shortcuts for Bash

Keys Action
Ctrl R reverse-i-search
Ctrl I i-search
Ctrl A Move to start of line
Ctrl E Move to end of line
Ctrl B Move backward one word
Ctrl F Move forward one word
Ctrl K Delete everything from the cursor to the end of the line
Ctrl U Delete everything from the cursor to the start of the line
( Continue Reading )

Matlab Live Script Keyboard Shortcuts

Keys Action
Ctrl Alt L Insert Title (#)
Ctrl Alt H Insert Heading (##)
Ctrl Alt Enter Insert Section Break (%%)
Ctrl I Italic
Ctrl B Bold
Ctrl U Underline
Ctrl M Monospaced
Ctrl K Hyperlink
Ctrl Alt G Latex/Equation
Ctrl Alt Enter Toggle Code/Text Mode

Title, Heading and Section Break can be triggered without the shortcut by typing #, ## and %% respectively.

( Continue Reading )

Fixing the MEvent. CASE! error in MATLAB

This rather annoying issue in MATLAB causes the string MEvent. CASE! to be repeatedly printed to the command window every time you scroll any window in the application. The suggested solution is to run

!synclient HorizTwoFingerScroll=0

as part of your startup file to disable horizontal scrolling. This however does not work on more recent linux versions because the synaptics touchpad driver is being deprecated in favour of libinput. The new solution to this problem is slightly more complex however.

First we need to find the id of the touchpad device with the xinput list command from a terminal (not …

( Continue Reading )

Tips for MATLAB on Linux

Setting up MATLAB can be a hassle. On Linux systems some of the defaults settings can be rather baffling. Here are the first things I do after installing.

Install the matlab-support package

After installing MATLAB you may be frustrated to find that it doesn't appear in the launcher. To run it you need to open a terminal and manually run the matlab command. On Ubuntu-derived systems the matlab-support packages solves this. It creates the missing .desktop files to let the launcher know about MATLAB and offers to solve some common problems with MATLAB on Linux. Pi To install it simply …

( Continue Reading )

Virtual Reference Feedback Tuning

Virtual Reference Feedback Tuning is a data driven method used to design a controller for an unknown plant based on input/output measurements. The method uses a single set of input/output data generated by the plant without the need for specific experiments or model identification.

This is the first post in a series of posts detailing the method and explaining how to use it in MATLAB.

  1. Virtual Reference Feedback Tuning (this post)
  2. VRFT in MATLAB - Coming Soon

How Does It Work ?

The general idea behind VRFT is to transform the control problem of designing the …

( Continue Reading )

Open In File Browser for Linux

On windows when right clicking in the file browser pane you can select Open in file browser to open the current folder in windows explorer. On linux you will simply get an error message stating that this action is not supported on this operating system (as of MATLAB 2016a). The following script provides the same functionality:

%
% Open the specified path in the default file browser (linux only)
%
% The Unix version of MATLAB does not support 'Open in File Browser' so I
% implemented a basic version here. All modern linux distributions should
% support `xdg-open`
%
% To open the MATLAB userpath directory (Documents …
( Continue Reading )

Quickly changing projects in MATLAB

When working on several different projects in MATLAB changing from one project folder to another can be a hassle. Some projects are in a shared dropbox folders, others are in my main MATLAB directory. Most powerful text editors offer a way of quickly changing project directories without having to navigate the folder structure by hand.

This is my solution for MATLAB.

%
% Hacky way of making projects in MATLAB
%
% One hassle of MATLAB is that moving from one project to another is a hassle.
% With this script you only have to type:
%
%   open_project 'my project'
%
% to move from the current directory …
( Continue Reading )

Quick Tip: Receive a pushbullet notification when a ssh login is successful

Pushbullet is a service that allows you to push notifications to your devices. We will be using it to send us a notification any-time somebody successfully connects to our server with ssh.

The cleanest way to run a script when on ssh logins is through PAM. PAM or "Pluggable Authentication Module" is a library called by all the authentication related functions in Linux that provides a way to hook into the authentication process.

The pam_exec module gives us easy access to this system. It allows us to run scripts to react to events like user logins.

Save this script to …

( Continue Reading )

Exporting MATLAB figures to LaTeX

The standard way of exporting figures from MATLAB to LaTeX is to use the builtin print command to export the figure to an eps file using:

print -depsc $filename

Unfortunately figures exported this way generally do not look like the original MATLAB figures. The aspect ratio is different and the fonts are too small. This can be overcome by tweaking the paper size, the font size and the line thickness but the process is fiddly and resulting eps files are hard to use since alternative TeX compilers like XeTeX and LuaTeX do not support eps files out of the box …

( Continue Reading )