pubvel_toggle_plus.cpp
Go to the documentation of this file.
1 //this program toggles between rotation and translation
2 //commands,based on calls to a service.
3 #include <ros/ros.h>
4 #include <std_srvs/Empty.h>
5 #include <geometry_msgs/Twist.h>
6 #include <demodoc/Changerate.h>
7 
8 bool forward = true;
9 double newfrequency;
10 bool ratechanged = false;
11 
12 
21  std_srvs::Empty::Request &req,
22  std_srvs::Empty::Response &resp){
23  forward = !forward;
24  ROS_INFO_STREAM("Now sending "<<(forward?
25  "forward":"rotate")<< " commands.");
26  return true;
27 }
28 
37  demodoc::Changerate::Request &req,
38  demodoc::Changerate::Response &resp){
39 
40  ROS_INFO_STREAM("Changing velocity to "<<req.newrate);
41 
42  newfrequency = req.newrate;
43  ratechanged = true;
44  return true;
45 }
46 
55 int main(int argc, char **argv){
56  ros::init(argc,argv,"pubvel_toggle");
57  ros::NodeHandle nh;
58 
59  ros::ServiceServer server =
60  nh.advertiseService("toggle_forward",&toggleForward);
61 
62  ros::ServiceServer server0 =
63  nh.advertiseService("change_rate",&changeRate);
64 
65  ros::Publisher pub=nh.advertise<geometry_msgs::Twist>(
66  "turtle1/cmd_vel",1000);
67 
68  ros::Rate rate(2);
69  while(ros::ok()){
70  geometry_msgs::Twist msg;
71  msg.linear.x = forward?1.0:0.0;
72  msg.angular.z=forward?0.0:1.0;
73  pub.publish(msg);
74  ros::spinOnce();
75  if(ratechanged) {
76  rate = ros::Rate(newfrequency);
77  ratechanged = false;
78  }
79  rate.sleep();
80  }
81 }
bool changeRate(demodoc::Changerate::Request &req, demodoc::Changerate::Response &resp)
changeRate is a sevice that allows you to change the rate at which the node is publishing the command...
int main(int argc, char **argv)
The main function creates the node that: a) publishes commanded velocities of type <geometry_msgs::Tw...
bool forward
bool ratechanged
bool toggleForward(std_srvs::Empty::Request &req, std_srvs::Empty::Response &resp)
toggleForward is a sevice that allows you to toggle between translational and rotational velocities t...
double newfrequency


demodoc
Author(s):
autogenerated on Mon Mar 12 2018 17:58:35