# Using Collision Prevention

**URL:** <https://discuss.px4.io/t/using-collision-prevention/40013>\
**Category:** PX4 Autopilot\
**Created:** [August 2, 2024, 8:57am UTC](https://discuss.px4.io/t/using-collision-prevention/40013 "2024-08-02T08:57:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![serkan](https://discuss.px4.io/user_avatar/discuss.px4.io/serkan/32/15385_2.png) [@serkan](https://discuss.px4.io/u/serkan)\
**Post date:** [August 2, 2024, 8:57am UTC](https://discuss.px4.io/t/using-collision-prevention/40013/1 "2024-08-02T08:57:53Z")

</div>

I want to test the PX4 Collision Prevention feature in a simulation environment. For this, I added a lidar to the drone and ensured a specific angle is covered. I populated the data into a variable I created from the px4\_msgs::msg::ObstacleDistance message in ROS2 and publish it at certain intervals. However, the vehicle does not arm on the PX4 side. I am sharing the error I received and the parameters I changed via QGC in the images below. (The steps I followed are on [this page](https://docs.px4.io/main/en/computer_vision/collision_prevention.html#angle_change_tuning)).

PX4 Output

 ![image](https://discuss.px4.io/uploads/default/original/3X/5/0/50e3590d0465dea2bfe7086e767dc53fa8e33b32.jpeg)

 ![image](https://discuss.px4.io/uploads/default/original/3X/7/e/7effc8bd15d684278792cc36134b58dc1cac1596.png)

![image](https://discuss.px4.io/uploads/default/original/3X/5/f/5f83c17c5642c76b3c28cbe7c8522cff726361da.jpeg)

PX4 Parameter

 ![image](https://discuss.px4.io/uploads/default/original/3X/8/d/8d87efcaf9bcda97c7a6ace6cac218f962324b49.png)

Gz Sim Lidar Data:

 ![image](https://discuss.px4.io/uploads/default/original/3X/a/f/afc10bc03c8906ab18d4afd556d50d54415408f8.jpeg)

code:

```cpp
using obstacleDistanceMsg = px4_msgs::msg::ObstacleDistance;
obstacleDistanceMsg obs_distance;
obs_distance.frame = obs_distance.MAV_FRAME_BODY_FRD;
obs_distance.sensor_type = obs_distance.MAV_DISTANCE_SENSOR_LASER;
obs_distance.increment = 1;
obs_distance.min_distance = 8;
obs_distance.max_distance = 600;
obs_distance.angle_offset = -36;

```

```cpp
void SensorListener::lidarCallabck(const laserScanMsg::SharedPtr msg)
{
    for(int i = 0; i < msg->ranges.size(); i++)
    {
        obs_distance.distances[i] = std::min(std::max(static_cast<int>(msg->ranges[i] * 1e2), static_cast<int>(obs_distance.min_distance)), static_cast<int>(obs_distance.max_distance));
    }
    obs_distance.timestamp = px4_time;
    obstacle_distance->publish(obs_distance);
}

```

- The right side is the system’s timestamp value, and the left side is the timestamp value I published.

 ![image](https://discuss.px4.io/uploads/default/original/3X/8/7/87f9e7b6cfdebd4021b3232f5c68fed2b7bf9c86.png)

@hamishwillee , have you done any work related to this?

---

<div class="post-metadata">

**Author:** ![hamishwillee](https://discuss.px4.io/user_avatar/discuss.px4.io/hamishwillee/32/3469_2.png) [@hamishwillee](https://discuss.px4.io/u/hamishwillee)\
**Post date:** [August 7, 2024, 12:29am UTC](https://discuss.px4.io/t/using-collision-prevention/40013/2 "2024-08-07T00:29:34Z")

</div>

@serkan I subedited / wrote some of the docs you point to working with the engineers who wrote this code. Unfortunately I’m an author - I haven’t actually tried this myself.

You’re not the only person to have had this problem [[Bug] Avoidance system not ready · Issue #22994 · PX4/PX4-Autopilot · GitHub](https://github.com/PX4/PX4-Autopilot/issues/22994)

It may be that there is some additional check for a companion computer. Have asked @Jaeyoung-Lim for advice on that issue - or a pointer to anyone who might know more.

---

<div class="post-metadata">

**Author:** ![serkan](https://discuss.px4.io/user_avatar/discuss.px4.io/serkan/32/15385_2.png) [@serkan](https://discuss.px4.io/u/serkan)\
**Post date:** [August 7, 2024, 5:38am UTC](https://discuss.px4.io/t/using-collision-prevention/40013/3 "2024-08-07T05:38:01Z")

</div>

Thank you for your response and guidance

---

<div class="post-metadata">

**Author:** ![Claudio-Chies](https://discuss.px4.io/user_avatar/discuss.px4.io/claudio-chies/32/16980_2.png) [@Claudio-Chies](https://discuss.px4.io/u/Claudio-Chies)\
**Post date:** [August 7, 2024, 6:23am UTC](https://discuss.px4.io/t/using-collision-prevention/40013/4 "2024-08-07T06:23:39Z")

</div>

> [@serkan](#):
>
> k you for your response and guidance

if you just want e SITL envoirement for testing CP, download px4 Main, or a commit where this PR is present: [[gz-sim] x500\_lidar for testing CollisionPrevention by dakejahl · Pull Request #22418 · PX4/PX4-Autopilot · GitHub](https://github.com/PX4/PX4-Autopilot/pull/22418)  
with that you should be able to call ` make px4_sitl gz_x500_lidar`  
then you can enable CP in the Settings, but make sure to change [MPC\_POS\_MODE](https://docs.px4.io/main/en/advanced_config/parameter_reference.html#MPC_POS_MODE) to 0 or 3

---

<div class="post-metadata">

**Author:** ![serkan](https://discuss.px4.io/user_avatar/discuss.px4.io/serkan/32/15385_2.png) [@serkan](https://discuss.px4.io/u/serkan)\
**Post date:** [August 7, 2024, 10:49am UTC](https://discuss.px4.io/t/using-collision-prevention/40013/5 "2024-08-07T10:49:52Z")

</div>

I tried it, but it didn’t work very well. The drone is crashing into all the obstacles in front of it.

![image](https://discuss.px4.io/uploads/default/original/3X/3/8/389567edcef3f5594952fecaf4ffb3b75a34593a.png)

 ![image](https://discuss.px4.io/uploads/default/original/3X/0/c/0ca8131efcebedccff70d012d9565d4681729f14.png)

 ![image](https://discuss.px4.io/uploads/default/original/3X/3/d/3ddc84b81e50f2d55d07c83aaca3e1c5fc4fd7ab.png)

 ![image](https://discuss.px4.io/uploads/default/original/3X/a/7/a75513d760502729b7c8488c7586d18ba2e50273.png)

---

<div class="post-metadata">

**Author:** ![Claudio-Chies](https://discuss.px4.io/user_avatar/discuss.px4.io/claudio-chies/32/16980_2.png) [@Claudio-Chies](https://discuss.px4.io/u/Claudio-Chies)\
**Post date:** [August 12, 2024, 6:01am UTC](https://discuss.px4.io/t/using-collision-prevention/40013/6 "2024-08-12T06:01:25Z")

</div>

try with a higher cp\_delay, as it scales the possible commanded velocities. and no delay is not realistic. and set mpc\_jerk\_max to the default value, if your halfing the jerk, your also increasing the delay related to velocity change, as your limiting the rate of change of acceleration.
