turtlebot_marker_server.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <ros/ros.h>
32 #include <string.h>
33 #include <geometry_msgs/Twist.h>
34 #include <geometry_msgs/Pose.h>
35 #include <tf/tf.h>
36 
37 using namespace visualization_msgs;
38 
40 {
41  public:
43  : nh("~"), server("turtlebot_marker_server")
44  {
45  std::string cmd_vel_topic;
46 
47  nh.param<std::string>("link_name", link_name, "/base_link");
48  nh.param<double>("linear_scale", linear_scale, 1.0);
49  nh.param<double>("angular_scale", angular_scale, 2.2);
50 
51  vel_pub = nh.advertise<geometry_msgs::Twist>("/cmd_vel", 1);
52  createInteractiveMarkers();
53 
54  ROS_INFO("[turtlebot_marker_server] Initialized.");
55  }
56 
57  void processFeedback(
58  const InteractiveMarkerFeedbackConstPtr &feedback );
59 
60  private:
61  void createInteractiveMarkers();
62 
66 
67  double linear_scale;
68  double angular_scale;
69 
70  std::string link_name;
71 };
72 
74  const InteractiveMarkerFeedbackConstPtr &feedback )
75 {
76  // Handle angular change (yaw is the only direction in which you can rotate)
77  double yaw = tf::getYaw(feedback->pose.orientation);
78 
79  geometry_msgs::Twist vel;
80  vel.angular.z = angular_scale*yaw;
81  vel.linear.x = linear_scale*feedback->pose.position.x;
82 
83  vel_pub.publish(vel);
84 
85  // Make the marker snap back to turtlebot
86  server.setPose("turtlebot_marker", geometry_msgs::Pose());
87 
88  server.applyChanges();
89 }
90 
92 {
93  // create an interactive marker for our server
94  InteractiveMarker int_marker;
95  int_marker.header.frame_id = link_name;
96  int_marker.name = "turtlebot_marker";
97  //int_marker.description = "Move the turtlebot";
98 
99  InteractiveMarkerControl control;
100 
101  control.orientation_mode = InteractiveMarkerControl::FIXED;
102  control.orientation.w = 1;
103  control.orientation.x = 1;
104  control.orientation.y = 0;
105  control.orientation.z = 0;
106  control.name = "move_x";
107  control.interaction_mode = InteractiveMarkerControl::MOVE_AXIS;
108  int_marker.controls.push_back(control);
109 
110  control.orientation.w = 1;
111  control.orientation.x = 0;
112  control.orientation.y = 1;
113  control.orientation.z = 0;
114  control.name = "rotate_z";
115 
116  control.interaction_mode = InteractiveMarkerControl::MOVE_ROTATE;
117  //control.interaction_mode = InteractiveMarkerControl::ROTATE_AXIS;
118  int_marker.controls.push_back(control);
119 
120  // Commented out for non-holonomic turtlebot. If holonomic, can move in y.
121  /*control.orientation.w = 1;
122  control.orientation.x = 0;
123  control.orientation.y = 0;
124  control.orientation.z = 1;
125  control.name = "move_y";
126  control.interaction_mode = InteractiveMarkerControl::MOVE_AXIS;
127  int_marker.controls.push_back(control);*/
128 
129  server.insert(int_marker, boost::bind( &TurtlebotMarkerServer::processFeedback, this, _1 ));
130 
131  server.applyChanges();
132 }
133 
134 
135 int main(int argc, char** argv)
136 {
137  ros::init(argc, argv, "turtlebot_marker_server");
138  TurtlebotMarkerServer turtleserver;
139 
140  ros::spin();
141 }
void processFeedback(const InteractiveMarkerFeedbackConstPtr &feedback)
static double getYaw(const Quaternion &bt_q)
double yaw
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
boost::shared_ptr< interactive_markers::InteractiveMarkerServer > server
control
ROSCPP_DECL void spin(Spinner &spinner)
#define ROS_INFO(...)
int main(int argc, char **argv)
interactive_markers::InteractiveMarkerServer server


turtlebot_interactive_markers
Author(s): Helen Oleynikova
autogenerated on Mon Jun 10 2019 15:39:17