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

Hello, I am new to ros and want to know how access the subscription data in another function using rclpy. I keep geeting the error: AttributeError: 'Pose_sub' object has no attribute 'pose'

asked 2023-03-22 14:01:21 -0600

siddarth236 gravatar image
 #!/usr/bin/env python3
import rclpy
from rclpy.node import Node
from turtlesim.msg import Pose

class Pose_sub(Node):
    def __init__(self):
        super().__init__("pose_subscriber")

        self.pose_subs = self.create_subscription(
            Pose,"/turtle1/pose",self.pose_callback,10)

        self.get_pose()



    def pose_callback(self,pose: Pose):
        #self.get_logger().info(f'{pose.x},{pose.y}')
        self.pose = pose
        return pose

    def get_pose(self):
        new = self.pose
        self.get_logger().info(f'{new.x}')


def main(args=None):

    rclpy.init(args=args)
    node = Pose_sub()
    rclpy.spin(node)
    rclpy.shutdown()
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2023-03-22 17:48:06 -0600

feltarock gravatar image

It's essentially impossible that the subscription set up will be done and a message processed by your callback between the subscription creation time and the time you are calling "self.get_pose()".

What are you trying to accomplish with the pose? Most of the time I see folks use a timer with its own callback for some sort of processing code that runs regularly, using the latest stored sensor data from class state.

That timer callback would initially also have the same error (fyi you probably want to initialize self.pose = None in the constructor and check if self.pose is None in a conditional within other code that depends on pose to avoid exceptions being thrown). You can have your timer callback return early doing nothing until all required data is present.

edit flag offensive delete link more
0

answered 2023-03-23 04:02:30 -0600

ahopsu gravatar image

Hi,

For me it seems, that you haven't defined the 'pose' member for the Pose_sub node.

class Pose_sub(Node):
def __init__(self):
    super().__init__("pose_subscriber")

    self.pose_subs = self.create_subscription(
        Pose,"/turtle1/pose",self.pose_callback,10)

    self.pose = None # <- Add this line

    self.get_pose()
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2023-03-22 14:01:21 -0600

Seen: 612 times

Last updated: Mar 23 '23