ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Gazebo service through c++ code

asked 2012-02-08 04:44:11 -0600

Penny gravatar image

updated 2014-01-28 17:11:18 -0600

ngrennan gravatar image

I have created a server and client about set model state service of gazebo. This is the error I get when I run make:

/home/phadjic/ros_workspace/pool_party/src/client.cpp: In function ‘int main(int, char)’: /home/phadjic/ros_workspace/pool_party/src/client.cpp:36:39: error: no match for ‘operator=’ in ‘setmodelstate.gazebo_msgs::SetModelState::request.gazebo_msgs::SetModelStateRequest_<std::allocator<void> >::model_state = modelstate’ /opt/ros/electric/stacks/simulator_gazebo/gazebo_msgs/msg_gen/cpp/include/gazebo_msgs/ModelState.h:23:20: note: candidate is: gazebo_msgs::ModelState_<std::allocator<void> >& gazebo_msgs::ModelState_<std::allocator<void> >::operator=(const gazebo_msgs::ModelState_<std::allocator<void> >&) /home/phadjic/ros_workspace/pool_party/src/client.cpp:39:22: error: ‘SetModelState’ was not declared in this scope /home/phadjic/ros_workspace/pool_party/src/client.cpp:42:8: error: no matching function for call to ‘print(int, log4cxx::Logger&, ros::console::Level&, const char [54], int, const char [22], double&)’ /opt/ros/electric/stacks/ros_comm/tools/rosconsole/include/ros/console.h:132:22: note: candidates are: void ros::console::print(ros::console::FilterBase, log4cxx::Logger, ros::console::Level, const char, int, const char, const char, ...) /opt/ros/electric/stacks/ros_comm/tools/rosconsole/include/ros/console.h:136:22: note: void ros::console::print(ros::console::FilterBase, log4cxx::Logger, ros::console::Level, const std::stringstream&, const char, int, const char)

The lines of my client file mentioned above are:

ros::ServiceClient client = n.serviceClient<gazebo_msgs::setmodelstate("/gazebo_msgs/SetModelState"); 
gazebo_msgs::SetModelState setmodelstate;
setmodelstate.request.model_state=modelstate;
client.call(setmodelstate); 

if (client.call(setModelState))
{
   ROS_INFO("BRILLIANT!!!");
   ROS_INFO(start_pose.position.x);
}
else
{
   ROS_ERROR("Failed to call service ");
   return 1;
}
edit retag flag offensive close merge delete

Comments

Correction for the first line; ros::ServiceClient client = n.serviceClient<gazebo_msgs::setmodelstate>("/gazebo_msgs/SetModelState");

Abdulbaki gravatar image Abdulbaki  ( 2017-12-30 08:52:46 -0600 )edit

4 Answers

Sort by » oldest newest most voted
6

answered 2012-02-08 07:56:59 -0600

hsu gravatar image

updated 2012-02-22 08:18:06 -0600

Here is a complete set of instructions to get things working,

Open a new terminal, start gazebo empty world, spawn an object:

source /opt/ros/electric/setup.bash
roslaunch gazebo_worlds empty_world.launch

Open yet another terminal, spawn a model:

source /opt/ros/electric/setup.bash
rosrun gazebo spawn_model -urdf -file `rospack find gazebo_worlds`/objects/14_4v_cordless_drill.urdf -model drill -z 1

Open a third terminal, setup ros and create a package named gazebo_test:

cd /tmp
source /opt/ros/electric/setup.bash
roscreate-pkg gazebo_test gazebo gazebo_msgs
cd gazebo_test
export ROS_PACKAGE_PATH=`pwd`:${ROS_PACKAGE_PATH}

edit CMakeLists.txt so it looks like this:

cmake_minimum_required(VERSION 2.4.6)
include($ENV{ROS_ROOT}/core/rosbuild/rosbuild.cmake)

# Set the build type.  Options are:
#  Coverage       : w/ debug symbols, w/o optimization, w/ code-coverage
#  Debug          : w/ debug symbols, w/o optimization
#  Release        : w/o debug symbols, w/ optimization
#  RelWithDebInfo : w/ debug symbols, w/ optimization
#  MinSizeRel     : w/o debug symbols, w/ optimization, stripped binaries
#set(ROS_BUILD_TYPE RelWithDebInfo)

rosbuild_init()

#set the default path for built executables to the "bin" directory
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
#set the default path for built libraries to the "lib" directory
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)

rosbuild_add_executable(example src/example.cpp)

next, save following code to src/example.cpp:

