Error with custom service message

I’m doing the exam and I’m facing an issue I cannot solve by myself.

the problem is with task 2b, that requires creating a custom message.
my code is

#! /usr/bin/env python

import rospy

from std_msgs.msg import Empty as EmptyMsg
#from std_msgs.msg import Empty
from std_srvs.srv import EmptyResponse
from std_srvs.srv import Empty
from path_exam.srv import MyCustomServiceMessage, MyCustomServiceMessageResponse
#from std_msgs.msg import Empty as EmptyMsg
from geometry_msgs.msg import Twist

def my_callback(request):
    i = 0

    while i<5:
        rospy.loginfo("taking off")
        i += 1
    vel_cmd.linear.x = 1
    j = 0 

    while j<5:
        rospy.loginfo("moving foward")
        j += 1
    vel_cmd.linear.x = 0

    k = 0 

    while k<5:
        k += 1

    response = MyCustomServiceMessageResponse()

    #response.length = "The drone has moved 5 meters."
    response.length = "The drone has moved 5 meters."
    response.success = True

    return response

# for the takeoff
takeoff_pub = rospy.Publisher('/drone/takeoff', EmptyMsg, queue_size=1)
takeoff_cmd = EmptyMsg()

# for the landing
landind_pub = rospy.Publisher('/drone/land', EmptyMsg, queue_size=1)
landing_cmd = EmptyMsg()

# for the movement
vel_pub = rospy.Publisher('/cmd_vel', Twist, queue_size=1)
vel_cmd = Twist()
rate = rospy.Rate(1)
my_service = rospy.Service('/my_service' ,Empty, my_callback)


and my custom messages are

bool success
string length

when the callback function is completed and the responses written I get the error

ERROR: service [/my_service] responded with an error: service cannot process request: handler returned invalid value: Invalid number of arguments, args should be [] args are(success: True
length: "The drone has moved 5 meters.",)

I’ve tried using a float and a bool as responses but the problem persists.
what am I doing wrong?

Hi @patruska,
So your service object definition has Empty as the service_class (check the code snippet below)
my_service = rospy.Service('/my_service' ,Empty, my_callback)

But your service callback function my_callback returns a response of the type MyCustomServiceMessageResponse.

I’d suggest you try to fix it on your own as it is a very small mistake, but if your still stuck, feel free to click on the arrow below to see the solution.


Your Service object and the callback function should be using the same custom message you created, so the only line that needs to be changed is ur service definition like this
my_service = rospy.Service('/my_service' ,MyCustomServiceMessage, my_callback)

Happy Learning…!!!

now i’m facing the issue:

ERROR: Unable to load type [path_exam/MyCustomServiceMessage].
Have you typed 'make' in [path_exam]?

which happens whether I add a service message of type int or leave it empty.
there are no examples of empty message that I can find find around in a custom message, so I’m quite lost at the moment.

Check the configuration of your CMakeList.txt and package.xml and build your workspace again, maybe your service isn’t defined yet

I have checked the files and something was missing, but it still doesn’t accept an empty message or any type I have tried like int32 ot float32

this solved my issue. I don’t know if this is written in the course and reported somewhere, but here it is.

What exactly solved your issue? @patruska

I cannot find the link to the thread anymore, but basically source devel/setup.bash has to be run in every shell