ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Error creating array type custom message for ros service in python

asked 2014-02-03 12:33:26 -0600

Vijeth gravatar image

updated 2021-07-16 07:19:16 -0600

Andromeda gravatar image

Hey,

I m trying to create a script that sends a service message to the server. Everything is good on the server side but I'm trying to build the client side using Python, just to get familiar with Python. However, with my limited knowledge of Python, building complex msgs is proving rather difficult.

I have HighLevelGoals.srv under nuric_sytem which is an array of HighLevelGoal.msg.

HighLevelGoals.srv

*HighLevelGoal[] goals

bool result*


HighLevelGoal.msg

geometry_msgs/PoseStamped goal_pose

string goal_source


My basic code is:


#!/usr/bin/env python

import roslib; roslib.load_manifest('nuric_system')

import sys
import rospy

from nuric_system.srv import HighLevelGoals
from nuric_system.msg import HighLevelGoal


def send_goal():
    rospy.wait_for_service('send_goals')

    send_goal_proxy = rospy.ServiceProxy('send_goals', HighLevelGoals)

    srv_goals=HighLevelGoals()
    goal_1=HighLevelGoal()

    goal_1.goal_source = "goal_sender"  
    goal_1.goal_pose.header.frame_id = "/sender"
    goal_1.goal_pose.pose.position.x =-5.17850098489
    goal_1.goal_pose.pose.position.y =-3.45
    goal_1.goal_pose.pose.position.z =0.0
    goal_1.goal_pose.pose.orientation.x= 0.0
    goal_1.goal_pose.pose.orientation.y= 0.0
    goal_1.goal_pose.pose.orientation.z= 0.192
    goal_1.goal_pose.pose.orientation.w=1.0


    srv_goals.goals.append(goal_1)
    resp1 = send_goal_proxy(srv_goals)

    return 
if __name__ == "__main__":
   send_goal()
   print("goal sent")

when I run it, I get the following error :

**Traceback (most recent call last):
  File "test_goal.py", line 40, in <module>
    send_goal()
  File "test_goal.py", line 33, in send_goal
    srv_goals.goals.append(goal_1)
AttributeError: 'HighLevelGoals' object has no attribute 'goals'**

But HighLevelGoals.srv does have a goals array which should be the same as goal_1 type. Clearly, Im not creating the message correctly. If anyone has any link for appropriate filling of msg arrays, please let me know.

Regards

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2014-02-06 20:08:53 -0600

fergs gravatar image

You're using the wrong type. The service type is HighLevelGoals, but your goal is of type HighLevelGoalsRequest (and the response type will be HighLevelGoalsResponse). However, the service proxy will actually create the request from your parameters to it, thus the call would be:

send_goal_proxy(goals)

Where goals is of type list(HighLevelGoal). See http://wiki.ros.org/rospy/Overview/Se... for more details

edit flag offensive delete link more

Comments

Thank you so much Mike. That fixed it. Just to let others know, I added goals=nuric_system.srv.HighLevelGoalsRequest() which gave me NameError: global name 'nuric_system' is not defined Then I changed from nuric_system.srv import HighLevelGoals to from nuric_system.srv import * And that was it!!

Vijeth gravatar image Vijeth  ( 2014-02-07 07:09:22 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2014-02-03 12:33:26 -0600

Seen: 1,866 times

Last updated: Jul 16 '21