0
0
Pcb-designHow-ToBeginner · 4 min read

How to Use Companion Computer with Drone for Advanced Control

To use a companion computer with a drone, connect it via a communication link like serial or USB to the drone's flight controller, then run software (e.g., MAVSDK or DroneKit) on the companion computer to send commands and receive telemetry. This setup allows advanced processing and autonomous control beyond the flight controller's capabilities.
📐

Syntax

Using a companion computer involves these main parts:

  • Connection: Establish a link between the companion computer and the drone's flight controller (usually via serial port or USB).
  • Communication Protocol: Use a protocol like MAVLink to exchange messages.
  • Software API: Run a drone control library (e.g., MAVSDK in Python) on the companion computer to send commands and receive data.
python
from mavsdk import System
import asyncio

async def run():
    drone = System()
    await drone.connect(system_address="serial:///dev/ttyUSB0:57600")

    async for state in drone.core.connection_state():
        if state.is_connected:
            print("Drone connected to companion computer")
            break

asyncio.run(run())
Output
Drone connected to companion computer
💻

Example

This example shows how to connect a companion computer to a drone using MAVSDK in Python, then arm the drone and take off to 5 meters altitude.

python
from mavsdk import System
import asyncio

async def run():
    drone = System()
    await drone.connect(system_address="serial:///dev/ttyUSB0:57600")

    print("Waiting for drone to connect...")
    async for state in drone.core.connection_state():
        if state.is_connected:
            print("Drone connected")
            break

    print("Arming drone...")
    await drone.action.arm()

    print("Taking off...")
    await drone.action.takeoff()

    await asyncio.sleep(10)  # Wait 10 seconds to reach altitude

    print("Landing...")
    await drone.action.land()

if __name__ == "__main__":
    asyncio.run(run())
Output
Waiting for drone to connect... Drone connected Arming drone... Taking off... Landing...
⚠️

Common Pitfalls

  • Wrong connection string: Using incorrect serial port or baud rate causes failure to connect.
  • Missing permissions: Companion computer may need permission to access serial devices (use chmod or run as root).
  • Ignoring async: MAVSDK uses async calls; forgetting await leads to errors.
  • Not waiting for connection: Trying to send commands before the drone is connected will fail.
python
from mavsdk import System
import asyncio

async def wrong_run():
    drone = System()
    # Wrong port or missing await
    drone.connect(system_address="serial:///dev/ttyUSB1:9600")  # Missing await
    print("Trying to arm without connection")
    await drone.action.arm()  # This will fail

async def correct_run():
    drone = System()
    await drone.connect(system_address="serial:///dev/ttyUSB0:57600")
    async for state in drone.core.connection_state():
        if state.is_connected:
            break
    await drone.action.arm()  # Correct usage

# Run correct_run() instead of wrong_run()
📊

Quick Reference

Key steps to use companion computer with drone:

  • Connect companion computer to flight controller via serial/USB.
  • Use MAVLink protocol for communication.
  • Run drone control software (MAVSDK, DroneKit) on companion computer.
  • Wait for connection before sending commands.
  • Handle permissions and correct ports carefully.

Key Takeaways

Connect the companion computer to the drone's flight controller using the correct serial port and baud rate.
Use a drone control library like MAVSDK to communicate via MAVLink protocol.
Always wait for a confirmed connection before sending commands to the drone.
Ensure the companion computer has proper permissions to access hardware interfaces.
Use asynchronous programming patterns correctly to avoid runtime errors.