Author Archives: admin

pthread class implemented in C++ inheritance

I am sharing a set of code generating a thread with pthread by taking an advantage of C++ class inheritance.

You can download the code in this link.

Now, the creation of thread in c++ is easier than before with the standard “thread” class. But still “pthread” is more generic and available in various environments. For my case, I need to control the priority of a thread for real-time applications. In this case, pthread is a good selection.

The creation of pthread in a class environment is very common but it needs some careful implementation because it needs a static function. Hence, the inheritance of C++ can simplify the implementation. We can use a base class including the fundamental functions of pthread, and an application class which needs thread can be created by inheriting the base class.

Simply, You can create your application class using thread like below:

#include <iostream>
#include <unistd.h> // for sleep function

#include "basethreadclass.h"

class MyAppThread: public BaseThreadClass{
    void InternalThreadEntry(){
        int i=0;
        while(1){
            std::cout << i++ << " hello from pthread\n";
            usleep(10000);
        }
    };
};

int main(){
    MyAppThread a;
    a.StartInternalThread();
    int i=0;
    while(1){
        std::cout << i++ << " hello from main\n";
        usleep(10000);
    }
    return 0;
}

The base class, “BaseThreadClass” looks like below:

#ifndef BASETHREADCLASS_H
#define BASETHREADCLASS_H

class BaseThreadClass
{
public:
    BaseThreadClass() {/* empty */}
    virtual ~BaseThreadClass() {/* empty */}

    /** Returns true if the thread was successfully started, false if there was an error starting the thread */
    bool StartInternalThread()
    {
        return (pthread_create(&_thread, NULL, InternalThreadEntryFunc, this) == 0);
    }

    /** Will not return until the internal thread has exited. */
    void WaitForInternalThreadToExit()
    {
        (void) pthread_join(_thread, NULL);
    }

protected:
    /** Implement this method in your subclass with the code you want your thread to run. */
    virtual void InternalThreadEntry() = 0;

private:
    static void * InternalThreadEntryFunc(void * This) {((BaseThreadClass *)This)->InternalThreadEntry(); return NULL;}
    pthread_t _thread;
};


#endif // BASETHREADCLASS_H

You can compile them with the included cmake file, and execute the application like below:

crop_thead

I hope this post help your project and understanding. Good luck!

Again you can download the codes link.

 

Reference

[1] https://stackoverflow.com/questions/1151582/pthread-function-from-a-class

 

EsmaCAT: Easy yet powerful robot mechatronics with EtherCAT

EsmaCAT Logo Color

This time I am going to introduce our new product line-up called EsmaCAT developed by Harmonic Bionics.

The EsmaCAT solution allows users easily develop a robotic system without low-level hardware knowledge.

For example, you can control multiple motors while reading analog sensors at faster than 1kHz.

For more info, please visit https://esmacat.com

Split and merge a 32-bit float with two 16-bit unsigned integers

I am sharing a code with an example that splits and merges a 32-bit float with two 16-bit unsigned integers.

Especially, when you work with an embedded system, 16-bit unsigned integer (or 8-bit unsigned integer, that is char) is a common format for data transmission. You can use this function when you want to send a 32-bit float variable with these 16-bit data format.

You can also use this function without the LSB-part. For the details, see the code below.

Sending 32-bit float variable with 16-bit data format

I needed to send a 32-bit float variable with a 16-bit data format. In this case, we can transmit the data by sacrificing some less significant bits. This is the code for the conversion of 32-bit float into 16-bit unsigned integer, and re-conversion from the 16-bit unsigned integer to 32-bit float. This code would be especially useful when you program some embedded system.

This is the result of execution of this code.

I got some ideas from this thread.
https://stackoverflow.com/questions/21005845/how-to-get-float-bytes

Good luck!

Easycat and SOEM

In this post, I am sharing a code that I developed for integrating Easycat slaves and Simple Open Ethercat Master (SOEM).

This is still an ongoing project, and thus there are many insufficient parts.

The info about Easycat:

www.bausano.net/

The info about SOEM

https://openethercatsociety.github.io/

The code is attached here

http://youngmok.com/public_data/SOEM.zip

http://youngmok.com/public_data/TestEasyCat.zip

 

The below is the setup pic.

IMG_0099

The below is an execution example.

reneu@reneu-Precision-T1600:~/soem/SOEM/build/test/linux/my_test$ sudo ./my_test eth0
SOEM (Simple Open EtherCAT Master)
Simple test
Starting simple test
ec_init succeeded.
5 slaves found and configured.
Slaves mapped, state to SAFE_OP.

Slave:1
Name:EK1100
Output size: 0bits
Input size: 0bits
State: 18
Delay: 0[ns]
Has DC: 1
Configured address: 1001
Outputs address: 0
Inputs address: 0
FMMUfunc 0:0 1:0 2:0 3:0

Slave:2
Name:EL1014
Output size: 0bits
Input size: 4bits
State: 18
Delay: 0[ns]
Has DC: 1
Configured address: 1002
Outputs address: 0
Inputs address: 8062180
FMMU0 Ls:40 Ll:   1 Lsb:0 Leb:3 Ps:1000 Psb:0 Ty:1 Act:1
FMMUfunc 0:2 1:0 2:0 3:0

Slave:3
Name:EL3102
Output size: 0bits
Input size: 48bits
State: 2
Delay: 0[ns]
Has DC: 1
Configured address: 1003
Outputs address: 0
Inputs address: 8062181
FMMU0 Ls:41 Ll:   6 Lsb:0 Leb:7 Ps:1180 Psb:0 Ty:1 Act:1
FMMUfunc 0:2 1:3 2:0 3:0