#include <ros/ros.h>
#include <gazebo_msgs/SetModelState.h>
int main (int argc, char** argv)
{
  ros::init(argc,argv,"test_node");
  ros::NodeHandle n;
  ros::ServiceClient client = n.serviceClient<gazebo_msgs::SetModelState>("/gazebo/set_model_state"); 
  gazebo_msgs::SetModelState setmodelstate;
  gazebo_msgs::ModelState modelstate;
  modelstate.model_name = "drill";
  setmodelstate.request.model_state = modelstate;

  if (client.call(setmodelstate))
  {
    ROS_INFO("BRILLIANT!!!");
    ROS_INFO("%f",modelstate.pose.position.x);
  }
  else
  {
    ROS_ERROR("Failed to call service ");
    return 1;
  }
  return 0;
}

make and run:

make
./bin/example

Output:

$ ./bin/example 
[ INFO] [1329941615.597042253]: BRILLIANT!!!
[ INFO] [1329941615.597092973]: 0.000000
edit flag offensive delete link more

Comments

Thank you a lot for the code above, now I have managed to make my package and create the server and client, but I have the following prob: "Failed to call service", do you now why this happens? should I change something else?

Penny gravatar image Penny  ( 2012-02-22 01:44:36 -0600 )edit

i rosrun again my code, now "Failed to call service" does not appear, but neither "BRILLIANT" appears.

Penny gravatar image Penny  ( 2012-02-22 02:33:21 -0600 )edit

i made roswtf and below i posted what i get:

Penny gravatar image Penny  ( 2012-02-22 03:47:58 -0600 )edit

I just edited my post to a fully functional tutorial, you should be able to follow step by step and see the final output :)

hsu gravatar image hsu  ( 2012-02-22 08:17:19 -0600 )edit

Thank you very much, really your post above solved my problem :)

Penny gravatar image Penny  ( 2012-02-25 02:44:17 -0600 )edit
1

answered 2012-02-08 04:51:11 -0600

DimitriProsser gravatar image

The message type is gazebo_msgs::SetModelState, not gazebo_msgs::setmodelstate. Also, the service name is probably "/gazebo/set_model_state".

Next, what type is "modelstate"?

Additionally, you call the service twice. Each instance of client.call() calls the service. You probably want to delete the first call that is not being checked for completion.

edit flag offensive delete link more

Comments

The type of modelstate is:
Penny gravatar image Penny  ( 2012-02-08 06:03:20 -0600 )edit
You didn't finish your comment
DimitriProsser gravatar image DimitriProsser  ( 2012-02-08 06:13:26 -0600 )edit
gazebo_msgs::ModelState modelstate; modelstate.model_name = (std::string) "my_robot"; modelstate.reference_frame = (std::string) "world"; modelstate.pose = start_pose; modelstate.twist = start_twist;
Penny gravatar image Penny  ( 2012-02-08 06:20:26 -0600 )edit
I corrected some things and now I get the errors I posted below as an answer:
Penny gravatar image Penny  ( 2012-02-08 06:25:01 -0600 )edit
0

answered 2012-02-08 06:25:06 -0600

Penny gravatar image

/home/phadjic/ros_workspace/pool_party/src/client.cpp: In function ‘int main(int, char*)’: /home/phadjic/ros_workspace/pool_party/src/client.cpp:49:8: error: no matching function for call to ‘print(int, log4cxx::Logger&, ros::console::Level&, const char [54], int, const char [22], double&)’ /opt/ros/electric/stacks/ros_comm/tools/rosconsole/include/ros/console.h:132:22: note: candidates are: void ros::console::print(ros::console::FilterBase, log4cxx::Logger, ros::console::Level, const char, int, const char, const char, ...) /opt/ros/electric/stacks/ros_comm/tools/rosconsole/include/ros/console.h:136:22: note: void ros::console::print(ros::console::FilterBase, log4cxx::Logger, ros::console::Level, const std::stringstream&, const char, int, const char*)

edit flag offensive delete link more

Comments

It appears that you're calling some kind of "print()" method on line 49. That's incorrect.
DimitriProsser gravatar image DimitriProsser  ( 2012-02-08 06:29:03 -0600 )edit
it is the ROS_INFO(start_pose.position.x); If I understood right, the function in the parentheses cannot be found
Penny gravatar image Penny  ( 2012-02-08 06:32:43 -0600 )edit
0

answered 2012-02-22 03:48:13 -0600

Penny gravatar image

WARNING The following node subscriptions are unconnected: * /gazebo: * /gazebo/set_model_state * /gazebo/set_link_state

WARNING No tf messages

Found 4 error(s).

ERROR Communication with [/rosout] raised an error: ERROR Communication with [/Set_Model_Test] raised an error: ERROR Communication with [/gazebo] raised an error: ERROR The following nodes should be connected but aren't: * /gazebo->/gazebo (/clock) * /gazebo->/rosout (/clock) * /gazebo->/Set_Model_Test (/clock)

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2012-02-08 04:44:11 -0600

Seen: 4,148 times

Last updated: Feb 22 '12