Ask Your Question
0

ros client failed to get param

asked 2020-04-10 13:08:30 -0600

sueee gravatar image

I was following ros beginner tutorials and trying to write a server and client to add elements in an array This is my my_params.yaml file

origin: [0, 0, 0, 0, 0]

My client.cpp is:

#include "ros/ros.h"
#include "exercise/ArraySum.h"
#include <cstdlib>

int main(int argc, char **argv)
{
  ros::init(argc, argv, "array_sum_client");
  ros::NodeHandle n;
  ros::ServiceClient client = n.serviceClient<exercise::ArraySum>("array_sum");
  exercise::ArraySum srv;
  int input[5];
  if(n.getParam("/origin",*input)) // get input from .yaml
{
 ROS_INFO("getParam");
}
else
{
 ROS_ERROR("Failed to get param 'origin'");
}
 for(int i = 0 ; i < 5 ; i++)
 {
  srv.request.a[i] = input[i]; // pass input data to server
 }
 // srv.request.a = atoll(argv[1]);
  if (client.call(srv))
  {
    ROS_INFO("Sum: %ld", (long int)srv.response.sum); // get result from server
  }
  else
  {
    ROS_ERROR("Failed to call service array_sum");
    return 1;
  }

  return 0;
}

My server.cpp is:

#include "ros/ros.h"
#include "exercise/ArraySum.h"

bool sumArray(exercise::ArraySum::Request  &req,
         exercise::ArraySum::Response &res)
{
int sum = 0;
for(int i = 0 ; i < 5 ; i++)
{  
sum += req.a[i];
ROS_INFO("request: x=%ld", (long int)req.a[i]);
}
res.sum = sum; 
ROS_INFO("sending back response: [%ld]", (long int)res.sum);
  return true;
}

int main(int argc, char **argv)
{
  ros::init(argc, argv, "array_sum_server");
  ros::NodeHandle n;

  ros::ServiceServer service = n.advertiseService("array_sum", sumArray);
  ROS_INFO("Ready to add the array.");
  ros::spin();

  return 0;
}

And this is what I added to CMakeLists.txt

add_executable(array_sum_server src/array_sum_server.cpp)
target_link_libraries(array_sum_server ${catkin_LIBRARIES})
add_dependencies(array_sum_server exercise_gencpp)

add_executable(array_sum_client src/array_sum_client.cpp)
target_link_libraries(array_sum_client ${catkin_LIBRARIES})
add_dependencies(array_sum_client exercise_gencpp)

The command I am using is:

rparam load ./config/my_params.yaml
rosparam set /origin "[1, 1, 1, 1, 1]"

Then is I use rosparam list, I will get:

/origin
/rosdistro
/roslaunch/uris/host_sue_xps_15_7590__44833
/rosversion
/run_id

If I use rosparam get /, I will get:

origin: [1, 1, 1, 1, 1]
...

Then my command is:

 rosrun exercise array_sum_client

But I get a

[ERROR] [1586540531.965667007]: Failed to get param 'origin'

Also if I set paramerters like below instead of using rosparam set /origin "[1, 1, 1, 1, 1]":

rosrun exercise array_sum_client --ros-args -p "origin:=[2,2,2,2,2]"

I will get:

terminate called after throwing an instance of 'ros::InvalidNameException'
  what():  Character [[] is not valid as the first character in Graph Resource Name [[2,2,2,2,2]].  Valid characters are a-z, A-Z, / and in some cases ~.
Aborted (core dumped)

Does anyone have any idea of what's wrong with this? Thank you!

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2020-04-10 14:28:02 -0600

gvdhoorn gravatar image

updated 2020-04-10 14:40:06 -0600

It's likely you'll have to use a std::vector<int> for your input variable.

Afaik there is no getter for an array.

See also roscpp/Overview/Parameter Server: Retrieving Lists.

Also:

rosrun exercise array_sum_client --ros-args -p "origin:=[2,2,2,2,2]"

afaik --ros-args is a ROS 2 construct. It isn't used in ROS 1.

The error message you receive also provides a hint: the origin:=[..] is being interpreted as a ROS 1 topic remapping, not as a parameter.

edit flag offensive delete link more

Comments

Thanks it solves my problem! Regarding the command, I tried to use :

 rosrun exercise array_sum_client _origin:=[2,2,2,2,2]

However, parameter "origin" does not change and there is another parameter "/array_sum_client/origin" added. The parameter value becomes:

array_sum_client: {origin: '[2,2,2,2,2]'}
origin: [0, 0, 0, 0, 0]

Is there any way to solve this? Thanks a lot

sueee gravatar image sueee  ( 2020-04-10 21:53:13 -0600 )edit

The _<parameter_name>:=<parameter_value> syntax only supports setting private parameters.

You're retrieving a global one (as you've prefixed it with a /).

You cannot set global parameters with the underscore-syntax of rosrun.

gvdhoorn gravatar image gvdhoorn  ( 2020-04-11 03:24:23 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2020-04-10 13:08:30 -0600

Seen: 501 times

Last updated: Apr 10 '20