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

ROS Serialization Values Error

asked 2018-06-24 03:31:13 -0500

updated 2018-06-24 03:32:02 -0500

Hi there,

I am trying to do a simple ipc over ros serial using the publish subscribe module. The data is a 2D array shaped thus: 10001 x 7. I write my poublisher like so

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

import os
import numpy as np
from os.path import expanduser, join


def talker(data):
    pub = rospy.Publisher('/torobo/teach_joints', numpy_msg(Floats),queue_size=1)
    r = rospy.Rate(10) # 10hz
    while not rospy.is_shutdown():
        pub.publish(data)
        r.sleep()

if __name__ == '__main__':
    filepath = join(expanduser('~'), 'Documents', 'LyapunovLearner', 'ToroboTakahashi', 'data')
    name = 'state_joint_pos_only.csv'
    filename = join(filepath, name)
    data =  np.ravel(np.loadtxt(filename).astype(np.float64), order='A')
    np.set_printoptions(suppress=True)
    print(data[:70])
    try:
        rospy.init_node('joints_pub_node')
        talker(data)
        rospy.spin()
    except KeyboardInterrupt:
        rospy.logerror("shutting down ros")

And my subscriber is something like this: import rospy import numpy as np from rospy_tutorials.msg import Floats from rospy.numpy_msg import numpy_msg

def callback(data):
    np.set_printoptions(suppress=True)
    print rospy.get_name(), "I heard %s"%str(data.data.shape)
    ret_data = data.data.astype(np.float64)
    print(ret_data[:70])#.reshape(10, 7))

def listener():
    rospy.init_node('listener')
    rospy.Subscriber("/torobo/teach_joints", numpy_msg(Floats), callback)
    rospy.spin()

if __name__ == '__main__':
    listener()

However, I find that the data I am sending and the one I am receiving are vastly different in values. Here are printouts from both nodes:

publisher (first 70 elements):

[ 0.          0.001       0.016       0.002      -0.008       0.008
 -0.03        0.          0.          0.015       0.019      -0.009
  0.006      -0.03       -0.005      -0.053       0.009       0.60699999
 -0.126       0.26300001 -0.034      -0.004      -0.185      -0.036
  1.96300006 -0.396       1.59899998 -0.032      -0.001      -0.361
 -0.16        3.52600002 -0.81699997  3.08800006 -0.03       -0.032
 -0.57599998 -0.28200001  4.90999985 -1.23099995  4.26499987 -0.031
 -0.094      -0.77700001 -0.38299999  6.48899984 -1.62300003  5.74100018
 -0.03       -0.13       -0.90399998 -0.49900001  7.80299997 -1.94700003
  6.85900021 -0.03       -0.168      -1.11899996 -0.55000001  9.29899979
 -2.28200006  8.10599995 -0.03       -0.197      -1.29499996 -0.69
 10.80900002 -2.71300006  9.59599972 -0.03      ]

And subscriber equivalent:

[ 0.00000000e+00  0.00000000e+00 -3.68934881e+19  8.13999951e-01
 -3.68934881e+19  1.12799990e+00 -3.68934881e+19  8.76499951e-01
 -3.68934881e+19 -1.00299990e+00 -3.68934881e+19  1.00299990e+00
 -3.68934881e+19 -1.23999989e+00  0.00000000e+00  0.00000000e+00
  0.00000000e+00  0.00000000e+00 -3.68934881e+19  1.11499989e+00
  3.68934881e+19  1.15199995e+00 -0.00000000e+00 -1.01899993e+00
 -2.00000000e+00  9.70999956e-01 -3.68934881e+19 -1.23999989e+00
  2.00000000e+00 -9.54999983e-01 -3.68934881e+19 -1.33699989e+00
 -0.00000000e+00  1.01899993e+00  2.00000000e+00  1.77674997e+00
 -1.08420217e-19 -1.50099993e+00  0.00000000e+00  1.63150001e+00
 -2.00000000e+00 -1.26099992e+00 -3.68934881e+19 -9 ...
(more)
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2018-06-24 05:07:38 -0500

Turns out that rospy_tutorials.Floats is a float32 object. In the subscriber code, ROS was downcasting the np.float64 being transmitted over ros serial to a float32 dtype. I wrote a new message file that took in Float64 and all is now history. Thanks and sorry for my trouble.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-06-24 03:31:13 -0500

Seen: 682 times

Last updated: Jun 24 '18