# Problem with exercise 3.5

**URL:** <https://get-help.theconstruct.ai/t/problem-with-exercise-3-5/15851>\
**Category:** ROS Navigation In 5 Days\
**Created:** [March 1, 2022, 4:24pm UTC](https://get-help.theconstruct.ai/t/problem-with-exercise-3-5/15851 "2022-03-01T16:24:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![saxosun](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/saxosun/32/9368_2.png) [@saxosun](https://get-help.theconstruct.ai/u/saxosun)\
**Post date:** [March 1, 2022, 4:24pm UTC](https://get-help.theconstruct.ai/t/problem-with-exercise-3-5/15851/1 "2022-03-01T16:24:08Z")

</div>

Hi,  
about exercise 3.5, I creates the working requested server

when I execute the command to call the service I get the pose of the robot even if I get the error below too.  
user:~/catkin\_ws/src/get\_pose/launch$ rosservice call /get\_pose\_service “{}”  
ERROR: service [/get\_pose\_service] responded with an error: b’service cannot process request: service handler returned None’

I get the pose

user:/opt/ros/noetic/share/std\_srvs/srv$ roslaunch get\_pose get\_pose\_launch.launch  
… logging to /home/user/.ros/log/83877550-9966-11ec-aa79-0242ac120007/roslaunch-1\_xterm-13947.log  
Checking log directory for disk usage. This may take a while.  
Press Ctrl-C to interrupt  
Done checking log file disk usage. Usage is \<1GB.

started roslaunch server http://1\_xterm:37491/

# SUMMARY

PARAMETERS

- /rosdistro: noetic
- /rosversion: 1.15.9

NODES  
/  
get\_pose\_node (get\_pose/get\_pose\_service.py)

ROS\_MASTER\_URI=http://1\_simulation:11311

process[get\_pose\_node-1]: started with pid [13955]  
[INFO] [1646150041.332582, 7447.687000]: [‘position: 0.07, 0.00, 0.00 - orientation: 0.00, 0.00, -0.01, 1.00’]  
[INFO] [1646150407.848289, 7813.066000]: [‘position: -1.98, 3.90, 0.00 - orientation: 0.00, 0.00, 1.00, 0.03’]  
[INFO] [1646150602.986185, 8007.600000]: [‘position: 13.07, 11.58, 0.00 - orientation: 0.00, 0.00, 0.71, 0.71’]

How can I avoid this error?  
thanks  
Salvatore

---

<div class="post-metadata">

**Author:** ![marco.nc.arruda](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/marco.nc.arruda/32/2639_2.png) [@marco.nc.arruda](https://get-help.theconstruct.ai/u/marco.nc.arruda)\
**Post date:** [March 1, 2022, 4:27pm UTC](https://get-help.theconstruct.ai/t/problem-with-exercise-3-5/15851/2 "2022-03-01T16:27:29Z")

</div>

Hi @saxosun ,

Could you post the code of your service server?

---

<div class="post-metadata">

**Author:** ![saxosun](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/saxosun/32/9368_2.png) [@saxosun](https://get-help.theconstruct.ai/u/saxosun)\
**Post date:** [March 1, 2022, 4:28pm UTC](https://get-help.theconstruct.ai/t/problem-with-exercise-3-5/15851/3 "2022-03-01T16:28:36Z")

</div>

Hi Marco, sure

```auto
#! /usr/bin/env python
import rospy
from geometry_msgs.msg import PoseWithCovarianceStamped
from std_srvs.srv import Trigger, TriggerResponse
class GetPoseClass(object):

    def __init__ (self):
        self.sub = rospy.Subscriber(
            "/amcl_pose", PoseWithCovarianceStamped, self.subscriber_callback)

        self.get_pose_service_server = rospy.Service(
            "/get_pose_service", Trigger, self.service_callback)

        self.data = PoseWithCovarianceStamped()
        self.response = TriggerResponse()
        self.string = []

    def service_callback(self, request):
        self.response.success = True
        self.response.message = self.string
        rospy.loginfo(self.string)

    def subscriber_callback(self, msg):
        x = msg.pose.pose.position.x
        y = msg.pose.pose.position.y
        z = msg.pose.pose.position.z
        a = msg.pose.pose.orientation.x
        b = msg.pose.pose.orientation.y
        g = msg.pose.pose.orientation.z
        w = msg.pose.pose.orientation.w
        self.string = [
            'position: %.2f, %.2f, %.2f - orientation: %.2f, %.2f, %.2f, %.2f' % (x, y, z, a, b, g, w)]

if __name__ == " __main__":
    rospy.init_node("get_pose_node")
    try:
        GetPoseClass()
        rospy.spin()
    except rospy.ROSInterruptException:
        pass

```

---

<div class="post-metadata">

**Author:** ![marco.nc.arruda](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/marco.nc.arruda/32/2639_2.png) [@marco.nc.arruda](https://get-help.theconstruct.ai/u/marco.nc.arruda)\
**Post date:** [March 1, 2022, 4:36pm UTC](https://get-help.theconstruct.ai/t/problem-with-exercise-3-5/15851/4 "2022-03-01T16:36:27Z")

</div>

@saxosun ,

Your `service_callback` must return a `TriggerResponse` object

There is an assignment inside the ` __init__ ` that may be not necessary:

```auto
self.response = TriggerResponse()

```

You need to do this inside the callback and return this object

---

<div class="post-metadata">

**Author:** ![saxosun](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/saxosun/32/9368_2.png) [@saxosun](https://get-help.theconstruct.ai/u/saxosun)\
**Post date:** [March 1, 2022, 5:03pm UTC](https://get-help.theconstruct.ai/t/problem-with-exercise-3-5/15851/5 "2022-03-01T17:03:25Z")

</div>

I changed the code as you suggested

```auto
#! /usr/bin/env python
import rospy
from geometry_msgs.msg import PoseWithCovarianceStamped
from std_srvs.srv import Trigger, TriggerResponse

class GetPoseClass(object):

    def __init__ (self):
        self.sub = rospy.Subscriber(
            "/amcl_pose", PoseWithCovarianceStamped, self.subscriber_callback)
        self.get_pose_service_server = rospy.Service(
            "/get_pose_service", Trigger, self.service_callback)
        self.string = []

    def service_callback(self, request):
        self.response = TriggerResponse()
        self.response.success = True
        self.response.message = self.string
        rospy.loginfo(self.string)
        return self.response

    def subscriber_callback(self, msg):
        x = msg.pose.pose.position.x
        y = msg.pose.pose.position.y
        z = msg.pose.pose.position.z
        a = msg.pose.pose.orientation.x
        b = msg.pose.pose.orientation.y
        g = msg.pose.pose.orientation.z
        w = msg.pose.pose.orientation.w
        self.string = [x, y, z, a, b, g, w]

if __name__ == " __main__":
    rospy.init_node("get_pose_node")
    try:
        GetPoseClass()
        rospy.spin()
    except rospy.ROSInterruptException:

```

but

process[get\_pose\_node-1]: started with pid [21018]  
[INFO] [1646154057.428599, 11424.972000]: [13.070857331917631, 11.581616244908755, 0.0, 0.0, 0.0, 0.7072980836522808, 0.7069154269513512]  
[ERROR] [1646154057.431728, 11424.975000]: Error processing request: ‘list’ object has no attribute ‘encode’  
[‘Traceback (most recent call last):\n’, ’ File “/opt/ros/noetic/lib/python3/dist-packages/rospy/impl/tcpros\_service.py”, line 636, in \_handle\_request\n transport.send\_message(response, self.seq)\n’, ’ File “/opt/ros/noetic/lib/python3/dist-packages/rospy/impl/tcpros\_base.py”, line672, in send\_message\n serialize\_message(self.write\_buff, seq, msg)\n’, ’ File “/opt/ros/noetic/lib/python3/dist-packages/rospy/msg.py”, line 152, in serialize\_message\n msg.serialize(b)\n’, ’ File “/opt/ros/noetic/lib/python3/dist-packages/std\_srvs/srv/\_Trigger.py”, line 154, in serialize\n \_x = \_x.encode(‘utf-8’)\n’, “AttributeError: ‘list’ object has no attribute ‘encode’\n”]

user:~/catkin\_ws/src/get\_pose/launch$ rosservice call /get\_pose\_service “{}”  
ERROR: service [/get\_pose\_service] responded with an error: b"error processing request: ‘list’ object has no attribute ‘encode’"  
user:~/catkin\_ws/src/get\_pose/launch$  
pass

---

<div class="post-metadata">

**Author:** ![marco.nc.arruda](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/marco.nc.arruda/32/2639_2.png) [@marco.nc.arruda](https://get-help.theconstruct.ai/u/marco.nc.arruda)\
**Post date:** [March 1, 2022, 5:12pm UTC](https://get-help.theconstruct.ai/t/problem-with-exercise-3-5/15851/6 "2022-03-01T17:12:16Z")

</div>

In the code, it tries to assign a list to a string

`self.response.message = self.string`

You would need to convert this list to a string first, or even declare as a string from the beginning:

Something like: `"%s %s %s" % (msg.pose.pose.position.x, ...)`

Because python runtime is trying to apply the `encode` method to a list, and it does not exist.

---

<div class="post-metadata">

**Author:** ![saxosun](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/saxosun/32/9368_2.png) [@saxosun](https://get-help.theconstruct.ai/u/saxosun)\
**Post date:** [March 1, 2022, 5:31pm UTC](https://get-help.theconstruct.ai/t/problem-with-exercise-3-5/15851/7 "2022-03-01T17:31:15Z")

</div>

I resolved. tks----------------------------------------------------
