Question on distances array in OBSTACLE_DISTANCE

Hi @baumanta,

Thank you for the quick reply!

I have been using this lidar model ->https://github.com/PX4/sitl_gazebo/blob/0823f5e006011d36858c2dd39881874c1edac10b/models/lidar/model.sdf which is the default built-in px4 lidar model. I do look through the lidar-plugin cpp file but that is too complicated for me and I am not sure where to edit.

Meanwhile the bottom attached is my cutomized iris sdf model, and it shows how i defined the lidar position and how it attached to the iris quadcopter.

<sdf version='1.6'>

    <model name='my_vehicle'>
        <include>
            <uri>model://iris</uri>
        </include>

        <!--forward-facing lidar-->
        <include>
            <uri>model://lidar</uri>
            <pose>0.1 0.05 0.06 0 -1.57079633 0</pose>
            <name>lidar0</name>
        </include>

        <joint name="lidar0_joint" type="fixed">
            <parent>iris::base_link</parent>
            <child>lidar0::link</child>
        </joint>

        <!--forward1-facing lidar-->
        <include>
            <uri>model://lidar</uri>
            <pose>0.1 0.05 -0.06 0 -1.57079633 0</pose>
            <name>lidar1</name>
        </include>

        <joint name="lidar1_joint" type="fixed">
            <parent>iris::base_link</parent>
            <child>lidar1::link</child>
        </joint>


        <!--forward2-facing lidar-->
        <include>
            <uri>model://lidar</uri>
            <pose>0.1 -0.05 0.06 0 -1.57079633 0</pose>
            <name>lidar2</name>
        </include>

        <joint name="lidar2_joint" type="fixed">
            <parent>iris::base_link</parent>
            <child>lidar2::link</child>
        </joint>

        <!--forward3-facing lidar-->
        <include>
            <uri>model://lidar</uri>
            <pose>0.1 -0.05 -0.06 0 -1.57079633 0</pose>
            <name>lidar3</name>
        </include>

        <joint name="lidar3_joint" type="fixed">
            <parent>iris::base_link</parent>
            <child>lidar3::link</child>
        </joint>
    </model>
</sdf>

Please let me know if you can see the possible existing problem that leads to my issue.

Regards,
ziimiin14

Hii!

This gives a good overview of things we should while use a range finder sensor.

I’m using a Velodyne Puck lite 360 degree lidar sensor. It gives a 3D scan points in ROS as input.
Can you please explain what should be the changes or format in order to do collision prevention with the sensor mentioned above.

Collision prevention is only implemented for in 2D. So you could convert your 3D data in a 1D array by just taking into account e.g. only the points at -20deg to +20deg elevation and taking the minimum distance at each azimuth angle. You would obviously be throwing away a lot of data but the collision prevention algorithm cannot handle elevation data. Another option is to look into PX4 Avoidance (GitHub - PX4/PX4-Avoidance: PX4 avoidance ROS node for obstacle detection and avoidance.) which does not stop at obstacles but avoid them. This algorithm works with 3D data but is a lot more compute intensive (it needs to be run on a companion computer). Also the project is not maintained anymore, so it will be some effort to get it to work again.

Thanks!
I already converted the 3D data into 2D. But Obstacle_message is not displayed by QGC. I’m sending some dummy data through /mavros/cmd/command with command : 330 .
Command_ack confirms that it is recieving the data but with result: 4 while broadcasting: True.
Can you explain why?

And I already setup comapnion computer but don’t have any node for velodyne data to give as input in avoidance node.
Suggest me someone or something with I can take help with?

hello @baumanta

i tried obstacle avoidance with answer from here
https://discuss.px4.io/t/question-on-distances-array-in-obstacle-distance/12911/2?u=suryaprakash_lokula

and i could find proximity data in the mission planner but the drone does not get effected by the obstacle ahead in (front)

below is the code which i tried


from pymavlink import mavutil
import time
import RPi.GPIO as GPIO 
from threading import Thread
from dronekit import connect, VehicleMode, LocationGlobalRelative
from pymavlink.dialects.v20 import ardupilotmega as mavlink2
import math


latt = "17.XXXXXX"
long = "78.XXXXXX"
alt = 1.5

vehicle = connect("/dev/ttyAMA0", wait_ready=True, baud=921600)
vehicle.wait_ready(True, raise_exception=False)

vehicle.parameters['LAND_SPEED'] = 25 ##Descent speed of 30cm/s
vehicle.parameters["WPNAV_SPEED"]=80
# vehicle.parameters["PRX_TYPE"]=2
# vehicle.parameters["OA_TYPE"]=2
# vehicle.parameters["OA_LOOKAHEAD"]=2
# vehicle.parameters["OA_BR_TYPE"]=1
# vehicle.parameters["OA_DB_EXPIRE"]=10
# vehicle.parameters["OA_DB_QUEUE_SIZE"]=100
# vehicle.parameters["OA_DB_OUTPUT"]=1
print("connected")
GPIO.setwarnings(False) 
GPIO.setmode(GPIO.BCM) 

obstacledist = 70
vehicleheading = 0

distance = 1
distancespeed = 1

FRONTTRIG = 4
FRONTECHO = 17

sonardist ={}

front = 300
UNIT16_MAX = 251


GPIO.setup(FRONTTRIG,GPIO.OUT) 
GPIO.setup(FRONTECHO,GPIO.IN)



