Ask Your Question
0

TypeError: cannot marshal <type 'numpy.float64'> objects

asked 2015-01-29 09:44:17 -0600

carrello gravatar image

I have a script that doesn't let me write on the parameter server . This is the script

  #!/usr/bin/env python

import rospy
from rospy.numpy_msg import numpy_msg
from rospy_tutorials.msg import Floats

import numpy as np
from scipy import integrate
from matplotlib.pylab import *

def diffDrive(t, y, u):
    # Assign some variables for convenience of notation
    X = y[0]
    Y = y[1]
    T = y[2]
    V = u[0]
    W = u[1]

    # Output from ODE function must be a COLUMN vector, with n rows
    n = len(y)      # 3: implies we have three ODEs
    dydt = np.zeros((n,1))
    dydt[0] = V * cos(T)
    dydt[1] = V * sin(T)
    dydt[2] = W
    return dydt
def callback(data):
    # Start by specifying the integrator:
    # use ``vode`` with "backward differentiation formula"
    r = integrate.ode(diffDrive).set_integrator('vode', method='bdf')

    # Set the time range
    t_start = 0.0
    delta_t = 0.01

    # Set initial condition(s): for integrating variable and time!
    X_t_zero = float(rospy.get_param('/x', '0.0'))
    Y_t_zero = float(rospy.get_param('/y', '0.0'))
    T_t_zero = float(rospy.get_param('/theta', '0.0'))

    r.set_initial_value([X_t_zero, Y_t_zero, T_t_zero], t_start)

    # Integrate the ODE
    r.set_f_params(data.data)
    r.integrate(r.t + delta_t)

    # results
    t = r.t
    X = r.y[0]
    Y = r.y[1]
    T = r.y[2]

    # Publish on topic
    pub = rospy.Publisher('x_y_theta', numpy_msg(Floats) ,queue_size=10)
    pub.publish(r.y)

    # Publish on parameter server
    rospy.set_param('x', X)
    rospy.set_param('y', Y)
    rospy.set_param('theta', T)

def sim():
    rospy.init_node('sim', anonymous=True)
    rospy.Subscriber("v_omega", numpy_msg(Floats), callback)
    rospy.spin()

if __name__ == '__main__':
    sim()

and this is the error

[ERROR] [WallTime: 1422545577.129270] bad callback: <function callback at 0x7f0be6c8f6e0>
Traceback (most recent call last):
  File "/opt/ros/indigo/lib/python2.7/dist-packages/rospy/topics.py", line 702, in _invoke_callback
    cb(msg)
  File "/home/viki/catkin_ws/src/wheelchair/scripts/sim.py", line 58, in callback
    rospy.set_param('x', X)
  File "/opt/ros/indigo/lib/python2.7/dist-packages/rospy/client.py", line 502, in set_param
    _param_server[param_name] = param_value #MasterProxy does all the magic for us
  File "/opt/ros/indigo/lib/python2.7/dist-packages/rospy/msproxy.py", line 148, in __setitem__
    self.target.setParam(rospy.names.get_caller_id(), rospy.names.resolve_name(key), val)
  File "/usr/lib/python2.7/xmlrpclib.py", line 1224, in __call__
    return self.__send(self.__name, args)
  File "/usr/lib/python2.7/xmlrpclib.py", line 1572, in __request
    allow_none=self.__allow_none)
  File "/usr/lib/python2.7/xmlrpclib.py", line 1085, in dumps
    data = m.dumps(params)
  File "/usr/lib/python2.7/xmlrpclib.py", line 632, in dumps
    dump(v, write)
  File "/usr/lib/python2.7/xmlrpclib.py", line 646, in __dump
    raise TypeError, "cannot marshal %s objects" % type(value)
TypeError: cannot marshal <type 'numpy.float64'> objects

The params are initialised with a .launch or loading a .yaml . The result is the same. I use ROS indigo on a virtual box machine running on a mac osx. Does someone know how to help me ?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2015-01-29 09:58:18 -0600

dornhege gravatar image

set_param only accepts python's building floats. You are passing a numpy float, which is a different data type. Converting that to float should help.

edit flag offensive delete link more

Comments

it works! thank you!

carrello gravatar imagecarrello ( 2015-01-29 10:12:40 -0600 )edit

Your Answer

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

Add Answer

Question Tools

1 follower

Stats

Asked: 2015-01-29 09:44:17 -0600

Seen: 1,414 times

Last updated: Jan 29 '15