How can I use multiple subscriber for only one publisher?

asked 2021-02-09 05:46:41 -0500

Swayinke gravatar image

updated 2021-02-11 15:51:48 -0500

jayess gravatar image

Hi Everybody,

I am really new to Ros and I have tried to read the position of the robot (x,y) to see in real-time. I have this code, but it's just don't do it. I would really appreciate any help. Thanks

class MoveRobot:
    def __init__(self):
        rospy.init_node('move_the_robot',anonymous=True)
        self.pub = rospy.Publisher('/miro/control/cmd_vel',TwistStamped, queue_size=1)
        self.sub = rospy.Subscriber('/miro/sensors/package', sensors_package, self.callback)
        self.sub_position = ros.Subscriber('/miro/sensors/odom', Odometry, self.call_position)
        self.rate = rospy.Rate(10)
        self.move = TwistStamped()

    def callback(self,msg):
        self.pub.publish(self.move)
        self.sonar_sensor = msg
        print(msg.sonar.range)

    def read_sonar(self):
        return self.sonar_sensor.sonar.range

    def call_position(self)
        self.x = msg.pose.pose.position.x
        self.y = msg.pose.pose.position.y
        #rospy.loginfo('x: {}, y: {}'.format(x,y))

    def get_position(self):
        return self.position_x.pose.pose.position.x

    def move_forward(self,x,z):
        while not rospy.is_shutdown():
            self.move.twist.linear.x = 0.5
            self.move.twist.angular.z = 0.0
            self.rate.sleep()

mr = MoveRobot()
mr.move_forward(0.5,0.0)

I would like to see the x and y position of the robot in real-time. I would like to return these two sensor data to see where is the robot in the x,y plane. I am trying to do a reinforcement learning and my teacher said that I need to know the grid, so I guess this is the way to figure it out.

edit retag flag offensive close merge delete

Comments

Can you link to the entire class definition? There are a few lines that seem buggy

trunc8 gravatar image trunc8  ( 2021-02-09 14:32:00 -0500 )edit

What's not working, exactly? Is something publishing messages on the /miro/sensors/odom topic, but you're not seeing expected output?

tryan gravatar image tryan  ( 2021-02-09 15:55:50 -0500 )edit

You aren't adding timestamp to the TwistStamped message. There are few other weird details but you need to explicitly specify what is the problem you're currently facing. "I would like to see the x and y position of the robot in real-time"- when you uncomment rospy.loginfo('x: {}, y: {}'.format(x,y)) are you unable to see the output?

trunc8 gravatar image trunc8  ( 2021-02-17 06:33:57 -0500 )edit