0
0
Pcb-designHow-ToBeginner · 4 min read

How to Publish and Subscribe MAVROS Topics in Drone Programming

To publish and subscribe to mavros topics in drone programming, create ROS nodes that use rospy.Publisher to send messages and rospy.Subscriber to receive messages on specific MAVROS topics. Use the correct message types from mavros_msgs or standard ROS messages and initialize the ROS node properly.
📐

Syntax

Publishing and subscribing to MAVROS topics in ROS Python involves these parts:

  • rospy.init_node(): Starts your ROS node.
  • rospy.Publisher(topic, msg_type, queue_size): Creates a publisher to send messages on a topic.
  • rospy.Subscriber(topic, msg_type, callback): Creates a subscriber to listen to messages on a topic and run a callback function when a message arrives.
  • Message types: Use message classes like mavros_msgs.msg.State or std_msgs.msg.String depending on the topic.
python
import rospy
from std_msgs.msg import String
from mavros_msgs.msg import State

def state_callback(data):
    rospy.loginfo(f"Current state: {data.mode}")

rospy.init_node('mavros_example_node')

# Subscriber example
state_sub = rospy.Subscriber('/mavros/state', State, state_callback)

# Publisher example
pub = rospy.Publisher('/mavros/setpoint_position/local', String, queue_size=10)

rate = rospy.Rate(10)  # 10 Hz

while not rospy.is_shutdown():
    pub.publish("Hello MAVROS")
    rate.sleep()
💻

Example

This example shows a ROS Python node that subscribes to the /mavros/state topic to print the drone's current mode and publishes a simple string message to /mavros/setpoint_position/local at 1 Hz.

python
import rospy
from std_msgs.msg import String
from mavros_msgs.msg import State

def state_callback(data):
    print(f"Drone mode: {data.mode}")

rospy.init_node('mavros_pub_sub_example')

state_subscriber = rospy.Subscriber('/mavros/state', State, state_callback)
position_publisher = rospy.Publisher('/mavros/setpoint_position/local', String, queue_size=10)

rate = rospy.Rate(1)  # 1 Hz

while not rospy.is_shutdown():
    position_publisher.publish("Setpoint message")
    rate.sleep()
Output
Drone mode: MANUAL Drone mode: MANUAL Drone mode: OFFBOARD ... (prints current mode each time a message is received)
⚠️

Common Pitfalls

  • Wrong message type: Using a message type that does not match the topic causes errors. Always check the topic's message type with rostopic info <topic>.
  • Not initializing ROS node: Forgetting rospy.init_node() will prevent your node from working.
  • Publishing too fast or too slow: MAVROS expects setpoints at a steady rate (usually 10 Hz). Publishing too slow can cause the drone to disarm.
  • Callback not defined properly: The subscriber callback must accept one argument (the message).
python
import rospy
from std_msgs.msg import String

# Wrong: Missing rospy.init_node()
pub = rospy.Publisher('/mavros/setpoint_position/local', String, queue_size=10)

# Wrong: Callback with no parameters
# def bad_callback():
#     print("No message received")

# Correct usage:
rospy.init_node('correct_node')

# Correct callback
# def good_callback(msg):
#     print(msg.data)
📊

Quick Reference

Remember these key points when working with MAVROS topics:

  • Use rospy.Publisher to send messages and rospy.Subscriber to receive messages.
  • Check topic message types with rostopic info <topic>.
  • Initialize your node with rospy.init_node() before publishing or subscribing.
  • Publish setpoints at a steady rate (usually 10 Hz) for stable drone control.
  • Use correct callback functions that accept the message parameter.

Key Takeaways

Always initialize your ROS node with rospy.init_node() before publishing or subscribing.
Use rospy.Publisher to send messages and rospy.Subscriber with a proper callback to receive messages on MAVROS topics.
Match the message type exactly to the topic's expected message to avoid errors.
Publish setpoints at a steady rate (usually 10 Hz) to maintain drone control.
Check topic details with rostopic info to confirm message types and topic names.