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 
19 #include <sensor_msgs/LaserScan.h>
20 
21 namespace mavros {
22 namespace extra_plugins {
24 static constexpr double RAD_TO_DEG = 180.0 / M_PI;
26 using mavlink::common::MAV_DISTANCE_SENSOR;
27 
36 public:
38  obstacle_nh("~obstacle")
39  { }
40 
41  void initialize(UAS &uas_)
42  {
44 
46  }
47 
49  {
50  return { /* Rx disabled */ };
51  }
52 
53 private:
56 
64  {
65  mavlink::common::msg::OBSTACLE_DISTANCE obstacle {};
66 
67  if (req->ranges.size() <= obstacle.distances.size()) {
68  // all distances from sensor will fit in obstacle distance message
69  Eigen::Map<Eigen::Matrix<uint16_t, Eigen::Dynamic, 1> > map_distances(obstacle.distances.data(), req->ranges.size());
70  auto cm_ranges = Eigen::Map<const Eigen::VectorXf>(req->ranges.data(), req->ranges.size()) * 1e2;
71  map_distances = cm_ranges.cast<uint16_t>();
72  std::fill(obstacle.distances.begin() + req->ranges.size(), obstacle.distances.end(), UINT16_MAX);
73  obstacle.increment = req->angle_increment * RAD_TO_DEG;
74  } else {
75  // all distances from sensor will not fit so we combine adjacent distances always taking the shortest distance
76  size_t scale_factor = ceil(double(req->ranges.size()) / obstacle.distances.size());
77  for (size_t i = 0; i < obstacle.distances.size(); i++) {
78  obstacle.distances[i] = UINT16_MAX;
79  for (size_t j = 0; j < scale_factor; j++) {
80  size_t req_index = i * scale_factor + j;
81  if (req_index < req->ranges.size()) {
82  const float dist_m = req->ranges[req_index];
83  if (!std::isnan(dist_m)) {
84  obstacle.distances[i] = std::min(obstacle.distances[i], (uint16_t)(dist_m * 1e2));
85  }
86  }
87  }
88  }
89  obstacle.increment = ceil(req->angle_increment * RAD_TO_DEG * scale_factor);
90  }
91 
92  obstacle.time_usec = req->header.stamp.toNSec() / 1000;
93  obstacle.sensor_type = utils::enum_value(MAV_DISTANCE_SENSOR::LASER);
94  obstacle.min_distance = req->range_min * 1e2;
95  obstacle.max_distance = req->range_max * 1e2;
96 
97  ROS_DEBUG_STREAM_NAMED("obstacle_distance", "OBSDIST: sensor type: " << utils::to_string_enum<MAV_DISTANCE_SENSOR>(obstacle.sensor_type)
98  << std::endl << obstacle.to_yaml());
99 
100  UAS_FCU(m_uas)->send_message_ignore_drop(obstacle);
101  }
102 };
103 } // namespace extra_plugins
104 } // namespace mavros
105 
std::shared_ptr< MAVConnInterface const > ConstPtr
#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())
#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_)
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)
constexpr std::underlying_type< _T >::type enum_value(_T e)


mavros_extras
Author(s): Vladimir Ermakov
autogenerated on Mon Jul 8 2019 03:20:18