viz.cpp
Go to the documentation of this file.
1 #include <rosflight_utils/viz.h>
2 
3 namespace rosflight_utils
4 {
5 
7  nh_private_("~")
8 {
9  // retrieve params
10 
11  // initialize variables
12  mag_sum_ = 0;
13  mag_skip_ = 20;
14  mag_count_ = 0;
15  mag_throttle_ = 0;
16 
17  // Magnetometer visualization
18  mag_sub_ = nh_.subscribe("/magnetometer", 1, &Viz::magCallback, this);
19  mag_pub_ = nh_.advertise<geometry_msgs::PoseStamped>("viz/magnetometer", 1);
20  pts_pub_ = nh_.advertise<visualization_msgs::Marker>("viz/cloud", 1);
21 
22  // Attitude visualization
23  att_sub_ = nh_.subscribe("/attitude", 1, &Viz::attCallback, this);
24  pose_pub_ = nh_.advertise<geometry_msgs::PoseStamped>("viz/attitude", 1);
25 }
26 
27 void Viz::magCallback(const sensor_msgs::MagneticFieldConstPtr &msg)
28 {
30  {
31  // unpack message
32  double x = msg->magnetic_field.x;
33  double y = msg->magnetic_field.y;
34  double z = msg->magnetic_field.z;
35 
36  // get euler angles from vector (assume no roll)
37  double yaw = atan2(y, x);
38  double pitch = atan2(-z, sqrt(x*x + y*y));
39 
40  // convert to body quaternion and rotation into the vehicle frame
41  tf::Quaternion q_v = tf::createQuaternionFromRPY(0, pitch, yaw);
42 
43  // pack data into pose message
44  geometry_msgs::PoseStamped pose_msg;
45  pose_msg.header = msg->header;
46  pose_msg.header.frame_id = fixed_frame_;
47  pose_msg.pose.position.x = 0;
48  pose_msg.pose.position.y = 0;
49  pose_msg.pose.position.z = 0;
50  tf::quaternionTFToMsg(q_v, pose_msg.pose.orientation);
51 
52  // Publish the messages
53  mag_pub_.publish(pose_msg);
54 
55 
56  // MEASUREMENT CLOUD //
57 
58  // store the current measurement
59  geometry_msgs::Point p;
60  p.x = x;
61  p.y = y;
62  p.z = z;
63  pts_list_.push_back(p);
64 
65  // begin packing marker message
66  visualization_msgs::Marker pts_msg;
67  pts_msg.header.frame_id = fixed_frame_;
68  pts_msg.header.stamp = msg->header.stamp;
69  pts_msg.type = visualization_msgs::Marker::POINTS;
70  pts_msg.action = visualization_msgs::Marker::ADD;
71 
72  // set points style
73  pts_msg.scale.x = 0.1;
74  pts_msg.scale.y = pts_msg.scale.x;
75  pts_msg.scale.z = pts_msg.scale.x;
76  pts_msg.color.a = 1.0;
77  pts_msg.color.r = 0.0;
78  pts_msg.color.g = 1.0;
79  pts_msg.color.b = 0.0;
80 
81  for (uint32_t i = 0; i < pts_list_.size(); ++i)
82  {
83  pts_msg.points.push_back(pts_list_[i]);
84  }
85 
86  // publish point cloud
87  pts_pub_.publish(pts_msg);
88  }
89  mag_throttle_++;
90 }
91 
92 void Viz::attCallback(const rosflight_msgs::AttitudeConstPtr &msg)
93 {
94 
95  geometry_msgs::PoseStamped pose;
96  pose.header = msg->header;
97  pose.header.frame_id = fixed_frame_;
98  pose.pose.orientation = msg->attitude;
99 
100  pose_pub_.publish(pose);
101 }
102 
103 } // namespace rosflight_utils
104 
105 int main(int argc, char** argv)
106 {
107  ros::init(argc, argv, "viz_node");
109  ros::spin();
110  return 0;
111 }
112 
ros::NodeHandle nh_
Definition: viz.h:26
ros::Subscriber att_sub_
Definition: viz.h:35
void magCallback(const sensor_msgs::MagneticFieldConstPtr &msg)
Definition: viz.cpp:27
void publish(const boost::shared_ptr< M > &message) const
ros::Publisher pose_pub_
Definition: viz.h:36
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
ros::Publisher pts_pub_
Definition: viz.h:32
This file defines a simple PID controller to be used by other classes to implement a PID control loop...
Definition: simple_pid.h:44
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
ROSCPP_DECL void spin()
std::string fixed_frame_
Definition: viz.h:43
ros::Publisher mag_pub_
Definition: viz.h:31
int main(int argc, char **argv)
Definition: viz.cpp:105
void attCallback(const rosflight_msgs::AttitudeConstPtr &msg)
Definition: viz.cpp:92
std::vector< geometry_msgs::Point > pts_list_
Definition: viz.h:40
int mag_throttle_
Definition: viz.h:42
double mag_sum_
Definition: viz.h:41
ros::Subscriber mag_sub_
Definition: viz.h:30


rosflight_utils
Author(s):
autogenerated on Wed Jul 3 2019 20:00:31