Author Archives: admin

“Failed to open USBDynamixel” troubleshooting

Recently I met a problem while opening Dynamixel motors with USBDynamixel like the below picture.

My environment is Ubuntu 12.04 and patched with RT-preempted to make my PC real-time.

The solution is very simple, just use “sudo“.  You can see the result in the below picture.
Screenshot from 2014-11-04 12:52:52

Precise Elapsed Time Measurement in C Program

This code is the simplest code to measure a program execution time more precisely.

It is using “struct timeval”.

#include <stdio.h>
#include <sys/time.h>

double getTimeElapsed(struct timeval end, struct timeval start)
{
    return (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec) / 1000000.00;
}

void main(){
	struct timeval tvalBefore, tvalAfter; 
	gettimeofday(&tvalBefore, NULL);
	
    // Do something here, this is the simplest program to measure the elapsed time. It is more precise than simple ctimer
    
    
	gettimeofday(&tvalAfter, NULL);
	double time_elapsed = getTimeElapsed(tvalAfter, tvalBefore);
	
	printf("time_elapsed: %lf\n",time_elapsed);
}


I have referred the below two sites

http://levicui.blogspot.com/2011/04/calculate-elapsed-time-in-c.html

http://stackoverflow.com/questions/12722904/how-to-use-struct-timeval-to-get-the-execution-time

Good luck ^_^

Gaussian Kernel Bandwidth Optimization with Matlab Code

Gaussian Kernel Bandwidth Optimization with Matlab Code

In this article, I write on “Optimization of Gaussian Kernel Bandwidth” with Matlab Code.

First, I will briefly explain a methodology to optimize bandwidth values of Gaussian Kernel for regression problems. In other words, I will explain about “Cross validation Method.”

Then, I will share my Matlab code which optimizes the bandwidths of Gaussian Kernel for Gaussian Kernel Regression. For the theory and source code of the regression, read my previous posts <link for 1D input>, <link for multidimensional input>. This Matlab code can optimize bandwidths for multidimensional inputs. If you know the theory of cross validation, or if you don’t need to know the algorithm of my program, just download the zip file from the below link, then execute demo programs. Probably, you can use the program without big difficulties.

<Download>

1. Bandwidth optimization by a cross validation method

The most common way to optimize a regression parameter is to use a cross validation method. If you want to know about the cross validation deeply, I want to recommend to read this article. Here I will shortly explain about the cross validation method that I am using. This is just a way of cross validation.

1. Randomly sample 75% of the data set, and put into the training data set, and put the remaining part into the test set.

2. Using the training data set, build a regression model. Based on the model, predict the outputs of the test set.

3. Compare between the predicted output, and the actual output. Then, find the best model (best bandwidth) to minimize the gap (e.g, RMSE) between the predicted and actual outputs.

2. Matlab code for the algorithm

You can download all functions and demo programs from the below link.

<Download>

This program is for multidimensional inputs (of course, 1D is also OK). The most important function is Opt_Hyp_Gauss_Ker_Reg( h0,x,y ) and it requires Matlab optimization toolbox. I am attaching two demo programs and their results. I made these demo programs as much as I can. So, I believe that everybody can understand. 

<Demo 1D>

demo_1d

demo_1d_result

<Demo 2D>

demo_2d demo_2d_result

 

I wish this post can save your time and efforts in your work. If you have any question, please leave a reply.

-Mok-

—————————————————————————————————

I am Youngmok Yun, and writing about robotics theories and my research.

My main site is http://youngmok.com, and Korean ver. is  http://yunyoungmok.tistory.com.

—————————————————————————————————

 

 

Gaussian Kernel Regression for Multidimensional Feature with Matlab code

Gaussian Kernel Regression for Multidimensional Feature with Matlab code (Gaussian Kernel or RBF Smoother)

I am sharing a Matlab code for Gaussian Kernel Regression algorithm for multidimensional input (feature).

In the previous post (link), I posted a theory of Gaussian Kernel Regression, and shared a Matlab code for one dimensional input. If you want to know about the theory, read the previous post. In the previous post, many visitors asked me for a multidimensional input version. Finally I made a Gaussian Kernel Regression Program for a general dimensional input

