Ask Your Question
0

Compiler error when trying to read message values

asked 2017-08-15 03:35:44 -0600

Sergey Kravchenko gravatar image

updated 2017-08-15 03:36:22 -0600

Here is my SWrite.msg file

int16 id
int16 pos
int16 time

Here is my CMakeLists.txt

set (CMAKE_CXX_STANDARD 11)

# %Tag(FULLTEXT)%
cmake_minimum_required(VERSION 2.8.3)
project(scs_servo_control)

find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs genmsg)

add_message_files(FILES SWrite.msg)

generate_messages(DEPENDENCIES std_msgs)

catkin_package()

include_directories(include ${catkin_INCLUDE_DIRS} )

add_executable(scs_listener src/scs_listener.cpp)
add_library(hardwareserial src/HardwareSerial.cpp)
add_library(scservo src/SCServo.cpp)
add_library(scsprotocol src/SCSProtocol.cpp)
target_link_libraries(scs_listener ${catkin_LIBRARIES} hardwareserial scservo scsprotocol)
add_dependencies(scs_listener beginner_tutorials_generate_messages_cpp)

# %EndTag(FULLTEXT)%

And here is my callback function:

void chatterCallback(const std_msgs::String::ConstPtr& msg)
{
  int id = msg->id;
}

I get compiler error with this:

/home/serge/NetBeansProjects/ROS_beginner_tutorials/ros_ws/src/scs_servo_control/src/scs_listener.cpp: In function ‘void chatterCallback(const ConstPtr&)’:
/home/serge/NetBeansProjects/ROS_beginner_tutorials/ros_ws/src/scs_servo_control/src/scs_listener.cpp:26:19: error: ‘const struct std_msgs::String_<std::allocator<void> >’ has no member named ‘id’
     int id = msg->id;
                   ^
scs_servo_control/CMakeFiles/scs_listener.dir/build.make:62: recipe for target 'scs_servo_control/CMakeFiles/scs_listener.dir/src/scs_listener.cpp.o' failed

It seems to be logical, because id (and also pos and time) is nowhere defined as member.

But I read this https://answers.ros.org/question/5458...

and this

https://answers.ros.org/question/1589...

and it confuses me.

So how can I read values from a message in my callback function? What I missed?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2017-08-15 04:11:27 -0600

gvdhoorn gravatar image

updated 2017-08-15 04:11:59 -0600

And here is my callback function:

void chatterCallback(const std_msgs::String::ConstPtr& msg)
{
  int id = msg->id;
}

you define a custom message (SWrite.msg), but in your callback signature you still appear to be using std_msgs::String. That doesn't make sense to me.

The error message ("there is no field id in std_msgs::String") does make sense in that case.

edit flag offensive delete link more

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: 2017-08-15 03:35:44 -0600

Seen: 259 times

Last updated: Aug 15 '17