Excrices 2.4 drone (service server)

hello. i create a service server for drone trajectories. but when i call the service i got the error:

rosservice call /execute_trajectory {“circle”}
ERROR: Incompatible arguments to call service:
No field name [circle]
Provided arguments are:

  • {‘circle’: None} (type dict)

Service arguments are: [label]

here is my code for the service server:

#!/usr/bin/env python3

import rospy
from geometry_msgs.msg import Twist
from my_service_pkg.srv import drone_msg,drone_msgResponse

def execute_circle():
    # Command the drone to execute a circle trajectory
    rospy.loginfo("Moving in a circle...")
    radius = 1.0  # the radius of the circle
    angular_speed = 1.0  # the angular speed of the drone

    # Move the drone in a circular path
    t0 = rospy.Time.now().to_sec()
    while rospy.Time.now().to_sec() - t0 < 10.0:  # move for 10 seconds
        _move_msg.linear.x = angular_speed * radius
        _move_msg.angular.z = angular_speed

    rospy.loginfo("Executing circle trajectory...")

    _move_msg.linear.x = 0.0
    _move_msg.angular.z = 0.0

    # ... code to execute the circle trajectory ...

def execute_square():

    # Command the drone to execute a square trajectory
    rospy.loginfo("Executing square trajectory...")
    # ... code to execute the square trajectory ...

def execute_triangle():
    # Command the drone to execute a triangle trajectory
    rospy.loginfo("Executing triangle trajectory...")
    # ... code to execute the triangle trajectory ...

# function that makes the drone stop
def stop_drone(self):
  self._move_msg.linear.x = 0.0
  self._move_msg.angular.z = 0.0

# function that makes the drone turn
def turn_drone(self):
  self._move_msg.linear.x = 0.0
  self._move_msg.angular.z = 1.0

# function that makes the drone move forward
def move_forward_drone(self):
  rospy.loginfo("Moving forward...")
  self._move_msg.linear.x = 1.0
  self._move_msg.angular.z = 0.0

def handle_trajectory_service(req):
    # Process the service request

     if req.label == "circle" | req.label == "square" |  req.label == "triangle" : 

        if req.label == "circle":
        elif req.label == "square":
        elif req.label == "triangle":

        return drone_msgResponse(success=True, message="Trajectory executed successfully")

      return drone_msgResponse(success=False, message="Invalid request")

my_pub = rospy.Publisher('/cmd_vel', Twist, queue_size=1)

    # Create a service server and register the callback function
srv = rospy.Service('execute_trajectory', drone_msg, handle_trajectory_service)

rospy.loginfo("Trajectory Service Server is ready.")

    # Spin the node to prevent it from exiting

can anyone help me?

The problem is that you are providing the arguments incorrectly.

If your service has been properly set up, you can use TAB TAB (pressed in quick succession) to auto-fill the message structure.

source ~/catkin_ws/devel/setup.bash
rosservice call /execute_trajectory  # press TAB TAB now
# Supply the arguments to the structure provided

thanl you very much. it is solved this.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.