Slave:4
Name:EasyCAT 32+32
Output size: 256bits
Input size: 256bits
State: 18
Delay: 0[ns]
Has DC: 1
Configured address: 1004
Outputs address: 8062140
Inputs address: 8062187
FMMU0 Ls:0 Ll:  32 Lsb:0 Leb:7 Ps:1000 Psb:0 Ty:2 Act:1
FMMU1 Ls:47 Ll:  32 Lsb:0 Leb:7 Ps:1200 Psb:0 Ty:1 Act:1
FMMUfunc 0:1 1:2 2:0 3:0

Slave:5
Name:EasyCAT 32+32
Output size: 256bits
Input size: 256bits
State: 18
Delay: 0[ns]
Has DC: 1
Configured address: 1005
Outputs address: 8062160
Inputs address: 80621a7
FMMU0 Ls:20 Ll:  32 Lsb:0 Leb:7 Ps:1000 Psb:0 Ty:2 Act:1
FMMU1 Ls:67 Ll:  32 Lsb:0 Leb:7 Ps:1200 Psb:0 Ty:1 Act:1
FMMUfunc 0:1 1:2 2:0 3:0
Request operational state for all slaves
Operational state reached for all slaves.

0:
O: ff 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
I: d4 d2 db 79 25 86 ab cd ef 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 6c 94
1:
O: ff 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
I: d4 d2 db 79 25 86 ab cd ef 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 6c 94
2:
O: ff 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
I: d3 cf db 79 25 86 ab cd ef 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 6c 94
3:
O: ff 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
I: d3 d1 db 79 25 86 ab cd ef 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 6c 94
4:
O: ff 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
I: d3 d1 db 79 25 86 ab cd ef 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 6c 94
5:
O: ff 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
I: d3 d1 db 79 25 86 ab cd ef 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 6c 94
6:
O: ff 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
I: d4 d1 db 79 25 86 ab cd ef 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 6c 94
7:
O: ff 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
I: d3 d1 dc 79 24 86 ab cd ef 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 70 90
8:
O: ff 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
I: d3 d1 dc 79 24 86 ab cd ef 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 70 90
9:
O: ff 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
I: d3 d1 dc 79 24 86 ab cd ef 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 70 90
10:
O: ff 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
I: d3 d2 dc 79 24 86 ab cd ef 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 70 90

Four bar mechanism made of paper

Today I am introducing “Four bar mechanism made of paper”

The four-bar mechanism is very useful in the design of mechanical components. The best way to understand four-bar mechanisms is to build it by yourself.  Here, I am introducing how to make a four-bar mechanism with only a paper, screws and nuts. It is very easy to follow.

If you want to know the theories of four-bar mechanisms, see the wiki. Here

1. What you need to prepare

One sheet of paper, four screws and nuts, and a nail (or any sharp one)

2. Procedure

  1.   Prepare a sheet of paperSlide2
  2.  Tear the paper like thisSlide3
  3.   Fold two timesSlide4
  4.  Write numbers like the pictureSlide5
  5.  Make holesSlide6
  6.  Now you should have four bars with eight holesSlide7
  7.  Connect bars. Here the sequence is very important. If you do not follow this sequence the linkage will not rotate because interference with other linkages.Slide8
  8.  Connect allSlide9
  9.  Hold (4) and rotate (1)

This is the result.

This is a Grashof mechanism.

So far, I have introduced how to build a four-bar mechanism made of paper. I hope that this post helps your projects. If you have any question, please leave a reply.

ADS7813 ADS7812 Arduino Code

Hi, Today I am sharing ADS7813 ADS7812 Arduino Code. <Download>

1. ADS7813 ADS7812: Low-Power, Serial 16/12-Bit Sampling ANALOG-TO-DIGITAL CONVERTER

ADS7813/ADS7812 is a general purpose ADC made by Texas Instrument, and it is pretty easy to operate through SPI. You can see the datasheet from here.

2. Connections

This is the electric connection diagram that I used for my setup. I have used ADS7813 but all configurations are same with ADS7812. Just only difference is that the resolution of ADS7812 is 12bit .

ads7813

 

This electric connection is actually a reference connection provided by TI datasheet. With this connection, I made the Arduino code like this.

3. Arduino code

#include <SPI.h>

const int i_conv = 8;
const int i_busy = 9;

void setup() {
  Serial.begin(9600);

  // start the SPI library:
  SPI.begin();

  pinMode(i_conv, OUTPUT);
  pinMode(i_busy, INPUT);

  delay(100);
}

void loop(){

  bool i_busy_status=1;
  byte data1;
  byte data2;
  digitalWrite(i_conv,HIGH);
  delay(1);
  digitalWrite(i_conv,LOW);
  while ( digitalRead(i_busy) == LOW ){
  }
  data1 = SPI.transfer(0x00);
  data2 = SPI.transfer(0x00);
  digitalWrite(i_conv,HIGH);
  Serial.print(data1);
  Serial.print(" ");
  Serial.print(data2);
  Serial.print("\n");
 
}

I included only very essential part so that you might be easily able to understand the code. I am also attaching the code. <Download>

4. Operation video

This is the video showing the operation.

 

Nowadays I am playing with an Arduino, and I got many helps from communities. I hope this ADS7813 ADS7812 Arduino Code can help your project.