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

float error while using a float - custom messages

asked 2022-05-01 18:13:26 -0500

Electrolight gravatar image

Hello all,

I am receiving this error.

[example36-1]     self.age.year = 2022
[example36-1]   File "/home/user/ros2_ws/install/custom_interfaces/lib/python3.8/site-packages/custom_interfaces/msg/_age.py", line 131, in year
[example36-1]     assert \
[example36-1] AssertionError: The 'year' field must be of type 'float'

Only, I am using a float.

from custom_interfaces.msg import Age

...

    def on_shutdown(self):
        self.age.year = 2022
        self.age.month = 5
        self.age.day = 1
        self.get_logger().info('date this program was made : %s' %
                               self.age.day + '/%s' % self.age.month + '/%s' % self.age.year)

And if you'd like to see the age message type

float32 year
float32 month
float32 day

Pretty sure I compiled it correctly and stuff since when I run

user:~/ros2_ws$ ros2 interface show custom_interfaces/msg/Age
float32 year
float32 month
float32 day

It spits it out.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-05-02 03:57:54 -0500

gvdhoorn gravatar image

Only, I am using a float.

from custom_interfaces.msg import Age

...

    def on_shutdown(self):
        self.age.year = 2022

isn't 2022 an int?

at least Python seems to think it is:

>>> type(2022)
<class 'int'>

To make it a float:

>>> type(float(2022))
<class 'float'>
edit flag offensive delete link more

Comments

Thank you for your help :) You put me on the right path. So best I can tell. Python sees "2022" and takes a guess that it's an integer. Then ros2 panics since the data type and message don't agree.

.

Storing the value as: self.age.year = float(2022)

Or as: self.age.year = 2022.0

Both work... but yield a result like: [example36]: date this program was made : 1.0/5.0/2022.0

.

So it seems the easiest course of action is to update the message type to an integer and then perhaps the whole fiasco is averted.

.

Thank you!

-rosnoob

Electrolight gravatar image Electrolight  ( 2022-05-03 14:13:03 -0500 )edit

Question Tools

Stats

Asked: 2022-05-01 18:13:26 -0500

Seen: 889 times

Last updated: May 02 '22