Ask Your Question
1

python deep copy of ROS message

asked 2012-07-02 02:03:09 -0500

narcispr gravatar image

updated 2014-01-28 17:12:52 -0500

ngrennan gravatar image

I want to publish two odometry messages that share most of the fields. To avoid copying all the elements one by one I try:

odom_new = Odometry(odom_old)

But this give me an error.

File "/opt/ros/electric/ros/core/roslib/src/roslib/message.py", line 362, in __init__ raise TypeError("Invalid number of arguments, args should be %s"%str(self.__slots__)+" args are"+str(args)) TypeError: Invalid number of arguments, args should be ['header', 'child_frame_id', 'pose', 'twist']

Then I try:

odom_new = Odometry(odom_old.header, odom_old.child_frame_id, odom_old.pose, odom_old.twist)

This doesn't give me any errors but just creates a pointer from the new object to the old one. Then, when the new one is modified the old one too (I need an independent object)

After this I try:

import copy

odom_new = copy.deepcopy(odom_old)

But again this solution fails.

odom_new = copy.deepcopy(odom_old) AttributeError: 'function' object has no attribute 'deepcopy'

Any Idea about how to create a deep copy of a ROS message in Python? Sorry, my Python knowledge is very limited :P!

edit retag flag offensive close merge delete

Comments

What's the failure you are getting? Please always post backtraces and error messages when posting questions to save your and other people's time.

Lorenz gravatar imageLorenz ( 2012-07-02 02:21:14 -0500 )edit

Added error messages.

narcispr gravatar imagenarcispr ( 2012-07-02 03:08:51 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2012-07-02 02:22:48 -0500

Lorenz gravatar image

updated 2012-07-02 05:27:55 -0500

copy.deepcopy is the recommended way of creating a deep copy of a message, according to this ticket.

Edit: You probably named a function copy after the import line. Python has only one namespace for variables and functions (in contrast to Common Lisp for instance), i.e. by naming a function copy, you overwrite the package copy. Either do

from copy import deepcopy

and just call deepcopy, not copy.deepcopy or rename your function. You can also import the module copy under a different name:

import copy as copy_module

Then you can call the deepcopy function like this:

odom_new = copy_module.deepcopy(odom_old)
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2012-07-02 02:03:09 -0500

Seen: 2,733 times

Last updated: Jul 02 '12