# Using obstacle\_distance uorb msgs for collision prevention

**URL:** <https://discuss.px4.io/t/using-obstacle-distance-uorb-msgs-for-collision-prevention/37524>\
**Category:** PX4 Autopilot\
**Created:** [March 28, 2024, 11:17am UTC](https://discuss.px4.io/t/using-obstacle-distance-uorb-msgs-for-collision-prevention/37524 "2024-03-28T11:17:33Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashutoshramola](https://discuss.px4.io/user_avatar/discuss.px4.io/ashutoshramola/32/16040_2.png) [@ashutoshramola](https://discuss.px4.io/u/ashutoshramola)\
**Post date:** [March 28, 2024, 11:17am UTC](https://discuss.px4.io/t/using-obstacle-distance-uorb-msgs-for-collision-prevention/37524/1 "2024-03-28T11:17:33Z")

</div>

I am working on collision prevention in px4 (v1.14) VIO based drone using YDLidar T-mini Pro 360 deg Lidar (2D).  
The setup includes:  
Cubepilot cube orange plus.  
Nvidia issac ros for realsense based VIO running on jetson orin nano DK.  
Intel realsense D435i

I am using [px4\_msgs](https://github.com/PX4/px4_msgs) (obstacle\_dostance) for conversion of “/scan” (LaserScan) data from ros2 to px4 using microxrce\_dds.  
I am getting values in “/fmu/in/obstacle\_distance” uorb topic created by xrce\_dds, but the distances are not visible and usable in px4 (param CP\_DIST=1 enabled).

Here is the script (C++) for ros2 node:

```auto
#include "rclcpp/rclcpp.hpp"
#include "sensor_msgs/msg/laser_scan.hpp"
#include "px4_msgs/msg/obstacle_distance.hpp"

class LaserScanToObstacleDistanceNode : public rclcpp::Node
{
public:
    LaserScanToObstacleDistanceNode()
        : Node("laserscan_to_obstacle_distance_node")
    {
        auto qos = rclcpp::QoS(20); // History depth is 10
        qos.reliability(rclcpp::ReliabilityPolicy::BestEffort);
        publisher_ = this->create_publisher<px4_msgs::msg::ObstacleDistance>("/fmu/in/obstacle_distance", qos);
        subscription_ = this->create_subscription<sensor_msgs::msg::LaserScan>(
            "/scan", qos, std::bind(&LaserScanToObstacleDistanceNode::listener_callback, this, std::placeholders::_1));
    }

private:
    void listener_callback(const sensor_msgs::msg::LaserScan::SharedPtr msg)
    {
        auto obstacle_distance = laserscan_to_obstacle_distance(msg);
        publisher_->publish(obstacle_distance);
    }
    
    px4_msgs::msg::ObstacleDistance laserscan_to_obstacle_distance(const sensor_msgs::msg::LaserScan::SharedPtr scan)
    {
        px4_msgs::msg::ObstacleDistance obstacle_distance;
        obstacle_distance.timestamp = scan->header.stamp.sec * 1000000 + scan->header.stamp.nanosec / 1000; // Convert to microseconds
	    obstacle_distance.frame = 12;     
	    obstacle_distance.sensor_type = 0;   
	    // obstacle_distance.increment = scan->angle_increment;
        obstacle_distance.increment = 5.0;
        obstacle_distance.min_distance = scan->range_min * 100; // Convert to centimeters
        obstacle_distance.max_distance = scan->range_max * 100; // Convert to centimeters
        obstacle_distance.angle_offset = 0.0; // Assuming the lidar is mounted in the front

        for (size_t i = 0; i < scan->ranges.size() && i < obstacle_distance.distances.size(); ++i)
        {
            obstacle_distance.distances[i] = std::min(std::max(static_cast<int>(scan->ranges[i] * 100), static_cast<int>(obstacle_distance.min_distance)), static_cast<int>(obstacle_distance.max_distance)); // Convert to centimeters
        }

        // Print the total length of the final array with distances
        RCLCPP_INFO(this->get_logger(), "Total length of the final array with distances: %zu", obstacle_distance.distances.size());

        return obstacle_distance;
    }

    rclcpp::Publisher<px4_msgs::msg::ObstacleDistance>::SharedPtr publisher_;
    rclcpp::Subscription<sensor_msgs::msg::LaserScan>::SharedPtr subscription_;
};

int main(int argc, char *argv[])
{
    rclcpp::init(argc, argv);
    rclcpp::spin(std::make_shared<LaserScanToObstacleDistanceNode>());
    rclcpp::shutdown();
    return 0;
}

```

Please help me with this.  
Thank You !!

---

<div class="post-metadata">

**Author:** ![Neo\_man](https://discuss.px4.io/user_avatar/discuss.px4.io/neo_man/32/15460_2.png) [@Neo\_man](https://discuss.px4.io/u/Neo_man)\
**Post date:** [April 3, 2024, 7:09am UTC](https://discuss.px4.io/t/using-obstacle-distance-uorb-msgs-for-collision-prevention/37524/2 "2024-04-03T07:09:50Z")

</div>

Hi @ashutoshramola, have you figured out the way to send obstacle distance messages? Is the obstacle overlay QGC present?

Suggestion is that have to tried to turn on the Obstacle distance overlay in QGC?

---

<div class="post-metadata">

**Author:** ![ashutoshramola](https://discuss.px4.io/user_avatar/discuss.px4.io/ashutoshramola/32/16040_2.png) [@ashutoshramola](https://discuss.px4.io/u/ashutoshramola)\
**Post date:** [April 3, 2024, 11:46am UTC](https://discuss.px4.io/t/using-obstacle-distance-uorb-msgs-for-collision-prevention/37524/3 "2024-04-03T11:46:50Z")

</div>

from ros2 i am sending data to uorb topic /fmu/in/obstacle\_distance using xrce\_dds. but still unable to receive that in px4.  
My problem in that i am not getting obstacle\_distance message in mavlink inspector.  
The obstacle overlay (as given in documentation) is not present in my QGC.

Are you also encountering the same problem?

---

<div class="post-metadata">

**Author:** ![Neo\_man](https://discuss.px4.io/user_avatar/discuss.px4.io/neo_man/32/15460_2.png) [@Neo\_man](https://discuss.px4.io/u/Neo_man)\
**Post date:** [April 4, 2024, 12:43pm UTC](https://discuss.px4.io/t/using-obstacle-distance-uorb-msgs-for-collision-prevention/37524/4 "2024-04-04T12:43:41Z")

</div>

@ashutoshramola can you check in mavlink console by using the following command.

```auto
listener obstacle_distance

```

I’m able to see the message in mavlink console. Can you also?

---

<div class="post-metadata">

**Author:** ![ashutoshramola](https://discuss.px4.io/user_avatar/discuss.px4.io/ashutoshramola/32/16040_2.png) [@ashutoshramola](https://discuss.px4.io/u/ashutoshramola)\
**Post date:** [April 4, 2024, 1:59pm UTC](https://discuss.px4.io/t/using-obstacle-distance-uorb-msgs-for-collision-prevention/37524/5 "2024-04-04T13:59:21Z")

</div>

In Mavlink inspector “obstacle\_distance” message is not available.  
Also in Mavlink console (in qgc) when i use command `listener obstacle_distance`.  
it shows error like the obstacle\_distance not available.  
in command `uorb top` also obstacle\_distance not available.

it’s amazing that you were able to see messages. how are you sending data? did you use my script given above?

---

<div class="post-metadata">

**Author:** ![ashutoshramola](https://discuss.px4.io/user_avatar/discuss.px4.io/ashutoshramola/32/16040_2.png) [@ashutoshramola](https://discuss.px4.io/u/ashutoshramola)\
**Post date:** [April 5, 2024, 12:41pm UTC](https://discuss.px4.io/t/using-obstacle-distance-uorb-msgs-for-collision-prevention/37524/6 "2024-04-05T12:41:34Z")

</div>

@Neo_man I got data in `listener obstacle_distance` uorb topic, it was QoS problem in ros cpp node.

```auto
listener obstacle_distance

TOPIC: obstacle_distance
 obstacle_distance
    timestamp: 148012871 (0.112466 seconds ago)
    increment: 5.00000
    angle_offset: 0.00000
    distances: [2, 248, 242, 237, 232, 226, 210, 204, 199, 198, 197, 194, 192, 190, 190, 193, 189, 187, 184, 182, 180, 178, 176, 174, 172, 170, 168, 167, 165, 164, 162, 161, 160, 158, 74, 75, 75, 74, 74, 74, 74, 74, 73, 73, 74, 74, 74, 74, 74, 72, 72, 72, 72, 73, 74, 76, 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 143, 143, 143, 136, 133]
    min_distance: 2
    max_distance: 1200
    frame: 12
    sensor_type: 0 

```

But while flying (position mode) Collision prevention is not working. i did parameter configuration as given in the docs:

> **[Collision Prevention | PX4 Guide (main)](https://docs.px4.io/main/en/computer_vision/collision_prevention.html#px4-software-setup)**
>
> PX4 User and Developer Guide

---

<div class="post-metadata">

**Author:** ![Divyam\_Garg](https://discuss.px4.io/user_avatar/discuss.px4.io/divyam_garg/32/15317_2.png) [@Divyam\_Garg](https://discuss.px4.io/u/Divyam_Garg)\
**Post date:** [April 12, 2024, 8:37am UTC](https://discuss.px4.io/t/using-obstacle-distance-uorb-msgs-for-collision-prevention/37524/7 "2024-04-12T08:37:10Z")

</div>

Hi @ashutoshramola ,  
Did you get it to work while flying yet?  
If yes, what was the fix?

Thanks

---

<div class="post-metadata">

**Author:** ![ashutoshramola](https://discuss.px4.io/user_avatar/discuss.px4.io/ashutoshramola/32/16040_2.png) [@ashutoshramola](https://discuss.px4.io/u/ashutoshramola)\
**Post date:** [April 12, 2024, 12:12pm UTC](https://discuss.px4.io/t/using-obstacle-distance-uorb-msgs-for-collision-prevention/37524/8 "2024-04-12T12:12:56Z")

</div>

the problem is that the collision prevention is not working in PX4 as of now.  
see this [https://github.com/PX4/PX4-Autopilot/issues/22464](https://github.com/PX4/PX4-Autopilot/issues/22464)  
and check this PR: [https://github.com/PX4/PX4-Autopilot/pull/22418](https://github.com/PX4/PX4-Autopilot/pull/22418)

Until these problems get resolved we can’t fly with collision prevention.
