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

Append data to vector3[] array - custom message

asked 2015-04-27 02:07:39 -0600

Holzroller gravatar image

Hey guys out there,

with lovely help of ROSanswers I got into creating and defining custom messages. My aim is to create a custom message which contains of an array of vector3(). I basically got a couple of points, which i want to publish in a single message.

So I created a custom message which looks like:

geometry_msgs/Vector3[] Vector3DArray

I was able to create a package with catkin and I'm able to use the message.

I got the following code:

...

from geometry_msgs.msg import Vector3
from vector_3_array.msg import Vector3DArray

...

self.stelldaten = rospy.Publisher('Stelldaten', Vector3DArray, queue_size=10)
self.vectordaten_tabelle = Vector3DArray()

...

        vec3 = Vector3()

        for i in range(0, self.anzahl_punkte):
            print 'Vektor ' + str(i)
            vec3.x = self.q1_v[i]
            vec3.y = self.q2_v[i]
            vec3.z = self.tges[i]
            print vec3
            self.vectordaten_tabelle.Vector3DArray.append(vec3)

        print self.q1_v
        print self.q2_v

        print self.vectordaten_tabelle

        self.stelldaten.publish(self.vectordaten_tabelle)

So the problem right now is, that self.vectordaten_tabelle has anzahl_punkte entries of vector3() (which is perfect), but each entry gets overridden by the last vec3 which is added. Just to show you an example:

The prints in the for loop show smth like this:

...

Vektor 11
x: -35.2332092159
y: 2.95642070607
z: 0.057369297869
Vektor 12
x: -29.4943008067
y: 3.36452718727
z: 0.0590352126953
Vektor 13
x: -29.4943008067
y: 3.36452718727
z: 0.0518744285557
Vektor 14
x: 0.0
y: 0.0
z: 0.0518744285557

But the self.vectordaten_tabelle looks like:

...

    x: 0.0
    y: 0.0
    z: 0.0518744285557
  - 
    x: 0.0
    y: 0.0
    z: 0.0518744285557
  - 
    x: 0.0
    y: 0.0
    z: 0.0518744285557
  - 
    x: 0.0
    y: 0.0
    z: 0.0518744285557

Does somebody now what exactly is going wrong here? Thanks alot in advance!

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2015-04-27 02:37:52 -0600

gvdhoorn gravatar image

updated 2015-04-27 02:45:14 -0600

So I created a custom message which looks like:

geometry_msgs/Vector3[] Vector3DArray

I'm just guessing, but it might be that Python doesn't like the fact that you have a message field (essentially a member variable of your msg class) that has the same name as the type of your msg class.

Alternative names I've seen used in these cases (msg with just a single field) are: data or vectors (see geometry_msgs/PoseArray for a similar msg). Using more semantically relevant names is encouraged though (but try not to tie your msg definition to your current application too much).


Edit: hm, just noticed this:

vec3 = Vector3()

for i in range(0, self.anzahl_punkte):
    ...
    self.vectordaten_tabelle.Vector3DArray.append(vec3)

You create a single Vector3 instance outside your for-loop, which you then update by setting its fields, and then append to the vectordaten_tabelle msg object. Afaik, Python is reference-based, which means that every .append(..) you call merely adds a new reference to the same Vector3 instance.

If you move your vec3 = Vector3() line into your for-loop, you'll get the behaviour you are after.

I would still recommend changing the name of your msg field though.

edit flag offensive delete link more

Comments

Thanks so much! I didnt know about both facts. Putting the vec3 = Vector3() in the for loop works fine! Additionally I'll change the name though!

Holzroller gravatar image Holzroller  ( 2015-04-27 03:33:12 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-04-27 02:07:39 -0600

Seen: 4,682 times

Last updated: Apr 27 '15