Ask Your Question
0

Error in /opt/ros/melodic/include/ros/ros.h

asked 2021-04-13 12:07:25 -0500

bach gravatar image

updated 2021-04-13 12:43:26 -0500

Hi,

I'm trying to read user input from keyboard, inspired by turtlesim. When I compile I get this error:

In file included from /opt/ros/melodic/include/ros/ros.h:40:0,
                 from /home/alberto/tiago_dual_public_ws/src/continuos_input/src/key_reader.cpp:1:
/home/alberto/tiago_dual_public_ws/src/continuos_input/src/key_reader.cpp: In member function ‘void key_reader::keyLoop()’:
/opt/ros/melodic/include/ros/console.h:381:3: error: expected ‘;’ before ‘do’
   do \
   ^
/opt/ros/melodic/include/ros/console.h:572:35: note: in expansion of macro ‘ROS_LOG_COND’
 #define ROS_LOG(level, name, ...) ROS_LOG_COND(true, level, name, __VA_ARGS__)
                                   ^~~~~~~~~~~~
/opt/ros/melodic/include/rosconsole/macros_generated.h:58:24: note: in expansion of macro ‘ROS_LOG’
 #define ROS_DEBUG(...) ROS_LOG(::ros::console::levels::Debug, ROSCONSOLE_DEFAULT_NAME, __VA_ARGS__)
                        ^~~~~~~
/home/alberto/tiago_dual_public_ws/src/continuos_input/src/key_reader.cpp:86:17: note: in expansion of macro ‘ROS_DEBUG’
                 ROS_DEBUG("value: 0x%02X\n", c);
                 ^~~~~~~~~
make[2]: *** [CMakeFiles/key_reader.dir/src/key_reader.cpp.o] Error 1
make[1]: *** [CMakeFiles/key_reader.dir/all] Error 2
make: *** [all] Error 2
cd /home/alberto/tiago_dual_public_ws/build/continuos_input; catkin build --get-env continuos_input | catkin env -si  /usr/bin/make --jobserver-fds=6,7 -j; cd -

I don't understand why. It's a read-only file so I cannot modify it. Can someone help me?

This is my complete code:

  #include "ros/ros.h"
#include <signal.h>
#include <termios.h>
#include <stdio.h>
#include "continuos_input/input_command.h"

#define KEYCODE_R 0x43
#define KEYCODE_L 0x44
#define KEYCODE_U 0x41
#define KEYCODE_D 0x42
#define KEYCODE_Q 0x71

class key_reader
{
public:
    key_reader();
    void keyLoop();

private:
    ros::NodeHandle nh_;
    int direction; //1 if left, -1 if right, 0 no input
    ros::Publisher key_pub;
};

int kfd = 0;
struct termios cooked, raw;

key_reader::key_reader()
{
    key_pub = nh_.advertise<continuos_input::input_command>("user_input", 1000);
}

void quit(int sig)
{
    tcsetattr(kfd, TCSANOW, &cooked);
    ros::shutdown();
    exit(0);
}

void key_reader::keyLoop()
{
    char c;
    bool dirty = false;
    fd_set set;
    struct timeval timeout;
    int rv;

    // get the console in raw mode
    tcgetattr(kfd, &cooked);
    memcpy(&raw, &cooked, sizeof(struct termios));
    raw.c_lflag &= ~(ICANON | ECHO);
    // Setting a new line, then end of file
    raw.c_cc[VEOL] = 1;
    raw.c_cc[VEOF] = 2;
    tcsetattr(kfd, TCSANOW, &raw);

    FD_ZERO(&set);     /* clear the set */
    FD_SET(kfd, &set); /* add our file descriptor to the set */

    timeout.tv_sec = 0;
    timeout.tv_usec = 10000;

    puts("Reading from keyboard");
    puts("---------------------------");
    puts("Use arrow keys to move the turtle.");

    ros::Rate r(10);
    while (ros::ok())
    {
        // get the next event from the keyboard
        rv = select(kfd + 1, &set, NULL, NULL, &timeout);
        if (rv == -1)
        {
            perror("select:"); /* an error accured */
            exit(-1);
        }
        else if (rv == 0)
        {
            printf("Timeout"); /* a timeout occured */
            direction = 0;
        }
        else
        {
            read(kfd, &c, 1)

                ROS_DEBUG("value: 0x%02X\n", c);

            switch (c)
            {
            case KEYCODE_L:
                ROS_DEBUG("LEFT");
                direction = 1;
                dirty = true;
                break;
            case KEYCODE_R:
                ROS_DEBUG("RIGHT");
                direction = -1;
                dirty = true;
                break;
            }
            r.sleep();
        }
    }
}

int main(int argc, char **argv)
{
    ros::init(argc, argv, "key_reader");
    key_reader kr;

    signal(SIGINT, quit);

    kr.keyLoop();

    return (0);
}
edit retag flag offensive close merge delete

Comments

1

I'm sorry to have to do this for something so seemingly unimportant, but please don't post screenshots of terminal text in question on ROS Answers. It's all text, so there is no need. Just copy-paste the text from the terminal into your question text. Do make sure to format it properly by selecting the text and pressing ctrl+k (or clicking the Preformatted Text button (the one with 101010 on it)).

You don't need to post a new question, just edit your curent one. You can use the edit button/link for this.

After you replace the screenshot with the error message itself, we can re-open your question.

gvdhoorn gravatar image gvdhoorn  ( 2021-04-13 12:34:17 -0500 )edit

Sorry, I thought it was more readable. Anyway I have update the question, but I can't reopen because >200 points are required. Can you reopen please? Thank you.

bach gravatar image bach  ( 2021-04-13 12:46:03 -0500 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2021-04-14 07:47:24 -0500

updated 2021-04-14 09:30:57 -0500

I think this is caused by an error in the formatting format.

ROS_DEBUG("value: 0x%02X\n", c);

Try it with the X in lower case instead of upper case.

ROS_DEBUG("value: 0x%02x\n", c);

Update

I found another one.

 read(kfd, &c, 1)

There is no semicolon.

edit flag offensive delete link more

Comments

1

HI, thank you for your answer. I tried and the result is the same. More over I have tried also another strategy, without select() and using fcntl:

if (fcntl(kfd, F_SETFL, O_NONBLOCK) == -1)
    {
        perror("fcntl:"); // an error accured
        exit(-1);
    }

And it works fine (even if with ROS_DEBUG("value: 0x%02X\n", c);). So there must be something wrong on the usage of select(). Anyway with the latest modification the program code compiles.

UPDATE

You hit the problem. I close, thank you!

bach gravatar image bach  ( 2021-04-14 09:15:18 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2021-04-13 12:07:25 -0500

Seen: 33 times

Last updated: Apr 14