twist_marker.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (CC BY-NC-SA 4.0 License)
3  *
4  * Copyright (c) 2014, PAL Robotics, S.L.
5  * All rights reserved.
6  *
7  * This work is licensed under the Creative Commons
8  * Attribution-NonCommercial-ShareAlike 4.0 International License.
9  *
10  * To view a copy of this license, visit
11  * http://creativecommons.org/licenses/by-nc-sa/4.0/
12  * or send a letter to
13  * Creative Commons, 444 Castro Street, Suite 900,
14  * Mountain View, California, 94041, USA.
15  *********************************************************************/
16 
17 /*
18  * @author Enrique Fernandez
19  */
20 
21 #include <ros/ros.h>
22 #include <geometry_msgs/Twist.h>
23 #include <visualization_msgs/Marker.h>
24 #include <visualization_msgs/MarkerArray.h>
25 
26 #include <string>
27 
29 {
30 public:
31 
32  TwistMarker(double scale = 1.0, double z = 0.0, const std::string& frame_id = "base_footprint")
33  : frame_id_(frame_id)
34  , scale_(scale)
35  , z_(z)
36  {
37  // ID and type:
38  marker_.id = 0;
39  marker_.type = visualization_msgs::Marker::ARROW;
40 
41  // Frame ID:
42  marker_.header.frame_id = frame_id_;
43 
44  // Pre-allocate points for setting the arrow with the twist:
45  marker_.points.resize(2);
46 
47  // Vertical position:
48  marker_.pose.position.z = z_;
49 
50  // Scale:
51  marker_.scale.x = 0.05 * scale_;
52  marker_.scale.y = 2 * marker_.scale.x;
53 
54  // Color:
55  marker_.color.a = 1.0;
56  marker_.color.r = 0.0;
57  marker_.color.g = 1.0;
58  marker_.color.b = 0.0;
59  }
60 
61  void update(const geometry_msgs::Twist& twist)
62  {
63  marker_.points[1].x = twist.linear.x;
64 
65  if (fabs(twist.linear.y) > fabs(twist.angular.z))
66  {
67  marker_.points[1].y = twist.linear.y;
68  }
69  else
70  {
71  marker_.points[1].y = twist.angular.z;
72  }
73  }
74 
75  const visualization_msgs::Marker& getMarker()
76  {
77  return marker_;
78  }
79 
80 private:
81  visualization_msgs::Marker marker_;
82 
83  std::string frame_id_;
84  double scale_;
85  double z_;
86 };
87 
89 {
90 public:
91 
92  TwistMarkerPublisher(double scale = 1.0, double z = 0.0)
93  : marker_(scale, z)
94  {
95  ros::NodeHandle nh;
96 
97  pub_ = nh.advertise<visualization_msgs::Marker>("marker", 1, true);
98  sub_ = nh.subscribe("twist", 1, &TwistMarkerPublisher::callback, this);
99  }
100 
101  void callback(const geometry_msgs::TwistConstPtr& twist)
102  {
103  marker_.update(*twist);
104 
105  pub_.publish(marker_.getMarker());
106  }
107 
108 private:
111 
113 };
114 
115 int
116 main(int argc, char *argv[])
117 {
118  ros::init(argc, argv, "twist_marker");
119 
120  TwistMarkerPublisher t(1.0, 2.0);
121 
122  while (ros::ok())
123  {
124  ros::spin();
125  }
126 
127  return EXIT_SUCCESS;
128 }
129 
visualization_msgs::Marker marker_
void callback(const geometry_msgs::TwistConstPtr &twist)
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
def twist(x=0.0, r=0.0)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
ros::Subscriber sub_
int main(int argc, char *argv[])
ros::Publisher pub_
std::string frame_id_
void update(const geometry_msgs::Twist &twist)
ROSCPP_DECL bool ok()
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
ROSCPP_DECL void spin()
TwistMarker(double scale=1.0, double z=0.0, const std::string &frame_id="base_footprint")
TwistMarkerPublisher(double scale=1.0, double z=0.0)
const visualization_msgs::Marker & getMarker()


twist_mux
Author(s): Enrique Fernandez , Siegfried-A. Gevatter Pujals
autogenerated on Wed Oct 26 2022 02:14:17