Category Archives: Programming

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-

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 ^_^

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.

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

 

 

Simple C++ Code for Multi Dynamixel Servo Motor Control

Simple C++ Code for Multi Dynamixel Servo Motor Control

 

// this post is updated to eliminate the dependency on QT, 2015.06.26

 

In this post, I am sharing my C++ class to control multi Dynamixel servo motors.

In my case, I am using MX-24f, but most of dynamixel servo motors are using the same protocol. Thus, you can use this code directly. I am attaching all files (dynamixel library and others) to execute a demo program.

<Download>

If you don’t need common files, just use “multi_dnmx_motor.h” and “multi_dnmx_motor.cpp”. For the usage, see “main.cpp” file. It is super easy.  Demo program was made for Linux environment, but Windows uses still can use “multi_dnmx_motor” class

If you don’t know anything about Dynamixel program. Just follow the below.

 

0. Before following this instruction, check if the connection and settings are correct with “RoboPlus Manager” which can be downloaded in Robotis website, >> Support >> Download.

1. Go to “Dynamixel_src” folder.

2. “Make” : It will generate a library and automatically copy the lib. to the library folder.

3. Go to “Multi_Dynamixel_Motor_Control” folder.

4. Open “multi_dnmx_motor.h” file

5. Change “user setting”. You have to set the baud_rate, number of motors, and their motor IDs. Just change defines. (If you don’t know motors’ these values, use “RoboPlus”, a super easy program, and available in the Robotis website, http://support.robotis.com/en/ )

// User setting
#define BAUD_NUM 34 // 1: 1Mbps 34:57142bps
#define NUM_OF_MOTORS 4 // Number of motors

#define MOTOR_ID_1 1 // Motor 1 ID
#define MOTOR_ID_2 2 // Motor 2 ID
#define MOTOR_ID_3 3 // Motor 3 ID
#define MOTOR_ID_4 4 // Motor 4 ID

6.  compile it with this command

” g++ main.cpp multi_dnmx_motor.cpp -I../include -L../lib -ldxl  ”

7. Execute the program.

 

Good luck ^_^

<Download>

Add a shared object (*.so) file in qt project file (pro), example

The following is an example.

#————————————————-
#
# Project created by QtCreator 2013-12-20T11:36:03
#
#————————————————-

QT       -= gui

TARGET = Mocap_QT
CONFIG   += console
CONFIG   -= app_bundle

LIBS += ./lib/libowlsock.so

INCLUDEPATH += ./include/

TEMPLATE = app

SOURCES += main.cpp

Matlab compiler installation. If you have trouble after installation of Microsoft Windows SDK 7.1

Solution for the Matlab compiler installation. If you have trouble after installation of Microsoft Windows SDK 7.1

Today I am writing a tip to solve a Matlab compiler setup problem. 

If you want to use MEX or any Matlab compiling technology, you need a compiler. Windows does not have a default compiler. So you have to install. If you don’t have any compiler, you would see the below message.

>> mex -setup

Welcome to mex -setup. This utility will help you set up 
a default compiler. For a list of supported compilers, see 
http://www.mathworks.com/support/compilers/R2013a/win64.html 

Please choose your compiler for building MEX-files: 

Would you like mex to locate installed compilers [y]/n? y

No supported SDK or compiler was found on this computer. 
For a list of supported compilers, see 
http://www.mathworks.com/support/compilers/R2013a/win64.html 

Error using mex (line 206)
Unable to complete successfully.

If you follow the link, you can install “Microsoft Windows SDK 7.1”

BUT, IMPORTANT, if you don’t have “.NET Framework 4.0,” you cannot install the compiler. As shown in the below figure, you MUST install .NET Framework 4.0 for installation of the windows compiler.

Matlab compiler problem, if you have trouble after the installation of Microsoft Windows SDK 7.1

Matlab compiler problem, if you have trouble after the installation of Microsoft Windows SDK 7.1

Then, I wish this post can help to solve your problem.

Thank you.

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

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.

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

C++ code for reading unknown size matrix from text file

C++ code for reading unknown size matrix from text file

In this post, I upload my program {C++ code for reading unknown size matrix from text file } <download> which imports an arbitrary size matrix (or 2D array) from text (txt) file. This is c++ code.

The below is an execution result. The first figure is a text file including a matrix, the next figure is an execution screen. It’s very easy to use.

xtxt

text file to import. It has an arbitrary size matrix

screen

execution screen image, it imported the matrix from the above text fileSometimes, we want to import a matrix that we don’t know its size, especially when the file was generated from other programs.

I had googled for long time, and I realized that it is not easy to find. So, I made.

You don’t need to indicate the size of matrix, (cols and rows). It automatically imports and outputs, double type vector,  column and row size. It is very easy to use, and you can download the source code from here  <download>.

The only thing you should do is just download the source code, and import “x.txt” or “y.txt” which includes an arbitrary matrix.

Enjoy my program { C++ code for reading unknown size matrix from text file } Then good luck.

I referred partially this site.

http://stackoverflow.com/questions/1321137/convert-string-containing-several-numbers-into-integers

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

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.

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

Very good source code for serial communication in QT Linux envirionment

< Source code for serial communication in QT Linux envirionment>

The below site has pretty good source code for serial communication in Linux QT environment.

You can download and learn how to use it.

http://www.embedded4fun.com/serial-port-interfacing-in-qt/

 

The below is how to install the code.  ( from the upper site)

  • cd <any folder (preferrably to the QtSDK installation folder)>
  • git clone https://code.google.com/p/qextserialport/
  • cd qextserialport
  • qmake
  • make
  • make install

<download>

Hyperterminal for serial communication (rs232 or rs485), download

Hyperterminal for serial communication (rs232 or rs485), download 

When I used Windows XP, I loved to use Hyperterminal because it is pretty simple and easy to use for serial communication. Especially it is pretty good for RS232 or RS485 communication. But now Windows does not support Hyperterminal any more.

So. I am attaching the previous program. You can download the program from the below link.

<Hyperterminal Download>