ROS Service in C++ not being built
Hi All,
I am using ROS Kinetic on ubuntu 16.04. I am trying to write a c++ service to send some multi array data on request from the client from server I wrote the following code for that in c++
service definition file : RequestWaypoints.srv
---
int64multiarray waypoints
RequestWaypoints_server.cpp
#include "ros/ros.h"
#include "visualize_waypoints/RequestWaypoints.h"
float waypoints[9][3] = {
{-6.922, 12.437,-1.315},
{3.067, -17.730, -1.315},
{3.067, -17.730, -2.956},
{-1.645, -19.531,-2.956},
{-1.645, -19.531,1.892},
{-12.163, 10.458,1.892},
{-12.163, 10.458,0.351},
{-6.922, 12.437,0.351},
{-6.922, 12.437,-1.315}
};
bool send_waypoints(visualize_waypoints::RequestWaypoints::Request &req,
visualize_waypoints::RequestWaypoints::Response &res)
{
res.waypoints = waypoints;
return true;
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "RequestWaypoints_server");
ros::NodeHandle n;
ros::ServiceServer service = n.advertiseService("RequestWaypoints", send_waypoints);
ROS_INFO("Waypoints were sent.");
ros::spin();
return 0;
}
RequestWaypoints_client.cpp
#include "ros/ros.h"
#include "visualize_waypoints/RequestWaypoints.h"
#include <cstdlib>
int main(int argc, char **argv)
{
ros::init(argc, argv, "visualize_waypoints_client");
ros::NodeHandle n;
ros::ServiceClient client = n.serviceClient<beginner_tutorials::AddTwoInts>("add_two_ints");
visualize_waypoints::RequestWaypoints srv;
waypoints = client.call(srv)
ROS_INFO("Received Waypoints are : %f", (float)srv.response.waypoints);
return 0;
}
I added following lines of code in Package.xml
<buildtool_depend>catkin</buildtool_depend>
<build_depend>roscpp</build_depend>
<build_depend>rospy</build_depend>
<build_depend>std_msgs</build_depend>
<build_depend>message_generation</build_depend>
<build_export_depend>roscpp</build_export_depend>
<build_export_depend>rospy</build_export_depend>
<build_export_depend>std_msgs</build_export_depend>
<exec_depend>roscpp</exec_depend>
<exec_depend>rospy</exec_depend>
<exec_depend>std_msgs</exec_depend>
<exec_depend>message_runtime</exec_depend>
I added following code in cmakelist.txt file
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
message_generation
)
add_service_files(
FILES
RequestWaypoints.srv
)
generate_messages(
DEPENDENCIES
std_msgs
)
add_executable(RequestWaypoints_server src/scripts/RequestWaypoints_server.cpp)
target_link_libraries(RequestWaypoints_server ${catkin_LIBRARIES})
add_dependencies(RequestWaypoints_server RequestWaypoints_gencpp)
add_executable(RequestWaypoints_client src/scripts/RequestWaypoints_client.cpp)
target_link_libraries(RequestWaypoints_client ${catkin_LIBRARIES})
add_dependencies(RequestWaypoints_client RequestWaypoints_gencpp)
Once I run catkin_make I get following error
Base path: /home/rnd424/ros_ws
Source space: /home/rnd424/ros_ws/src
Build space: /home/rnd424/ros_ws/build
Devel space: /home/rnd424/ros_ws/devel
Install space: /home/rnd424/ros_ws/install
####
#### Running command: "make cmake_check_build_system" in "/home/rnd424/ros_ws/build"
####
-- Using CATKIN_DEVEL_PREFIX: /home/rnd424/ros_ws/devel
-- Using CMAKE_PREFIX_PATH: /home/rnd424/ros_ws/devel;/opt/ros/kinetic
-- This workspace overlays: /home/rnd424/ros_ws/devel;/opt/ros/kinetic
-- Using PYTHON_EXECUTABLE: /usr/bin/python
-- Using Debian Python package layout
-- Using empy: /usr/bin/empy
-- Using CATKIN_ENABLE_TESTING: ON
-- Call enable_testing()
-- Using CATKIN_TEST_RESULTS_DIR: /home/rnd424/ros_ws/build/test_results
-- Found gmock sources under '/usr/src/gmock': gmock will be built
-- Found gtest sources under '/usr/src/gmock': gtests will be built
-- Using Python nosetests: /usr/bin/nosetests-2.7
-- catkin 0.7.18
-- BUILD_SHARED_LIBS is on
-- BUILD_SHARED_LIBS is on
-- Using CATKIN_WHITELIST_PACKAGES: visualize_waypoints
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- ~~ traversing 1 packages in topological order:
-- ~~ - visualize_waypoints
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- +++ processing catkin package: 'visualize_waypoints'
-- ==> add_subdirectory(visualize_waypoints)
-- Using these message generators: gencpp;geneus;genlisp;gennodejs;genpy
/opt/ros/kinetic/share/genmsg/cmake/pkg-genmsg.cmake.em:56: error: <class 'genmsg.base.InvalidMsgSpec'>: Invalid declaration: int64multiarray
Traceback (most recent call last):
File "/usr/bin/empy", line 3302, in <module>
if __name__ == '__main__': main()
File "/usr/bin/empy", line 3300, in main
invoke(sys ...
having same issue here. the c++ source file is unable to include the ros service message :/
I guess there is an issue with either your includes, how you set up your dependencies or your service file. Please open a new question and provide the relevant details. Thanks.