mavlink / MAVSDK-Python

MAVSDK client for Python.
https://mavsdk.mavlink.io
BSD 3-Clause "New" or "Revised" License
311 stars 218 forks source link

Why can't my python code connect to Mavsdk_server with IP added? #659

Open boom6314 opened 6 months ago

boom6314 commented 6 months ago

When I open two Mavsdk_servers, one with IP and one without IP, they can connect to each other. However, when I use python code to connect to the Mavsdk_server with IP, there is no response.

Screenshot from 2024-02-27 17-24-29 Screenshot from 2024-02-27 17-24-38

 import asyncio
 from mavsdk import System 

 async def run():
     drone = System(mavsdk_server_address="localhost", port=50051)
     print("123")
     await drone.connect()
     print("456")

 if __name__ == "__main__":
     asyncio.run(run())

I think I'm stuck at drone.connect because I only see 123

I wonder what kind of problem this is and how to solve it

julianoes commented 6 months ago

I don't think all the pieces are in place to use server functionality in Python, so it might be tricky.

One problem I see is that both are waiting for a drone, right? So that requires the compid to be 1.

JonasVautherin commented 6 months ago

MAVSDK-Python will listen on 14540 by default, but you apparently send on 14550. Can you try mavsdk_server_address="udp://:14550"?

boom6314 commented 6 months ago

One problem I see is that both are waiting for a drone, right? So that requires the compid to be 1.

Thank you for your response. Yes, our goal is to use python code to simulate a drone and throw out information through telemetry_server in MAVSDK-Python. We are currently using the simplest method to test the connection, but our Python code cannot connect to Mavsdk_server

boom6314 commented 6 months ago

MAVSDK-Python will listen on 14540 by default, but you apparently send on 14550. Can you try mavsdk_server_address="udp://:14550"?

Thank you for your response.I tried it but the code still only shows 123

import asyncio
from mavsdk import System 

async def run():
    drone = System(mavsdk_server_address="udp://:14550", port=50051)
    print("123")
    await drone.connect()
    print("456")
JonasVautherin commented 6 months ago

And what is on the other side?

boom6314 commented 6 months ago

And what is on the other side?

I set the same settings in the terminal as before, and changed the mavsdk_server_address in the python code to mavsdk_server_address="udp://:14550", but it still failed to connect successfully, and it still only showed 123

import asyncio
from mavsdk import System 

async def run():
    drone = System(mavsdk_server_address="udp://:14550", port=50051)
    print("123")
    await drone.connect()
    print("456")

if __name__ == "__main__":
    asyncio.run(run())

Screenshot from 2024-03-04 17-52-29 Screenshot from 2024-03-04 17-52-44

JonasVautherin commented 6 months ago

Are you sure that your python script does not interfere with your server? Seems like both will try to bind to 50051. On the screenshots below, the "listener" is on -p 50055, but your python script uses 50051. Is that expected?

boom6314 commented 6 months ago

Are you sure that your python script does not interfere with your server? Seems like both will try to bind to 50051.

According to the instructions of Mavsdk, the Mavsdk_server with IP added will throw out the information. So my idea is to set the port of the python code to be the same as the Mavsdk_server with added IP, and let the python code control what information my Mavsdk_server will throw out. So first I want to try to use python code to connect to Mavsdk_server with IP added.

JonasVautherin commented 6 months ago

What I mean is that your screenshots have two setups:

Your Python code does not do one or the other, but a mix of them: -p 50051 udp://:14550.

boom6314 commented 6 months ago

What I mean is that your screenshots have two setups: Sorry for taking a while to reply

When I use mavsdk_server_address='udp://127.0.0.1:14550',port=50051 that code only show 123

import asyncio
from mavsdk import System 

async def run():
    drone = System(mavsdk_server_address='udp://127.0.0.1:14550',port=50051)
    print("123")
    await drone.connect()
    print("456")

if __name__ == "__main__":
    asyncio.run(run())

When I use port=50055 and that code can show 123 and 456

import asyncio
from mavsdk import System 

async def run():
    drone = System(port=50055)
    print("123")
    await drone.connect()
    print("456")

if __name__ == "__main__":
    asyncio.run(run())

Does this mean that my python code can connect to Mavsdk_server without IP but cannot connect to Mavsdk_server with IP?

boom6314 commented 5 months ago

I don't think all the pieces are in place to use server functionality in Python, so it might be tricky.

I finally used a tricky method. I first set the IP of Mavsdk_server to 127.0.0.1, connected it to my python code on the local side, and then used Cmavnode to forward the information to the target IP.