Tag Archives: example

Simplest Bidirectional UDP Program in Labview

< Simplest Bidirectional UDP Program in Labview >

Today, I am sharing the simplest bidirectional UDP program made for Labview

<Download>

I have tried to find a simple birectional UDP program, but it was not easy. Most of programs were incomplete. So I made the thinnest program for only bidirectional UDP communication.

The program is based on the examples provided by NI.

You can download the program at <Download>.  The below is screenshots of the UDP_1 UDP_2program.

 

 

 

I hope this helps your projects.

-Mok-

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.

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

 

 

Parallel computation with Matlab, SPMD. Explanation with simple example code

Today I’d like to introduce a parallel computation skill in Matlab.

I also heard that the use of parallel computation in Matlab is very easy. But it was better than my expectation. Wonderful!! Smile

You need to know the usage of SPMD. It is pretty easy.

First let’s the code, you can also download this file in the attachment, click <here>.
parallel_computation_spmd

 

The result is the below

Starting matlabpool using the ‘local’ configuration … connected to 4 labs.
Elapsed time is 120.482356 seconds.
Sending a stop signal to all the labs … stopped.
Elapsed time is 167.796932 seconds.

Because, I did many other works in the calculation time, Its result is not very fast, The slowest CPU core determined the final computation time. But its performance is really good if you have more than 4 multi cpu cores.

I wish this post can help your understanding about the use of SPMD in Matlab.

if you have any question, please leave me a comment below.

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

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.

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

 

 

 

Feedback linearization control (FBL) for nonlinear system control proof, practical implementation, and easy example part 2

This article explains about Feedback linearization control (FBL) method for control of a nonlinear system. By demonstrating a control strategy of the inverted pendulum problem, I am going to explain how to implement its algorithm into a real system. The basic idea is that we can cancel control input by manipulating control input. The below is its practical implementation method and example.

If you want to know the proof of feedback linearization control method. refer this.

 

fbl_ex1

fbl_ex2

 

 

fbl_ex3 fbl_ex4

As we can see from the inverted pendulum example. nonlinear part can be canceled by control input. I wish it can help your understanding. If you have any question or need any help, 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.

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