ROS2 Basics in 5 Days Humble (C++) - 4.7 services quiz - can not call service manually

Hello, I have a problem with the manual service server call that should be programmed for the quiz.
I tried manually calling the server with the following command in order to see if I had properly programmed the server:

ros2 service call /rotate services_quiz_srv/srv/Spin '{direction: "right", angular_velocity: 0.2, time: 5}'

but i recieve the following error:

The passed service type is invalid

When i check which type the interface has it returns the following:

user:~/ros2_ws$ ros2 interface show services_quiz_srv/srv/Spin
string direction               # Direction to spin (right or left)
float64 angular_velocity       # Angular Velocity (in rad/s)
int32 time                     # Duration of the spin (in seconds)
---
bool success                   # Did it achieve it?
user:~/ros2_ws$ ros2 service list -t | grep rotate
/rotate [services_quiz_srv/srv/Spin]

Here is a part of the cpp file with the first part of the class:

class ServerNode : public rclcpp::Node {
public:
  ServerNode() : Node("spin_robot_server_node") {

    srv_ = create_service<spin_msg>(
        "rotate", std::bind(&ServerNode::rotate_callback, this, _1, _2));
    publisher_ =
        this->create_publisher<geometry_msgs::msg::Twist>("cmd_vel", 10);
  }

private:
  rclcpp::Service<spin_msg>::SharedPtr srv_;
  rclcpp::Publisher<geometry_msgs::msg::Twist>::SharedPtr publisher_;

I am very thankful for any help.
Greethings Tim

The complaint is about the structure of your message. Can you try this and see the structure that auto-filled for you?

ros2 service call /rotate [TAB] [TAB]  # press tab twice very fast.

Also, what is the output of:

ros2 service info /rotate

?

Here the output of the double TAB:

user:~/ros2_ws$ ros2 service call /rotate
--rate                      -r                          services_quiz_srv/srv/Spin

also here the output:

user:~/ros2_ws$ ros2 service info /rotate
usage: ros2 service [-h] [--include-hidden-services] Call `ros2 service <command> -h` for more detailed usage. ...
ros2 service: error: argument Call `ros2 service <command> -h` for more detailed usage.: invalid choice: 'info' (choose from 'call', 'find', 'list', 'type')

Thank you very much for your help!

Okay, for this, you should type services_quiz_srv/srv/Spin and press tab tab again to suggest the structure.

This error means info is not a valid command, my bad. Try:

ros2 service type /rotate
user:~/ros2_ws$ ros2 service call /rotate services_quiz_srv/srv/Spin
build/               create_subscription  install/             log/                 src/

when i choose create_subscription and double TAB again, than the following occours:

user:~/ros2_ws$ ros2 service call /rotate services_quiz_srv/srv/Spin create_subscription -
--rate  -r

here the output is:

user:~/ros2_ws$ ros2 service type /rotate
services_quiz_srv/srv/Spin

This is okay.

This is not okay. You should have the structure suggested. create subscription is not the right option.

See the following example with the service in the intro.

user:~$ ros2 service call /moving std_srvs/srv/Empty
--rate  -r      {}\

Here {} is the right structure. It’s optional in this case because it’s empty.

Try the following:

  1. Run `source ~/ros2_ws/install/setup.bash
  2. Ensure you leave a space after the message type before pressing tab tab.

Yes, this makes sense. Tanks again for the hint with the double tab. This is quite helpful!

This is not working…

user:~/ros2_ws$ source ~/ros2_ws/install/devel/setup.bash
bash: /home/user/ros2_ws/install/devel/setup.bash: No such file or directory

Should this has the same effect as the following command?

source ~/ros2_ws/install/setup.bash

I am using this every time after a package was build.

Yes, you are right; the source command was wrong. It is the one you suggested.
Quick question - are you sure your package was properly built? Are you able to call the service within your service client?

Before I program the client, I wanted to test this first. I thought that would work.

I can launch the server node:

user:~/ros2_ws$ ros2 launch services_quiz services_quiz_server.launch.py
[INFO] [launch]: All log files can be found below /home/user/.ros/log/2023-04-26-10-57-38-932745-1_xterm-31715
[INFO] [launch]: Default logging verbosity is set to INFO
[INFO] [spin_robot_server_node-1]: process started with pid [31716]

It should work from the terminal, so check that your custom message and service server code are properly created and compiled.

I check again and will give an update tomorrow.

Thank you for your help so far! @bayodesegun

I am running again into problems when loading the course to continue working on it. Had this issue several times now. Do you know what problem causes this?
You can see in the picture below, that the workspace / vs code isn’t loading properly…
grafik

Hi @TimSch

I apologize for this glitch. Please try the workaround described here (create a new file).

Hi @TimSch,

I have just deployed another fix for the IDE workspace problem and now it should go away.

Please let us know if you ever see this problem again.

If you don’t see it, please let us know too - we’ll be glad to know!

1 Like

I checked again my Spin.srv and the corresponding package.xml and CMakeLists.txt. They are now looking like the following:

Spin.srv:

string direction               # Direction to spin (right or left)
float64 angular_velocity       # Angular Velocity (in rad/s)
int32 time                     # Duration of the spin (in seconds)
---
bool success                   # Did it achieve it?

package.xml:

<?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_srv</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>std_msgs</depend>

  <build_depend>rosidl_default_generators</build_depend>
  <exec_depend>rosidl_default_runtime</exec_depend>
  <member_of_group>rosidl_interface_packages</member_of_group>

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

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

CMakeLists.txt:

cmake_minimum_required(VERSION 3.5)
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(std_msgs 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()

After the succesful build, when i do “tab tab” after i typed in the command:
ros2 service call /rotate services_quiz_srv/srv/Spin
the following is returned:

user:~$ ros2 service call /rotate services_quiz_srv/srv/Spin
--rate                                                 -r                                                     direction:\ \'\'\^Jangular_velocity:\ 0.0\^Jtime:\ 0\

when i then start to write ‘dir’ and press tab again for auto completion, i get the following output:

user:~$ ros2 service call /rotate services_quiz_srv/srv/Spin direction:\ \'\'\
angular_velocity:\ 0.0\
time:\ 0\

When I change than the values I am than in a kind of editor or something which i am not able to leave:

user:~$ ros2 service call /rotate services_quiz_srv/srv/Spin direction:\ \'right'\
angular_velocity:\ 0.5\
time:\ 5\
>
>
>

I got it working!
I had to type the following:

ros2 service call /rotate services_quiz_srv/srv/Spin "{direction: 'right', angular_velocity: 0.5, time: 5}"

With these files it is now working:

1 Like

Great! Good job!

Just for everyone’s learning, ros 2 autocompletion for the message structure has one tiny catch:

You need to type " and the first part of the structure to get auto-completion.

In this case that would be

"{dir

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