mavlink / MAVSDK

API and library for MAVLink compatible systems written in C++17
https://mavsdk.mavlink.io
BSD 3-Clause "New" or "Revised" License
593 stars 491 forks source link

MAVSDK collision avoidance #1665

Open PeymanAmirii opened 2 years ago

PeymanAmirii commented 2 years ago

Hi, Does MAVSDK have obstacle avoidance feature? If yes, how can we activate it? If no, can we use MAVROS besides MAVSDK to activate this feature? Many Thanks.

julianoes commented 2 years ago

It does not in itself. MAVSDK is just an interface to vehicle supporting mavlink, so it depends on what the drone can do.

What exactly are you trying to do?

Also have a look at https://github.com/PX4/PX4-Avoidance.

PeymanAmirii commented 2 years ago

Thanks for your quick reply. On https://github.com/PX4/PX4-Avoidance , it is mentioned that "PX4 computer vision algorithms packaged as ROS nodes for depth sensor fusion and obstacle avoidance". So avoidance feature just can be used by MAVROS not MAVSDK, am I right?

julianoes commented 2 years ago

I would recommend to use MAVROS for that purpose, yes. And yes, nothing around avoidance is implemented in MAVSDK.

AzeemMuzammil commented 2 years ago

Hi, I have used, the above ROS implementation of obstacle avoidance (https://github.com/PX4/PX4-Avoidance) and when I plan the path using QGroundControl it works perfectly. Now I have a code written in Python using MAVSDK to control drone in offboard.

when I start a gazebo simulation through the command make px4_sitl gazebo and if I run the code it works perfectly. but when I run the drone through ROS in gazebo, my code doesn't connect to the drone. Can you check the issue please?

Part of my code that connects to the drone is

from mavsdk import System

async def run():
        drone = System()
        await drone.connect(system_address="udp://:14540")
julianoes commented 2 years ago

MAVROS also uses the port 14540, so that's probably why you can't have both connected at the same time.

You can also use port 14550 but that's usually used by QGC.

If you need more ports, you should be able to add one like this:

https://github.com/PX4/PX4-Autopilot/blob/8a01135a937db05ad812bbf412adff74562e8486/ROMFS/px4fmu_common/init.d-posix/px4-rc.mavlink#L26

AzeemMuzammil commented 2 years ago

Thanks @julianoes

BTW, mavlink start -x -u $udp_offboard_port_local -r 4000000 -f -m onboard -o $udp_offboard_port_remote in this command, what is offboard_port_local and remote? is it 14540 the local? and should I add a random unused port for remote? and which port should I use to connect to the drone? remote or local?

julianoes commented 2 years ago

See here: https://github.com/PX4/PX4-Autopilot/blob/8a01135a937db05ad812bbf412adff74562e8486/ROMFS/px4fmu_common/init.d-posix/px4-rc.mavlink#L5

udp_offboard_port_remote is 14540, the one that MAVSDK requires. The other one can be any free port.

AzeemMuzammil commented 2 years ago

Hi, I ran something like this in px4 shell, pxh> mavlink start -x -u 14570 -r 4000000 -f -m onboard -o 14540 and it printed something like this INFO [mavlink] mode: Onboard, data rate: 4000000 B/s on udp port 14570 remote port 14540

But, even after this, if I rewrite the code to this, it doesent work. I mean doesent connects to the drone

from mavsdk import System

async def run():
        drone = System()
        await drone.connect(system_address="udp://:14570")
PeymanAmirii commented 2 years ago

Hi Azeem, Did you use offboard of MAVSDK along with Collision avoidance of ROS? Did it work correctly?

AzeemMuzammil commented 2 years ago

Hi @PeymanAmirii Offboard mode doesn't avoid obstacle, but when I run in mission(auto) mode. it worked. Do you have any idea on making it work for offboard mode as well?

dmeirap commented 2 years ago

Hii!! I'm also trying to avoid obstacles using MAVSDK. Have you figured out how to add the port correctly to use mavros and mavlink??

julianoes commented 2 years ago

@AzeemMuzammil that won't work. You're using 14540 in your line which is already used.

Try something like this:

mavlink start -x -u 14570 -r 4000000 -f -m onboard -o 17873
AzeemMuzammil commented 2 years ago

@julianoes thanks and, it works :). BTW do you have any idea why obstacle avoidance doesn't work with off-board mode?

julianoes commented 2 years ago