def send_distance_message():
   
    abc = [int(front),  UNIT16_MAX,UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, 
    UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, 
    UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, 
    UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, 
    UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, 
    UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX,
     UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX, UNIT16_MAX]

    msg = mavlink2.MAVLink_obstacle_distance_message(
        0, #time
        1, #sensor type
        abc, # abc is usually the array of 72 elements
        15, #angular width
        10, #min distance
        250, #max distance
        10,#https://mavlink.io/en/messages/common.html#OBSTACLE_DISTANCE
        -40,
        12
        )
    time.sleep(0.3)
    if vehicle.location.global_relative_frame.alt  >= 1.0:
        vehicle.send_mavlink(msg)
        print(msg)
        vehicle.flush()




def frontsonar():
    global front
    while True:
        dist1=251
        GPIO.output(FRONTTRIG, True) 
        time.sleep(0.00001) 
        GPIO.output(FRONTTRIG, False) 
        echo_state=0 
        
        while echo_state == 0: 
            echo_state = GPIO.input(FRONTECHO) 
            pulse_start = time.time() 
        while GPIO.input(FRONTECHO)==1: 
            pulse_end = time.time() 
        pulse_duration = pulse_end - pulse_start 
        distance = pulse_duration * 17150 
        distance = round(distance, 2) 
        
        if(distance<250 and distance>5): #To filter out junk values 
            front=distance
                            
        else:
            front = dist1
        #print("front",front)
        time.sleep(0.1)

        
Thread(target = frontsonar).start()

def get_distance_meters(targetLocation,currentLocation):
    dLat=targetLocation.lat - currentLocation.lat
    dLon=targetLocation.lon - currentLocation.lon

    return math.sqrt((dLon*dLon)+(dLat*dLat))*1.113195e5



def arm_and_takeoff(aTargetAltitude):
        
        print("Arming motors")
        # Copter should arm in GUIDED mode
        vehicle.mode = VehicleMode("GUIDED")
        vehicle.armed = True

        # Confirm vehicle armed before attempting to take off
        while not vehicle.armed:
            print(" Waiting for arming...")
            time.sleep(1)

        print("Taking off!")
        vehicle.simple_takeoff(aTargetAltitude)  # Take off to target altitude
        
        while True:
            print(" Altitude: ", vehicle.location.global_relative_frame.alt)
            # Break and return from function just below target altitude.
            if vehicle.location.global_relative_frame.alt >= aTargetAltitude * 0.95:
                print("Reached target altitude")
                break
            time.sleep(1)
        
print(vehicle.battery.voltage)
arm_and_takeoff(alt)
vehicle.airspeed = 5
print("Take off complete")

# Hover for 10 seconds
time.sleep(3)
print("Vehicle going to the location")
point1 = LocationGlobalRelative(float(latt),float(long), alt)
distanceToTargetLocation = get_distance_meters(point1,vehicle.location.global_relative_frame)
vehicle.simple_goto(point1)
while True:
    send_distance_message()
    currentDistance = get_distance_meters(point1,vehicle.location.global_relative_frame)
    print("current distance: ", currentDistance,distanceToTargetLocation*.02,currentDistance<distanceToTargetLocation*.02)
    if currentDistance<distanceToTargetLocation*.02:
        print("Reached target location.")
        time.sleep(2)
        break
    time.sleep(1)
vehicle.mode = VehicleMode("LAND")
while True:
    print(" Altitude: ", vehicle.location.global_relative_frame.alt)
    # Break and return from function just below target altitude.
    if vehicle.location.global_relative_frame.alt <=0:
        print("Reached ground")
        vehicle.close() 
        break
    time.sleep(1)

i am using ultrasonic sensor at front , am i missing anything in the above code
and my aim is to perform bendyruler obstacle avoidance with px4(version: 4.1),raspberrypi,ultrasonic sensors

Thanks in advance.

@baumanta @ashutosh_badave @chipper
could you help me with this topic please

ArduPilot avoidance and PX4 avoidance aren’t interchangeable.
If you’re intending to use ArduPilot on your flight controller (you currently are), best to ask at discuss.ardupilot.org

Hello @baumanta,

I was able to get this message to work on my drone, however, we are also incorporating an ark flow sensor which creates an instance for a distance sensor. From my understanding the OBSTACLE_DISTANCE message also created a distance sensor instance. Is this correct? And is there anyway to nullify this instance so that it does not interfere with the position control by the flow sensor? The only thing that I can think of is to run a “stop” command in the shell to stop that instance.

Thank you for your time.

If you are having issues with CP and an obstacle distance msg, I would make sure you call out timestamps in the obstacle distance message for your distance data. I am not sure if MAVROS takes care of this internally.

I don’t think the PX4 CP library knows what to do with obstacle distance data that has no timestamps given in the message…although in the code it does switch to a loiter/alt hold if it detects stale rangefinder data.

Hola !
I want to implement collision prevention using PX4 in my quadcopter. I dont want to use lidar and ultrasonic sensors but a realsense depth camera. I need help in the process to follow to do the same. For now, I am doing a calculated guess that PX4 has an inbuilt library of collision prevention and I just have to pass the range array via mavlink to trigger that library.
So, I need help in two things:
a) First please tell me the guess I am making is correct or not. And if PX4 has a library then please tell me how to pass the trigger it.
b) Seconldy please tell me how to pass my image data in the range array. If there is any library to do the same, please tell me about that too.

Hoping a reply soon and further helps from the forum.
Thanks in advance…

Hi @shloksharma273 .
Were you able to get it to work with realsense depth camera ?
If yes, can you please share the process. Much appreciated!