0
0
Pcb-designHow-ToBeginner · 4 min read

How to Use ROS with Drone: Setup and Example

To use ROS with a drone, install ROS on your control computer and connect it to the drone's flight controller via a communication interface like MAVROS. Then, use ROS nodes to send commands and receive telemetry data, enabling autonomous flight and control.
📐

Syntax

Using ROS with a drone typically involves these parts:

  • ROS Master: Coordinates communication between nodes.
  • MAVROS Node: Bridges ROS and the drone's flight controller using MAVLink protocol.
  • Publisher Nodes: Send commands like velocity or waypoints.
  • Subscriber Nodes: Receive telemetry data such as GPS or battery status.

The basic command to start MAVROS is:

bash
roslaunch mavros px4.launch fcu_url:=udp://:14540@
💻

Example

This example shows how to publish velocity commands to a drone using ROS Python client rospy and geometry_msgs/Twist. It commands the drone to move forward.

python
import rospy
from geometry_msgs.msg import Twist

def move_drone():
    rospy.init_node('drone_velocity_publisher')
    pub = rospy.Publisher('/mavros/setpoint_velocity/cmd_vel_unstamped', Twist, queue_size=10)
    rate = rospy.Rate(10)  # 10 Hz

    twist = Twist()
    twist.linear.x = 1.0  # Move forward at 1 m/s
    twist.linear.y = 0.0
    twist.linear.z = 0.0
    twist.angular.x = 0.0
    twist.angular.y = 0.0
    twist.angular.z = 0.0

    while not rospy.is_shutdown():
        pub.publish(twist)
        rate.sleep()

if __name__ == '__main__':
    try:
        move_drone()
    except rospy.ROSInterruptException:
        pass
Output
No direct output; drone moves forward at 1 m/s while node runs.
⚠️

Common Pitfalls

Common mistakes when using ROS with drones include:

  • Not starting roscore before launching MAVROS, causing communication failure.
  • Incorrect fcu_url in MAVROS launch, so ROS can't connect to the flight controller.
  • Publishing commands before the drone is in offboard mode, which ignores external commands.
  • Not sending commands at a steady rate (usually 10 Hz), causing failsafe triggers.

Example of wrong and right MAVROS launch:

bash
# Wrong: missing fcu_url
roslaunch mavros px4.launch

# Right: specify connection URL
roslaunch mavros px4.launch fcu_url:=udp://:14540@
📊

Quick Reference

Command/ConceptDescription
roscoreStarts the ROS master node for communication
roslaunch mavros px4.launch fcu_url:=udp://:14540@Launch MAVROS node connected to drone via UDP
Publisher to /mavros/setpoint_velocity/cmd_vel_unstampedSend velocity commands to drone
Subscriber to /mavros/stateMonitor drone connection and mode
Offboard modeFlight mode allowing external control via ROS

Key Takeaways

Always start roscore before launching MAVROS to enable ROS communication.
Use MAVROS as the bridge between ROS and the drone's flight controller.
Publish commands at a steady rate and ensure the drone is in offboard mode.
Verify connection URLs and network settings to avoid communication issues.
Use ROS topics to send commands and receive telemetry for autonomous control.