No idea. You have to ask @Jaeyoung-Lim.

AzeemMuzammil commented 2 years ago

Hi @julianoes

When I run the command make px4_sitl gazebo I get the px4 console where I can enter this command mavlink start -x -u 14570 -r 4000000 -f -m onboard -o 17873 to add a new port which I use for my separate program to control the drone.

But, in a multi-drone simulation scenario, when I launch like 3 drones with this command Tools/gazebo_sitl_multiple_run.sh -n 3 -w baylands, I have no access to the px4 console to run the command to enable additional ports. In this case, how can I enable additional ports? when I don't have access to the px4 console. Is there any way I can attach a px4 console to each drone in the simulation?

julianoes commented 2 years ago

You need to add it to the the startup script here: https://github.com/PX4/PX4-Autopilot/blob/master/ROMFS/px4fmu_common/init.d-posix/px4-rc.mavlink

AzeemMuzammil commented 2 years ago

Hi @julianoes

thank you so much for your responses. BTW, as of your suggestion, I amended the file https://github.com/PX4/PX4-Autopilot/blob/master/ROMFS/px4fmu_common/init.d-posix/px4-rc.mavlink

what I did was, I added below lines,

udp_custom_offboard_port_local=$((14680+px4_instance))
udp_custom_offboard_port_remote=$((14640+px4_instance))

mavlink start -x -u $udp_custom_offboard_port_local -r 4000000 -f -m onboard -o $udp_custom_offboard_port_remote

and did run the command to make the PX4_Autopiolet.

But the issue is, in the multi-drone simulation scenario, (I have initiated two drones)

when I run the command ./mavsdk_server -p 50040 udp://:14640, it discovers a drone. But when I run the command ./mavsdk_server -p 50041 udp://:14641 while the initial command is on run, the second command fails with the error

[03:24:49|Error] bind error: Address in use (udp_connection.cpp:86)
[03:24:49|Error] Connection failed: Bind error (connection_initiator.h:47)

but if I terminate the first command and run the second one it works, and the first command fails with the same bind error.

You got any idea why this happens? (Thanks in advance)

julianoes commented 2 years ago

I tried to reproduce this and it works for me, exactly as you described it: Screenshot from 2022-02-11 12-01-54

TSC21 commented 2 years ago

@julianoes thanks and, it works :). BTW do you have any idea why obstacle avoidance doesn't work with off-board mode?

Because Avoidance is only aimed to function in Auto modes in PX4.

AzeemMuzammil commented 2 years ago

Hi @julianoes I was in a process of spawning two drones in a single gazebo where the gazebo is in the host machine with a px4 instance running, and in the remote machine in the same network, I initiated a px4 instance. (Two drones are spawning in the host machine). But when I try to take off and land it works only in the host machine.

Bashrc configs(Host)

#set ROS Network
export ROS_MASTER_URI=http://192.168.8.101:11311
export ROS_HOSTNAME=192.168.8.101

export GAZEBO_MASTER_URI=http://192.168.8.101:11345

Bashrc configs(Remote)

#set ROS Network
export ROS_MASTER_URI=http://192.168.8.101:11311
export ROS_HOSTNAME=1192.168.8.102

export GAZEBO_MASTER_URI=http://192.168.8.101:11345

Launch File (Host)

