I’m attempting to do the services quiz with the custom interface but my server.cpp file or any other file does not seem to recognize that the file exists? As you can see there isn’t an underline in the #include section at the top of the code while if it recognizes like geometry_msgs there is an underline. What could be the problem?
I have created another package with the same name, fixed its CMake and package.xml and compiled it. I entered ros2 interface list and clearly the interface is there:
In the server and client package, in the CMake file i’ve added:
This is the code for the services_quiz_srv CMakeLists. Thank you for taking a look at it.
cmake_minimum_required(VERSION 3.8)
project(services_quiz_srv)
# Default to C99
if(NOT CMAKE_C_STANDARD)
set(CMAKE_C_STANDARD 99)
endif()
# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
endif()
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()
# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rosidl_default_generators REQUIRED)
if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
# the following line skips the linter which checks for copyrights
# comment the line when a copyright and license is added to all source files
set(ament_cmake_copyright_FOUND TRUE)
# the following line skips cpplint (only works in a git repo)
# comment the line when this package is in a git repo and when
# a copyright and license is added to all source files
set(ament_cmake_cpplint_FOUND TRUE)
ament_lint_auto_find_test_dependencies()
endif()
rosidl_generate_interfaces(${PROJECT_NAME}
"srv/Spin.srv"
)
ament_package()
is the placement of the services_quiz_srv correct? I’ve already added the lines for std_msgs on my CMake and package.xml file of my service_quiz_srv package and it has compiled succesfully!
This is the main server.cpp file:
#include "geometry_msgs/msg/twist.hpp"
#include "rclcpp/rclcpp.hpp"
#include "services_quiz_srv/srv/spin.hpp"
#include <memory>
using Spin_srv = services_quiz_srv::srv::Spin;
using std::placeholders::_1;
using std::placeholders::_2;
class RotateServer : public rclcpp::Node {
public:
RotateServer() : Node("server_rotate") {
srv_ = create_service<Spin_srv>(
"rotate", std::bind(&RotateServer::spin_callback, this, _1, _2));
publisher_ =
this->create_publisher<geometry_msgs::msg::Twist>("cmd_vel", 10);
}
private:
rclcpp::Service<Spin_srv>::SharedPtr srv_; // error comes from this line because spin.srv is not being detected
rclcpp::Publisher<geometry_msgs::msg::Twist>::SharedPtr publisher_;
void spin_callback(const std::shared_ptr<Spin_srv::Request> request,
const std::shared_ptr<Spin_srv::Response> response) {
auto cmd = geometry_msgs::msg::Twist();
if (request->direction == "Right") {
cmd.linear.x = 0.1; // trial
cmd.angular.z = 0.1;
publisher_->publish(cmd);
response->success = true;
}
if (request->direction == "Left") {
cmd.linear.x = -0.1;
cmd.angular.z = -0.1;
publisher_->publish(cmd);
response->success = false;
}
}
};
int main(int argc, char *argv[]) {
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<RotateServer>());
rclcpp::shutdown();
return 0;
}
And when I compile it, I’m still getting ‘no such file or directory’. Is the placement of the srv package file wrong? or maybe I’m also missing something in the CMake and package.xml file in the main package.
Below is the Cmake and xml for the main package and NOT the custom srv package.
cmake_minimum_required(VERSION 3.8)
project(services_quiz)
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()
# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(geometry_msgs REQUIRED)
find_package(std_srvs REQUIRED)
find_package(services_quiz_srv REQUIRED)
if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
# the following line skips the linter which checks for copyrights
# comment the line when a copyright and license is added to all source files
set(ament_cmake_copyright_FOUND TRUE)
# the following line skips cpplint (only works in a git repo)
# comment the line when this package is in a git repo and when
# a copyright and license is added to all source files
set(ament_cmake_cpplint_FOUND TRUE)
ament_lint_auto_find_test_dependencies()
endif()
add_executable(server_rotate_node src/server.cpp)
ament_target_dependencies(server_rotate_node rclcpp geometry_msgs services_quiz_srv std_srvs)
add_executable(client_rotate_node src/client.cpp)
ament_target_dependencies(client_rotate_node rclcpp services_quiz_srv std_srvs)
install(TARGETS
server_rotate_node
client_rotate_node
DESTINATION lib/${PROJECT_NAME}
)
install(DIRECTORY
launch
DESTINATION share/${PROJECT_NAME}/
)
ament_package()
Yes, your folder structure and organization of the files are correct.
But it is better to rename your cpp files to something non generic. client.cpp and server.cpp are generic names which will be used by the ROS framework internally. So program paths may get confused.
If your services_quiz_srv package compiled successfully, then you have no issues with that package. There is probably an error in services_quiz package.
I did not go through your server program much as error says there is a problem in line 3.
Your CMakeLists.txt and package.xml files also look good.
So, I have a final suggestion for you.
Delete the build, install and log folders and do colcon build to rebuild from scratch.
Hi @girishkumar.kannan, Thank you for your response. I’ve deleted build, install, and log now it is just issuing warnings, which i know isnt a big deal but it is still working.