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

Can't publish a message that is composed of several fields

asked 2022-12-15 15:00:32 -0500

arad2456 gravatar image

updated 2022-12-19 06:44:47 -0500

Hey everyone, I am trying to publish a weather message that is composed out of several fields (temperature, pressure, wind velocity, water velocity and a general weather code), but it doesn't work. In the creation process, I first try to send a message that contains only with the temperature field, but I get a weird output:

Traceback (most recent call last):
File "/home/user/ros2_ws/install/proj_pubsub/lib/proj_pubsub/talker", line 11, in <module>
load_entry_point('proj-pubsub==0.0.0', 'console_scripts', 'talker')()
File "/home/aradhilel/ros2_ws/install/proj_pubsub/lib/python3.8/site-packages/proj_pubsub/publisher.py", line 30, in main
rclpy.spin(minimal_publisher)
File "/opt/ros/foxy/lib/python3.8/site-packages/rclpy/__init__.py", line 191, in spin
executor.spin_once()
File "/opt/ros/foxy/lib/python3.8/site-packages/rclpy/executors.py", line 718, in spin_once
raise handler.exception()
File "/opt/ros/foxy/lib/python3.8/site-packages/rclpy/task.py", line 239, in __call__
self._handler.send(None)
File "/opt/ros/foxy/lib/python3.8/site-packages/rclpy/executors.py", line 429, in handler
await call_coroutine(entity, arg)
File "/opt/ros/foxy/lib/python3.8/site-packages/rclpy/executors.py", line 343, in _execute_timer
await await_or_execute(tmr.callback)
File "/opt/ros/foxy/lib/python3.8/site-packages/rclpy/executors.py", line 118, in await_or_execute
return callback(*args)
File "/home/user/ros2_ws/install/proj_pubsub/lib/python3.8/site-packages/proj_pubsub/publisher.py", line 20, in timer_callback
msg.temperature = 15.0
File "/home/user/ros2_ws/install/proj_msgs/lib/python3.8/site-packages/proj_msgs/msg/_weather_composite.py", line 169, in temperature
assert \
AssertionError: The 'temperature' field must be a sub message of type 'Temperature'

This is my publisher file:

import rclpy
from rclpy.node import Node
from proj_msgs.msg import WeatherComposite
from sensor_msgs.msg import Temperature


class MinimalPublisher(Node):

    def __init__(self):
        super().__init__('minimal_publisher')
        self.publisher_ = self.create_publisher(WeatherComposite, 'weather', 10)
        timer_period = 0.5  # seconds
        self.timer = self.create_timer(timer_period, self.timer_callback)
        self.i = 0

    def timer_callback(self):
        msg = WeatherComposite()
        msg.temperature = 15.0
        self.publisher_.publish(msg)
        self.get_logger().info('Publishing: "%s"' % msg.temperature)


    def main(args=None):
        rclpy.init(args=args)
        minimal_publisher = MinimalPublisher()
        rclpy.spin(minimal_publisher)
        minimal_publisher.destroy_node()
        rclpy.shutdown()


if __name__ == '__main__':
    main()

The problem happens only when I try to send the temperature field inside the composed message. When I change the message type from WeatherComposite to Temperature it works.

This is the message file "WeatherComposite.msg":

sensor_msgs/Temperature temperature
sensor_msgs/FluidPressure pressure
proj_msgs/WaterFlowVelocity water_flow_velocity
proj_msgs/WindVelocity wind_velocity
proj_msgs/GeneralWeather weather_id

the proj_msgs are messages that I created. Since they are not relevant to the question, I don't post their definition (they are made out of the standard primitives).

I know that it's bad that I have two temperature fields in the same file (one from Temperature and one from WeatherComposite). I did it only for the demonstration here, in my real code I am going to remove sensor_msgs/Temperature.

It would be awesome if you could help me here. Thank you in advance.

Edit:

Ok, I played with it a bit ... (more)

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-12-16 11:32:44 -0500

ChuiV gravatar image

The error output you posted says The 'temperature' field must be a sub message of type 'Temperature'. You've got a line in your code that says msg.temperature = 15.0. float is not a sensor_msgs/Temperature type, hence the error.

I believe you meant to do something like msg.temperature.temperature = 15.0 instead.

edit flag offensive delete link more

Comments

Thank you, it works now. I edited my post. Before I posted here I tried it, but wrote msg.Temperature.temperature instead, and when it didn't work I was confused

arad2456 gravatar image arad2456  ( 2022-12-19 06:46:02 -0500 )edit

Question Tools

Stats

Asked: 2022-12-15 15:00:32 -0500

Seen: 180 times

Last updated: Dec 19 '22