ros client failed to get param
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!