Category Archives: Programming

Maxon EPOS2 Driver C++ Class using USB-CAN Gateway for Multiple Motor Actuation

I am sharing a C++ Class for multiple Maxon motor actuation by EPOS2 via USB-Can gateway.

The source code is written for two motors, but it is possible to modify easily to actuate multiple motors.


If you want to see a simpler version for one motor actuation, see this article. 

I hope this article makes your life easier.


UDP Server C++ Class with a listening thread

I am sharing a code for “UDP Server C++ Class with a listening thread”. In the class “UDPThread”, a thread is running to receive a UDP packet.


Just compile a demo program with this command. Then you will understand.

g++ demo_udp_server.cpp UDPThread.cpp -lpthread -o demo_udp_server

This is the result.

Screenshot from 2015-03-04 15:44:40



I hope this code helps your project.



Minimal c++ class for Maxon EPOS2

Minimal c++ class for Maxon EPOS2

I am sharing the minimal C++ class for Maxon EPOS2. It includes a basic “initialization”, “Move”, “Read position”, “Close device” functions. The program consists of a class called “cmaxonmotor”.  You can download the class and demo program here


You can see a simple demo program here.

#include <stdio.h>
#include <iostream>
#include "cmaxonmotor.h"

using namespace std;

int main(int argc, char *argv[])
    CMaxonMotor motor("USB0",1);
    motor.initializeDevice(); // initialize EPOS2

    long TargetPosition = -200000;
    int CurrentPosition = 0;

    motor.Move(TargetPosition); // move to the target position

    cout << "Press <Enter> to stop and quit..." << endl;
    motor.GetCurrentPosition(CurrentPosition); // get the current position
    cout << "Current Position: " << CurrentPosition << endl;

    motor.closeDevice(); // close EPOS2

    return 0;


You can control one motor just via USB, and also able to control multiple motors via USB-CAN gateway. For this version, see this article.

I hope this helps your project.




Simplest Bidirectional UDP Program in Labview

< Simplest Bidirectional UDP Program in Labview >

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


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.


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

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.


#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);


    return a.exec();


#ifndef MYTHREAD_H
#define MYTHREAD_H

#include <QtCore>
#include <QMutex>

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

    void run();


#endif // MYTHREAD_H


#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;
        if ( *num != myNumber ){
            qDebug() << "! collision ! at Thread" <<  myNumber ;
        qDebug() << i << "After  Change, My Number : "<< myNumber << "*num" << *num  ;

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


QT       += core
QT       -= gui

CONFIG   += console
SOURCES += main.cpp \


Result ( with Mutex)



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.

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, and Korean ver. is




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.


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, )

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