<?xml version="1.0"?>
<launch>
    <!-- MAVROS posix SITL environment launch script -->
    <!-- launches Gazebo environment and 2x: MAVROS, PX4 SITL, and spawns vehicle -->
    <!-- vehicle model and world -->
    <arg name="est" default="ekf2"/>
    <arg name="vehicle" default="iris"/>
    <arg name="uav_number" default="uav0"/>
    <arg name="world" default="$(find mavlink_sitl_gazebo)/worlds/empty.world"/>
    <!-- gazebo configs -->
    <arg name="gui" default="true"/>
    <arg name="debug" default="false"/>
    <arg name="verbose" default="false"/>
    <arg name="paused" default="false"/>
    <!-- Gazebo sim -->
    <include file="$(find gazebo_ros)/launch/empty_world.launch">
        <arg name="gui" value="$(arg gui)"/>
        <arg name="world_name" value="$(arg world)"/>
        <arg name="debug" value="$(arg debug)"/>
        <arg name="verbose" value="$(arg verbose)"/>
        <arg name="paused" value="$(arg paused)"/>
    </include>
    <!-- UAV0 -->
    <group ns="uav0">
        <!-- MAVROS and vehicle configs -->
        <arg name="ID" value="0"/>
        <arg name="fcu_url" default="udp://:14540@192.168.8.101:14550"/>
        <arg name="gcs_url" value=""/>
        <!-- PX4 SITL and vehicle spawn -->
       <include file="$(find px4)/launch/single_vehicle_spawn.launch">
           <arg name="x" value="0"/>
           <arg name="y" value="0"/>
           <arg name="z" value="1"/>
           <arg name="R" value="0"/>
           <arg name="P" value="0"/>
           <arg name="Y" value="0"/>
           <arg name="vehicle" value="$(arg vehicle)"/>
           <arg name="mavlink_udp_port" value="14560"/>
           <arg name="mavlink_tcp_port" value="4560"/>
           <arg name="ID" value="$(arg ID)"/>
       </include>
       <!-- MAVROS -->
       <include file="$(find mavros)/launch/px4.launch">
           <arg name="fcu_url" value="$(arg fcu_url)"/>
           <arg name="gcs_url" value="$(arg gcs_url)"/>
           <arg name="tgt_system" value="$(eval 1 + arg('ID'))"/>
           <arg name="tgt_component" value="1"/>
       </include>
   </group>
</launch>

Launch File (Remote)

<?xml version="1.0"?>
<launch>
    <!-- MAVROS posix SITL environment launch script -->
    <!-- launches Gazebo environment and 2x: MAVROS, PX4 SITL, and spawns vehicle -->
    <!-- vehicle model and world -->
    <arg name="id" default="1"/>
    <arg name="est" default="ekf2"/>
    <arg name="vehicle" default="iris"/>

    <group ns="uav1">
        <!-- MAVROS and vehicle configs -->
        <arg name="ID" value="$(arg id)"/>
        <arg name="fcu_url" default="udp://:14541@192.168.8.102:14551"/>
        <arg name="gcs_url" value=""/>
        <!-- PX4 SITL and vehicle spawn -->
        <include file="$(find px4)/launch/single_vehicle_spawn.launch">
            <arg name="x" value="0"/>
            <arg name="y" value="1"/>
            <arg name="z" value="1"/>
            <arg name="R" value="0"/>
            <arg name="P" value="0"/>
            <arg name="Y" value="0"/>
            <arg name="vehicle" value="$(arg vehicle)"/>
            <arg name="mavlink_udp_port" value="14561"/>
            <arg name="mavlink_tcp_port" value="4561"/>
            <arg name="ID" value="$(arg ID)"/>
        </include>
        <!-- MAVROS -->
        <include file="$(find mavros)/launch/px4.launch">
            <arg name="fcu_url" value="$(arg fcu_url)"/>
            <arg name="gcs_url" value="$(arg gcs_url)"/>
            <arg name="tgt_system" value="$(eval 1 + arg('ID'))"/>
            <arg name="tgt_component" value="1"/>
        </include>
    </group>    

</launch>

And of course I amended the file https://github.com/PX4/PX4-Autopilot/blob/master/ROMFS/px4fmu_common/init.d-posix/px4-rc.mavlink as below,

udp_custom_offboard_port_local=$((14680+px4_instance))
udp_custom_offboard_port_remote=$((14640+px4_instance))

mavlink start -x -u $udp_custom_offboard_port_local -r 4000000 -f -m onboard -o $udp_custom_offboard_port_remote

After all these configs,

When I try to run the script https://github.com/mavlink/MAVSDK-Python/blob/main/examples/takeoff_and_land.py, in the host machine with the system_address being set to: await drone.connect(system_address="udp://:14640") one drone takes off and lands. (the drone which is launched from the host machine)

When I run the same script from the remote machine with the system_address being set to: await drone.connect(system_address="udp://:14641") nothing happens. Could you please clarify this?

Thanks in advance.

julianoes commented 2 years ago

When I run the same script from the remote machine with the system_address being set to: await drone.connect(system_address="udp://:14641") nothing happens. Could you please clarify this?

udp://:14641 will bind to the port locally, so the drone on the remote system needs to talk to the "local" IP to reach MAVSDK.

To do that you need to tell the remote one where to send the traffic like so:

tpaluavio commented 1 year ago

Hi, I am trying to perform collision prevention with px4 offboard mode with mavros/mavsdk. Can anyone point me to a spacefic link for that?