obstacle_distance.cpp
Go to the documentation of this file.
1 
9 /*
10  * Copyright 2017 Nuno Marques.
11  *
12  * This file is part of the mavros package and subject to the license terms
13  * in the top-level LICENSE file of the mavros repository.
14  * https://github.com/mavlink/mavros/tree/master/LICENSE.mdA
15  */
16 
17 #include <mavros/mavros_plugin.h>
18 #include <mavros/utils.h>
19 
20 #include <sensor_msgs/LaserScan.h>
21 
22 namespace mavros {
23 namespace extra_plugins {
25 static constexpr double RAD_TO_DEG = 180.0 / M_PI;
27 using mavlink::common::MAV_DISTANCE_SENSOR;
28 
37 public:
39  obstacle_nh("~obstacle")
40  { }
41 
42  void initialize(UAS &uas_) override
43  {
45 
46  std::string mav_frame;
47  obstacle_nh.param<std::string>("mav_frame", mav_frame, "GLOBAL");
48  frame = utils::mav_frame_from_str(mav_frame);
49 
51  }
52 
54  {
55  return { /* Rx disabled */ };
56  }
57 
58 private:
61 
62  mavlink::common::MAV_FRAME frame;
63 
71  {
72  mavlink::common::msg::OBSTACLE_DISTANCE obstacle {};
73 
74  if (req->ranges.size() <= obstacle.distances.size()) {
75  // all distances from sensor will fit in obstacle distance message
76  for (int i = 0; i < req->ranges.size(); i++) {
77  float distance_cm = req->ranges[i] * 1e2;
78  if (std::isnan(distance_cm) || distance_cm >= UINT16_MAX || distance_cm < 0) {
79  obstacle.distances[i] = UINT16_MAX;
80  } else {
81  obstacle.distances[i] = static_cast<uint16_t>(distance_cm);
82  }
83  }
84  std::fill(obstacle.distances.begin() + req->ranges.size(), obstacle.distances.end(), UINT16_MAX);
85 
86  const float increment_deg = req->angle_increment * RAD_TO_DEG;
87  obstacle.increment = static_cast<uint8_t>(increment_deg + 0.5f);
88  obstacle.increment_f = increment_deg;
89  } else {
90  // all distances from sensor will not fit so we combine adjacent distances always taking the shortest distance
91  size_t scale_factor = ceil(double(req->ranges.size()) / obstacle.distances.size());
92  for (size_t i = 0; i < obstacle.distances.size(); i++) {
93  obstacle.distances[i] = UINT16_MAX;
94  for (size_t j = 0; j < scale_factor; j++) {
95  size_t req_index = i * scale_factor + j;
96  if (req_index < req->ranges.size()) {
97  float distance_cm = req->ranges[req_index] * 1e2;
98  if (!std::isnan(distance_cm) && distance_cm < UINT16_MAX && distance_cm > 0) {
99  obstacle.distances[i] = std::min(obstacle.distances[i], static_cast<uint16_t>(distance_cm));
100  }
101  }
102  }
103  }
104  obstacle.increment = ceil(req->angle_increment * RAD_TO_DEG * scale_factor);
105  }
106 
107  obstacle.time_usec = req->header.stamp.toNSec() / 1000;
108  obstacle.sensor_type = utils::enum_value(MAV_DISTANCE_SENSOR::LASER);
109  obstacle.min_distance = req->range_min * 1e2;
110  obstacle.max_distance = req->range_max * 1e2;
111  obstacle.frame = utils::enum_value(frame);
112  // Assume angle_increment is positive and incoming message is in a FRD/NED frame
113  obstacle.angle_offset = req->angle_min * RAD_TO_DEG;
114 
115  ROS_DEBUG_STREAM_NAMED("obstacle_distance", "OBSDIST: sensor type: " << utils::to_string_enum<MAV_DISTANCE_SENSOR>(obstacle.sensor_type)
116  << std::endl << obstacle.to_yaml());
117 
118  UAS_FCU(m_uas)->send_message_ignore_drop(obstacle);
119  }
120 };
121 } // namespace extra_plugins
122 } // namespace mavros
123 
std::shared_ptr< MAVConnInterface const > ConstPtr
mavlink::common::MAV_FRAME mav_frame_from_str(const std::string &mav_frame)
#define ROS_DEBUG_STREAM_NAMED(name, args)
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
bool param(const std::string &param_name, T &param_val, const T &default_val) const
#define UAS_FCU(uasobjptr)
void obstacle_cb(const sensor_msgs::LaserScan::ConstPtr &req)
Send obstacle distance array to the FCU.
static constexpr double RAD_TO_DEG
Radians to degrees.
std::vector< HandlerInfo > Subscriptions
void initialize(UAS &uas_) override
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)
MAV_FRAME mav_frame
constexpr std::underlying_type< _T >::type enum_value(_T e)


mavros_extras
Author(s): Vladimir Ermakov , Amilcar Lucas
autogenerated on Tue Jun 1 2021 02:36:36