RosGUI Development Log: Launch File Generator using Boost

Previously I wrote a script using python to generate roslaunch files for my experiments using markup.py. The script got user’s inputs from terminal. In order to generate ros launch files efficiently, I add a function in the RosGUI for me to setup parameters easily by clicking the mouse.

I have used Boost property_tree what is Property tree to generate XML files.

By defintion, “the Property Tree library provides a data structure that stores an arbitrarily deeply nested tree of values, indexed at each level by some key. Each node of the tree stores its own value, plus an ordered list of its subnodes and their keys. The tree allows easy access to any of its nodes by means of a path, which is a concatenation of multiple keys.”

struct ptree
{
   data_type data;                         // data associated with the node
   list< pair<key_type, ptree> > children; // ordered list of named children
};

Usually, the data_type and key_type are std::string.

Continue reading this post...

RosGUI Development Log: Implement Entry Completion using GTKMM

Introduction

I have used ROS to implement our robotics algorithms for years. My usual way to write a ROS program is command-line based:

open emacs

create a package using catkin command, and write programs using c++ or python

configure CMake files for the project

run catkin_make to compile

To run a ros program, also I open emacs or terminal first, then type:

roscore

rosrun or roslaunch

The fact is that I have dozens of launch files to test. For me, the command: roslannch package_name launchfile_name is the most frequently used one, so my hands are always busy on the keyboard.

To improve the user experience of using my program to run various tests and experiments, I want to implement a simple GUI, so that those frequently used operations can be achieved without constant typing commands on the keyboard.

Continue reading this post...

Efficient Implementation of MergeSort using STL

Merge Sort

Merge sort is a divide-and-conquer algorithm, the idea is first splitting data into two subsets, then sort then merge. The time complexity is \( O(n\log{n}) \) with \(O(n)\) extra memory.

It is not difficult to implement a recursive version merge sort in C++ or Java, or Python, etc. However, using STL, it takes no more than 10 lines of code to implement a merge sort algorithm [1].

The key is using function std::inplace_merge [1]: the function comes with two signatures:

template< class BidirIt >
void inplace_merge( BidirIt first, BidirIt middle, BidirIt last );

template< class BidirIt, class Compare>
void inplace_merge( BidirIt first, BidirIt middle, BidirIt last, Compare comp );

The function merges two consecutive sorted ranges [first, middle) and [middle, last) into a sorted range [first, last). The default comparison operation is less than.

Continue reading this post...

Pointers as function arguments

Be careful with pointer arguments : Do not try to allocate memory or change address

Start from an example below:

#include <iostream>
using namespace std;

struct ListNode {
    int val;
    ListNode * next;
    ListNode(int x) : val(x), next(NULL){}
};

void foo(ListNode * a, ListNode * b){
    a = b;
}

int main(int argc, char *argv[]){
    ListNode * head = new ListNode(0);
    ListNode * headcp = NULL;
    foo(headcp, head);
    cout<<headcp->val<<endl;
    return 0;
}

The intuition is: I want to use function foo(T* a, T* b) to let a save a copy of pointer b.

Unfortunately, the above code will give a runtime error since after the function call, the variable headcp is still a null pointer.

Continue reading this post...

Command to new a terminal and run command

Solution from Run command on another(new) terminal window:

run command gnome-terminal -x sh -c "ls ; bash"

in which - gnome-terminal -x means to execute system command sh.

  • sh -c means the commands for bash to execute are read from the first non-option argument command_string "ls; bash".
Continue reading this post...

Variable Length Array for non POD element type

Consider following code:

struct Foo{
    std::string val;
};
int n = 5;
Foo a[n];
const size_t m =10;
Foo b[m];

Even a variable length array (VLA) is declared, the program compiles using g++ -std=c++0x. VLA is not an official part of C++11 [1]. Clang supports such variable length arrays in very limited circumstances [2], that is :

- The element type of a variable length array must be a POD ("plain old data") type, which means that it cannot have any user-declared constructors or destructors, any base classes, or any members of non-POD type. All C types are POD types.

- Variable length arrays cannot be used as the type of a non-type template parameter.
Continue reading this post...

Install Gtest on Mac OS (Yosemite)

After following the steps of installing Google Test described in my article Use Gtest in Ros Program on my mac book, I compile the test program using CMake. However, the CMake reports an error of Could NOT find GTest (missing: GTEST_INCLUDE_DIR).

Problem: CMake find_package could not find GTest

When I seek for a solution, I was on the wrong track by reading [1]. After struggling for a while, I figured out that the root reason is I did not install gtest properly on Mac OS.

By referring Tonatiuh’s guide [2], I solve the problem by copying the gtest directory in the source folder to the path /usr/local/include/.

Namely, the complete procedure to install gtest on mac os is:

Continue reading this post...

Use gtest in ROS program

Problem: How to use gtest in a ROS program?

Gtest is very powerful for unit testing. I would like to use it in my ros program.

Unfortunately, the documentation on Ros.org talking about the gtest is far from helpful [1].

Suggestion from ros.answer [2] and the article written by Billy McCafferty [3] are more useful for me to understand how to use gtest in ros.

Here I would like to show the complete procedure to use gtest in ROS program through a minimum example.

Result of Running Gtest with ROS program

Generally, the article is organized as:

  1. Create a ros package using catkin
  2. Install gtest
  3. Use gtest in ros

Split a string using C++

Problem: Split a string by a delim, and return a vector of strings containing no delim

1. Use stringstream getline

Signature istream& getline (istream&& is, string& str, char delim)

Usage [1]:

vector<string> split(const string &s, char delim) {
    stringstream ss(s);
    string item;
    vector<string> tokens;
    while (getline(ss, item, delim)) {
        tokens.push_back(item);
    }
    return tokens;
}

Note that this solution does not skip empty tokens, so the following will find 4 items, one of which is empty: std::vector<std::string> x = split("one:two::three", ':');

Continue reading this post...

Determine array size if passed as a function parameter

A quick way to determine array size using sizeof is known as: sizeof(array)/sizeof(*array)

for example:

#include<iostream>
using namespace std;
int main(){
    int A[5] = {1,2,3,4,5};
    cout<<sizeof(A)/sizeof(*A)<<endl;
    return 0;
}

The output is 5.

However, if passing an array to a function:

Continue reading this post...