rospy.get_param gives strings instead of negative values in a list
Hello,
I am trying to pass a matrix through a param.yaml file to be read by a python file. I am following the advice here with the exception that I am using rospy rather than roscpp. So I have formatted my .yaml file to include my 6x6 matrix as a flat list, however when I go to read it in and reshape it back into a matrix using rospy.get_param, I end up with a list full of floats for the positive values, and strings for the negative ones. I want to avoid having to use a for loop to go through each entry of the matrix and typecast these strings back into floats, but I'm at a loss for how else to get my matrix through the param server. How else can I get it to read my negative values as floats instead of strings? I've put a minimal example below.
the param.yaml file:
matrix: [1.0,0.0,0.0,0.0,0.0,0.0, 0.0,-1.9,0.0,0.0,0.0,0.0, 0.0,0.0,1.0,0.0,0.0,0.0, 0.0,0.0,1.0,0.0,0.0,0.0, 0.0,0.0,1.0,0.0,0.0,1.0, 0.0,0.0,1.0,0.0,1.0,1.0]
loaded through a launch file within the appropriate node:
<rosparam file='param.yaml' command='load'/>
called within the node as:
if rospy.has_param('~matrix'):
listmatrix=rospy.get_param('~matrix')
toarray=np.array(listmatrix)
self.matrix=toarray.reshape(6,6)
rospy.loginfo(str(type(listmatrix[0])) + str(type(listmatrix[7])))
rospy.loginfo(str(type(toarray[0])) + str(type(toarray[7])))
rospy.loginfo(str(type(self.matrix[0,0])) + str(type(listmatrix[1,1])))
which returns
[INFO] [########.####, ########]: <class 'float'><class 'str'>
[INFO] [########.####, ########]: <class 'numpy.str_'><class 'numpy.str_'>
[INFO] [########.####, ########]: <class 'numpy.str_'><class 'numpy.str_'>
update: I've just checked in an attempt to debug, and it actually does this with any negative value I try to pass as a parameter, not just ones in lists.
second update: I checked on values I pass through either the launch file or the terminal, and they come back as floats, it is only ones loaded through .yaml files (and I fixed a typo, as per Mike's comment)
Could the minus character in your yaml file be some other similar-looking character? Check the file using
xxd -g 1 matrix.yaml
utility and confirm the minus is a single byte hex 2d.It is a 2d yes.