You can download the program from this link.

<Download>

I wrote a demo program to show how to use the code as easy as possible.

The below is the demo program, and a demo result plot. In this demo program, the dimension of input is 2 because of visualization, but it is expendable to an arbitrary dimension.

 

demo_codedemo_result

 

For the optimization of kernel bandwidth, see my other article <Link>.

 

I wish this program can save your time and effort for your work.

If you have any question, please leave a reply.

—————————————————————————————————————————–

I am Youngmok Yun, and writing about robotics theories and my research.

My main site is http://youngmok.com, and Korean ver. is  http://yunyoungmok.tistory.com.

—————————————————————————————————————————–

 

 

 

Simple Example Using Shared Variable in QT Multi-thread with QMutex

Simple Example Using shared variable in QT multi-thread with QMutex

Today, in this post, I want to share my simple example code to explain how to use shared variables in QT multi-thread environments with QMutex.

In the example, first I will generate an Int variable to be shared.

Then, I will make two threads. Each thread has their own ID. and they will try to change the shared number with their ID number.

After changing the shared number in their threads to be same with its ID, the thread will check if the shared number is really same with the ID. If the other thread changes the shared variable just before the check, it will print out an error message.

Without Mutex, it will give us many error message, and with Mutex, we can remove this collision error.

You can download full package of source code from here  <Download>.

Also, you can just see and understand by watching the below codes and results with or without Mutex.

main.cpp

#include <QtCore/QCoreApplication>
#include "mythread.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QMutex mMutex;

    int sharedVar=10;
    qDebug() << "sharedVar :"<< sharedVar;

    MyThread mThread1(&mMutex,1,&sharedVar);
    MyThread mThread2(&mMutex,2,&sharedVar);

    mThread1.start();
    mThread2.start();

    return a.exec();
}

mythread.h

#ifndef MYTHREAD_H
#define MYTHREAD_H

#include <QtCore>
#include <QMutex>

class MyThread : public QThread
{
private:
    QMutex* mutex;
public:
    MyThread(QMutex* mu, int myNum, int* sharedNum);
    int myNumber;
    int* num;


protected:
    void run();

};

#endif // MYTHREAD_H

mythread.cpp

#include "mythread.h"
#include <QtCore>
#include <QDebug>
#include <QMutexLocker>

MyThread::MyThread(QMutex* mu, int myNum, int* sharedNum)
{
    mutex= mu;
    myNumber = myNum;
    num = sharedNum;

}

void MyThread::run(){

    qDebug() << "Thread " << myNumber << "num: " << num << "*num: " << *num;
    for (int i=0;i<5;i++){

        mutex->lock(); // To avoid collision

        qDebug() << i << "Before Change, My Number : "<< myNumber << "*num" << *num  ;
        (*num) = myNumber;
        usleep(10);
        if ( *num != myNumber ){
            qDebug() << "! collision ! at Thread" <<  myNumber ;
        }
        qDebug() << i << "After  Change, My Number : "<< myNumber << "*num" << *num  ;

        mutex->unlock(); // Let's release the lock
        usleep(1);

    }
}


QMutexTest.pro

QT       += core
QT       -= gui

CONFIG   += console
SOURCES += main.cpp \
    mythread.cpp
HEADERS += \
    mythread.h

 

Result ( with Mutex)

QMutex

 

Result ( If I remove mutex part)QMutex2

 

I wish everybody understands this one. If you want to study more deeply, I recommend to read this post. http://stackoverflow.com/questions/8971168/how-to-use-qmutex

If you cannot understand, please leave me a reply.

 

—————————————————————————————————————————–

I am Youngmok Yun, and writing about robotics theories and my research.

My main site is http://youngmok.com, and Korean ver. is  http://yunyoungmok.tistory.com.

—————————————————————————————————————————–

 

 

Terminator installation and setting as a default terminal program

I strongly recommend to use Terminator as a default terminal program in Linux.

It has many great functions such as horizontal/vertical tab split.

First. Installation.

$ sudo apt-get install terminator

Second, Setting as a default terminal program

