Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

close, but I suggest going over the ROS tutorials in more detail. In particular, the examples on c++ service servers and clients.

Below fixes should compile

#include <ros/ros.h>
#include <gazebo_msgs/SetModelState.h>
int main (int argc, char** argv)
{

  ros::NodeHandle n;
  ros::ServiceClient client = n.serviceClient<gazebo_msgs::SetModelState>("/gazebo_msgs/set_model_state"); 
  gazebo_msgs::SetModelState setmodelstate;
  gazebo_msgs::ModelState modelstate;
  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;
}

close, but I suggest going over the ROS tutorials in more detail. In particular, the examples on c++ service servers 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/fuerte/setup.bash
roslaunch gazebo_worlds empty_world.launch

Open yet another terminal, spawn a model:

source /opt/ros/fuerte/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 clients.

Below fixes should compilecreate a package named gazebo_test:

cd /tmp
source /opt/ros/fuerte/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_msgs/set_model_state"); 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

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/fuerte/setup.bash
roslaunch gazebo_worlds empty_world.launch

Open yet another terminal, spawn a model:

source /opt/ros/fuerte/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/fuerte/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: Output:

$ ./bin/example 
 [ INFO] [1329941615.597042253]: BRILLIANT!!!
 [ INFO] [1329941615.597092973]: 0.000000

0.000000

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/fuerte/setup.bash
/opt/ros/electric/setup.bash
roslaunch gazebo_worlds empty_world.launch

Open yet another terminal, spawn a model:

source /opt/ros/fuerte/setup.bash
/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/fuerte/setup.bash
/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