[LINUX] We have reviewed the C language code sample for serial communication for Petit Robo, so please let us know.

Introduction

During the holidays at the end of 2020, the development environment for ROS1 ( melodic) was organized into wsl (1) + VS Code. As a test of using the development environment, we modified the dummy code for Petit Robo created last year, so ** New Year's greetings to the community ** (!?) Also remember the updated version of the code. I will upload it as a record.

・ Original production article, link to Kyoritsu Eleshop's Petit Robo support site https://qiita.com/ivvakanni/items/8b5c00b37abe983d9ac1

Usage environment

・ Windows 10 Home ・ Wsl1/ubuntu18.04 ・ WondeRoid Motion Maker Ver3.1.1 ・ WR-2R4 USB Adapter X 2 (Buffalo: via USB hub)

Connect two WR-2R4 USB adapters to a Windows PC via a USB hub with theattached cable (4 wires)of Petit Robo, and one adapter connects the communication COM port of Wondows 10 from the Motion Maker software. I'm testing the operation of this program with half-duplex communication by connecting another adapter with ubuntu on wsl using tty (ttyS3).

The nice thing about this method is that ** you only need to plug and unplug the device on one port **. In addition, it is two birds with one stone because it can prevent the two adapters from scattering. By the way, I also have a wireless module for WR-2R4, so in the future I would like to support commands when using wireless.

Of course, if the device is recognized on the Windows PC side, a cheap USB/serial conversion adapter on the market will work. However, since there were some articles such as wsl2 does not connect to the COM port, I have not confirmed the authenticity, but I am using wsl1 for the time being.

The command from Windows PowerShell is as follows.

C:\> wsl --set-version Ubuntu-18.04 1

Major changes

・ Comments have been added everywhere for the convenience of later maintenance. -Supported replies other than command 5.

c:mytty-wr-xx-0.2.1.c


/***********************************************************************************************************
 * WR-XX Dummy for Linux ver. 0.2.2
 * Author : Lian Ivvakanni (01-03-2021)
 * License: Dual BSD/GPL (with absolutely no warranty.)
 * Purpose: send return message to Wonder Roid Motion Maker V3.1.1
***********************************************************************************************************/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

/* change this definition for the correct port */
#define MODEM_DEVICE "/dev/ttyS3"
#define BAUD_RATE   B38400
/* command item number  */
#define M 8
/* buffer size  */
#define N 32

#define HEADER 0xFFFFFFFD
#define FOOTER 0xFFFFFFFE  

int main(int argc, char *argv[])
{
    int  fd, i, j, k, len;
    struct termios tio;
    char s[N], sprn[N], buf[N], ret[M][N];
/*  Command 5: A/D status dummy data  */
    char dum_5[] = {0xFD,0x0F,0x15,0x46,0x46,0x46,0x46,0x46,\
                    0x46,0x46,0x46,0x46,0x46,0x00,0xFE};
/*  set return data  */
    for (i = 0; i <= 7; i++)
        ret[i][2] = i + 0x10;
/*  clear structure for port settings  */
    memset(&tio, 0, sizeof(tio));
/* 
    BAUDRATE: 38400 bps
    CS8     : 8bit,no parity,1 stopbit (8n1)
    CLOCAL  : local connection, no modem contol
    CREAD   : enable receiving characters
*/
    tio.c_cflag = BAUD_RATE | CS8 | CLOCAL | CREAD;
/*  Open modem device for reading and writing */
    fd = open(MODEM_DEVICE, O_RDWR);
    if (fd < 0) {perror(MODEM_DEVICE); exit(-1);}
    printf("Device Opened...\n");
/*  clean the modem line and activate the settings for the port  */
    tcflush(fd, TCIFLUSH);
    ioctl(fd, TCSETS, &tio);

    while(1){
    /*  clear buffer memory  */
        memset(buf, '\0', N);
    /*  open device to read  */
        len = read(fd,buf, sizeof(buf));
        if(len < 0) printf("Device Read Error...\n");
    /*  find header  */
        if(buf[0] != HEADER) 
            continue;
    /*  check command data size  */ 
        if(buf[buf[1] - 1] != FOOTER) 
            continue;
    /*  optional: set buffer to string  */ 
        for (i = 0; i < buf[1]; i++){
            sprintf(s, "%02X ", buf[i]);
            strcat(sprn,s);
        }
        printf("Rec: %s: %d\r\n", sprn, i);
        memset(sprn, '\0', sizeof(sprn));
    /*  find command and send its return, if any */
        if(buf[2] == 0x00) 
            write(fd, ret[0], 4); 
        if(buf[2] == 0x02) 
            write(fd, ret[2], 4); 
        if(buf[2] == 0x03)
            write(fd, ret[3], 4); 
        if(buf[2] == 0x05) 
            write(fd, dum_5, 15);
        len = 0;
    }
    close(fd);
    return 0;
}

in conclusion

In 2020, I was too particular about the operating environment on ARM11, and the actual learning of ROS did not proceed as expected. Therefore, based on such reflection, I learned that the GUI environment of ROS works with wsl, so for ROS1 in the first half of this year, the development learning environment was changed to Melodic on wsl1 ( ubuntu 18.04). I will fix it. And if I calm down a little, I would like to challenge ROS2 by the summer.

By the way, I'm glad that by using wsl, the old ubuntu-equipped PC is hidden in the back and the desk can be used widely. I also installed a ROS dedicated server with ubuntu 18.04 on the Raspberry pi 2, so it was very easy to do.

Well then, I am looking forward to an interesting and useful article for everyone in the community, and I look forward to working with you this year as well.

Recommended Posts

We have reviewed the C language code sample for serial communication for Petit Robo, so please let us know.