0
0
Pcb-designHow-ToBeginner · 4 min read

How to Send Setpoint Using MAVROS in Drone Programming

To send a setpoint using mavros in drone programming, publish a geometry_msgs/PoseStamped message to the /mavros/setpoint_position/local topic. This tells the drone where to move by specifying the desired position in local coordinates.
📐

Syntax

Use a ROS publisher to send setpoints to the drone. The main parts are:

  • geometry_msgs/PoseStamped: Message type containing position and orientation.
  • /mavros/setpoint_position/local: Topic where setpoints are published.
  • ros::Publisher or rospy.Publisher: Publisher object to send messages.

Publish the setpoint repeatedly at a rate faster than 2 Hz to keep the drone in OFFBOARD mode.

cpp
ros::Publisher setpoint_pub = nh.advertise<geometry_msgs::PoseStamped>("/mavros/setpoint_position/local", 10);

geometry_msgs::PoseStamped pose;
pose.header.stamp = ros::Time::now();
pose.pose.position.x = 1.0;
pose.pose.position.y = 2.0;
pose.pose.position.z = 3.0;
pose.pose.orientation.w = 1.0;

setpoint_pub.publish(pose);
💻

Example

This example shows how to send a position setpoint to move the drone to coordinates (1, 2, 3) using Python and ROS with MAVROS.

python
import rospy
from geometry_msgs.msg import PoseStamped

def send_setpoint():
    rospy.init_node('setpoint_sender')
    pub = rospy.Publisher('/mavros/setpoint_position/local', PoseStamped, queue_size=10)
    rate = rospy.Rate(10)  # 10 Hz

    pose = PoseStamped()
    pose.pose.position.x = 1.0
    pose.pose.position.y = 2.0
    pose.pose.position.z = 3.0
    pose.pose.orientation.w = 1.0

    while not rospy.is_shutdown():
        pose.header.stamp = rospy.Time.now()
        pub.publish(pose)
        rate.sleep()

if __name__ == '__main__':
    try:
        send_setpoint()
    except rospy.ROSInterruptException:
        pass
Output
No direct output; drone receives setpoint commands to move to (1, 2, 3).
⚠️

Common Pitfalls

  • Not publishing setpoints continuously: The drone requires setpoints at >2 Hz to stay in OFFBOARD mode.
  • Incorrect message type or topic: Use geometry_msgs/PoseStamped and the exact topic /mavros/setpoint_position/local.
  • Not setting the header timestamp: Always update header.stamp before publishing.
  • Not arming or setting mode: Ensure the drone is armed and in OFFBOARD mode before sending setpoints.
python
## Wrong: Publishing once only
pose = PoseStamped()
pose.pose.position.x = 1.0
pub.publish(pose)  # Drone may ignore this

## Right: Publish continuously in a loop
while not rospy.is_shutdown():
    pose.header.stamp = rospy.Time.now()
    pub.publish(pose)
    rate.sleep()
📊

Quick Reference

ConceptDescription
Message Typegeometry_msgs/PoseStamped for position and orientation
Topic/mavros/setpoint_position/local to send local position setpoints
Publish RateAt least 2 Hz to maintain OFFBOARD mode
Header TimestampMust update header.stamp before publishing
Drone StateDrone must be armed and in OFFBOARD mode

Key Takeaways

Publish geometry_msgs/PoseStamped messages to /mavros/setpoint_position/local to send setpoints.
Send setpoints continuously at a rate above 2 Hz to keep OFFBOARD mode active.
Always update the message header timestamp before publishing.
Ensure the drone is armed and in OFFBOARD mode before sending setpoints.
Use the correct topic and message type to avoid communication issues.