[LINUX] I tried to make a serial communication single function module that controls the servo motor on the Petit Robo board in C language

Introduction

The WR-XX control board can control a total of 20 servo motors at the same time, but it transmits control data via serial communication. The communication mechanism itself is simple, so I made a module that operates only one servo motor.

This board is controlled using two PICs, but the source code on the PIC side is not open to the public. Therefore, the control command specifications are limited to the information from the "WR-XX Communication Protocol Guide (2nd Edition)".

** Servo control output command (command 6) ** This command controls the operation of the servo motor connected to the WR-XX board. You can specify the rotation position for each servo, the moving speed (all are the same), the time from receiving the command to the start of operation, and so on. (Omitted) Motors can only be specified with consecutive numbers. It is not possible to specify jumps such as M0 to M4, M10, and M15. The WR-XX board does not reply to the PC when it receives this command.

Overview of how to use

In the program created this time, the operation of the servo motor is specified by command line arguments.

[File name] [Target value M0] [Movement speed] [Wait time]

Target value: Approximately 160 ° is divided into 220 and specified. Movement speed: 15 steps. MG90S seems to operate at 0.1 seconds/60 ° 1.8kgf ・ cm (4.8V). Standby time: 15ms-3.3 seconds (220 steps)

(Example) Move only the servo motor M0 to the 110 position with a speed of 10 and a standby time of 15 ms.

python


$ gcc WR-XX_motion-script.c
$ ./a.out 110 10 1 

Source code

WR-XX_motion-script.c


/***********************************************************************************************************
 * WR-XX Motion Script for Linux ver. 0.1.2
 * Author : Lian Ivvakanni (12-19-2020)
 * 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/ttyS8"
#define BAUD_RATE    B38400
/* command item number  */
#define M 32
/* buffer size  */
#define N 32

int main(int argc, char *argv[]) {
    int  fd, i;
    struct termios tio;
    char s[N], buf[N], ret[M][N];
/*  Command 5: A/D status dummy data  */
    char com_6[M] = {0xFD,0x07,0x06,0x01,0x0F,0x6E,0xFE};
/*  target position: approx 160°  */
    if(atoi(argv[1]) > 0 && atoi(argv[1]) <= 220)
        com_6[5] = atoi(argv[1]);
    else
        com_6[5] = 0x00;
/*  velocity  */
    if(atoi(argv[2]) > 0 && atoi(argv[2]) <= 15)
        com_6[4] = atoi(argv[2]);
    else
        com_6[4] = 0x0A;
/*  wait time 15ms/1  */
    if(atoi(argv[3]) > 0 && atoi(argv[3]) <= 220)
        com_6[3] = atoi(argv[3]);
    else
        com_6[3] = 0x01;
/*  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);}
/*  clean the modem line and activate the settings for the port  */
    tcflush(fd, TCIFLUSH);
    ioctl(fd, TCSETS, &tio);

    write(fd, com_6, M);
 
    close(fd);
    return 0;
}

in conclusion

If you use it on the command line, I would like to be able to specify the device, but the result is that it is just right for using it as a function. To tell the truth, I'm impressed that I've finally come to this point.

Next, I will challenge a program that uses two servo motors.

After that, I'm not sure what role the "torque" value of the servo motor plays in the real world, so I'll investigate it properly in the future.

Recommended Posts

I tried to make a serial communication single function module that controls the servo motor on the Petit Robo board in C language
I tried to illustrate the time and time in C language
[Python] I tried to make a simple program that works on the command line using argparse.
A story that didn't work when I tried to log in with the Python requests module
I made a module in C language to filter images loaded by Python
I tried to make a translation BOT that works on Discord using googletrans
I tried adding a Python3 module in C
I tried to make a dictionary function that does not distinguish between cases
[LPIC 101] I tried to summarize the command options that are easy to make a mistake
I tried to make a function to judge whether the major stock exchanges in the world are daylight saving time with python
[C / C ++] Pass the value calculated in C / C ++ to a python function to execute the process, and use that value in C / C ++.
I tried to make a generator that generates a C # container class from CSV with Python
[Shell startup] I tried to display the shell on the TV with a cheap Linux board G-cluster
I also tried to imitate the function monad and State monad with a generator in Python
I tried to implement the mail sending function in Python
I tried to make a stopwatch using tkinter in python
I tried to make a script that traces the tweets of a specific user on Twitter and saves the posted image at once
"Cython" tutorial to make Python explosive: When a function on the C ++ side has an overload.
I tried to find out the difference between A + = B and A = A + B in Python, so make a note
I tried to build a SATA software RAID configuration that boots the OS on Ubuntu Server
I tried to create a server environment that runs on Windows 10
I tried to make a system that fetches only deleted tweets
I tried to register a station on the IoT platform "Rimotte"
I tried to make a system to automatically acquire the program guide → register it in the calendar in one day
I refactored "I tried to make a script that saves posted images at once by going back to the tweets of a specific user on Twitter".
[C language] I want to generate random numbers in the specified range
I tried to develop a Formatter that outputs Python logs in JSON
What is a C language library? What is the information that is open to the public?
[C language] How to use the crypt function on Linux [Password hashing]
I tried to display the altitude value of DTM in a graph
I tried to make a skill that Alexa will return as cold
I tried to wake up the place name that appears in the lyrics of Masashi Sada on the heat map
We have reviewed the C language code sample for serial communication for Petit Robo, so please let us know.
"Cython" tutorial to make Python explosive: Handling when a function on the C ++ side is passed by reference.
I tried to make a Web API
I tried to create a class that can easily serialize Json in Python
I tried to predict the horses that will be in the top 3 with LightGBM
I wrote a PyPI module that extends the parameter style in Python's sqlite3 module
[Python] I tried to make a Shiritori AI that enhances vocabulary through battles
I want to see a list of WebDAV files in the Requests module
[Python] I tried to get the type name as a string from the type function
I tried to make it on / off by setting "Create a plug-in that highlights double-byte space with Sublime Text 2".