gconftool --type string --set /desktop/gnome/applications/terminal/exec terminator

It is done!!!

Good luck

Monte Carlo Integration with a simple example

In this article, I will explain Monte Carlo Integration.

How can we do the “Integration”?

In many cases, the integration is not easy in an analytical method.

The Monte Carlo Integration method is a numerical integration method.

Let’s think about the below example. MCI1

 

The goal of this integration is to find the area of pink region.

The key idea of the Monte Carlo integration is to find \hat{f}} to represent f. See below.

MCI2Then, How can we find the \hat{f}} ? the Monte Carlo Integration method uses “Expectation method” (Average) See below

MCI3

 

With the random sampling method, we can get the \hat{f} by calculating the mean value.

This is a very useful way especially for the calculation of Bayesian posterior.

 

The below is an example of Monte Carlo Integration.

I will solve this problem  \int^2_{-1}x dx

>> N=10000;
>> 3*sum(rand(N,1)*3-1)/N

ans =

1.5202

Here \hat{f} is < sum(rand(N,1)*3-1)/N > and the range of the integration is 3.

Then, good luck

For more detail, I recommend to read the below article.

http://web.mit.edu/~wingated/www/introductions/mcmc-gibbs-intro.pdf

 

—————————————————————————————————————————–

I am Youngmok Yun, and writing about robotics theories and my research.

My main site is http://youngmok.com, and Korean ver. is  http://yunyoungmok.tistory.com.

—————————————————————————————————————————–

Gaussian kernel regression with Matlab code (Gaussian Kernel or RBF Smoother)

Gaussian kernel regression with Matlab code

In this article, I will explain Gaussian Kernel Regression (or Gaussian Kernel Smoother, or Gaussian Kernel-based linear regression, RBF kernel regression)  algorithm. Plus I will share my Matlab code for this algorithm. 

If you already know the theory. Just download  from here.  <Download>

You can see how to use this function from the below. It is super easy.

main figure

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

From here, I will explain the theory.

Basically, this algorithm is a kernel based linear smoother algorithm and just the kernel is the Gaussian kernel. With this smoothing method, we can find a nonlinear regression function.

The linear smoother is expressed with the below equation

y^* = \frac{\sum^N_{i=1}K(x^*,x_i)y_i}{\sum^N_{i=1}K(x^*,x_i)}

here x_i is the i_th training data input, y_i is the i_th training data output, K is a kernel function. x^* is a query point, y^* is the predicted output.

In this algorithm, we use the Gaussian Kernel which is expressed with the below equation. Another name of this functions is Radial Basis Function (RBF) because it is not exactly same with the Gaussian function.

K(x^*,x_i)=\exp\left(-\frac{ (x^*-x_i)^2}{2b^2}\right)

With these equation, we can smooth the training data outputs, thus we can find a regression function.

This program <Download> was made for one-dimensional inputs. If you need multi-dimension, please leave a reply, see this article. I recently made a new version for multidimensional input.

For the optimization of kernel bandwidth, see my other article <Link>.

 

Then good luck.

 

-Mok-

—————————————————————————————————————————–

I am Youngmok Yun, and writing about robotics theories and my research.

My main site is http://youngmok.com, and Korean ver. is  http://yunyoungmok.tistory.com.

—————————————————————————————————————————–

Control engineer’s one-year observation of my baby

As a control engineer, I see different features from my one year old baby, Chloe. When she was first born, she did not have a model for her biomechanical system, and the controller in her brain was immature. Nonetheless, her finger did not vibrate or diverge. The kinematics of her finger is same with that of a three-link pendulum, commonly referred to describe a chaotic dynamic system. She had never tried to make it stable, no, actually she disturbed the system. But, her biomechanical systems were amazingly stable. For the last one year, her control ability has been dramatically improved. Now she can walk and interact with highly uncertain dynamic objects like another baby. It is an extremely difficult control task. Furthermore, in someday, she would be able to drive a car and to manage (control) a business. She has a great system, modeling ability, and controller, three essential elements for control. As a control engineer, I cannot help being surprised and admiring.

 

Feb 11, 2014

-Mok-