Robotics StackExchange | Archived questions

Humble ROS2 topic data changed and inaccurate but it is accurate on FOXY

I'm not sure why but I am still learning with ROS2. Could you explain it to me as why it happened?

So, I'm using ros2 on mycobot through raspberry pi (22.04).

So, when I run humble on Raspberry PI, the topic's code in python is like this:

class ServoPosition(Node):
    def __init__(self):
        super().__init__('Servo_position')
        self.publisher_ = self.create_publisher(std_msgs.msg.String, 'servo_data', 0)
        timer_period = 0.1  # seconds
        self.timer = self.create_timer(timer_period, self.timer_callback)
        self.i = 0
def timer_callback(self):
    msg = std_msgs.msg.String()
    for i in range(capabilities['servo']['count']):
        if i != 0 and i != 2:
            new_data = arm.get_encoder(i)
            if new_data != -1:
                runtime_data['servo_position'][i] = new_data
    msg.data = str(runtime_data['servo_position'])
    self.publisher_.publish(msg)
    self.i += 1

It is giving unstable output. See the output below:

data: '{3: 1673, 1: 1314, 4: 1673}'
---
data: '{3: 1673, 1: 1314, 4: 1673}'
---
data: '{3: 1673, 1: 1314, 4: 1673}'
---
data: '{3: 1314, 1: 1314, 4: 1673}'
---
data: '{3: 1314, 1: 3755, 4: 1673}'
---
data: '{3: 1314, 1: 1314, 4: 1673}'
---
data: '{3: 1314, 1: 1314, 4: 1673}'
---
data: '{3: 1314, 1: 1314, 4: 1673}'
---
data: '{3: 1314, 1: 1314, 4: 1673}'
---
data: '{3: 1314, 1: 1314, 4: 3755}'
---
data: '{3: 1314, 1: 1314, 4: 3755}'
---
data: '{3: 3755, 1: 1314, 4: 3755}'
---
data: '{3: 3755, 1: 1673, 4: 3755}'
---

So, when I tested this code on Foxy (exact same code) on my laptop (20.04) and it's provide a stable output. See here:

data: '{1: 1317, 3: 3766, 4: 1724}'
---
data: '{1: 1317, 3: 3766, 4: 1724}'
---
data: '{1: 1317, 3: 3766, 4: 1724}'
---
data: '{1: 1317, 3: 3766, 4: 1724}'
---
data: '{1: 1317, 3: 3766, 4: 1724}'
---
data: '{1: 1317, 3: 3766, 4: 1724}'
---
data: '{1: 1317, 3: 3766, 4: 1724}'
---
data: '{1: 1317, 3: 3766, 4: 1724}'
---
data: '{1: 1317, 3: 3766, 4: 1724}'
---
data: '{1: 1317, 3: 3766, 4: 1724}'
---
data: '{1: 1317, 3: 3766, 4: 1724}'
---
data: '{1: 1317, 3: 3766, 4: 1724}'
---
data: '{1: 1317, 3: 3766, 4: 1724}'
---

Why is Foxy giving a stable data while Humble gives unstable data?

Brief summary as it's probably useful for mycobot developers: I get data through the pyserial (usb) by obtain the servo's position (get_encoder()). It was able to provide all data of positions in real time.

What is the problem with the data in humble?

The runtime dict will be like let's say, {1: 1317, 3: 3766, 4: 1724}

I don't see why humble change the data. Please let me know if you need more information. I'm very interested to continue with Humble!

Update 10/31/22:

The full code:

#! /usr/bin/env python3
import rclpy
from rclpy.node import Node
from std_msgs.msg import String


class ServoPosition(Node):
    def __init__(self):
        super().__init__('Servo_position')
        self.publisher_ = self.create_publisher(String, 'servo_data', 0)
        timer_period = 0.1  # seconds
        self.timer = self.create_timer(timer_period, self.timer_callback)
        self.i = 0

    def timer_callback(self):
        msg = String()
        runtime_data = []
        for i in range(7):
            if i != 0 and i != 2:
                new_data = self.get_encoder(i)
                if new_data != -1:
                    runtime_data.append(new_data)
        msg.data = str(runtime_data)
        self.publisher_.publish(msg)
        self.i += 1

    def get_encoder(self, encoder_id):
        data = {'0': 2144, '1': 4355, '2': 134343, '3': 1673, '4': 1314, '5': 1673, '6': 5555, '7': 98643}
        return data[str(encoder_id)]


def main(args=None):
    rclpy.init(args=args)

    minimal_publisher = ServoPosition()

    rclpy.spin(minimal_publisher)

    # Destroy the node explicitly
    # (optional - otherwise it will be done automatically
    # when the garbage collector destroys the node object)
    minimal_publisher.destroy_node()
    rclpy.shutdown()


if __name__ == '__main__':
    main()

Asked by kak13 on 2022-10-27 11:14:22 UTC

Comments

Sorry, but the given code is insufficient to run at my side or understand this strange behavior.

Asked by ravijoshi on 2022-10-31 04:51:09 UTC

Thank you for the comment! I realized my post gives a very lack of explanation, my deeply apologies.

I updated my post. I truly appreciate your time, I really do!

Asked by kak13 on 2022-10-31 08:20:44 UTC

I run your code on Foxy (Ubuntu 20.04) and Humble (Ubuntu 22.04). Your code gives the same output on both versions. Please see below:

$ ros2 topic echo /servo_data
data: '[4355, 1673, 1314, 1673, 5555]'

Asked by ravijoshi on 2022-11-02 09:33:53 UTC

Answers