0
$\begingroup$

Rosanswers logo

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!


Originally posted by narcispr on ROS Answers with karma: 307 on 2012-07-02

Post score: 2


Original comments

Comment by Lorenz on 2012-07-02:
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.

Comment by narcispr on 2012-07-02:
Added error messages.

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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)

Originally posted by Lorenz with karma: 22731 on 2012-07-02

This answer was ACCEPTED on the original site

Post score: 3

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.