# Mavros obstacle distance for collision prevention

**URL:** <https://discuss.px4.io/t/mavros-obstacle-distance-for-collision-prevention/25158>\
**Category:** ROS 1 / ROS 2\
**Created:** [November 22, 2021, 5:06pm UTC](https://discuss.px4.io/t/mavros-obstacle-distance-for-collision-prevention/25158 "2021-11-22T17:06:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![MatCa](https://discuss.px4.io/user_avatar/discuss.px4.io/matca/32/9728_2.png) [@MatCa](https://discuss.px4.io/u/MatCa)\
**Post date:** [November 22, 2021, 5:06pm UTC](https://discuss.px4.io/t/mavros-obstacle-distance-for-collision-prevention/25158/1 "2021-11-22T17:06:51Z")

</div>

Hello,  
I’m trying to test [collision prevention](https://docs.px4.io/master/en/computer_vision/collision_prevention.html) by publishing fake LaserScan messages on /mavros/obstacle/send. I’m using PX4 SITL Gazebo to test the drone behaviour.

My message is received as I can see an OBSTACLE\_DISTANCE message in QGC with the expected distances values. One thing to notice is that the component id for this message is 240 whereas most of the other mavlink messages have id 1. I’ve tried to change but I didn’t found the way to do it from ROS. I don’t know if this really is the problem?

The collision prevention does not seems to work as I can still move the quadcopter in all directions even though obstacle messages are published for all directions.  
I think I’m not creating my LaserScan message properly. I’ve read [Question on distances array in OBSTACLE\_DISTANCE](https://discuss.px4.io/t/question-on-distances-array-in-obstacle-distance/12911) but it is not about mavros messages.  
This is how I do it :

```auto
int num_readings = 36;
ros::Time scan_time = ros::Time::now(); //use_sim_time=true

//populate the LaserScan message
sensor_msgs::LaserScan scan;

 scan.header.stamp = scan_time;
 scan.header.frame_id = "base_link";
 // scan.angle_min = 0.;
 // scan.angle_max = 0.;
 scan.angle_increment = 10.;
 // scan.time_increment = 0.;
 // scan.range_min = 0.0;
 // scan.range_max = 20.0;

 scan.ranges.resize(num_readings);
 scan.ranges[0] = 2.5;

 for(unsigned int i = 0; i < num_readings; ++i){
    scan.ranges[i] = 2.5; //obstacle at 2.5m
 }

```

Any help would be appreciated.

---

<div class="post-metadata">

**Author:** ![MatCa](https://discuss.px4.io/user_avatar/discuss.px4.io/matca/32/9728_2.png) [@MatCa](https://discuss.px4.io/u/MatCa)\
**Post date:** [December 2, 2021, 4:25pm UTC](https://discuss.px4.io/t/mavros-obstacle-distance-for-collision-prevention/25158/2 "2021-12-02T16:25:24Z")

</div>

Seems you need to set 2 parameters :

- MPC\_POS\_MODE = Smooth position control (Jerk optimized)
- EKF2\_RNG\_AID = Disabled

---

<div class="post-metadata">

**Author:** ![bhaskar-glitch1](https://discuss.px4.io/user_avatar/discuss.px4.io/bhaskar-glitch1/32/9387_2.png) [@bhaskar-glitch1](https://discuss.px4.io/u/bhaskar-glitch1)\
**Post date:** [August 18, 2023, 5:27pm UTC](https://discuss.px4.io/t/mavros-obstacle-distance-for-collision-prevention/25158/3 "2023-08-18T17:27:02Z")

</div>

Hi  
Did u tried implementing the code on drone?? is it working??  
actually i also want to implement collision prevention sort of things on my drone using depth camera i need some help in sending obstacle distance message  
can you please help me out

---

<div class="post-metadata">

**Author:** ![Asfandyar\_Khattak](https://discuss.px4.io/user_avatar/discuss.px4.io/asfandyar_khattak/32/18998_2.png) [@Asfandyar\_Khattak](https://discuss.px4.io/u/Asfandyar_Khattak)\
**Post date:** [October 17, 2025, 4:52am UTC](https://discuss.px4.io/t/mavros-obstacle-distance-for-collision-prevention/25158/4 "2025-10-17T04:52:22Z")

</div>

What is the parameter for minimum distance between drone and obstacle?  
Because if that is less than 2.5m, even if the drone moves in any direction, the obstacle always stays outside this buffer/safe zone.
