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

Calling a service in C++ without write it on terminal

asked 2014-10-07 10:48:17 -0500

astur gravatar image

updated 2014-10-07 11:08:40 -0500

I wanna clear my turtlesim path when I press a specific button of my joystick, like when I write it on terminal: rosservice call clear .

Afterwards, I wanna do the same idea but changing the background like rosparam set background-r 150

#include <ros/ros.h>
#include <geometry_msgs/Twist.h>
#include <sensor_msgs/Joy.h>

#include <ros/service.h>

class TeleopTurtle
{
public:
  TeleopTurtle();

private:
  void joyCallback(const sensor_msgs::Joy::ConstPtr& joy);

  ros::NodeHandle nh_;

  int linear_, angular_;
  double l_scale_, a_scale_;
  ros::Publisher vel_pub_;
  ros::Subscriber joy_sub_;

};

TeleopTurtle::TeleopTurtle():
  linear_(1),
  angular_(2)
{

  nh_.param("axis_linear", linear_, linear_);
  nh_.param("axis_angular", angular_, angular_);
  nh_.param("scale_angular", a_scale_, a_scale_);
  nh_.param("scale_linear", l_scale_, l_scale_);

  vel_pub_ = nh_.advertise<geometry_msgs::Twist>("turtle1/cmd_vel", 1);
  joy_sub_ = nh_.subscribe<sensor_msgs::Joy>("joy", 10, &TeleopTurtle::joyCallback, this);

}

void TeleopTurtle::joyCallback(const sensor_msgs::Joy::ConstPtr& joy)
{
  geometry_msgs::Twist twist;
  twist.linear.x = l_scale_*joy->axes[linear_];
  twist.angular.z = a_scale_*joy->axes[angular_];
  vel_pub_.publish(twist);
  if (joy->buttons[2] == 1)
  {
   //ros::service::call(clear);  // Call service:  rosservice call clear
   } 


}
int main(int argc, char** argv)
{
  ros::init(argc, argv, "teleop_turtle");
  TeleopTurtle teleop_turtle;      

  ros::spin();
}
edit retag flag offensive close merge delete

Comments

All this is possible, but what is your question?

dornhege gravatar image dornhege  ( 2014-10-07 10:54:33 -0500 )edit

Calling a ros service (rosservice call clear) in C++ without write it on terminal when i press a specific button

astur gravatar image astur  ( 2014-10-07 11:03:21 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
3

answered 2014-10-07 10:54:10 -0500

paulbovbel gravatar image

updated 2014-10-07 10:54:30 -0500

Tutorial, please research in the future.

edit flag offensive delete link more

Comments

Sorry I lacked: without write it on terminal. I checked all tutorials and some answers like http://answers.ros.org/question/12661...

astur gravatar image astur  ( 2014-10-07 11:16:01 -0500 )edit
2

You should work through all the beginner tutorials listed here, it will save you a lot of trouble down the line.

paulbovbel gravatar image paulbovbel  ( 2014-10-07 11:33:26 -0500 )edit
1

answered 2018-01-29 15:01:08 -0500

AaronBecker gravatar image

Here is simple code to do this, adapted from the examples at A Gentle Introduction to ROS. The last three lines are key, since they call the service. Remember to #include the relevant .h file for the service you are using.

// This program waits for the clear service to be available
// (potentially waiting for turtlesim to start up), and
// clears the screen.
#include <ros/ros.h>
#include <std_srvs/Empty.h>

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

  // Wait until the clear service is available, which
  // indicates that turtlesim has started up, and has
  // set the background color parameters.
  ros::service::waitForService("clear");  //this is optional

  // Get turtlesim to pick up the new parameter values.
  ros::ServiceClient clearClient
    = nh.serviceClient<std_srvs::Empty>("/clear");
  std_srvs::Empty srv;
  clearClient.call(srv);
}
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2014-10-07 10:48:17 -0500

Seen: 7,764 times

Last updated: Oct 07 '14