20 #include <sensor_msgs/LaserScan.h> 23 namespace extra_plugins {
27 using mavlink::common::MAV_DISTANCE_SENSOR;
62 mavlink::common::MAV_FRAME
frame;
72 mavlink::common::msg::OBSTACLE_DISTANCE obstacle {};
74 if (req->ranges.size() <= obstacle.distances.size()) {
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;
81 obstacle.distances[i] =
static_cast<uint16_t
>(distance_cm);
84 std::fill(obstacle.distances.begin() + req->ranges.size(), obstacle.distances.end(), UINT16_MAX);
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;
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));
104 obstacle.increment = ceil(req->angle_increment * RAD_TO_DEG * scale_factor);
107 obstacle.time_usec = req->header.stamp.toNSec() / 1000;
109 obstacle.min_distance = req->range_min * 1e2;
110 obstacle.max_distance = req->range_max * 1e2;
113 obstacle.angle_offset = req->angle_min *
RAD_TO_DEG;
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());
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())
mavlink::common::MAV_FRAME frame
Subscriptions get_subscriptions() override
ros::Subscriber obstacle_sub
bool param(const std::string ¶m_name, T ¶m_val, const T &default_val) const
#define UAS_FCU(uasobjptr)
ros::NodeHandle obstacle_nh
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
void initialize(UAS &uas_) override
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)
constexpr std::underlying_type< _T >::type enum_value(_T e)