Custom Interface Problem

image

Hello,

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:
image

In the server and client package, in the CMake file i’ve added:
image
and

In the package.xml file i’ve added:
image

When I compile I just get:

Thank you for the help!

Cheers.

Hi @william.lubiantoro ,

Could you please share the contents of services_quiz_srv package’s CMakeList.txt file as a code-block?

I guess you are having a problem in that file.

Regards,
Girish

Hi @girishkumar.kannan,

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()

William

Hi @william.lubiantoro ,

I found a couple of things missing in your CMakeLists.txt.

  1. You need to add std_msgs as a dependency under “find dependencies”. Don’t forget to add std_msgs in package.xml as <depend>std_msgs</depend>.
# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)   # add this line here
find_package(rosidl_default_generators REQUIRED)
  1. You must add DEPENDENCIES ... under rosidl_generate_interfaces(...):
rosidl_generate_interfaces(${PROJECT_NAME}
  "srv/Spin.srv"
  DEPENDENCIES std_msgs   # add this line here
)

Doing these steps should fix your problem. Let me know if you still have issues.

Regards,
Girish

Hi @girishkumar.kannan,

Many thanks for the help, will post back if I run into more problems.

Regards,
William

Hi @william.lubiantoro ,

So is your problem solved for now?
The custom interface now works correctly for you?

– Girish

Hi @girishkumar.kannan,

It seems I still have a problem. Currently the workspace looks like this:


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()

<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
  <name>services_quiz</name>
  <version>0.0.0</version>
  <description>TODO: Package description</description>
  <maintainer email="user@todo.todo">user</maintainer>
  <license>TODO: License declaration</license>

  <buildtool_depend>ament_cmake</buildtool_depend>

  <depend>rclcpp</depend>
  <depend>geometry_msgs</depend>
  <depend>services_quiz_srv</depend>
  <depend>std_srvs</depend>

  <test_depend>ament_lint_auto</test_depend>
  <test_depend>ament_lint_common</test_depend>

  <export>
    <build_type>ament_cmake</build_type>
  </export>
</package>

Many Thanks on all the help,
William

Hi @william.lubiantoro ,

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.

cd ~/ros2_ws
rm -rf ./build ./install ./log
colcon build
source install/setup.bash

This should hopefully fix your problem. Let me know if you still have the issue.

Regards,
Girish

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.



The colcon build seems to have finished alright and service callable :slight_smile:. just getting those red error compiles.

Thank you very much.

Regards,
William

Hi @william.lubiantoro ,

You can safely ignore those warnings. They do not cause any issues with the packages.
Even I have had those. And telling you from my experience.

All that is important is that colcon build output reports “Finished”.
If it says “Failed” then we have a problem!

So yeah, you are good for now.

Please mark the corresponding post as “Solution” if your problem is solved, so that this issue can be closed.

Regards,
Girish

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.